ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
spy.C
Go to the documentation of this file.
1 // Client program which allows the snooping of objects from a spyserv process.
2 // To run this demo do the following (see spyserv.C):
3 // - open two or more windows
4 // - start root in all windows
5 // - execute in the first window: .x spyserv.C (or spyserv.C++)
6 // - execute in the other window(s): .x spy.C (or spy.C++)
7 // - in the "spy" client windows click the "Connect" button and snoop
8 // the histograms by clicking on the "hpx", "hpxpy" and "hprof"
9 // buttons
10 //Author: Fons Rademakers
11 
12 #include "TGButton.h"
13 #include "TRootEmbeddedCanvas.h"
14 #include "TGLayout.h"
15 #include "TH2.h"
16 #include "TCanvas.h"
17 #include "TSocket.h"
18 #include "TMessage.h"
19 #include "RQ_OBJECT.h"
20 
21 
22 class Spy {
23 
24 RQ_OBJECT("Spy")
25 
26 private:
27  TGMainFrame *fMain;
28  TRootEmbeddedCanvas *fCanvas;
29  TGHorizontalFrame *fHorz;
30  TGHorizontalFrame *fHorz2;
31  TGLayoutHints *fLbut;
32  TGLayoutHints *fLhorz;
33  TGLayoutHints *fLcan;
34  TGButton *fHpx;
35  TGButton *fHpxpy;
36  TGButton *fHprof;
37  TGButton *fConnect;
38  TGButton *fQuit;
39  TSocket *fSock;
40  TH1 *fHist;
41 
42 public:
43  Spy();
44  ~Spy();
45 
46  void Connect();
47  void DoButton();
48 };
49 
50 void Spy::DoButton()
51 {
52  // Ask for histogram...
53 
54  if (!fSock->IsValid())
55  return;
56 
57  TGButton *btn = (TGButton *) gTQSender;
58  switch (btn->WidgetId()) {
59  case 1:
60  fSock->Send("get hpx");
61  break;
62  case 2:
63  fSock->Send("get hpxpy");
64  break;
65  case 3:
66  fSock->Send("get hprof");
67  break;
68  }
69  TMessage *mess;
70  if (fSock->Recv(mess) <= 0) {
71  Error("Spy::DoButton", "error receiving message");
72  return;
73  }
74 
75  if (fHist) delete fHist;
76  if (mess->GetClass()->InheritsFrom(TH1::Class())) {
77  fHist = (TH1*) mess->ReadObject(mess->GetClass());
78  if (mess->GetClass()->InheritsFrom(TH2::Class()))
79  fHist->Draw("cont");
80  else
81  fHist->Draw();
82  fCanvas->GetCanvas()->Modified();
83  fCanvas->GetCanvas()->Update();
84  }
85 
86  delete mess;
87 }
88 
89 void Spy::Connect()
90 {
91  // Connect to SpyServ
92  fSock = new TSocket("localhost", 9090);
93  fConnect->SetState(kButtonDisabled);
94  fHpx->SetState(kButtonUp);
95  fHpxpy->SetState(kButtonUp);
96  fHprof->SetState(kButtonUp);
97 }
98 
99 Spy::Spy()
100 {
101  // Create a main frame
102  fMain = new TGMainFrame(0, 100, 100);
103  fMain->SetCleanup(kDeepCleanup);
104 
105  // Create an embedded canvas and add to the main frame, centered in x and y
106  // and with 30 pixel margins all around
107  fCanvas = new TRootEmbeddedCanvas("Canvas", fMain, 600, 400);
108  fLcan = new TGLayoutHints(kLHintsCenterX|kLHintsCenterY,30,30,30,30);
109  fMain->AddFrame(fCanvas, fLcan);
110 
111  // Create a horizonal frame containing three text buttons
112  fLhorz = new TGLayoutHints(kLHintsExpandX, 0, 0, 0, 30);
113  fHorz = new TGHorizontalFrame(fMain, 100, 100);
114  fMain->AddFrame(fHorz, fLhorz);
115 
116  // Create three text buttons to get objects from server
117  // Add to horizontal frame
118  fLbut = new TGLayoutHints(kLHintsCenterX, 10, 10, 0, 0);
119  fHpx = new TGTextButton(fHorz, "Get hpx", 1);
120  fHpx->SetState(kButtonDisabled);
121  fHpx->Connect("Clicked()", "Spy", this, "DoButton()");
122  fHorz->AddFrame(fHpx, fLbut);
123  fHpxpy = new TGTextButton(fHorz, "Get hpxpy", 2);
124  fHpxpy->SetState(kButtonDisabled);
125  fHpxpy->Connect("Clicked()", "Spy", this, "DoButton()");
126  fHorz->AddFrame(fHpxpy, fLbut);
127  fHprof = new TGTextButton(fHorz, "Get hprof", 3);
128  fHprof->SetState(kButtonDisabled);
129  fHprof->Connect("Clicked()", "Spy", this, "DoButton()");
130  fHorz->AddFrame(fHprof, fLbut);
131 
132  // Create a horizonal frame containing two text buttons
133  fHorz2 = new TGHorizontalFrame(fMain, 100, 100);
134  fMain->AddFrame(fHorz2, fLhorz);
135 
136  // Create "Connect" and "Quit" buttons
137  // Add to horizontal frame
138  fConnect = new TGTextButton(fHorz2, "Connect");
139  fConnect->Connect("Clicked()", "Spy", this, "Connect()");
140  fHorz2->AddFrame(fConnect, fLbut);
141  fQuit = new TGTextButton(fHorz2, "Quit");
142  fQuit->SetCommand("gApplication->Terminate()");
143  fHorz2->AddFrame(fQuit, fLbut);
144 
145  // Set main frame name, map sub windows (buttons), initialize layout
146  // algorithm via Resize() and map main frame
147  fMain->SetWindowName("Spy on SpyServ");
148  fMain->MapSubwindows();
149  fMain->Resize(fMain->GetDefaultSize());
150  fMain->MapWindow();
151 
152  fHist = 0;
153 }
154 
155 Spy::~Spy()
156 {
157  // Clean up
158 
159  delete fHist;
160  delete fSock;
161  delete fLbut;
162  delete fLhorz;
163  delete fLcan;
164  delete fHpx;
165  delete fHpxpy;
166  delete fHprof;
167  delete fConnect;
168  delete fQuit;
169  delete fHorz;
170  delete fHorz2;
171  delete fCanvas;
172  delete fMain;
173 }
174 
175 void spy()
176 {
177  new Spy;
178 }
void spy()
Definition: spy.C:175
virtual Bool_t IsValid() const
Definition: TSocket.h:162
R__EXTERN void * gTQSender
Definition: TQObject.h:49
virtual Int_t Send(const TMessage &mess)
Send a TMessage object.
Definition: TSocket.cxx:520
virtual Int_t Recv(TMessage *&mess)
Receive a TMessage object.
Definition: TSocket.cxx:818
TCanvas * GetCanvas() const
virtual TObject * ReadObject(const TClass *cl)
Read object from I/O buffer.
void Class()
Definition: Class.C:29
virtual void SetCommand(const char *command)
Definition: TGWidget.h:91
void Error(const char *location, const char *msgfmt,...)
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 Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
#define RQ_OBJECT(sender_class)
Definition: RQ_OBJECT.h:101
Int_t WidgetId() const
Definition: TGWidget.h:86
The TH1 histogram class.
Definition: TH1.h:80
virtual void AddFrame(TGFrame *f, TGLayoutHints *l=0)
Add frame to the composite frame using the specified layout hints.
Definition: TGFrame.cxx:1099
Bool_t InheritsFrom(const char *cl) const
Return kTRUE if this class inherits from a class with name "classname".
Definition: TClass.cxx:4498
TClass * GetClass() const
Definition: TMessage.h:76
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2179
void Modified(Bool_t flag=1)
Definition: TPad.h:407
virtual void SetState(EButtonState state, Bool_t emit=kFALSE)
Set button state.
Definition: TGButton.cxx:185