ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
hserv2.C
Go to the documentation of this file.
1 {
2  // This script shows how to make a simple iterative server that
3  // can accept connections while handling currently open connections.
4  // Compare this script to hserv.C that blocks on accept.
5  // In this script a server socket is created and added to a monitor.
6  // A monitor object is used to monitor connection requests on
7  // the server socket. After accepting the connection
8  // the new socket is added to the monitor and immediately ready
9  // for use. Once two connections are accepted the server socket
10  // is removed from the monitor and closed. The monitor continues
11  // monitoring the sockets.
12  //
13  // To run this demo do the following:
14  // - Open three windows
15  // - Start ROOT in all three windows
16  // - Execute in the first window: .x hserv2.C
17  // - Execute in the second and third windows: .x hclient.C
18  //Author: Fons Rademakers
19 
20  // Create canvas and pads to display the histograms
21  TCanvas *c1 = new TCanvas("c1","The Ntuple canvas",200,10,700,780);
22  TPad *pad1 = new TPad("pad1","This is pad1",0.02,0.52,0.98,0.98,21);
23  TPad *pad2 = new TPad("pad2","This is pad2",0.02,0.02,0.98,0.48,21);
24  pad1->Draw();
25  pad2->Draw();
26 
27  // Open a server socket looking for connections on a named service or
28  // on a specified port.
29  //TServerSocket *ss = new TServerSocket("rootserv", kTRUE);
30  TServerSocket *ss = new TServerSocket(9090, kTRUE);
31 
32  TMonitor *mon = new TMonitor;
33 
34  mon->Add(ss);
35 
36  TSocket *s0 = 0, *s1 = 0;
37 
38  while (1) {
39  TMessage *mess;
40  TSocket *s;
41 
42  s = mon->Select();
43 
44  if (s->IsA() == TServerSocket::Class()) {
45  if (!s0) {
46  s0 = ((TServerSocket *)s)->Accept();
47  s0->Send("go 0");
48  mon->Add(s0);
49  } else if (!s1) {
50  s1 = ((TServerSocket *)s)->Accept();
51  s1->Send("go 1");
52  mon->Add(s1);
53  } else
54  printf("only accept two client connections\n");
55 
56  if (s0 && s1) {
57  mon->Remove(ss);
58  ss->Close();
59  }
60  continue;
61  }
62 
63  s->Recv(mess);
64 
65  if (mess->What() == kMESS_STRING) {
66  char str[64];
67  mess->ReadString(str, 64);
68  printf("Client %d: %s\n", s==s0 ? 0 : 1, str);
69  mon->Remove(s);
70  if (mon->GetActive() == 0) {
71  printf("No more active clients... stopping\n");
72  break;
73  }
74  } else if (mess->What() == kMESS_OBJECT) {
75  //printf("got object of class: %s\n", mess->GetClass()->GetName());
76  TH1 *h = (TH1 *)mess->ReadObject(mess->GetClass());
77  if (h) {
78  if (s == s0)
79  pad1->cd();
80  else
81  pad2->cd();
82  h->Print();
83  h->DrawCopy(); //draw a copy of the histogram, not the histo itself
84  c1->Modified();
85  c1->Update();
86  delete h; // delete histogram
87  }
88  } else {
89  printf("*** Unexpected message ***\n");
90  }
91 
92  delete mess;
93  }
94 
95  printf("Client 0: bytes recv = %d, bytes sent = %d\n", s0->GetBytesRecv(),
96  s0->GetBytesSent());
97  printf("Client 1: bytes recv = %d, bytes sent = %d\n", s1->GetBytesRecv(),
98  s1->GetBytesSent());
99 
100  // Close the socket.
101  s0->Close();
102  s1->Close();
103 }
virtual void Remove(TSocket *sock)
Remove a socket from the monitor.
Definition: TMonitor.cxx:214
virtual Int_t Send(const TMessage &mess)
Send a TMessage object.
Definition: TSocket.cxx:520
virtual Int_t Recv(TMessage *&mess)
Receive a TMessage object.
Definition: TSocket.cxx:818
TH1 * h
Definition: legend2.C:5
virtual void Add(TSocket *sock, Int_t interest=kRead)
Add socket to the monitor's active list.
Definition: TMonitor.cxx:168
printf("Client 0: bytes recv = %d, bytes sent = %d\n", s0->GetBytesRecv(), s0->GetBytesSent())
UInt_t GetBytesSent() const
Definition: TSocket.h:149
TSocket * s1
Definition: hserv2.C:36
UInt_t GetBytesRecv() const
Definition: TSocket.h:150
virtual TObject * ReadObject(const TClass *cl)
Read object from I/O buffer.
TVirtualPad * cd(Int_t subpadnumber=0)
Set Current pad.
Definition: TPad.cxx:514
void Class()
Definition: Class.C:29
virtual char * ReadString(char *s, Int_t max)
Read string from I/O buffer.
virtual TH1 * DrawCopy(Option_t *option="", const char *name_postfix="_copy") const
Copy this histogram and Draw in the current pad.
Definition: TH1.cxx:2925
virtual void Draw(Option_t *option="")
Draw Pad in Current pad (re-parent pad if necessary).
Definition: TPad.cxx:1192
TSocket * Select()
Return pointer to socket for which an event is waiting.
Definition: TMonitor.cxx:322
virtual void Close(Option_t *opt="")
Close the socket.
Definition: TSocket.cxx:388
virtual void Print(Option_t *option="") const
Print some global quantities for this histogram.
Definition: TH1.cxx:6573
The most important graphics class in the ROOT system.
Definition: TPad.h:46
The Canvas class.
Definition: TCanvas.h:48
Int_t GetActive(Long_t timeout=-1) const
Return number of sockets in the active list.
Definition: TMonitor.cxx:438
The TH1 histogram class.
Definition: TH1.h:80
UInt_t What() const
Definition: TMessage.h:80
TClass * GetClass() const
Definition: TMessage.h:76
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2179
const Bool_t kTRUE
Definition: Rtypes.h:91
void Modified(Bool_t flag=1)
Definition: TPad.h:407