AGXDynamics  2.31.1.0
RemoteCommandServer.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2021. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGXIO_REMOTE_COMMAND_SERVER_H
18 #define AGXIO_REMOTE_COMMAND_SERVER_H
19 
20 #include <agx/config/AGX_USE_WEBSOCKETS.h>
21 
22 #if AGX_USE_WEBSOCKETS()
23 
24 #include <agx/Component.h>
25 #include <external/civetweb/civetweb.h>
26 #include <agxNet/PortRange.h>
27 
28 #include <agx/Json.h>
29 
30 namespace agxIO
31 {
32  AGX_DECLARE_POINTER_TYPES(RemoteCommandServer);
33  class AGXCORE_EXPORT RemoteCommandServer : public agx::Component
34  {
35  public:
36  // Explicit/temporary etc. to handle e.g., Lua.
37  class AGXCORE_EXPORT ScriptListener : public agx::Referenced
38  {
39  public:
40  ScriptListener() {}
41 
45  virtual bool handle( const agx::String& command, agx::String& result, agx::String& error ) = 0;
46 
50  virtual bool autoComplete( const agx::String& /*objOrType*/, agx::StringVector& /*result*/, agx::String& /*error*/ ) { return true; }
51 
52  protected:
53  virtual ~ScriptListener() {}
54  };
55 
56  typedef agx::ref_ptr< ScriptListener > ScriptListenerRef;
57  typedef agx::Vector< ScriptListenerRef > ScriptListenerContainer;
58 
59  // typedef agx::HashTable<agx::String, agx::String> QueryArgumentTable;
60  class QueryArgumentTable;
61 
62  public:
63  static RemoteCommandServer *instance();
64  static bool hasInstance();
65 
66  public:
67  RemoteCommandServer(const agx::Name& name = "RemoteCommandServer");
68 
69  bool start(agxNet::PortRange *range, bool allowExternalRequests = false);
70  bool start(agx::UInt16 port = 5656, bool allowExternalRequests = false);
71  void stop();
72 
73  void service();
74 
75  agx::UInt16 getPort() const;
76 
77  bool isStarted() const;
78  bool isUsingSSL() const;
79 
80  void setAuthenticationToken(const agx::String& token);
81  const agx::String& getAuthenticationToken() const;
82 
83  bool add( agxIO::RemoteCommandServer::ScriptListener* scriptListener );
84  bool remove( agxIO::RemoteCommandServer::ScriptListener* scriptListener );
85 
86 
87  class AGXCORE_EXPORT RequestHandler
88  {
89  public:
90  typedef std::function<bool (RemoteCommandServer *, mg_connection *, const QueryArgumentTable&)> CallbackFunction;
91 
92  RequestHandler() {}
93  RequestHandler(const CallbackFunction& func) : m_callback(func) {}
94 
95  template<class ClassT>
96  RequestHandler(bool (ClassT::*fun)(RemoteCommandServer *, mg_connection *, const QueryArgumentTable&), ClassT* obj) : m_callback(std::bind(fun, obj,std::placeholders:: _1,std::placeholders:: _2,std::placeholders:: _3)) {}
97 
98  bool isValid() const
99  {
100  return m_callback ? true : false;
101  }
102 
103  bool operator() (RemoteCommandServer *server, mg_connection *connection, const QueryArgumentTable& arguments) const
104  {
105  return m_callback(server, connection, arguments);
106  }
107 
108  private:
109  CallbackFunction m_callback;
110  };
111 
112  void registerHandler(const agx::String& uri, const RequestHandler& handler);
113  void unregisterHandler(const agx::String& uri);
114 
115  static agx::String get_var(const char* buf, size_t buf_len, const char* name);
116  static agx::String get_qsvar(const mg_connection* connection, const char* name);
117 
118  static void writeResponse(const agxJson::Value& eResponse, mg_connection* connection);
119 
120  // Safari ajax breaks if we do not write any response, use this if no response is sent from a handler
121  static void writeDummyResponse(mg_connection *connection);
122 
123  void log(const char *format, ...);
124 
125  protected:
126  virtual ~RemoteCommandServer();
127 
128  private:
129  bool handleAutoComplete(RemoteCommandServer *server, mg_connection *connection, const QueryArgumentTable& arguments);
130  bool handleCommand(RemoteCommandServer *server, mg_connection *connection, const QueryArgumentTable& arguments);
131  bool handleScript(RemoteCommandServer *server, mg_connection *connection, const QueryArgumentTable& arguments);
132  bool handleScriptAutoComplete(RemoteCommandServer *server, mg_connection *connection, const QueryArgumentTable& arguments);
133  bool handleJournalNavigationTree(RemoteCommandServer *server, mg_connection *connection, const QueryArgumentTable& arguments);
134  bool handleJournalConfigurationList(RemoteCommandServer *server, mg_connection *connection, const QueryArgumentTable& arguments);
135  bool readComponent(RemoteCommandServer *server, mg_connection *connection, const QueryArgumentTable& arguments);
136  bool writeComponent(RemoteCommandServer *server, mg_connection *connection, const QueryArgumentTable& arguments);
137  // bool handleComponentRequest(mg_connection *connection, const agx::String& path);
138 
139 
140 
141  agx::Object* findMatchingNode(agx::Path path, agx::Component* root, agx::Path& matchedPath, agx::StringVector& matchList);
142  void writeNodeDescription(agx::Object* object, agxJson::Value& eNode);
143  int handleHttpEvent(mg_connection* connection);
144  int handleHttpAuth(mg_connection* connection);
145  static int mgAuthCallback(mg_connection* connection, void* server);
146  static int mgRequestCallback(mg_connection* connection, void* server);
147  static bool handle_jsonp(mg_connection* connection, const mg_connection* request_info);
148  void parseArguments(QueryArgumentTable& result, const mg_connection* connection);
149 
150  void writeTreeStructureJson(const agx::Object* node, agxJson::Value& eNode);
151 
152 
153  private:
154  agx::UInt16 m_port;
155  mg_context *m_mongoose;
156  agx::String m_htmlRoot;
157  agx::String m_token;
158  agx::ComponentRef m_activeContext;
159  ScriptListenerContainer m_scriptListeners;
160  bool m_allowExternalRequests;
161  bool m_started;
162  bool m_useSSL;
163 
164 
165  typedef agx::HashTable<agx::String, RequestHandler> RequestHandlerTable;
166  RequestHandlerTable m_requestHandlerTable;
167 
168  typedef agx::HashSet<agx::String> StringSet;
169  StringSet m_allowedComponentFileTypes;
170  };
171 
172  class RemoteCommandServer::QueryArgumentTable : public agx::HashTable<agx::String, agx::String>
173  {
174  public:
175 
177 
178  // Overrider []-operator to return empty string if not inserted
179  AGX_FORCE_INLINE const agx::String& operator[](const agx::String& key ) const
180  {
181  const_iterator it = find( key );
182  static agx::String emptyString;
183  return it != end() ? it->second : emptyString;
184  }
185 
186  };
187 
188 }
189 
190 // End AGX_USE_WEBSOCKETS.
191 #endif
192 
193 #endif /* AGXIO_REMOTE_COMMAND_SERVER_H */
Representation of a path, a list of name components.
Definition: Path.h:33
Inheritance with partial specialization due to bug with ref_ptr containers.
Definition: HashSet.h:665
The agxIO namespace contains classes for reading, writing and finding files.
Definition: Material.h:41
A component is an object containing other objects, enabling hierarchical structuring.
Definition: Component.h:38
STL namespace.
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
Representation of a name string.
Definition: Name.h:32
#define AGX_FORCE_INLINE
Definition: Object.h:63
Base class providing referencing counted objects.
Definition: Referenced.h:113
Smart pointer for handling referenced counted objects.
Definition: ref_ptr.h:29
Handles testing of ports in a range.
Definition: PortRange.h:30
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)
uint16_t UInt16
Definition: Integer.h:39
AGXCORE_EXPORT Component * root()
Inheritance with partial specialization due to bug with ref_ptr containers.
#define AGXCORE_EXPORT
agx::Object is a refcounted object with a name.
Definition: Object.h:59