Ejemplo n.º 1
0
int authserv(int po = 3000)
{

   UChar_t oauth = kSrvAuth;

   TServerSocket *ss = 0;
   TSocket *s = 0;

   cout << "authserv: starting a (parallel) server socket on port "
        << po << " with authentication" << endl;
 
   ss = new TPServerSocket(po);

   // Get the connection
   s = ss->Accept(oauth);

   // Print out;
   if (s) 
      if (s->IsAuthenticated()) 
         cout << "authserv: srv auth socket: OK" << endl;
      else
         cout << "authserv: srv auth socket: failed" << endl;

   // Cleanup
   if (s) delete s;
   if (ss) delete ss;
}
void KVINDRAOnlineDataAnalyser::ecouteSockets(void* arg)
{
   // static method, lance dans un thread
   // listen to the sockets

   TThread::Printf("<KVINDRAOnlineDataAnalyser::ecouteSockets> : beginning thread");
   KVINDRAOnlineDataAnalyser* ANA = (KVINDRAOnlineDataAnalyser*)arg;
   TServerSocket* fServer = new TServerSocket(ANA->port, kTRUE);

   if (!fServer->IsValid()) TThread::Printf("<KVINDRAOnlineDataAnalyser::ecouteSockets> : Port %d not valid", ANA->port);
   TMonitor* fMonitor = new TMonitor;
   fMonitor->Add(fServer);

   while (1) {

      TSocket* ss = fMonitor->Select();
      if (ss <= (TSocket*)0) continue;

      if (ss->IsA() == TServerSocket::Class()) {
         // new connection ?
         TSocket* s0 = fServer->Accept();
         if (s0) {
            TThread::Printf("<KVINDRAOnlineDataAnalyser::ecouteSockets> : Client found at %s",
                            s0->GetInetAddress().GetHostName());
            fMonitor->Add(s0);
         }
         continue;
      }
      if (ss->IsA() != TSocket::Class()) continue;

      TMessage* mess;
      if (ss->Recv(mess) <= 0) {
         // socket has been closed (?)
         TThread::Printf("<KVINDRAOnlineDataAnalyser::ecouteSockets> : connection from %s has been closed", ss->GetInetAddress().GetHostName());
         fMonitor->Remove(ss);
         delete ss;
         continue;
      }

      if (mess->What() == kMESS_STRING) {

         // deal with commands
         char str[256];
         mess->ReadString(str, 256);
         TThread::Printf("<KVINDRAOnlineDataAnalyser::ecouteSockets> : Received: %s", str);
         TString command(str);
         //command.ToUpper();
         ANA->HandleCommands(command, ss);

      }/* else if (mess->What() == kMESS_OBJECT) {
         TThread::Printf("<KVINDRAOnlineDataAnalyser::ecouteSockets> : Received UN P***IN d'OBJET!!!");
      } else {
         TThread::Printf("<KVINDRAOnlineDataAnalyser::ecouteSockets> : *** Unexpected message ***");
      }*/

      delete mess;
   }
}
Ejemplo n.º 3
0
int CCommProxy::Initialize(configInfo *config)
{
    m_pConfig = config;
    string sIp = m_pConfig->nodeInfo[m_pConfig->remoteNodeId].ip;
    int port = m_pConfig->nodeInfo[m_pConfig->remoteNodeId].port;
    cout << "Trying to connect to " << sIp  << ":" << port << endl;
    TSocket *tsock = new TSocket(sIp.c_str(), port);
    tsock->setConnTimeout(20000); //20secs
    boost::shared_ptr<TTransport> socket(tsock);
    boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
    boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
    pClient = new DynLBServerClient(protocol);
    transport->open();
    return SUCCESS;
}
Ejemplo n.º 4
0
void TClientInfoServerHandler::processContext(void* connectionContext,
                                              shared_ptr<TTransport> transport) {
  Connect* call = static_cast<Connect*>(connectionContext);
  if (call->callInfo_ == NULL) {
    if (typeid(*(transport.get())) == typeid(TSocket)) {
      TSocket* tsocket = static_cast<TSocket*>(transport.get());
      int fd = tsocket->getSocketFD();
      if (fd < 0) {
        return;
      }
      call->callInfo_ = call->clientInfo_->getConnection(fd, true);
      assert(call->callInfo_ != NULL);
      socklen_t len;
        call->callInfo_->recordAddr(tsocket->getCachedAddress(&len));
    }
  }
}
void fastMergeServer(bool cache = false) {
   // This script shows how to make a simple iterative server that
   // can receive TMemFile from multiple clients and merge them into 
   // a single file without block.
   //
   // Note: This server assumes that the client will reset the histogram
   // after each upload to simplify the merging.
   // 
   // This server can accept connections while handling currently open connections.
   // Compare this script to hserv.C that blocks on accept.
   // In this script a server socket is created and added to a monitor.
   // A monitor object is used to monitor connection requests on
   // the server socket. After accepting the connection
   // the new socket is added to the monitor and immediately ready
   // for use. Once two connections are accepted the server socket
   // is removed from the monitor and closed. The monitor continues
   // monitoring the sockets.
   //
   // To run this demo do the following:
   //   - Open three windows
   //   - Start ROOT in all three windows
   //   - Execute in the first window: .x fastMergerServer.C
   //   - Execute in the second and third windows: .x treeClient.C
   //Author: Fons Rademakers
   
   // Open a server socket looking for connections on a named service or
   // on a specified port.
   //TServerSocket *ss = new TServerSocket("rootserv", kTRUE);
   TServerSocket *ss = new TServerSocket(9090, kTRUE);
   if (!ss->IsValid()) {
      return;
   }

   TMonitor *mon = new TMonitor;
   
   mon->Add(ss);

   UInt_t clientCount = 0;
   TMemFile *transient = 0;
   
   TFileMerger merger(kFALSE,kFALSE);
   merger.SetPrintLevel(0);

   enum StatusKind {
      kStartConnection = 0,
      kProtocol = 1,
      
      kProtocolVersion = 1
   };
   if (cache) new TFileCacheWrite(merger.GetOutputFile(),32*1024*1024);
   while (1) {
      TMessage *mess;
      TSocket  *s;

      s = mon->Select();

      if (s->IsA() == TServerSocket::Class()) {
         if (clientCount > 100) {
            printf("only accept 100 clients connections\n");
            mon->Remove(ss);
            ss->Close();         
         } else {
            TSocket *client = ((TServerSocket *)s)->Accept();
            client->Send(clientCount, kStartConnection);
            client->Send(kProtocolVersion, kProtocol);
            ++clientCount;
            mon->Add(client);
            printf("Accept %d connections\n",clientCount);
         }
         continue;
      }
      
      s->Recv(mess);

      if (mess==0) {
         Error("fastMergeServer","The client did not send a message\n");
      } else if (mess->What() == kMESS_STRING) {
         char str[64];
         mess->ReadString(str, 64);
         printf("Client %d: %s\n", clientCount, str);
         mon->Remove(s);
         printf("Client %d: bytes recv = %d, bytes sent = %d\n", clientCount, s->GetBytesRecv(),
                s->GetBytesSent());
         s->Close();
         --clientCount;
         if (mon->GetActive() == 0 || clientCount == 0) {
            printf("No more active clients... stopping\n");
            break;
         }
      } else if (mess->What() == kMESS_ANY) {

         Long64_t length;
         TString filename;
         Int_t clientId;
         mess->ReadInt(clientId);
         mess->ReadTString(filename);
         mess->ReadLong64(length); // '*mess >> length;' is broken in CINT for Long64_t.

         Info("fastMergeServer","Receive input from client %d for %s",clientId,filename.Data());
         
         delete transient;
         transient = new TMemFile(filename,mess->Buffer() + mess->Length(),length);
         mess->SetBufferOffset(mess->Length()+length);
         merger.OutputFile(filename,"UPDATE");
         merger.AddAdoptFile(transient);

         merger.PartialMerge(TFileMerger::kAllIncremental);
         transient = 0;
      } else if (mess->What() == kMESS_OBJECT) {
         printf("got object of class: %s\n", mess->GetClass()->GetName());
      } else {
         printf("*** Unexpected message ***\n");
      }

      delete mess;
   }
}
Ejemplo n.º 6
0
void ConnectToServer(const TInetAddress *hostb, Int_t port)
{
   // Called by the Bonjour resolver with the host and port to which
   // we can connect.

   // Connect only once...
   TBonjourResolver *resolver = (TBonjourResolver*) gTQSender;
   TInetAddress host = *hostb; 
   delete resolver;

   printf("ConnectToServer: host = %s, port = %d\n", host.GetHostName(), port);

   //--- Here starts original hclient.C code ---

   // Open connection to server
   TSocket *sock = new TSocket(host.GetHostName(), port);

   // Wait till we get the start message
   char str[32];
   sock->Recv(str, 32);

   // server tells us who we are
   int idx = !strcmp(str, "go 0") ? 0 : 1;

   Float_t messlen  = 0;
   Float_t cmesslen = 0;
   if (idx == 1)
      sock->SetCompressionLevel(1);

   TH1 *hpx;
   if (idx == 0) {
      // Create the histogram
      hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
      hpx->SetFillColor(48);  // set nice fillcolor
   } else {
      hpx = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
   }

   TMessage::EnableSchemaEvolutionForAll(gEvo);
   TMessage mess(kMESS_OBJECT);
   //TMessage mess(kMESS_OBJECT | kMESS_ACK);

   // Fill histogram randomly
   gRandom->SetSeed();
   Float_t px, py;
   const int kUPDATE = 1000;
   for (int i = 0; i < 25000; i++) {
      gRandom->Rannor(px,py);
      if (idx == 0)
         hpx->Fill(px);
      else
         hpx->Fill(px,py);
      if (i && (i%kUPDATE) == 0) {
         mess.Reset();              // re-use TMessage object
         mess.WriteObject(hpx);     // write object in message buffer
         sock->Send(mess);          // send message
         messlen  += mess.Length();
         cmesslen += mess.CompLength();
      }
   }
   sock->Send("Finished");          // tell server we are finished

   if (cmesslen > 0)
      printf("Average compression ratio: %g\n", messlen/cmesslen);

   gBenchmark->Show("hclient");

   // Close the socket
   sock->Close();
}
Ejemplo n.º 7
0
Archivo: hserv.C Proyecto: Y--/root
void hserv() {
   // Open a server socket looking for connections on a named service or
   // on a specified port.
   //TServerSocket *ss = new TServerSocket("rootserv", kTRUE);
   TServerSocket *ss = new TServerSocket(9090, kTRUE);

   // Accept a connection and return a full-duplex communication socket.
   TSocket *s0 = ss->Accept();
   TSocket *s1 = ss->Accept();

   // tell the clients to start
   s0->Send("go 0");
   s1->Send("go 1");

   // Close the server socket (unless we will use it later to wait for
   // another connection).
   ss->Close();

   // Check some options of socket 0.
   int val;
   s0->GetOption(kSendBuffer, val);
   printf("sendbuffer size: %d\n", val);
   s0->GetOption(kRecvBuffer, val);
   printf("recvbuffer size: %d\n", val);

   // Get the remote addresses (informational only).
   TInetAddress adr = s0->GetInetAddress();
   adr.Print();
   adr = s1->GetInetAddress();
   adr.Print();

   // Create canvas and pads to display the histograms
   TCanvas *c1 = new TCanvas("c1","The Ntuple canvas",200,10,700,780);
   TPad *pad1 = new TPad("pad1","This is pad1",0.02,0.52,0.98,0.98,21);
   TPad *pad2 = new TPad("pad2","This is pad2",0.02,0.02,0.98,0.48,21);
   pad1->Draw();
   pad2->Draw();

   TMonitor *mon = new TMonitor;

   mon->Add(s0);
   mon->Add(s1);

   while (1) {
      TMessage *mess;
      TSocket  *s;

      s = mon->Select();

      s->Recv(mess);

      if (mess->What() == kMESS_STRING) {
         char str[64];
         mess->ReadString(str, 64);
         printf("Client %d: %s\n", s==s0 ? 0 : 1, str);
         mon->Remove(s);
         if (mon->GetActive() == 0) {
            printf("No more active clients... stopping\n");
            break;
         }
      } else if (mess->What() == kMESS_OBJECT) {
         //printf("got object of class: %s\n", mess->GetClass()->GetName());
         TH1 *h = (TH1 *)mess->ReadObject(mess->GetClass());
         if (h) {
            if (s == s0)
               pad1->cd();
            else
               pad2->cd();
            h->Print();
            h->DrawCopy();  //draw a copy of the histogram, not the histo itself
            c1->Modified();
            c1->Update();
            delete h;       // delete histogram
         }
      } else {
         printf("*** Unexpected message ***\n");
      }

      delete mess;
   }

   printf("Client 0: bytes recv = %d, bytes sent = %d\n", s0->GetBytesRecv(),
          s0->GetBytesSent());
   printf("Client 1: bytes recv = %d, bytes sent = %d\n", s1->GetBytesRecv(),
          s1->GetBytesSent());

   // Close the socket.
   s0->Close();
   s1->Close();
}
Ejemplo n.º 8
0
bool NetAddress::setFromSocket( TSocket const& socket )
{
	int len = sizeof( mAddr );
	return ::getpeername( socket.getSocketObject() , (sockaddr*)&mAddr, &len) != 0;
}
Ejemplo n.º 9
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 ***********************/
Ejemplo n.º 10
0
void TRawServer :: handleEvent(TPollEvent * pevents, int n)
{
	for( int i = 0; i < n; i++ )
	{
		int err = SOCKERR_NONE;	

		TSocket * skp = (TSocket *)(pevents[i].data.ptr);
		UINT32 SelectFlags = skp->getSelectFlags();
		waitevents_t wevt = pevents[i].events;

		if( (wevt & (EPOLLIN|EPOLLERR)) && (SelectFlags & SF_ACCEPT) == SF_ACCEPT)
		{
			wevt = XPOLLACC;
		}

		if( (wevt & (EPOLLOUT|EPOLLERR)) &&	(SelectFlags & SF_CONNECT) == SF_CONNECT)
		{
			socklen_t errlen = sizeof(err);
			getsockopt(skp->getHandle(), SOL_SOCKET, SO_ERROR, &err, &errlen);
			wevt = XPOLLCNX;
		}

		if(wevt & EPOLLERR)
		{
			wevt = SelectFlags;
		}

		if( WAIT_EVENT_READ( wevt ) )
		{
			cout << "WAIT EVENT READ" << endl;
			skp->handleRead();
			skp->setMaskRead(false);
			skp->setMaskWrite(true);
			m_poller->modSocket(skp);
		}
		else if( WAIT_EVENT_WRITE( wevt ) )
		{
			cout << "WAIT EVENT WRITE" << endl;
			skp->handleWrite();
		//	pevents[i].data.ptr = NULL;
			m_poller->delSocket(skp);
		//	delete skp;

			if( m_sparesockets.size() >= MAX_SPARE_SOCKETS )
			{
				delete skp;
			}
			else
			{	
				skp->closeSocket();
				skp->setMaskRead(false);
				skp->setMaskWrite(false);
				m_sparesockets.push_back(skp);
			}
			
		}
		else if( WAIT_EVENT_ACCEPT( wevt ) )
		{
			cout << "WAIT EVENT ACCEPT" << endl;
			string ipaddr; UINT16 port;
			sockobj_t sock = dynamic_cast<ListenSocket*>(m_listensocket)->Accept(ipaddr, port);
			if(INVALID_SOCKET != sock )
			{
				TSocket *skp = NULL;
				
				if( !m_sparesockets.empty() )
				{   
					skp = m_sparesockets.front();
					m_sparesockets.pop_front();
				} 
				else
				{
					skp = new TcpSocket();
				}
				
				skp->attach(sock);
				skp->setMaskRead(true);
				m_poller->addSocket(skp);
				LOG(1)("connection from %s:%d\n", ipaddr.c_str(), port);
				cout <<"connection from " << ipaddr << ":"<< port << endl;
			} 
		}
		else if( WAIT_EVENT_CONNECT( wevt ) )
		{
			cout << "WAIT EVENT CONNECT" << endl;
		}
		else if( WAIT_EVENT_EXCEPT( wevt ) )
		{
			cout << "WAIT EVENT EXCEPT" << endl;
		}
	}
}
Ejemplo n.º 11
0
void treeClient(Bool_t evol=kFALSE) 
{
   // Client program which creates and fills 2 histograms and a TTree. 
   // Every 1000000 fills the histograms and TTree is send to the server which displays the histogram.
   //
   // To run this demo do the following:
   //   - Open at least 2 windows
   //   - Start ROOT in the first windows
   //   - Execute in the first window: .x fastMergeServer.C
   //   - Execute in the other windows: root.exe -b -l -q .x treeClient.C
   //     (You can put it in the background if wanted).
   // If you want to run the hserv.C on a different host, just change
   // "localhost" in the TSocket ctor below to the desired hostname.
   //
   //Author: Fons Rademakers, Philippe Canal
   
   gBenchmark->Start("treeClient");

   // Open connection to server
   TSocket *sock = new TSocket("localhost", 9090);
   if (!sock->IsValid()) {
      Error("treeClient","Could not establish a connection with the server %s:%d.","localhost",9090);
      return;
   }

   // Wait till we get the start message
   // server tells us who we are
   Int_t status, version, kind;
   sock->Recv(status, kind);
   if (kind != 0 /* kStartConnection */) 
   {
      Error("treeClient","Unexpected server message: kind=%d status=%d\n",kind,status);
      delete sock;
      return;
   }
   sock->Recv(version, kind);
   if (kind != 1 /* kStartConnection */) 
   {
      Fatal("treeClient","Unexpected server message: kind=%d status=%d\n",kind,status);
   } else {
      Info("treeClient","Connected to fastMergeServer version %d\n",version);
   }
   
   int idx = status;
   
   Float_t messlen  = 0;
   Float_t cmesslen = 0;

   TMemFile *file = new TMemFile("mergedClient.root","RECREATE");
   TH1 *hpx;
   if (idx == 0) {
      // Create the histogram
      hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
      hpx->SetFillColor(48);  // set nice fillcolor
   } else {
      hpx = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
   }
   Float_t px, py;
   TTree *tree = new TTree("tree","tree");
   tree->SetAutoFlush(4000000);
   tree->Branch("px",&px);
   tree->Branch("py",&py);
 
   TMessage::EnableSchemaEvolutionForAll(evol);
   TMessage mess(kMESS_OBJECT);

   // Fill histogram randomly
   gRandom->SetSeed();
   const int kUPDATE = 1000000;
   for (int i = 0; i < 25000000; ) {
      gRandom->Rannor(px,py);
      if (idx%2 == 0)
         hpx->Fill(px);
      else
         hpx->Fill(px,py);
      tree->Fill();
      ++i;
      if (i && (i%kUPDATE) == 0) {
         file->Write();
         mess.Reset(kMESS_ANY);              // re-use TMessage object
         mess.WriteInt(idx);
         mess.WriteTString(file->GetName());
         mess.WriteLong64(file->GetEND());   // 'mess << file->GetEND();' is broken in CINT for Long64_t
         file->CopyTo(mess);
         sock->Send(mess);          // send message
         messlen  += mess.Length();
         cmesslen += mess.CompLength();
         
         file->ResetAfterMerge(0);  // This resets only the TTree objects.
         hpx->Reset();
      }
   }
   sock->Send("Finished");          // tell server we are finished

   if (cmesslen > 0)
      printf("Average compression ratio: %g\n", messlen/cmesslen);

   gBenchmark->Show("hclient");

   // Close the socket
   sock->Close();
}