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