hserv2.C

Go to the documentation of this file.
00001 {
00002    // This script shows how to make a simple iterative server that
00003    // can accept connections while handling currently open connections.
00004    // Compare this script to hserv.C that blocks on accept.
00005    // In this script a server socket is created and added to a monitor.
00006    // A monitor object is used to monitor connection requests on
00007    // the server socket. After accepting the connection
00008    // the new socket is added to the monitor and immediately ready
00009    // for use. Once two connections are accepted the server socket
00010    // is removed from the monitor and closed. The monitor continues
00011    // monitoring the sockets.
00012    //
00013    // To run this demo do the following:
00014    //   - Open three windows
00015    //   - Start ROOT in all three windows
00016    //   - Execute in the first window: .x hserv2.C
00017    //   - Execute in the second and third windows: .x hclient.C
00018    //Author: Fons Rademakers
00019    
00020    // Create canvas and pads to display the histograms
00021    TCanvas *c1 = new TCanvas("c1","The Ntuple canvas",200,10,700,780);
00022    TPad *pad1 = new TPad("pad1","This is pad1",0.02,0.52,0.98,0.98,21);
00023    TPad *pad2 = new TPad("pad2","This is pad2",0.02,0.02,0.98,0.48,21);
00024    pad1->Draw();
00025    pad2->Draw();
00026 
00027    // Open a server socket looking for connections on a named service or
00028    // on a specified port.
00029    //TServerSocket *ss = new TServerSocket("rootserv", kTRUE);
00030    TServerSocket *ss = new TServerSocket(9090, kTRUE);
00031 
00032    TMonitor *mon = new TMonitor;
00033 
00034    mon->Add(ss);
00035 
00036    TSocket *s0 = 0, *s1 = 0;
00037 
00038    while (1) {
00039       TMessage *mess;
00040       TSocket  *s;
00041 
00042       s = mon->Select();
00043 
00044       if (s->IsA() == TServerSocket::Class()) {
00045          if (!s0) {
00046             s0 = ((TServerSocket *)s)->Accept();
00047             s0->Send("go 0");
00048             mon->Add(s0);
00049          } else if (!s1) {
00050             s1 = ((TServerSocket *)s)->Accept();
00051             s1->Send("go 1");
00052             mon->Add(s1);
00053          } else
00054             printf("only accept two client connections\n");
00055 
00056          if (s0 && s1) {
00057             mon->Remove(ss);
00058             ss->Close();
00059          }
00060          continue;
00061       }
00062 
00063       s->Recv(mess);
00064 
00065       if (mess->What() == kMESS_STRING) {
00066          char str[64];
00067          mess->ReadString(str, 64);
00068          printf("Client %d: %s\n", s==s0 ? 0 : 1, str);
00069          mon->Remove(s);
00070          if (mon->GetActive() == 0) {
00071             printf("No more active clients... stopping\n");
00072             break;
00073          }
00074       } else if (mess->What() == kMESS_OBJECT) {
00075          //printf("got object of class: %s\n", mess->GetClass()->GetName());
00076          TH1 *h = (TH1 *)mess->ReadObject(mess->GetClass());
00077          if (h) {
00078             if (s == s0)
00079                pad1->cd();
00080             else
00081                pad2->cd();
00082             h->Print();
00083             h->DrawCopy();  //draw a copy of the histogram, not the histo itself
00084             c1->Modified();
00085             c1->Update();
00086             delete h;       // delete histogram
00087          }
00088       } else {
00089          printf("*** Unexpected message ***\n");
00090       }
00091 
00092       delete mess;
00093    }
00094 
00095    printf("Client 0: bytes recv = %d, bytes sent = %d\n", s0->GetBytesRecv(),
00096           s0->GetBytesSent());
00097    printf("Client 1: bytes recv = %d, bytes sent = %d\n", s1->GetBytesRecv(),
00098           s1->GetBytesSent());
00099 
00100    // Close the socket.
00101    s0->Close();
00102    s1->Close();
00103 }

Generated on Tue Jul 5 15:44:52 2011 for ROOT_528-00b_version by  doxygen 1.5.1