Logo ROOT   6.21/01
Reference Guide
THttpWSHandler.h
Go to the documentation of this file.
1 // $Id$
2 // Author: Sergey Linev 20/10/2017
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2017, 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 #ifndef ROOT_THttpWSHandler
13 #define ROOT_THttpWSHandler
14 
15 #include "TNamed.h"
16 #include "THttpCallArg.h"
17 
18 #include <vector>
19 #include <memory>
20 #include <mutex>
21 
22 class THttpWSEngine;
23 class THttpServer;
24 
25 class THttpWSHandler : public TNamed {
26 
27 friend class THttpServer;
28 
29 private:
30  Bool_t fSyncMode{kTRUE}; ///<! is handler runs in synchronous mode (default, no multi-threading)
31  Bool_t fDisabled{kFALSE}; ///<! when true, all further operations will be ignored
32  Int_t fSendCnt{0}; ///<! counter for completed send operations
33  std::mutex fMutex; ///<! protect list of engines
34  std::vector<std::shared_ptr<THttpWSEngine>> fEngines; ///<! list of active WS engines (connections)
35 
36  std::shared_ptr<THttpWSEngine> FindEngine(UInt_t id, Bool_t book_send = kFALSE);
37 
38  Bool_t HandleWS(std::shared_ptr<THttpCallArg> &arg);
39 
40  Int_t RunSendingThrd(std::shared_ptr<THttpWSEngine> engine);
41 
42  Int_t PerformSend(std::shared_ptr<THttpWSEngine> engine);
43 
44  void RemoveEngine(std::shared_ptr<THttpWSEngine> &engine, Bool_t terminate = kFALSE);
45 
46  Int_t CompleteSend(std::shared_ptr<THttpWSEngine> &engine);
47 
48 protected:
49 
50  THttpWSHandler(const char *name, const char *title, Bool_t syncmode = kTRUE);
51 
52  /// Method called when multi-threaded send operation is completed
53  virtual void CompleteWSSend(UInt_t) {}
54 
55  /// Method used to accept or reject root_batch_holder.js request
56  virtual Bool_t ProcessBatchHolder(std::shared_ptr<THttpCallArg> &) { return kFALSE; }
57 
58  /// Method called when default page content is prepared for use
59  /// By default no-cache header is provided
60  virtual void VerifyDefaultPageContent(std::shared_ptr<THttpCallArg> &arg) { arg->AddNoCacheHeader(); }
61 
62 public:
63  virtual ~THttpWSHandler();
64 
65  /// Returns processing mode of WS handler
66  /// If sync mode is TRUE (default), all event processing and data sending performed in main thread
67  /// All send functions are blocking and must be performed from main thread
68  /// If sync mode is false, WS handler can be used from different threads and starts its own sending threads
69  Bool_t IsSyncMode() const { return fSyncMode; }
70 
71  /// Provides content of default web page for registered web-socket handler
72  /// Can be content of HTML page or file name, where content should be taken
73  /// For instance, file:/home/user/test.htm or file:$jsrootsys/files/canvas.htm
74  /// If not specified, default index.htm page will be shown
75  /// Used by the webcanvas
76  virtual TString GetDefaultPageContent() { return ""; }
77 
78  /// If returns kTRUE, allows to serve files from subdirectories where page content is situated
79  virtual Bool_t CanServeFiles() const { return kFALSE; }
80 
81  /// Allow processing of WS requests in arbitrary thread
82  virtual Bool_t AllowMTProcess() const { return kFALSE; }
83 
84  /// Allow send operations in separate threads (when supported by websocket engine)
85  virtual Bool_t AllowMTSend() const { return kFALSE; }
86 
87  /// Returns true when processing of websockets is disabled, set shortly before handler need to be destroyed
88  Bool_t IsDisabled() const { return fDisabled; }
89 
90  /// Disable all processing of websockets, normally called shortly before destructor
91  void SetDisabled() { fDisabled = kTRUE; }
92 
93  /// Return kTRUE if websocket with given ID exists
94  Bool_t HasWS(UInt_t wsid) { return !!FindEngine(wsid); }
95 
96  /// Returns current number of websocket connections
97  Int_t GetNumWS();
98 
99  UInt_t GetWS(Int_t num = 0);
100 
101  void CloseWS(UInt_t wsid);
102 
103  Int_t SendWS(UInt_t wsid, const void *buf, int len);
104 
105  Int_t SendHeaderWS(UInt_t wsid, const char *hdr, const void *buf, int len);
106 
107  Int_t SendCharStarWS(UInt_t wsid, const char *str);
108 
109  virtual Bool_t ProcessWS(THttpCallArg *arg) = 0;
110 
111  ClassDef(THttpWSHandler, 0) // abstract class for handling websocket requests
112 };
113 
114 #endif
Int_t SendWS(UInt_t wsid, const void *buf, int len)
Send binary data via given websocket id Returns -1 - in case of error 0 - when operation was executed...
Bool_t HasWS(UInt_t wsid)
Return kTRUE if websocket with given ID exists.
Int_t SendCharStarWS(UInt_t wsid, const char *str)
Send string via given websocket id Returns -1 - in case of error, 0 - when operation was executed imm...
virtual Bool_t AllowMTProcess() const
Allow processing of WS requests in arbitrary thread.
Bool_t HandleWS(std::shared_ptr< THttpCallArg > &arg)
Process request to websocket Different kind of requests coded into THttpCallArg::Method "WS_CONNECT" ...
virtual ~THttpWSHandler()
destructor Make sure that all sending threads are stopped correctly
Bool_t IsSyncMode() const
Returns processing mode of WS handler If sync mode is TRUE (default), all event processing and data s...
Int_t GetNumWS()
Returns current number of websocket connections.
Int_t fSendCnt
! counter for completed send operations
Basic string class.
Definition: TString.h:131
Int_t CompleteSend(std::shared_ptr< THttpWSEngine > &engine)
Complete current send operation.
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
virtual void VerifyDefaultPageContent(std::shared_ptr< THttpCallArg > &arg)
Method called when default page content is prepared for use By default no-cache header is provided...
#define ClassDef(name, id)
Definition: Rtypes.h:326
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
Int_t RunSendingThrd(std::shared_ptr< THttpWSEngine > engine)
Send data stored in the buffer Returns 0 - when operation was executed immediately 1 - when send oper...
void CloseWS(UInt_t wsid)
Close connection with given websocket id.
void RemoveEngine(std::shared_ptr< THttpWSEngine > &engine, Bool_t terminate=kFALSE)
Remove and destroy WS connection.
virtual Bool_t ProcessBatchHolder(std::shared_ptr< THttpCallArg > &)
Method used to accept or reject root_batch_holder.js request.
std::shared_ptr< THttpWSEngine > FindEngine(UInt_t id, Bool_t book_send=kFALSE)
Find websocket connection handle with given id If book_send parameter specified, have to book send op...
THttpWSHandler(const char *name, const char *title, Bool_t syncmode=kTRUE)
THttpWSHandler.
virtual TString GetDefaultPageContent()
Provides content of default web page for registered web-socket handler Can be content of HTML page or...
unsigned int UInt_t
Definition: RtypesCore.h:42
UInt_t GetWS(Int_t num=0)
Return websocket id with given sequential number Number of websockets returned with GetNumWS() method...
std::vector< std::shared_ptr< THttpWSEngine > > fEngines
! list of active WS engines (connections)
Bool_t fSyncMode
! is handler runs in synchronous mode (default, no multi-threading)
const Bool_t kFALSE
Definition: RtypesCore.h:88
Bool_t fDisabled
! when true, all further operations will be ignored
void SetDisabled()
Disable all processing of websockets, normally called shortly before destructor.
virtual Bool_t ProcessWS(THttpCallArg *arg)=0
virtual Bool_t AllowMTSend() const
Allow send operations in separate threads (when supported by websocket engine)
virtual void CompleteWSSend(UInt_t)
Method called when multi-threaded send operation is completed.
Bool_t IsDisabled() const
Returns true when processing of websockets is disabled, set shortly before handler need to be destroy...
std::mutex fMutex
! protect list of engines
const Bool_t kTRUE
Definition: RtypesCore.h:87
virtual Bool_t CanServeFiles() const
If returns kTRUE, allows to serve files from subdirectories where page content is situated...
Int_t SendHeaderWS(UInt_t wsid, const char *hdr, const void *buf, int len)
Send binary data with text header via given websocket id Returns -1 - in case of error, 0 - when operation was executed immediately, 1 - when send operation will be performed in different thread,.
char name[80]
Definition: TGX11.cxx:109
Int_t PerformSend(std::shared_ptr< THttpWSEngine > engine)
Perform send operation, stored in buffer.