hclientbonj.C

Go to the documentation of this file.
00001 #include "TBenchmark.h"
00002 #include "TList.h"
00003 #include "TInetAddress.h"
00004 #include "TSocket.h"
00005 #include "TMessage.h"
00006 #include "TH1.h"
00007 #include "TH2.h"
00008 #include "TRandom.h"
00009 #include "TBonjourBrowser.h"
00010 #include "TBonjourResolver.h"
00011 #include "TBonjourRecord.h"
00012 
00013 
00014 static Bool_t gEvo = kFALSE;
00015 
00016 void ConnectToServer(const TInetAddress *hostb, Int_t port)
00017 {
00018    // Called by the Bonjour resolver with the host and port to which
00019    // we can connect.
00020 
00021    // Connect only once...
00022    TBonjourResolver *resolver = (TBonjourResolver*) gTQSender;
00023    TInetAddress host = *hostb; 
00024    delete resolver;
00025 
00026    printf("ConnectToServer: host = %s, port = %d\n", host.GetHostName(), port);
00027 
00028    //--- Here starts original hclient.C code ---
00029 
00030    // Open connection to server
00031    TSocket *sock = new TSocket(host.GetHostName(), port);
00032 
00033    // Wait till we get the start message
00034    char str[32];
00035    sock->Recv(str, 32);
00036 
00037    // server tells us who we are
00038    int idx = !strcmp(str, "go 0") ? 0 : 1;
00039 
00040    Float_t messlen  = 0;
00041    Float_t cmesslen = 0;
00042    if (idx == 1)
00043       sock->SetCompressionLevel(1);
00044 
00045    TH1 *hpx;
00046    if (idx == 0) {
00047       // Create the histogram
00048       hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
00049       hpx->SetFillColor(48);  // set nice fillcolor
00050    } else {
00051       hpx = new TH2F("hpxpy","py vs px",40,-4,4,40,-4,4);
00052    }
00053 
00054    TMessage::EnableSchemaEvolutionForAll(gEvo);
00055    TMessage mess(kMESS_OBJECT);
00056    //TMessage mess(kMESS_OBJECT | kMESS_ACK);
00057 
00058    // Fill histogram randomly
00059    gRandom->SetSeed();
00060    Float_t px, py;
00061    const int kUPDATE = 1000;
00062    for (int i = 0; i < 25000; i++) {
00063       gRandom->Rannor(px,py);
00064       if (idx == 0)
00065          hpx->Fill(px);
00066       else
00067          hpx->Fill(px,py);
00068       if (i && (i%kUPDATE) == 0) {
00069          mess.Reset();              // re-use TMessage object
00070          mess.WriteObject(hpx);     // write object in message buffer
00071          sock->Send(mess);          // send message
00072          messlen  += mess.Length();
00073          cmesslen += mess.CompLength();
00074       }
00075    }
00076    sock->Send("Finished");          // tell server we are finished
00077 
00078    if (cmesslen > 0)
00079       printf("Average compression ratio: %g\n", messlen/cmesslen);
00080 
00081    gBenchmark->Show("hclient");
00082 
00083    // Close the socket
00084    sock->Close();
00085 }
00086 
00087 void UpdateBonjourRecords(TList *records)
00088 {
00089    // Browse for Bonjour record of type "_hserv2._tcp." in domain "local.".
00090    // When found, create Bonjour resolver to get host and port of this record.
00091 
00092    static Bool_t resolved = kFALSE;
00093 
00094    // we can be called multiple times whenever a new server appears
00095    printf("UpdateBonjourRecords (resolved = %s)\n", resolved ? "kTRUE" : "kFALSE");
00096 
00097    if (resolved) return;
00098 
00099    // Look for _hserv2._tcp. in local. domain and try to resolve it
00100    TBonjourRecord *rec;
00101    TIter next(records);
00102    while ((rec = (TBonjourRecord*) next())) {
00103       if (!strcmp(rec->GetRegisteredType(), "_hserv2._tcp.") &&
00104           !strcmp(rec->GetReplyDomain(), "local.")) {
00105          rec->Print();
00106          TBonjourResolver *resolver = new TBonjourResolver;
00107          resolver->Connect("RecordResolved(TInetAddress*,Int_t)", 0, 0,
00108                            "ConnectToServer(TInetAddress*,Int_t)");
00109          resolver->ResolveBonjourRecord(*rec);
00110          resolved = kTRUE;
00111       }
00112    }
00113 }
00114 
00115 void hclientbonj(Bool_t evol=kFALSE)
00116 {
00117    // Client program which creates and fills a histogram. Every 1000 fills
00118    // the histogram is send to the server which displays the histogram.
00119    //
00120    // To run this demo do the following:
00121    //   - Open three windows
00122    //   - Start ROOT in all three windows
00123    //   - Execute in the first window: .x hserv.C (or hserv2.C)
00124    //   - Execute in the second and third windows: .x hclient.C
00125    // If you want to run the hserv.C on a different host, just change
00126    // "localhost" in the TSocket ctor below to the desired hostname.
00127    //
00128    // The script argument "evol" can be used when using a modified version
00129    // of the script where the clients and server are on systems with
00130    // different versions of ROOT. When evol is set to kTRUE the socket will
00131    // support automatic schema evolution between the client and the server.
00132    //
00133    //Author: Fons Rademakers
00134 
00135    gEvo = evol;
00136 
00137    gBenchmark->Start("hclient");
00138 
00139    TBonjourBrowser *browser = new TBonjourBrowser;
00140    browser->Connect("CurrentBonjourRecordsChanged(TList*)", 0, 0,
00141                     "UpdateBonjourRecords(TList*)");
00142    browser->BrowseForServiceType("_hserv2._tcp");
00143 }

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