Logo ROOT   6.16/01
Reference Guide
hclientbonj.C File Reference

Detailed Description

Client program which creates and fills a histogram.

Every 1000 fills the histogram is send to the server which displays the histogram.

To run this demo do the following:

The script argument "evol" can be used when using a modified version of the script where the clients and server are on systems with different versions of ROOT. When evol is set to kTRUE the socket will support automatic schema evolution between the client and the server.

#include "TBenchmark.h"
#include "TList.h"
#include "TInetAddress.h"
#include "TSocket.h"
#include "TMessage.h"
#include "TH1.h"
#include "TH2.h"
#include "TRandom.h"
#include "TBonjourRecord.h"
static Bool_t gEvo = kFALSE;
void ConnectToServer(const TInetAddress *hostb, Int_t port)
{
// Called by the Bonjour resolver with the host and port to which
// we can connect.
// Connect only once...
TInetAddress host = *hostb;
delete resolver;
printf("ConnectToServer: host = %s, port = %d\n", host.GetHostName(), port);
//--- Here starts original hclient.C code ---
// Open connection to server
TSocket *sock = new TSocket(host.GetHostName(), port);
// Wait till we get the start message
char str[32];
sock->Recv(str, 32);
// server tells us who we are
int idx = !strcmp(str, "go 0") ? 0 : 1;
Float_t messlen = 0;
Float_t cmesslen = 0;
if (idx == 1)
TH1 *hpx;
if (idx == 0) {
// Create the histogram
hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
hpx->SetFillColor(48); // set nice fill-color
} else {
hpx = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
}
//TMessage mess(kMESS_OBJECT | kMESS_ACK);
// Fill histogram randomly
Float_t px, py;
const int kUPDATE = 1000;
for (int i = 0; i < 25000; i++) {
gRandom->Rannor(px,py);
if (idx == 0)
hpx->Fill(px);
else
hpx->Fill(px,py);
if (i && (i%kUPDATE) == 0) {
mess.Reset(); // re-use TMessage object
mess.WriteObject(hpx); // write object in message buffer
sock->Send(mess); // send message
messlen += mess.Length();
cmesslen += mess.CompLength();
}
}
sock->Send("Finished"); // tell server we are finished
if (cmesslen > 0)
printf("Average compression ratio: %g\n", messlen/cmesslen);
gBenchmark->Show("hclient");
// Close the socket
sock->Close();
}
void UpdateBonjourRecords(TList *records)
{
// Browse for Bonjour record of type "_hserv2._tcp." in domain "local.".
// When found, create Bonjour resolver to get host and port of this record.
static Bool_t resolved = kFALSE;
// we can be called multiple times whenever a new server appears
printf("UpdateBonjourRecords (resolved = %s)\n", resolved ? "kTRUE" : "kFALSE");
if (resolved) return;
// Look for _hserv2._tcp. in local. domain and try to resolve it
TIter next(records);
while ((rec = (TBonjourRecord*) next())) {
if (!strcmp(rec->GetRegisteredType(), "_hserv2._tcp.") &&
!strcmp(rec->GetReplyDomain(), "local.")) {
rec->Print();
resolver->Connect("RecordResolved(TInetAddress*,Int_t)", 0, 0,
"ConnectToServer(TInetAddress*,Int_t)");
resolver->ResolveBonjourRecord(*rec);
resolved = kTRUE;
}
}
}
void hclientbonj(Bool_t evol=kFALSE)
{
gEvo = evol;
gBenchmark->Start("hclient");
browser->Connect("CurrentBonjourRecordsChanged(TList*)", 0, 0,
"UpdateBonjourRecords(TList*)");
browser->BrowseForServiceType("_hserv2._tcp");
}
@ kMESS_OBJECT
Definition: MessageTypes.h:35
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: RtypesCore.h:88
bool Bool_t
Definition: RtypesCore.h:59
float Float_t
Definition: RtypesCore.h:53
const Bool_t kTRUE
Definition: RtypesCore.h:87
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:59
R__EXTERN void * gTQSender
Definition: TQObject.h:45
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:174
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:157
Int_t BrowseForServiceType(const char *serviceType)
Tell Bonjour to start browsing for a specific type of service.
const char * GetReplyDomain() const
void Print(Option_t *opt="") const
Print TBonjourRecord.
const char * GetRegisteredType() const
Int_t ResolveBonjourRecord(const TBonjourRecord &record)
Resolve Bonjour service to IP address and port.
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:571
The TH1 histogram class.
Definition: TH1.h:56
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3251
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:250
This class represents an Internet Protocol (IP) address.
Definition: TInetAddress.h:36
const char * GetHostName() const
Definition: TInetAddress.h:71
A doubly linked list.
Definition: TList.h:44
static void EnableSchemaEvolutionForAll(Bool_t enable=kTRUE)
Static function enabling or disabling the automatic schema evolution.
Definition: TMessage.cxx:116
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:867
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:589
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:481
void SetCompressionLevel(Int_t level=ROOT::RCompressionSetting::ELevel::kUseMin)
See comments for function SetCompressionSettings.
Definition: TSocket.cxx:1059
virtual Int_t Recv(TMessage *&mess)
Receive a TMessage object.
Definition: TSocket.cxx:817
virtual void Close(Option_t *opt="")
Close the socket.
Definition: TSocket.cxx:389
virtual Int_t Send(const TMessage &mess)
Send a TMessage object.
Definition: TSocket.cxx:522
Author
Fons Rademakers

Definition in file hclientbonj.C.