Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
spy.C File Reference

Detailed Description

Client program which allows the snooping of objects from a spyserv process.

To run this demo do the following (see spyserv.C):

  • open two or more windows
  • start root in all windows
  • execute in the first window: .x spyserv.C (or spyserv.C++)
  • execute in the other window(s): .x spy.C (or spy.C++)
  • in the "spy" client windows click the "Connect" button and snoop the histograms by clicking on the "hpx", "hpxpy" and "hprof" buttons
#include "TGButton.h"
#include "TGLayout.h"
#include "TH2.h"
#include "TCanvas.h"
#include "TSocket.h"
#include "TMessage.h"
#include "RQ_OBJECT.h"
class Spy {
RQ_OBJECT("Spy")
private:
TGMainFrame *fMain;
TGLayoutHints *fLbut;
TGLayoutHints *fLhorz;
TGLayoutHints *fLcan;
TGButton *fHpx;
TGButton *fHpxpy;
TGButton *fHprof;
TGButton *fConnect;
TGButton *fQuit;
TSocket *fSock;
TH1 *fHist;
public:
Spy();
~Spy();
void Connect();
void DoButton();
};
void Spy::DoButton()
{
// Ask for histogram...
if (!fSock->IsValid())
return;
switch (btn->WidgetId()) {
case 1:
fSock->Send("get hpx");
break;
case 2:
fSock->Send("get hpxpy");
break;
case 3:
fSock->Send("get hprof");
break;
}
TMessage *mess;
if (fSock->Recv(mess) <= 0) {
Error("Spy::DoButton", "error receiving message");
return;
}
if (fHist) delete fHist;
if (mess->GetClass()->InheritsFrom(TH1::Class())) {
fHist = (TH1*) mess->ReadObject(mess->GetClass());
if (mess->GetClass()->InheritsFrom(TH2::Class()))
fHist->Draw("cont");
else
fHist->Draw();
fCanvas->GetCanvas()->Modified();
fCanvas->GetCanvas()->Update();
}
delete mess;
}
void Spy::Connect()
{
// Connect to SpyServ
fSock = new TSocket("localhost", 9090);
fHpxpy->SetState(kButtonUp);
fHprof->SetState(kButtonUp);
}
Spy::Spy()
{
// Create a main frame
fMain = new TGMainFrame(0, 100, 100);
fMain->SetCleanup(kDeepCleanup);
// Create an embedded canvas and add to the main frame, centered in x and y
// and with 30 pixel margins all around
fCanvas = new TRootEmbeddedCanvas("Canvas", fMain, 600, 400);
fLcan = new TGLayoutHints(kLHintsCenterX|kLHintsCenterY,30,30,30,30);
fMain->AddFrame(fCanvas, fLcan);
// Create a horizontal frame containing three text buttons
fLhorz = new TGLayoutHints(kLHintsExpandX, 0, 0, 0, 30);
fHorz = new TGHorizontalFrame(fMain, 100, 100);
fMain->AddFrame(fHorz, fLhorz);
// Create three text buttons to get objects from server
// Add to horizontal frame
fLbut = new TGLayoutHints(kLHintsCenterX, 10, 10, 0, 0);
fHpx = new TGTextButton(fHorz, "Get hpx", 1);
fHpx->Connect("Clicked()", "Spy", this, "DoButton()");
fHorz->AddFrame(fHpx, fLbut);
fHpxpy = new TGTextButton(fHorz, "Get hpxpy", 2);
fHpxpy->Connect("Clicked()", "Spy", this, "DoButton()");
fHorz->AddFrame(fHpxpy, fLbut);
fHprof = new TGTextButton(fHorz, "Get hprof", 3);
fHprof->Connect("Clicked()", "Spy", this, "DoButton()");
fHorz->AddFrame(fHprof, fLbut);
// Create a horizontal frame containing two text buttons
fHorz2 = new TGHorizontalFrame(fMain, 100, 100);
fMain->AddFrame(fHorz2, fLhorz);
// Create "Connect" and "Quit" buttons
// Add to horizontal frame
fConnect = new TGTextButton(fHorz2, "Connect");
fConnect->Connect("Clicked()", "Spy", this, "Connect()");
fHorz2->AddFrame(fConnect, fLbut);
fQuit = new TGTextButton(fHorz2, "Quit");
fQuit->SetCommand("gApplication->Terminate()");
fHorz2->AddFrame(fQuit, fLbut);
// Set main frame name, map sub windows (buttons), initialize layout
// algorithm via Resize() and map main frame
fMain->SetWindowName("Spy on SpyServ");
fMain->MapSubwindows();
fMain->Resize(fMain->GetDefaultSize());
fMain->MapWindow();
fHist = 0;
}
Spy::~Spy()
{
// Clean up
delete fHist;
delete fSock;
delete fLbut;
delete fLhorz;
delete fLcan;
delete fHpx;
delete fHpxpy;
delete fHprof;
delete fConnect;
delete fQuit;
delete fHorz;
delete fHorz2;
delete fCanvas;
delete fMain;
}
void spy()
{
new Spy;
}
#define RQ_OBJECT(sender_class)
Definition RQ_OBJECT.h:87
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:187
@ kButtonDisabled
Definition TGButton.h:56
@ kButtonUp
Definition TGButton.h:53
@ kDeepCleanup
Definition TGFrame.h:42
@ kLHintsCenterY
Definition TGLayout.h:28
@ kLHintsCenterX
Definition TGLayout.h:25
@ kLHintsExpandX
Definition TGLayout.h:30
R__EXTERN void * gTQSender
Definition TQObject.h:46
TObject * ReadObject(const TClass *cl) override
Read object from I/O buffer.
void Update() override
Update canvas pad buffers.
Definition TCanvas.cxx:2502
Bool_t InheritsFrom(const char *cl) const
Return kTRUE if this class inherits from a class with name "classname".
Definition TClass.cxx:4860
A button abstract base class.
Definition TGButton.h:68
virtual void SetState(EButtonState state, Bool_t emit=kFALSE)
Set button state.
Definition TGButton.cxx:235
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition TGFrame.cxx:1117
A composite frame that layout their children in horizontal way.
Definition TGFrame.h:386
This class describes layout hints used by the layout classes.
Definition TGLayout.h:50
Defines top level windows that interact with the system Window Manager.
Definition TGFrame.h:398
Yield an action as soon as it is clicked.
Definition TGButton.h:142
virtual void SetCommand(const char *command)
Definition TGWidget.h:73
Int_t WidgetId() const
Definition TGWidget.h:68
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:58
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition TH1.cxx:3074
TClass * GetClass() const
Definition TMessage.h:71
void Modified(Bool_t flag=1) override
Definition TPad.h:413
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:869
This class creates a TGCanvas in which a TCanvas is created.
TCanvas * GetCanvas() const
virtual Int_t Recv(TMessage *&mess)
Receive a TMessage object.
Definition TSocket.cxx:818
virtual Bool_t IsValid() const
Definition TSocket.h:132
virtual Int_t Send(const TMessage &mess)
Send a TMessage object.
Definition TSocket.cxx:522
Author
Fons Rademakers

Definition in file spy.C.