Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
hclient.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_net
3 /// Client program which creates and fills a histogram. Every 1000 fills
4 /// the histogram is send to the server which displays the histogram.
5 ///
6 /// To run this demo do the following:
7 /// - Open three windows
8 /// - Start ROOT in all three windows
9 /// - Execute in the first window: .x hserv.C (or hserv2.C)
10 /// - Execute in the second and third windows: .x hclient.C
11 /// If you want to run the hserv.C on a different host, just change
12 /// "localhost" in the TSocket ctor below to the desired hostname.
13 ///
14 /// The script argument "evol" can be used when using a modified version
15 /// of the script where the clients and server are on systems with
16 /// different versions of ROOT. When evol is set to kTRUE the socket will
17 /// support automatic schema evolution between the client and the server.
18 ///
19 /// \macro_code
20 ///
21 /// \author Fons Rademakers
22 
23 void hclient(Bool_t evol=kFALSE)
24 {
25  gBenchmark->Start("hclient");
26 
27  // Open connection to server
28  TSocket *sock = new TSocket("localhost", 9090);
29 
30  // Wait till we get the start message
31  char str[32];
32  sock->Recv(str, 32);
33 
34  // server tells us who we are
35  int idx = !strcmp(str, "go 0") ? 0 : 1;
36 
37  Float_t messlen = 0;
38  Float_t cmesslen = 0;
39  if (idx == 1)
40  sock->SetCompressionLevel(1);
41 
42  TH1 *hpx;
43  if (idx == 0) {
44  // Create the histogram
45  hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
46  hpx->SetFillColor(48); // set nice fill-color
47  } else {
48  hpx = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
49  }
50 
51  TMessage::EnableSchemaEvolutionForAll(evol);
52  TMessage mess(kMESS_OBJECT);
53  //TMessage mess(kMESS_OBJECT | kMESS_ACK);
54 
55  // Fill histogram randomly
56  gRandom->SetSeed();
57  Float_t px, py;
58  const int kUPDATE = 1000;
59  for (int i = 0; i < 25000; i++) {
60  gRandom->Rannor(px,py);
61  if (idx == 0)
62  hpx->Fill(px);
63  else
64  hpx->Fill(px,py);
65  if (i && (i%kUPDATE) == 0) {
66  mess.Reset(); // re-use TMessage object
67  mess.WriteObject(hpx); // write object in message buffer
68  sock->Send(mess); // send message
69  messlen += mess.Length();
70  cmesslen += mess.CompLength();
71  }
72  }
73  sock->Send("Finished"); // tell server we are finished
74 
75  if (cmesslen > 0)
76  printf("Average compression ratio: %g\n", messlen/cmesslen);
77 
78  gBenchmark->Show("hclient");
79 
80  // Close the socket
81  sock->Close();
82 }