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;
   }
}
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;
   }
}
Beispiel #3
0
Datei: hserv.C Projekt: 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();
}