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