ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
hclientbonj.C
Go to the documentation of this file.
1 #include "TBenchmark.h"
2 #include "TList.h"
3 #include "TInetAddress.h"
4 #include "TSocket.h"
5 #include "TMessage.h"
6 #include "TH1.h"
7 #include "TH2.h"
8 #include "TRandom.h"
9 #include "TBonjourBrowser.h"
10 #include "TBonjourResolver.h"
11 #include "TBonjourRecord.h"
12 
13 
14 static Bool_t gEvo = kFALSE;
15 
16 void ConnectToServer(const TInetAddress *hostb, Int_t port)
17 {
18  // Called by the Bonjour resolver with the host and port to which
19  // we can connect.
20 
21  // Connect only once...
23  TInetAddress host = *hostb;
24  delete resolver;
25 
26  printf("ConnectToServer: host = %s, port = %d\n", host.GetHostName(), port);
27 
28  //--- Here starts original hclient.C code ---
29 
30  // Open connection to server
31  TSocket *sock = new TSocket(host.GetHostName(), port);
32 
33  // Wait till we get the start message
34  char str[32];
35  sock->Recv(str, 32);
36 
37  // server tells us who we are
38  int idx = !strcmp(str, "go 0") ? 0 : 1;
39 
40  Float_t messlen = 0;
41  Float_t cmesslen = 0;
42  if (idx == 1)
43  sock->SetCompressionLevel(1);
44 
45  TH1 *hpx;
46  if (idx == 0) {
47  // Create the histogram
48  hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
49  hpx->SetFillColor(48); // set nice fillcolor
50  } else {
51  hpx = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
52  }
53 
55  TMessage mess(kMESS_OBJECT);
56  //TMessage mess(kMESS_OBJECT | kMESS_ACK);
57 
58  // Fill histogram randomly
59  gRandom->SetSeed();
60  Float_t px, py;
61  const int kUPDATE = 1000;
62  for (int i = 0; i < 25000; i++) {
63  gRandom->Rannor(px,py);
64  if (idx == 0)
65  hpx->Fill(px);
66  else
67  hpx->Fill(px,py);
68  if (i && (i%kUPDATE) == 0) {
69  mess.Reset(); // re-use TMessage object
70  mess.WriteObject(hpx); // write object in message buffer
71  sock->Send(mess); // send message
72  messlen += mess.Length();
73  cmesslen += mess.CompLength();
74  }
75  }
76  sock->Send("Finished"); // tell server we are finished
77 
78  if (cmesslen > 0)
79  printf("Average compression ratio: %g\n", messlen/cmesslen);
80 
81  gBenchmark->Show("hclient");
82 
83  // Close the socket
84  sock->Close();
85 }
86 
88 {
89  // Browse for Bonjour record of type "_hserv2._tcp." in domain "local.".
90  // When found, create Bonjour resolver to get host and port of this record.
91 
92  static Bool_t resolved = kFALSE;
93 
94  // we can be called multiple times whenever a new server appears
95  printf("UpdateBonjourRecords (resolved = %s)\n", resolved ? "kTRUE" : "kFALSE");
96 
97  if (resolved) return;
98 
99  // Look for _hserv2._tcp. in local. domain and try to resolve it
100  TBonjourRecord *rec;
101  TIter next(records);
102  while ((rec = (TBonjourRecord*) next())) {
103  if (!strcmp(rec->GetRegisteredType(), "_hserv2._tcp.") &&
104  !strcmp(rec->GetReplyDomain(), "local.")) {
105  rec->Print();
106  TBonjourResolver *resolver = new TBonjourResolver;
107  resolver->Connect("RecordResolved(TInetAddress*,Int_t)", 0, 0,
108  "ConnectToServer(TInetAddress*,Int_t)");
109  resolver->ResolveBonjourRecord(*rec);
110  resolved = kTRUE;
111  }
112  }
113 }
114 
116 {
117  // Client program which creates and fills a histogram. Every 1000 fills
118  // the histogram is send to the server which displays the histogram.
119  //
120  // To run this demo do the following:
121  // - Open three windows
122  // - Start ROOT in all three windows
123  // - Execute in the first window: .x hserv.C (or hserv2.C)
124  // - Execute in the second and third windows: .x hclient.C
125  // If you want to run the hserv.C on a different host, just change
126  // "localhost" in the TSocket ctor below to the desired hostname.
127  //
128  // The script argument "evol" can be used when using a modified version
129  // of the script where the clients and server are on systems with
130  // different versions of ROOT. When evol is set to kTRUE the socket will
131  // support automatic schema evolution between the client and the server.
132  //
133  //Author: Fons Rademakers
134 
135  gEvo = evol;
136 
137  gBenchmark->Start("hclient");
138 
139  TBonjourBrowser *browser = new TBonjourBrowser;
140  browser->Connect("CurrentBonjourRecordsChanged(TList*)", 0, 0,
141  "UpdateBonjourRecords(TList*)");
142  browser->BrowseForServiceType("_hserv2._tcp");
143 }
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3159
virtual void Rannor(Float_t &a, Float_t &b)
Return 2 numbers distributed following a gaussian with mean=0 and sigma=1.
Definition: TRandom.cxx:460
const char * GetHostName() const
Definition: TInetAddress.h:75
ClassImp(TSeqCollection) Int_t TSeqCollection TIter next(this)
Return index of object in collection.
void UpdateBonjourRecords(TList *records)
Definition: hclientbonj.C:87
float Float_t
Definition: RtypesCore.h:53
Int_t ResolveBonjourRecord(const TBonjourRecord &record)
Resolve Bonjour service to IP address and port.
R__EXTERN void * gTQSender
Definition: TQObject.h:49
virtual Int_t Recv(TMessage *&mess)
Receive a TMessage object.
Definition: TSocket.cxx:818
This class represents an Internet Protocol (IP) address.
Definition: TInetAddress.h:40
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:155
void Print(Option_t *opt="") const
Print TBonjourRecord.
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
Int_t CompLength() const
Definition: TMessage.h:95
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
Float_t py
Definition: hprod.C:33
virtual void SetSeed(UInt_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:568
static Bool_t gEvo
Definition: hclientbonj.C:14
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:172
A doubly linked list.
Definition: TList.h:47
void Reset()
Reset the message buffer so we can use (i.e. fill) it again.
Definition: TMessage.cxx:171
Bool_t Connect(const char *signal, const char *receiver_class, void *receiver, const char *slot)
Non-static method is used to connect from the signal of this object to the receiver slot...
Definition: TQObject.cxx:1135
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:63
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:256
const char * GetReplyDomain() const
void ConnectToServer(const TInetAddress *hostb, Int_t port)
Definition: hclientbonj.C:16
static void EnableSchemaEvolutionForAll(Bool_t enable=kTRUE)
Static function enabling or disabling the automatic schema evolution.
Definition: TMessage.cxx:116
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
const char * GetRegisteredType() const
The TH1 histogram class.
Definition: TH1.h:80
void hclientbonj(Bool_t evol=kFALSE)
Definition: hclientbonj.C:115
Float_t px
Definition: hprod.C:33
Int_t Length() const
Definition: TBuffer.h:96
void WriteObject(const TObject *obj)
Write object to message buffer.
Definition: TMessage.cxx:418
Int_t BrowseForServiceType(const char *serviceType)
Tell Bonjour to start browsing for a specific type of service.
TH1F * hpx
Definition: hcons.C:32
const Bool_t kTRUE
Definition: Rtypes.h:91