Ejemplo n.º 1
0
  /**********************************************
   * client to                  ZDENEK HONS SERVER
   *              cat runx.dat |  nc -l -p 9302
   *                         this is a client that connects to a server...
   */
int* PLUG(int* par, int* par2){
 


  concurrent_queue<int> *buffer=(concurrent_queue<int>*)par;

   char ch[200];
   //   if(XTERM!=NULL)fprintf(XTERM,"PUSH RS push-remote (network)  par==%d; pointer==%d\n", par,(int)buffer );
   sprintf(ch,"%s","PUSHNET:  entered..." );table_log(0,ch);

      Long64_t cnt=0;

   char ipaddress[100];
   int port;


   //-------- here I will control with    control.mmap    file------   
    if ((mmapfd = open("control.mmap", O_RDWR, 0)) == -1) err(1, "open");
    mmap_file=(char*)mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED, mmapfd, 0);
    if (mmap_file == MAP_FAILED) errx(1, "either mmap");
    char mmap_result[100];
   //-------- here I will control with    control.mmap    file------   
  char  acqxml2[100];
  TokenGet( "file=" , mmap_file , acqxml2 ); // takes a value from mmap



      TSmallish_xml xml(    acqxml2   );
      xml.DisplayTele( xml.mainnode, 0, "plugins","pusher","ip" );
      sprintf( ipaddress,"%s", xml.output  );
      xml.DisplayTele( xml.mainnode, 0, "plugins","pusher","port" );
      port=atoi(xml.output  );



      //original  char strbuf[2000000];// 20MB
  char strbuf[4000000];// 2MB 711kB; 4MB 1700kB
  int *buffer4p;
  buffer4p=(int*)&strbuf[0];
  int d,i,ii;
  int maxtrans=2000000;
  TSocket *socket;

  double   resrun=1.0; 
  //  int downtimef;//, downtime;

  int trials=10; //10 seconds of timeouts

  
  while (resrun>0.0){// ----- - -- READ ALL REPEATING CONNECTIONS --- -  -- -- - --   - - -

    //  DRUHA STRANA LISTENS !!!!!!!!!!!!!!!!!!!!!!!!!!

    resrun=TokenGet(  "run=", mmap_file , mmap_result ); // if run==0 => KILL HERE
    if (resrun<1.0){ break;}


    Cat Blbka;
    Blbka.Print();

   socket=Blbka.GetSocket( ipaddress, port ) ;
 
   if ( Blbka.WasTimeOut()!=0 ) {
     sprintf(ch,"P %s\n", "After TSocket - fTimeOut==1"); table_log(0,ch);
     break;
   }
   int ii_init;
   trials=10; //GOOD TO BE DEFINED IN XML  as also select timeout
   ii_init=0;// offset if data%4 != 0


    resrun=TokenGet(  "run=", mmap_file , mmap_result ); // if run==0 => KILL HERE
    if (resrun<1.0){ break;}


   while ( (socket)&&(resrun>0.0) ){// ----- - -- READ ONE CONNECTION -------


     //DANGER THAT I MISS 3/10 of EVENTS..... MAYBE THIS IS TO TUNE:
     //3000:50 ==1.6%
     // i==0 => TIMEOUT...... ??
     //  FINALY  2sec timeout, 10x repeat, 50ms wait (TO BE TESTED)
     
     if (PUSHDEBUG!=0){sprintf(ch,"PUSH-net    waiting result=%d ", i ); table_log(0,ch);}
     i=(int)socket->Select(TSocket::kRead, 2000);//timeout 1sec, repeat 5x 
     if (PUSHDEBUG!=0){sprintf(ch,"PUSH-net    Select result i=%d ", i ); table_log(0,ch);}
     
     
     //d=0;//nevim jestli to tu nedela binec
     if (i>0) {//####CASE i>0 ####
       //--  sprintf(ch,"P %s\n", "before recvraw"); table_log(0,ch);
       
       d=(int)socket->RecvRaw( &strbuf[ii_init], maxtrans, kDontBlock  ); // read small buffer
       
       if (PUSHDEBUG!=0){ sprintf(ch,"PUSH-netw socket got %d bytes; init=%d ", d, ii_init ); table_log(0,ch);}
       //     ii=0;
       
       int sizenow;
       //---------------------------------------
       for (ii=0;4*ii<d-(d%4)+ii_init;ii++){
	 //     while( (ii*4<d)&&(d>=4) ){
	 buffer->push( buffer4p[ii] ); 
	 //this helps	 if ( i %10 == 0 ){usleep(1); }
	 /*
	   sizenow= buffer->size();
	 while (sizenow>5000){
	   usleep(100000); 
	   sizenow=buffer->size();
	 }
	 */
	 //  usleep(100000);
	 if (PUSHDEBUG!=0){ sprintf(ch,"%4lld ii= %4d  %08x   %4d",cnt,ii,  buffer4p[ii],d ); table_log(0,ch);}
	 //	ii++;
	 if ((cnt%25000)==0){
	    sizenow= buffer->size();
	   sprintf(ch,"P %7.1f MB : %d",4.*cnt/1000000, sizenow ); table_log(0,ch);    
	   resrun=TokenGet(  "run=", mmap_file , mmap_result ); // if run==0 => KILL HERE
	 
	   if (resrun<1.0){ break;}
	   // if (wait==0) {break;}
	   usleep(100000);
	 }
	 cnt++;
       }//for loop  xwhile - push
       
       
       if (PUSHDEBUG!=0){ sprintf(ch,"PUSH-net  modulo %4d  buf4pii=%08x ",((d+ii_init)%4) , buffer4p[ii] ); table_log(0,ch);}

       
       // I assume thAT D IS even
       if (( (d+ii_init)%4)==0){  ii_init=0;}else{
	 memcpy(&strbuf[0],&strbuf[4*(ii)], 2);
	 memcpy(&strbuf[2],&strbuf[4*(ii)], 2);
	 //       memcpy(&strbuf[0],&strbuf[4*(ii)], d%4);
	 ii_init=2;
       }
       
       if (PUSHDEBUG!=0){ sprintf(ch,"PUSH-net  offset %4d  buf4p0 =%08x ", ii_init , buffer4p[0] ); table_log(0,ch);}
     }//#### if i>0
     
     
 
     resrun=TokenGet(  "run=", mmap_file , mmap_result ); // if run==0 => KILL HERE

     if (resrun<1.0){ 
       sprintf(ch,"PUSH got BROADCAST SIGNAL... %s\n", "" );table_log(0,ch);
       socket->Close(); 
       sprintf(ch,"PUSH socket closed... %s\n", "" );table_log(0,ch);
     }//if wait ==0
     



     if (resrun>0.0){
       
       if (i<0){ //####CASE i<0 ####
	 sprintf(ch,"PUSH SOCKET LOST...%s; iii*4=%d, d=%d\n", ipaddress,ii*4,d );table_log(0,ch);
	 usleep(2000*1000);    
	 //============this helped to have  nice STOP THREAD ============
	 	 if (PUSHDEBUG!=0){sprintf(ch,"PUSH-net    closing,breakin=%d ", i ); table_log(0,ch);}
	 	 socket->Close(); // i dont know // delete socket;
	 	 if (PUSHDEBUG!=0){sprintf(ch,"PUSH-net    closed ,breaked=%d ", i ); table_log(0,ch);}
        break; // i removed this to have easy thread stop // 
       }//####CASE i<0 #### 
       


       if (i==0){ //####CASE i==0  ####
	 trials--;
	 sprintf(ch,"PUSH (ZERO)..%s;d=%d (%d)\n",ipaddress,d, trials);table_log(0,ch);
	 if (trials<=0){
	   sprintf(ch,"PUSH I RELEASE SOCKET(ZERO)..%s; iii*4==%d, d=%d",ipaddress,ii*4,d);table_log(0,ch);
	   socket->Close(); break; 
	 }//trials
       }//####     if  i==0
       
       
       
      //     if (resrun<1.0){break;}
     }//  if resrun >0.0
     if (resrun<1.0){socket->Close(); break;} // this must be
     if (PUSHDEBUG!=0){sprintf(ch,"PUSH-net    end of whil socket %d", i ); table_log(0,ch);}
     
   }// if sock and resrun>0.0
    //    if (wait==0){break;}
  }// while   resrun>0.0   ::::   wait!=0   1==1       WHILE read all the time - ONE CONNECTION


  sprintf(ch,"PUSH deleting socket..%s; iii*4==%d, d=%d",ipaddress,ii*4,d);table_log(0,ch);
  //  socket->Delete();
  delete socket;
  sprintf(ch,"PUSH socket deleted..%s; iii*4==%d, d=%d",ipaddress,ii*4,d);table_log(0,ch);
  

  //  if (wait!=0){ wait=MyCond.TimedWaitRelative( 5000  ) ; }else{ break;}
  //  if (wait==0)break;
  //       usleep(1000*300);
  //if(XTERM!=NULL)fprintf(XTERM,"S%s","" );
  
  //}// 0==0----- - -- READ ALL REPEATING CONNECTIONS --- -  -- -- - --   - - -
  
  
  
  // socket must be already closed here....socket->Close();
  //      if(XTERM!=NULL)fprintf(XTERM,"PUSH RS push-file call finished....%s: PUSHER FINISHED\n", ipaddress );
  sprintf(ch,"PUSH call finished....%s:%d\n", ipaddress,port );table_log(0,ch);
}/*****************************end of function ***********************/