AGX Dynamics  2.35.0.1
RemoteCommandServer.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. 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  bool start(agxNet::PortRange *range, bool allowExternalRequests = false);
68  bool start(agx::UInt16 port = 5656, bool allowExternalRequests = false);
69  void stop();
70 
71  void service();
72 
73  agx::UInt16 getPort() const;
74 
75  bool isStarted() const;
76  bool isUsingSSL() const;
77 
78  void setAuthenticationToken(const agx::String& token);
79  const agx::String& getAuthenticationToken() const;
80 
81  bool add( agxIO::RemoteCommandServer::ScriptListener* scriptListener );
82  bool remove( agxIO::RemoteCommandServer::ScriptListener* scriptListener );
83 
84 
85  class AGXCORE_EXPORT RequestHandler
86  {
87  public:
88  typedef std::function<bool (RemoteCommandServer *, mg_connection *, const QueryArgumentTable&)> CallbackFunction;
89 
90  RequestHandler() {}
91  RequestHandler(const CallbackFunction& func) : m_callback(func) {}
92 
93  template<class ClassT>
94  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)) {}
95 
96  bool isValid() const
97  {
98  return m_callback ? true : false;
99  }
100 
101  bool operator() (RemoteCommandServer *server, mg_connection *connection, const QueryArgumentTable& arguments) const
102  {
103  return m_callback(server, connection, arguments);
104  }
105 
106  private:
107  CallbackFunction m_callback;
108  };
109 
110  void registerHandler(const agx::String& uri, const RequestHandler& handler);
111  void unregisterHandler(const agx::String& uri);
112 
113  static agx::String get_var(const char* buf, size_t buf_len, const char* name);
114  static agx::String get_qsvar(const mg_connection* connection, const char* name);
115 
116  static void writeResponse(const agxJson::Value& eResponse, mg_connection* connection);
117 
118  // Safari ajax breaks if we do not write any response, use this if no response is sent from a handler
119  static void writeDummyResponse(mg_connection *connection);
120 
121  void log(const char *format, ...);
122 
123  protected:
124  RemoteCommandServer(const agx::Name& name = "RemoteCommandServer");
125  virtual ~RemoteCommandServer();
126 
127  friend class RemoteCommandServerSingleton;
128 
129  private:
130  bool handleAutoComplete(RemoteCommandServer *server, mg_connection *connection, const QueryArgumentTable& arguments);
131  bool handleCommand(RemoteCommandServer *server, mg_connection *connection, const QueryArgumentTable& arguments);
132  bool handleScript(RemoteCommandServer *server, mg_connection *connection, const QueryArgumentTable& arguments);
133  bool handleScriptAutoComplete(RemoteCommandServer *server, mg_connection *connection, const QueryArgumentTable& arguments);
134  bool handleJournalNavigationTree(RemoteCommandServer *server, mg_connection *connection, const QueryArgumentTable& arguments);
135  bool handleJournalConfigurationList(RemoteCommandServer *server, mg_connection *connection, const QueryArgumentTable& arguments);
136  bool readComponent(RemoteCommandServer *server, mg_connection *connection, const QueryArgumentTable& arguments);
137  bool writeComponent(RemoteCommandServer *server, mg_connection *connection, const QueryArgumentTable& arguments);
138  // bool handleComponentRequest(mg_connection *connection, const agx::String& path);
139 
140 
141 
142  agx::Object* findMatchingNode(agx::Path path, agx::Component* root, agx::Path& matchedPath, agx::StringVector& matchList);
143  void writeNodeDescription(agx::Object* object, agxJson::Value& eNode);
144  int handleHttpEvent(mg_connection* connection);
145  int handleHttpAuth(mg_connection* connection);
146  static int mgAuthCallback(mg_connection* connection, void* server);
147  static int mgRequestCallback(mg_connection* connection, void* server);
148  static bool handle_jsonp(mg_connection* connection, const mg_connection* request_info);
149  void parseArguments(QueryArgumentTable& result, const mg_connection* connection);
150 
151  void writeTreeStructureJson(const agx::Object* node, agxJson::Value& eNode);
152 
153 
154  private:
155  agx::UInt16 m_port;
156  mg_context *m_mongoose;
157  agx::String m_htmlRoot;
158  agx::String m_token;
159  agx::ComponentRef m_activeContext;
160  ScriptListenerContainer m_scriptListeners;
161  bool m_allowExternalRequests;
162  bool m_started;
163  bool m_useSSL;
164 
165 
166  typedef agx::HashTable<agx::String, RequestHandler> RequestHandlerTable;
167  RequestHandlerTable m_requestHandlerTable;
168 
169  typedef agx::HashSet<agx::String> StringSet;
170  StringSet m_allowedComponentFileTypes;
171  };
172 
173  class RemoteCommandServer::QueryArgumentTable : public agx::HashTable<agx::String, agx::String>
174  {
175  public:
176 
178 
179  // Overrider []-operator to return empty string if not inserted
180  AGX_FORCE_INLINE const agx::String& operator[](const agx::String& key ) const
181  {
182  const_iterator it = find( key );
183  static agx::String emptyString;
184  return it != end() ? it->second : emptyString;
185  }
186 
187  };
188 
189 }
190 
191 // End AGX_USE_WEBSOCKETS.
192 #endif
193 
194 #endif /* AGXIO_REMOTE_COMMAND_SERVER_H */
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
#define AGXCORE_EXPORT
Handles testing of ports in a range.
Definition: PortRange.h:30
A component is an object containing other objects, enabling hierarchical structuring.
Definition: Component.h:39
Inheritance with partial specialization due to bug with ref_ptr containers.
Definition: agx/HashSet.h:666
Inheritance with partial specialization due to bug with ref_ptr containers.
Representation of a name string.
Definition: Name.h:33
agx::Object is a refcounted object with a name.
Definition: Object.h:60
Representation of a path, a list of name components.
Definition: Path.h:34
Base class providing referencing counted objects.
Definition: Referenced.h:114
Smart pointer for handling referenced counted objects.
Definition: ref_ptr.h:30
#define AGX_FORCE_INLINE
Definition: macros.h:58
The agxIO namespace contains classes for reading, writing and finding files.
Definition: Material.h:42
uint16_t UInt16
Definition: Integer.h:39
AGXCORE_EXPORT Component * root()
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)