spy.C

Go to the documentation of this file.
00001 // Client program which allows the snooping of objects from a spyserv process.
00002 // To run this demo do the following (see spyserv.C):
00003 //   - open two or more windows
00004 //   - start root in all windows
00005 //   - execute in the first window:    .x spyserv.C  (or spyserv.C++)
00006 //   - execute in the other window(s): .x spy.C      (or spy.C++)
00007 //   - in the "spy" client windows click the "Connect" button and snoop
00008 //     the histograms by clicking on the "hpx", "hpxpy" and "hprof"
00009 //     buttons
00010 //Author: Fons Rademakers
00011    
00012 #include "TGButton.h"
00013 #include "TRootEmbeddedCanvas.h"
00014 #include "TGLayout.h"
00015 #include "TH2.h"
00016 #include "TCanvas.h"
00017 #include "TSocket.h"
00018 #include "TMessage.h"
00019 #include "RQ_OBJECT.h"
00020 
00021 
00022 class Spy {
00023 
00024 RQ_OBJECT("Spy")
00025 
00026 private:
00027    TGMainFrame         *fMain;
00028    TRootEmbeddedCanvas *fCanvas;
00029    TGHorizontalFrame   *fHorz;
00030    TGHorizontalFrame   *fHorz2;
00031    TGLayoutHints       *fLbut;
00032    TGLayoutHints       *fLhorz;
00033    TGLayoutHints       *fLcan;
00034    TGButton            *fHpx;
00035    TGButton            *fHpxpy;
00036    TGButton            *fHprof;
00037    TGButton            *fConnect;
00038    TGButton            *fQuit;
00039    TSocket             *fSock;
00040    TH1                 *fHist;
00041 
00042 public:
00043    Spy();
00044    ~Spy();
00045 
00046    void Connect();
00047    void DoButton();
00048 };
00049 
00050 void Spy::DoButton()
00051 {
00052    // Ask for histogram...
00053 
00054    if (!fSock->IsValid())
00055       return;
00056 
00057    TGButton *btn = (TGButton *) gTQSender;
00058    switch (btn->WidgetId()) {
00059       case 1:
00060          fSock->Send("get hpx");
00061          break;
00062       case 2:
00063          fSock->Send("get hpxpy");
00064          break;
00065       case 3:
00066          fSock->Send("get hprof");
00067          break;
00068    }
00069    TMessage *mess;
00070    if (fSock->Recv(mess) <= 0) {
00071       Error("Spy::DoButton", "error receiving message");
00072       return;
00073    }
00074 
00075    if (fHist) delete fHist;
00076    if (mess->GetClass()->InheritsFrom(TH1::Class())) {
00077       fHist = (TH1*) mess->ReadObject(mess->GetClass());
00078       if (mess->GetClass()->InheritsFrom(TH2::Class()))
00079          fHist->Draw("cont");
00080       else
00081          fHist->Draw();
00082       fCanvas->GetCanvas()->Modified();
00083       fCanvas->GetCanvas()->Update();
00084    }
00085 
00086    delete mess;
00087 }
00088 
00089 void Spy::Connect()
00090 {
00091    // Connect to SpyServ
00092    fSock = new TSocket("localhost", 9090);
00093    fConnect->SetState(kButtonDisabled);
00094    fHpx->SetState(kButtonUp);
00095    fHpxpy->SetState(kButtonUp);
00096    fHprof->SetState(kButtonUp);
00097 }
00098 
00099 Spy::Spy()
00100 {
00101    // Create a main frame
00102    fMain = new TGMainFrame(0, 100, 100);
00103    fMain->SetCleanup(kDeepCleanup);
00104 
00105    // Create an embedded canvas and add to the main frame, centered in x and y
00106    // and with 30 pixel margins all around
00107    fCanvas = new TRootEmbeddedCanvas("Canvas", fMain, 600, 400);
00108    fLcan = new TGLayoutHints(kLHintsCenterX|kLHintsCenterY,30,30,30,30);
00109    fMain->AddFrame(fCanvas, fLcan);
00110 
00111    // Create a horizonal frame containing three text buttons
00112    fLhorz = new TGLayoutHints(kLHintsExpandX, 0, 0, 0, 30);
00113    fHorz = new TGHorizontalFrame(fMain, 100, 100);
00114    fMain->AddFrame(fHorz, fLhorz);
00115 
00116    // Create three text buttons to get objects from server
00117    // Add to horizontal frame
00118    fLbut = new TGLayoutHints(kLHintsCenterX, 10, 10, 0, 0);
00119    fHpx = new TGTextButton(fHorz, "Get hpx", 1);
00120    fHpx->SetState(kButtonDisabled);
00121    fHpx->Connect("Clicked()", "Spy", this, "DoButton()");
00122    fHorz->AddFrame(fHpx, fLbut);
00123    fHpxpy = new TGTextButton(fHorz, "Get hpxpy", 2);
00124    fHpxpy->SetState(kButtonDisabled);
00125    fHpxpy->Connect("Clicked()", "Spy", this, "DoButton()");
00126    fHorz->AddFrame(fHpxpy, fLbut);
00127    fHprof = new TGTextButton(fHorz, "Get hprof", 3);
00128    fHprof->SetState(kButtonDisabled);
00129    fHprof->Connect("Clicked()", "Spy", this, "DoButton()");
00130    fHorz->AddFrame(fHprof, fLbut);
00131 
00132    // Create a horizonal frame containing two text buttons
00133    fHorz2 = new TGHorizontalFrame(fMain, 100, 100);
00134    fMain->AddFrame(fHorz2, fLhorz);
00135 
00136    // Create "Connect" and "Quit" buttons
00137    // Add to horizontal frame
00138    fConnect = new TGTextButton(fHorz2, "Connect");
00139    fConnect->Connect("Clicked()", "Spy", this, "Connect()");
00140    fHorz2->AddFrame(fConnect, fLbut);
00141    fQuit = new TGTextButton(fHorz2, "Quit");
00142    fQuit->SetCommand("gApplication->Terminate()");
00143    fHorz2->AddFrame(fQuit, fLbut);
00144 
00145    // Set main frame name, map sub windows (buttons), initialize layout
00146    // algorithm via Resize() and map main frame
00147    fMain->SetWindowName("Spy on SpyServ");
00148    fMain->MapSubwindows();
00149    fMain->Resize(fMain->GetDefaultSize());
00150    fMain->MapWindow();
00151 
00152    fHist = 0;
00153 }
00154 
00155 Spy::~Spy()
00156 {
00157    // Clean up
00158 
00159    delete fHist;
00160    delete fSock;
00161    delete fLbut;
00162    delete fLhorz;
00163    delete fLcan;
00164    delete fHpx;
00165    delete fHpxpy;
00166    delete fHprof;
00167    delete fConnect;
00168    delete fQuit;
00169    delete fHorz;
00170    delete fHorz2;
00171    delete fCanvas;
00172    delete fMain;
00173 }
00174 
00175 void spy()
00176 {
00177    new Spy;
00178 }

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