22 #include <netinet/in.h>
23 #include <sys/socket.h>
24 #include <sys/select.h>
73 printf(
"\n-I- user specified CTL C: ");
77 printf(
"close connection (socket %d), hold client for %d s\n",
79 else printf(
"close connection\n");
81 if (iSleep) sleep(iSleep);
87 static Int_t
rclose(Int_t *piSocket, Int_t iMode)
94 Char_t cModule[32] =
"rclose";
95 Char_t cMsg[128] =
"";
102 if ( (iMode < 0) || (iMode > 3) )
104 if (iClose == 0) iMode = -iMode;
105 printf(
"-E- %s: invalid shutdown mode: %d\n", cModule, iMode);
114 iRC = shutdown(iSocket, iMode);
117 sprintf(cMsg,
"-E- %s: shutdown(%d) rc = %d",
118 cModule, iMode, iRC);
123 printf(
" %s: shutdown(%d) successfull\n",
129 iRC = close(iSocket);
132 sprintf(cMsg,
"-E- %s: close rc = %d", cModule, iRC);
137 printf(
" %s: connection closed\n", cModule);
144 printf(
"-E- %s: invalid socket: %d\n", cModule, iSocket);
154 static Long_t
swaplw(Long_t *pp_source, Long_t l_len, Long_t* pp_dest)
156 UChar_t *p_source, *p_dest, *p_s, *p_d;
160 p_source = (UChar_t *) pp_source;
161 p_dest = (UChar_t *) pp_dest;
166 for (p_d = (UChar_t *) p_source,
167 p_s = (UChar_t *) &lu_save;
168 p_d < p_source + (l_len * 4);
171 lu_save = *( (Long_t *) p_d);
180 for (p_s = (UChar_t *) p_source,
181 p_d = (UChar_t *) p_dest;
182 p_s < p_source + (l_len * 4);
211 cout <<
"-I- client runs in debug mode (1)" << endl;
212 else if (iDebug == 2)
213 cout <<
"-I- client shows buffer numbers and select/receive (mode 2)"
215 else if (iDebug == 3)
216 cout <<
"-I- client shows buffer numbers (mode 3)" << endl;
218 iBufSizeAlloc = 32768;
219 piBuf =
new Int_t [iBufSizeAlloc/
sizeof(int)];
242 cout <<
"-E- number of requested events (" << iEvent
243 <<
") invalid" << endl;
255 if (iPort == 0) iPort = 7031;
256 cout <<
"-I- open connection to server " << pNode
257 <<
":" << iPort << endl;
259 pTSocket =
new TSocket(pNode, iPort);
260 if ( !pTSocket->IsValid() )
262 cout <<
"-E- open connection to server " << pNode
263 <<
" failed" << endl;
266 cout <<
" connection to server " << pNode
267 <<
":" << iPort <<
" okay" << endl;
269 iSocket = pTSocket->GetDescriptor();
272 cout <<
" new socket " << iSocket << endl;
274 else if (iDebug == 1)
275 cout <<
"-D- still socket " << iSocket << endl;
285 Int_t iint =
sizeof(int);
286 Int_t ibyte, ilen, inew = 0;
289 Int_t ioff=0, irem, ind, ii, jj;
291 Char_t cMsg[128] =
"";
297 Int_t iRetryMax = 1000;
299 Int_t iRetryRecv = 0;
300 Int_t iRetryRecvLim = 1;
304 Int_t iCommSize =
sizeof(sComm);
308 Int_t iInfoSize =
sizeof(sInfo);
311 if (iEvtNo >= iEvtMax)
goto gEndGet;
313 piComm = &(sComm.
iSize);
314 sComm.
iSize = htonl(iCommSize-iint);
315 sComm.
iMode = htonl(1);
323 cout <<
"-D- commbuf (data size " << ntohl(sComm.
iSize)
324 <<
" byte): mode(1) " << ntohl(sComm.
iMode)
325 <<
", request " << ntohl(sComm.
iBufRequ)
326 <<
" event buffer(s)" << endl;
329 ilen = pSocket->SendRaw(piComm, iCommSize, kDefault);
332 cout <<
"-E- sending request for events to server, rc = "
339 cout <<
" communication buffer sent (request info buffer) "
343 piInfo = &(sInfo.
iSize);
344 ilen = pSocket->RecvRaw(piInfo, iInfoSize, kDefault);
347 cout <<
"-E- receiving info buffer from server, rc = "
357 cout <<
"-D- info buffer received:" << endl;
358 cout <<
" size data " << ntohl(sInfo.
iSize)
359 <<
", mode (1) " << ntohl(sInfo.
iMode)
360 <<
", header parms " << iHeadPar
361 <<
", timeout " << iTimeOut << endl;
364 if ( (ntohl(sInfo.
iMode) != 1) ||
365 ( (
int) ntohl(sInfo.
iSize) != iInfoSize-iint) )
367 cout <<
"-E- invalid info buffer received: " << endl;
368 cout <<
" size data ( " << iInfoSize-iint
369 <<
") " << ntohl(sInfo.
iSize)
370 <<
", mode (1) " << ntohl(sInfo.
iMode)
371 <<
", header parms " << iHeadPar
372 <<
", timeout " << iTimeOut << endl;
387 cout <<
"-D- skip current buffer" << endl;
394 if (iEvtRel >= iEvtBuf)
397 cout <<
" request new buffer" << endl;
417 ilen = pSocket->SendRaw(piComm, iCommSize, kDefault);
420 cout <<
"-E- sending request for buffer " << iBufNo+1
421 <<
" to server, rc = " << ilen << endl;
426 if (
imySig == -1)
goto gEndGet;
429 cout <<
"-D- communication buffer sent (request next buffer) "
436 pcBuf = (Char_t *) piBuf;
439 if ( (
imySig == -1) && (iDebug) )
440 cout <<
" CTL C detected (before recv len)" << endl;
442 iRC = recv(iSocket, pcBuf, iSize, 0);
449 sprintf(cMsg,
"\n-E- receiving data length from server");
451 cout <<
" CTL C detected (during recv len)" << endl;
457 sprintf(cMsg,
"\n-E- receiving data length from server");
459 if (iDebug) cout <<
" retry" << endl;
462 if (iRetryRecv > iRetryRecvLim)
467 else goto gNextRecvL;
472 if ( (iDebug == 2) || (iDebug == 3) )
474 cout <<
"-E- receiving data length: connection closed by server"
492 if ( (
imySig == -1) && (iDebug) )
493 cout <<
" CTL C detected (after recv len)" << endl;
495 iBufSize = ntohl(piBuf[0]);
497 cout <<
" data size received: " << iBufSize << endl;
505 cout <<
"-W- server closed connection" << endl;
506 cout <<
" " << iEvtNo <<
" of " << iEvtMax
507 <<
" events received" << endl;
516 cout << endl <<
"-E- no data length received: ";
520 if (iRetry > iRetryMax)
522 cout << iRetryMax <<
"times" << endl;
530 cout << endl <<
"-E- invalid data length received: "
538 cout << iRetry <<
"times" << endl;
541 if (iBufSize+iint > iBufSizeAlloc)
544 iBufSizeAlloc = iBufSize+iint;
546 piBuf =
new Int_t [iBufSizeAlloc/iint];
550 cout <<
"-I- total buffer increased to "
551 << iBufSizeAlloc <<
" bytes" << endl;
557 pcBuf = (Char_t *) &(piBuf[1]);
560 if ( (
imySig == -1) && (iDebug) )
561 cout <<
" CTL C detected (before recv data)" << endl;
563 iRC = recv(iSocket, pcBuf, iSize, 0);
570 sprintf(cMsg,
"\n-E- receiving data from server");
572 cout <<
" CTL C detected (during recv data)" << endl;
578 sprintf(cMsg,
"\n-E- receiving data from server");
582 if (iRetryRecv > iRetryRecvLim)
587 else goto gNextRecvD;
592 if ( (iDebug == 2) || (iDebug == 3) )
594 cout <<
"-E- receiving data: connection closed by server"
615 cout <<
" CTL C detected (after recv data)" << endl;
619 iBufNoServ = ntohl(piBuf[1]);
620 iEvtBuf = ntohl(piBuf[2]);
624 printf(
" dummy buffer no. %d, %d events\n",
625 iBufNoServ, iEvtBuf);
626 printf(
"*** connection to remote event server okay, but currently no DAQ events (%d)\n",
633 if ( (piBuf[3] < 1) || (piBuf[3] >= iBufSize) )
644 swaplw( (Long_t*)(&piBuf[1+iHeadPar]), (iBufSize/iint)-iHeadPar, 0);
645 if ( (iBufNo == 0) && (iDebug) )
646 cout <<
" Event data swapped" << endl;
653 iBufNo1 = iBufNoServ;
655 iBufNo2 = iBufNoServ;
659 printf(
"%d:", iBufNoServ);
665 cout << endl <<
"buffer " << iBufNo
666 <<
" (" << iBufNoServ <<
"): "
668 << iBufSize <<
" byte" << endl;
672 piNextEvt = piBuf + iHeadPar+1;
678 piNextEvt += iEvtPar;
685 iBufNoServ = ntohl(piBuf[1]);
686 iBufNo1 = iBufNoServ;
692 ibyte = (piNextEvt[0]/8)*8;
693 if ( ibyte < piNextEvt[0] ) ibyte += 8;
694 iEvtPar = ibyte/iint;
698 cout <<
" event no. " << iEvtNo <<
" (" << piNextEvt[3]
699 <<
"): id " << piNextEvt[2] <<
", size " << piNextEvt[0]
705 irem = piNextEvt[0] - 32;
706 while ( (irem > 0) && (ii < imaxSE) )
709 if (ii == 1) ioff = 8;
710 else ioff += ibyte/iint;
713 ibyte = (piNextEvt[ioff]/8)*8;
714 if ( ibyte < piNextEvt[ioff] )
717 if (irem <= piNextEvt[ioff])
718 irem -= piNextEvt[ioff];
721 cout <<
" subevent " << ii <<
": size "
723 if (irem <= 0) cout << endl;
724 else cout <<
", padded " << ibyte
725 <<
", remainder " << irem << endl;
728 cout <<
"-W- only " << ii <<
" subevents tested" << endl;
732 if (iDebug == 2)
for (jj=1; jj<=ii; jj++)
735 cout <<
" subevent " << jj <<
": size " << piNextEvt[ind]
736 <<
" (index " << ind <<
")" << endl;
742 cout << endl <<
"-D- CTL C specified";
743 if (iDebug) cout <<
" (at end RevGet)" << endl;
748 if (iEvtNo == iEvtMax)
750 cout << endl <<
"-I- all required events ("
751 << iEvtMax <<
") received: " << iBufNo <<
" buffers ("
752 << iBufNo1 <<
" - " << iBufNo2 <<
")" << endl;
755 return( (UInt_t *) piNextEvt);
758 if ( (iError) || (
imySig == -1) )
761 cout <<
" RevGet: closing connection to server";
762 iRC =
rclose(&iSocket, 2);
763 if ( (iDebug) && (iRC == 0) )
764 cout <<
" - done" << endl;
768 cout <<
" RevGet: keeping connection to server" << endl;
785 if (iWait > 0) sleep(iWait);
795 Int_t iCommSize =
sizeof(sComm);
798 if (iSocket == 0)
return;
801 piComm = &(sComm.
iSize);
802 sComm.
iSize = htonl(iCommSize-
sizeof(
int));
803 sComm.
iMode = htonl(1);
808 cout <<
"-D- send communication buffer (close request, size data "
809 << ntohl(sComm.
iSize) <<
" byte): "
810 << ntohl(sComm.
iMode) <<
", "
813 iRC = pSocket->SendRaw(piComm, iCommSize, kDefault);
815 cout <<
"-E- sending close request to server, rc = "
817 else if (iDebug == 1)
818 cout <<
" close request sent" << endl;
821 cout <<
" RevClose: closing connection to server";
822 iRC =
rclose(&iSocket, 2);
823 if ( (iDebug) && (iRC == 0) )
824 cout <<
" - done" << endl;
827 cout <<
"-I- connection to server closed" << endl;
TSocket * RevOpen(const Char_t *pNode, Int_t iPort, Int_t iEvent)
static Int_t rclose(Int_t *piSocket, Int_t iMode)
void RevClose(TSocket *pSocket)
ClassImp(HRevBuffer) Int_t iTimeOut
static Long_t swaplw(Long_t *pp_source, Long_t l_len, Long_t *pp_dest)
void RevBufWait(Int_t iWait)
static void exitCli(Int_t signal)
UInt_t * RevGet(TSocket *pSocket, Int_t iFlush)