#include #include #include #include #include #include #include "dataspy.h" #include "transfer.h" #include "ganiltransfer.h" // 24 byte data block header - used for data blocks in shared memory and data blocks on disc or tape. typedef struct s_data_header { char header_id[8]; int header_sequence; short header_stream; short header_DataWidth; short header_MyEndian; short header_DataEndian; int header_dataLen; } DATA_HEADER; DATA_HEADER data_header; int convertMIDAStoGANIL(unsigned short *, int); void byteswop16(char*, int); void byteswop32(char*, int); unsigned short InputBuffer[32*1024]; // maximum length is 64 Kbytes int Input_Buffer_Length = 64 * 1024; int InputBufferLen = 0; /* in units of short */ unsigned short OutputBuffer[32*1024]; // maximum length is 64 Kbytes int Output_Buffer_Length = 16 * 1024; // units bytes int OutputBufferLen = 0; /* in units of short */ unsigned short EventBuffer[32*1024]; int Event_Buffer_Length = 64 * 1024; int EventBufferLen = 0; /* in units of short */ int Block_Event_Count=0; int Block_Sequence_Number=0; int Started = 0; /* waiting for 1495 to detect event */ int Waiting = 0; /* waiting for a data block to complete */ int WaitTime = 100; int GANILServerRunState = 0; unsigned int TimeStampL = 0; unsigned int TimeStampM = 0; unsigned int TimeStampH = 0; unsigned int EvNumberL = 0; unsigned int EvNumberH = 0; unsigned short EventNumberMSW = 0; unsigned short EventNumberLSW = 0; unsigned int QLong = 0; unsigned int QShort = 0; unsigned int TimeTagMSW = 0; unsigned int TimeTagLSW = 0; unsigned int TimeTagEXW = 0; unsigned int FineTime = 0; int Verbose = 0; int OutFlag = 0; void Usage(char *progname) { fprintf(stderr,"Usage\n%s -n server -t [delayticks] -p [port] -m [mode]\n",progname); fprintf(stderr,"\tserver is the IP address or name of destination\n"); fprintf(stderr,"\tdelayticks is the delay (in millisecs) when looking for buffers\n"); fprintf(stderr,"\tport is the TCP port to use\n"); fprintf(stderr,"\tmode is the protocol transfer option\n"); fprintf(stderr,"Defaults are (none); 6500; 4\n"); exit(1); } void signal_block () {} int main(int argc, char **argv) { unsigned short * Buffer16 = (unsigned short *)InputBuffer; unsigned int * Buffer32 = (unsigned int *)InputBuffer; int BufferState; int BufferLength; unsigned short * DataBlock; short MyEndian; short DataEndian; int DataLength; char *TS_Server= (char*)"localhost"; int Port=6500; int i, j; unsigned short * p16; unsigned int * p32; unsigned char * p8; int DelayTicks=10; struct timespec time_request; char spinner[4]; int spin = 0; if (argc >1) { for(i=1;i 0) { printf("calling GANILtransferInit...\n"); (void) GANILtransferInit(TS_Server); while (GANILServerRunState == 0) {sleep(1);} printf ("done\n"); } i = dataSpyOpen (0); printf ("dataSpyOpen returned %d\n", i); Block_Event_Count = 0; Block_Sequence_Number=0; InputBufferLen = 0; EventBufferLen = 0; OutputBufferLen = 0; Started = 0; Waiting = 0; WaitTime = 500/DelayTicks; for (;;) { /* obtain a data buffer from shared memory. This will have a 24 byte header */ DataLength = dataSpyRead (0, (char*)InputBuffer, Input_Buffer_Length); /* read into buffer; real data starts at offset 24 */ if (DataLength > 0) { if ((Verbose & 0x0001) == 0x0001) printf("read done with code=%d\n",DataLength); memcpy (data_header.header_id, (char*)&InputBuffer[sizeof(HEADER)-sizeof(DATA_HEADER)], sizeof(DATA_HEADER)); MyEndian = Buffer16[8]; DataEndian = Buffer16[9]; DataLength = Buffer32[5]; /* the actual application data less headers */ DataBlock = &Buffer16[12]; if ((Verbose & 0x0001) == 0x0001) printf("MyEndian = %d, DataEndian = %d, DataLength=%d\n",MyEndian, DataEndian, DataLength); if ((Verbose & 0x0002) == 0x0002) { printf("\n\nInputBlock >>>\n"); Buffer16 = (unsigned short *)InputBuffer; for (j = 0; j <64;) { printf(" 0x%04x", Buffer16[j]); j++; if ((j/16)*16 == j) printf("\n"); } printf("\n"); } (void) convertMIDAStoGANIL (DataBlock , DataLength/2); printf ("%c\b", spinner[spin]); fflush(NULL); if (++spin == 4) spin=0; } else { if (DelayTicks) { time_request.tv_sec = DelayTicks/1000; time_request.tv_nsec = (DelayTicks - (time_request.tv_sec * 1000)) * 1000000; i = nanosleep(&time_request, NULL); } Waiting++; if (Waiting > WaitTime) { /* GANIL server requires "KeepAlives" */ xfer_block(0); printf ("%c\b", spinner[spin]); fflush(NULL); if (++spin == 4) spin=0; Waiting = 0; } } } } void byteswop32(char* p, int l) { int len; char *ps; char *pd; char b0, b1, b2, b3; len = l/4; ps = p; pd = p; while (len > 0) { b0 = *ps++; b1 = *ps++; b2 = *ps++; b3 = *ps++; *pd++ = b3; *pd++ = b2; *pd++ = b1; *pd++ = b0; len--; } } void byteswop16(char* p, int l) { int len; char *ps; char *pd; char b0, b1; len = l/2; ps = p; pd = p; while (len > 0) { b0 = *ps++; b1 = *ps++; *pd++ = b1; *pd++ = b0; len--; } }