freq, recvBuffer+4, 4 );
memcpy( (char *)payl->att_lowpass_rate, recvBuffer+8, 2 );
memcpy( shmaddr, recvBuffer+4, 6 );
memcpy( shmaddr+6+j, recvBuffer+10, res-10 );
tempCounter = ntohl ( *(long *)recvBuffer );
counter++;
if( counter!=tempCounter ) {
gettimeofday( &stop, NULL );
fprintf( stderr, "Counter (%ld) differs from received counter (%ld) diff %ld %%=%f time=%f\n", counter, tempCounter, tempCounter-counter, (tempCounter-counter)*100.0/(tempCounter-oldTempCounter), (stop.tv_sec-start.tv_sec)+0.000001*(stop.tv_usec-start.tv_usec) );
counter=tempCounter;
oldTempCounter=tempCounter;
gettimeofday( &start, NULL );
}
*lung += res-10;
if ( res == -1 ) {
fprintf( stderr,"Recvfrom Error\n" );
exit( 10 );
}
}
pthread_cond_broadcast( (pthread_cond_t *)shm_cond );
if( con ) {
pthread_cond_signal( con );
}
}
return NULL;
}
void *FFTSend( void *pay ) {
double *co = NULL;
double *si = NULL;
double *fftr = NULL;
double *ffti = NULL;
double *coeff = NULL;
unsigned char *fftout = NULL;
int N;
double co1, si1;
double pi=3.141592653589793238462643383279502884197169399375105820974944592;
double fftdr, fftdi;
int j, k;
int m, p, md, ng, ig, i, ind, i1, i2, jd, kp;
long val;
char buf[1050];
struct timeval start, stop;
payloadp payl=(payloadp)pay;
p=10;
N=1<con_a, payl->mut_a);
fprintf( stderr, "FFT unlocked\n" );
fflush( stderr );
for( ;; ) {
pthread_cond_wait(payl->con, payl->mut);
// Time measurement
gettimeofday( &start, NULL );
// Read input data
for( j=0; jbuf)[2*j]*coeff[j];
ffti[j]=((float *)payl->buf)[2*j+1]*coeff[j];
}
m = N/2;
p = 10;
// FFT Compute
for( j=0,ng=1,md=m; j>= 1;
ng <<= 1;
}
// Coefficients reordering
for( j=0; j>= 1;
kp >>= 1;
}
if( k>j ) {
fftdr = fftr[j];
fftdi = ffti[j];
fftr[j] = fftr[k];
ffti[j] = ffti[k];
fftr[k] = fftdr;
ffti[k] = fftdi;
}
}
// Absolute value
for( j=0; jfreq, 4 );
memcpy( buf+8, payl->att_lowpass_rate, 2 );
memcpy( buf+10, fftout+512+64, 512-64 );
memcpy( buf+512+10-64, fftout, 512-64 );
if( *payl->outfile ) {
j=write( *payl->outfile, buf, 1024+10-128 );
if( j<0 ) {
fprintf( stderr, "sent error %d %s\n", errno, strerror(errno) );fflush( stderr );
}
}
usleep( 100000 );
}
return NULL;
}
int main( int ac, char *av[], char **ev ) {
int res;
char *host, *port, *ports;
int portn, portsn;
int outfile;
char cmdbuffer[1024];
key_t shm_key = 6166530;
long freq;
short att_lowpass_rate;
pthread_t pthread0;
pthread_attr_t attr0;
struct sched_param prio0;
payload payl0;
pthread_t pthread1;
pthread_attr_t attr1;
payload payl1;
pthread_t pthread2;
pthread_attr_t attr2;
struct sched_param prio2;
payload payl2;
pthread_t pthread3;
pthread_attr_t attr3;
struct sched_param prio3;
payload payl3;
char *passphrase;
// cond variables for FFT buffer unlock
pthread_cond_t con_a = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mut_a = PTHREAD_MUTEX_INITIALIZER;
// cond varables for WebSocket unlock FFT
pthread_cond_t con_b = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mut_b = PTHREAD_MUTEX_INITIALIZER;
// cond variables for commands received;
pthread_cond_t con_c = PTHREAD_COND_INITIALIZER;
pthread_mutex_t mut_c = PTHREAD_MUTEX_INITIALIZER;
static char buf_a[BUFFERSIZE];
int lung_a;
fprintf( stderr, "Operations init elad-server version 1.0\n" );
// Read host from Argument, if not localhost
if( ac<2 ) {
host = "localhost";
} else {
host = av[1];
}
fprintf( stderr, "host: %s\n", host );
// Read portnumber from Argument, if not 6666
if( ac<3 ) {
port = "6666";
} else {
port = av[2];
}
portn=atoi( port );
fprintf( stderr, "portnumber: %s (%d)\n", port, portn );
// Read shm_key from Argument, if not 6166530
if( ac<4 ) {
shm_key = 6166530;
} else {
shm_key = atol( av[3] );
}
if( ac<5 ) {
passphrase="accattatevillo";
} else {
passphrase = av[4];
}
// Read portnumber from Argument, if not 6666
if( ac<6 ) {
ports = port;
} else {
ports = av[5];
}
portsn=atoi( ports );
fprintf( stderr, "receive portnumber: %s (%d)\n", ports, portsn );
// create thread that reads data using udp
memset( &payl0, 0, sizeof( payl0 ) );
payl0.port=portsn;
payl0.portc=ports;
payl0.hostname=host;
payl0.con=&con_a;
payl0.buf_a=buf_a;
payl0.lung_a=&lung_a;
payl0.shm_key=shm_key;
payl0.freq=&freq;
payl0.att_lowpass_rate=&att_lowpass_rate;
prio0.sched_priority=99;
res=pthread_attr_init( &attr0 );
if( res ) {
fprintf( stderr, "pthread_attr_init attr0 failed\n" );
}
res=pthread_create( &pthread0, &attr0, udpReceive, &payl0 );
if( res ) {
fprintf( stderr, "pthread_create pthread 0 failed\n" );
}
res=pthread_setschedparam( pthread0, SCHED_RR, &prio0 );
if( res ) {
fprintf( stderr, "pthread_setschedparam pthread 0 failed\n" );
}
fprintf( stderr, "udp Receive thread created\n" );
// create thread that sends commands using tcp
memset( &payl1, 0, sizeof( payl1 ) );
payl1.con=&con_c;
payl1.mut=&mut_c;
payl1.buf=cmdbuffer;
payl1.port=portsn;
payl1.portc=ports;
payl1.hostname=host;
res=pthread_attr_init( &attr1 );
if( res ) {
fprintf( stderr, "pthread_attr_init attr1 failed\n" );
}
res=pthread_create( &pthread1, &attr1, tcpManage, (void *)&payl1 );
if( res ) {
fprintf( stderr, "pthread_create pthread 1 failed\n" );
}
fprintf( stderr, "Tcp manage thread created\n" );
// create thread that sends FFT
memset( &payl2, 0, sizeof( payl2 ) );
payl2.port=portn;
payl2.portc=port;
payl2.hostname=host;
payl2.con=&con_a;
payl2.mut=&mut_a;
payl2.con_a=&con_b;
payl2.mut_a=&mut_b;
payl2.outfile=&outfile;
payl2.buf=buf_a;
payl2.lung=&lung_a;
payl2.freq=&freq;
payl2.att_lowpass_rate=&att_lowpass_rate;
prio2.sched_priority=80;
res=pthread_attr_init( &attr2 );
if( res ) {
fprintf( stderr, "pthread_attr_init attr2 failed\n" );
}
res=pthread_create( &pthread2, &attr2, FFTSend, (void *)&payl2 );
if( res ) {
fprintf( stderr, "pthread_create pthread 2 failed\n" );
}
res=pthread_setschedparam( pthread2, SCHED_RR, &prio2 );
if( res ) {
fprintf( stderr, "pthread_setschedparam pthread 2 failed\n" );
}
fprintf( stderr, "shared Send FFT thread created\n" );
// create thread that manages websocket creation
memset( &payl3, 0, sizeof( payl3 ) );
payl3.port=portn;
payl3.con=&con_b;
payl3.mut=&mut_b;
payl3.con_a=&con_c;
payl3.mut_a=&mut_c;
payl3.outfile=&outfile;
payl3.buf=cmdbuffer;
payl3.passphrase=passphrase;
prio3.sched_priority=50;
res=pthread_attr_init( &attr3 );
if( res ) { fprintf( stderr, "pthread_attr_init attr3 failed\n" ); }
res=pthread_create( &pthread3, &attr3, wsManage, (void *)&payl3 );
if( res ) { fprintf( stderr, "pthread_create pthread 3 failed\n" ); }
res=pthread_setschedparam( pthread3, SCHED_RR, &prio3 );
if( res ) { fprintf( stderr, "pthread_setschedparam pthread 3 failed\n" ); }
fprintf( stderr, "websocket Processing thread a created\n" );
// main loop
for( ;; ) {
sleep( 10 );
}
// dirty exit
fprintf( stderr, "Operations end\n" );
return 0;
}