Logo ROOT   6.10/09
Reference Guide
THttpEngine.cxx
Go to the documentation of this file.
1 // $Id$
2 // Author: Sergey Linev 21/12/2013
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2013, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include "THttpEngine.h"
13 
14 #include <string.h>
15 
16 #include "TCanvas.h"
17 #include "TClass.h"
18 #include "TMethod.h"
19 #include "TMethodCall.h"
20 #include "TList.h"
21 #include "TROOT.h"
22 #include "THttpCallArg.h"
23 #include "TBufferJSON.h"
24 
25 //////////////////////////////////////////////////////////////////////////
26 // //
27 // THttpEngine //
28 // //
29 // Abstract class for implementing http protocol for THttpServer //
30 // //
31 //////////////////////////////////////////////////////////////////////////
32 
34 
35  ////////////////////////////////////////////////////////////////////////////////
36  /// normal constructor
37 
38  THttpEngine::THttpEngine(const char *name, const char *title)
39  : TNamed(name, title), fServer(0)
40 {
41 }
42 
43 ////////////////////////////////////////////////////////////////////////////////
44 /// destructor
45 
47 {
48  fServer = 0;
49 }
50 
52 
53  ////////////////////////////////////////////////////////////////////////////////
54  /// normal constructor
55 
56  THttpWSEngine::THttpWSEngine(const char *name, const char *title)
57  : TNamed(name, title), fReady(kFALSE), fModified(kFALSE), fGetMenu(kFALSE), fCanv(0)
58 {
59 }
60 
61 ////////////////////////////////////////////////////////////////////////////////
62 /// destructor
63 
65 {
66  AssignCanvas(0);
67 }
68 
69 ////////////////////////////////////////////////////////////////////////////////
70 /// Envelope for sending string via the websocket
71 
72 void THttpWSEngine::SendCharStar(const char *str)
73 {
74  if (str) Send(str, strlen(str));
75 }
76 
77 ////////////////////////////////////////////////////////////////////////////////
78 /// react on canvas modifications
79 
81 {
82  fModified = kTRUE;
83  CheckModifiedFlag();
84 }
85 
86 ////////////////////////////////////////////////////////////////////////////////
87 /// if canvas was modified, send new copy to the client
88 
90 {
91  if (!fReady || !fCanv) return;
92 
93  TString buf;
94 
95  if (fGetMenu) {
96  TClass *cl = fCanv->IsA();
97 
98  TList *lst = new TList;
99  cl->GetMenuItems(lst);
100  // while there is no streamer for TMethod class, one needs own implementation
101 
102  // TBufferJSON::ConvertToJSON(lst, 3);
103 
104  TIter iter(lst);
105  TMethod *m = 0;
106  Int_t cnt = 0;
107 
108  buf = "MENU[";
109  while ((m = (TMethod *)iter()) != 0) {
110  if (cnt++ > 0) buf.Append(",");
111  buf.Append("{");
112  buf.Append(TString::Format("\"name\":\"%s\",\"title\":\"%s\"", m->GetName(), m->GetTitle()));
113 
114  if (m->IsMenuItem() == kMenuToggle) {
115  TString getter;
116  if (m->Getter() && strlen(m->Getter()) > 0) {
117  getter = m->Getter();
118  } else if (strncmp(m->GetName(), "Set", 3) == 0) {
119  getter = TString(m->GetName())(3, strlen(m->GetName()) - 3);
120  if (cl->GetMethodAllAny(TString("Has") + getter))
121  getter = TString("Has") + getter;
122  else if (cl->GetMethodAllAny(TString("Get") + getter))
123  getter = TString("Get") + getter;
124  else if (cl->GetMethodAllAny(TString("Is") + getter))
125  getter = TString("Is") + getter;
126  else
127  getter = "";
128  }
129 
130  if ((getter.Length() > 0) && cl->GetMethodAllAny(getter)) {
131 
132  TMethodCall *call = new TMethodCall(cl, getter, "");
133 
134  if (call->ReturnType() == TMethodCall::kLong) {
135  Long_t l(0);
136  call->Execute(fCanv, l);
137  buf.Append(TString::Format(",\"chk\":%s", (l != 0) ? "true" : "false"));
138  buf.Append(TString::Format(",\"exec\":\"%s(%s)\"", m->GetName(), (l != 0) ? "0" : "1"));
139  // printf("Toggle %s getter %s chk: %s \n", m->GetName(), getter.Data(), (l!=0) ? "true" : "false");
140  } else {
141  printf("Cannot get toggle value with getter %s \n", getter.Data());
142  }
143  }
144  } else {
145  buf.Append(TString::Format(",\"exec\":\"%s()\"", m->GetName()));
146  }
147 
148  buf.Append("}");
149  }
150  buf += "]";
151  delete lst;
152 
153  fGetMenu = kFALSE;
154  } else if (fModified) {
155  buf = "JSON";
156  buf += TBufferJSON::ConvertToJSON(fCanv, 3);
157  fModified = kFALSE;
158  }
159 
160  if (buf.Length() > 0) {
161  fReady = kFALSE;
162  Send(buf.Data(), buf.Length());
163  }
164 }
165 
166 ////////////////////////////////////////////////////////////////////////////////
167 /// process data received from the client
168 
170 {
171  if ((arg == 0) && (arg->GetPostDataLength() <= 0)) return;
172 
173  const char *cdata = (const char *)arg->GetPostData();
174 
175  if (strncmp(cdata, "READY", 5) == 0) {
176  fReady = kTRUE;
177  CheckModifiedFlag();
178  return;
179  }
180 
181  if (strncmp(cdata, "GETMENU", 7) == 0) {
182  fGetMenu = kTRUE;
183  CheckModifiedFlag();
184  return;
185  }
186 
187  if (strncmp(cdata, "EXEC", 4) == 0) {
188 
189  if (fCanv != 0) {
190 
191  TString exec;
192  exec.Form("((%s*) %p)->%s;", fCanv->ClassName(), fCanv, cdata + 4);
193 
194  gROOT->ProcessLine(exec);
195  }
196 
197  return;
198  }
199 }
200 
201 ////////////////////////////////////////////////////////////////////////////////
202 /// assign canvas to the web socket
203 /// connects with CanvasModified signal
204 
206 {
207 
208  if (fCanv != 0) {
209  fCanv->Disconnect("Modified()", this, "CanvasModified()");
210  fCanv->GetListOfPrimitives()->Remove(this);
211  fCanv = 0;
212  }
213 
214  if (canv != 0) {
215  SetName("websocket");
216  canv->Connect("Modified()", "THttpWSEngine", this, "CanvasModified()");
217  canv->GetListOfPrimitives()->Add(this);
218  fCanv = canv;
219  }
220 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
TList * GetListOfPrimitives() const
Definition: TPad.h:236
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:131
virtual const char * Getter() const
Definition: TMethod.h:59
virtual void ProcessData(THttpCallArg *arg)
process data received from the client
#define gROOT
Definition: TROOT.h:375
static const EReturnType kLong
Definition: TMethodCall.h:43
Basic string class.
Definition: TString.h:129
int Int_t
Definition: RtypesCore.h:41
void * GetPostData() const
return pointer on posted with request data
Definition: THttpCallArg.h:121
virtual void AssignCanvas(TCanvas *canv)
assign canvas to the web socket connects with CanvasModified signal
TMethod * GetMethodAllAny(const char *method)
Return pointer to method without looking at parameters.
Definition: TClass.cxx:4141
virtual ~THttpEngine()
destructor
Definition: THttpEngine.cxx:46
virtual void SendCharStar(const char *str)
Envelope for sending string via the websocket.
Definition: THttpEngine.cxx:72
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString...
Definition: TString.cxx:2345
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
TString & Append(const char *cs)
Definition: TString.h:497
Method or function calling interface.
Definition: TMethodCall.h:37
A doubly linked list.
Definition: TList.h:43
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:867
THttpServer * fServer
! object server
Definition: THttpEngine.h:25
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition: TString.cxx:2332
TMarker * m
Definition: textangle.C:8
Ssiz_t Length() const
Definition: TString.h:388
TLine * l
Definition: textangle.C:4
void CheckModifiedFlag()
if canvas was modified, send new copy to the client
Definition: THttpEngine.cxx:89
The ROOT global object gROOT contains a list of all defined classes.
Definition: TClass.h:71
const Bool_t kFALSE
Definition: RtypesCore.h:92
long Long_t
Definition: RtypesCore.h:50
The Canvas class.
Definition: TCanvas.h:31
#define ClassImp(name)
Definition: Rtypes.h:336
static TString ConvertToJSON(const TObject *obj, Int_t compact=0, const char *member_name=0)
Converts object, inherited from TObject class, to JSON string Lower digit of compact parameter define...
EMenuItemKind IsMenuItem() const
Definition: TMethod.h:56
virtual void CanvasModified()
react on canvas modifications
Definition: THttpEngine.cxx:80
virtual ~THttpWSEngine()
destructor
Definition: THttpEngine.cxx:64
virtual void Add(TObject *obj)
Definition: TList.h:77
void Execute(const char *, const char *, int *=0)
Execute method on this object with the given parameter string, e.g.
Definition: TMethodCall.h:64
Each ROOT class (see TClass) has a linked list of methods.
Definition: TMethod.h:38
void GetMenuItems(TList *listitems)
Returns list of methods accessible by context menu.
Definition: TClass.cxx:3673
Long_t GetPostDataLength() const
return length of posted with request data
Definition: THttpCallArg.h:124
const Bool_t kTRUE
Definition: RtypesCore.h:91
const char * cnt
Definition: TXMLSetup.cxx:75
EReturnType ReturnType()
Returns the return type of the method.
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
const char * Data() const
Definition: TString.h:347