hserv2bonj.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    // Advertise our service using Bonjour
00028    TBonjourRegistrar *reg = new TBonjourRegistrar;
00029    reg->RegisterService(TBonjourRecord(Form("hserv2 on %s", gSystem->HostName()),
00030                                        "_hserv2._tcp", ""), 9090);
00031 
00032    // Open a server socket looking for connections on a named service or
00033    // on a specified port.
00034    //TServerSocket *ss = new TServerSocket("rootserv", kTRUE);
00035    TServerSocket *ss = new TServerSocket(9090, kTRUE);
00036 
00037    TMonitor *mon = new TMonitor;
00038 
00039    mon->Add(ss);
00040 
00041    TSocket *s0 = 0, *s1 = 0;
00042 
00043    while (1) {
00044       TMessage *mess;
00045       TSocket  *s;
00046 
00047       s = mon->Select();
00048 
00049       if (s->IsA() == TServerSocket::Class()) {
00050          if (!s0) {
00051             s0 = ((TServerSocket *)s)->Accept();
00052             s0->Send("go 0");
00053             mon->Add(s0);
00054          } else if (!s1) {
00055             s1 = ((TServerSocket *)s)->Accept();
00056             s1->Send("go 1");
00057             mon->Add(s1);
00058          } else
00059             printf("only accept two client connections\n");
00060 
00061          if (s0 && s1) {
00062             mon->Remove(ss);
00063             ss->Close();
00064          }
00065          continue;
00066       }
00067 
00068       s->Recv(mess);
00069 
00070       if (mess->What() == kMESS_STRING) {
00071          char str[64];
00072          mess->ReadString(str, 64);
00073          printf("Client %d: %s\n", s==s0 ? 0 : 1, str);
00074          mon->Remove(s);
00075          if (mon->GetActive() == 0) {
00076             printf("No more active clients... stopping\n");
00077             break;
00078          }
00079       } else if (mess->What() == kMESS_OBJECT) {
00080          //printf("got object of class: %s\n", mess->GetClass()->GetName());
00081          TH1 *h = (TH1 *)mess->ReadObject(mess->GetClass());
00082          if (h) {
00083             if (s == s0)
00084                pad1->cd();
00085             else
00086                pad2->cd();
00087             h->Print();
00088             h->DrawCopy();  //draw a copy of the histogram, not the histo itself
00089             c1->Modified();
00090             c1->Update();
00091             delete h;       // delete histogram
00092          }
00093       } else {
00094          printf("*** Unexpected message ***\n");
00095       }
00096 
00097       delete mess;
00098    }
00099 
00100    printf("Client 0: bytes recv = %d, bytes sent = %d\n", s0->GetBytesRecv(),
00101           s0->GetBytesSent());
00102    printf("Client 1: bytes recv = %d, bytes sent = %d\n", s1->GetBytesRecv(),
00103           s1->GetBytesSent());
00104 
00105    // Close the socket.
00106    s0->Close();
00107    s1->Close();
00108 }

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