Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
27class Spy {
28
29RQ_OBJECT("Spy")
30
31private:
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
47public:
48 Spy();
49 ~Spy();
50
51 void Connect();
52 void DoButton();
53};
54
55void 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
94void 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
104Spy::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);
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
160Spy::~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
180void spy()
181{
182 new Spy;
183}
#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:185
@ 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:2483
Bool_t InheritsFrom(const char *cl) const override
Return kTRUE if this class inherits from a class with name "classname".
Definition TClass.cxx:4874
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=nullptr)
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:385
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:397
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:59
static TClass * Class()
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3066
static TClass * Class()
TClass * GetClass() const
Definition TMessage.h:71
void Modified(Bool_t flag=true) override
Mark pad modified Will be repainted when TCanvas::Update() will be called next time.
Definition TPad.cxx:7298
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