17 #ifndef AGXOSG_EXAMPLEAPPLICATIONCONTROLLER_H
18 #define AGXOSG_EXAMPLEAPPLICATIONCONTROLLER_H
20 #include <agx/config/AGX_USE_WEBSOCKETS.h>
21 #include <agx/config/AGX_USE_WEBPLOT.h>
37 #include <osg/Geometry>
38 #include <osg/PositionAttitudeTransform>
39 #include <osg/LineWidth>
40 #include <osgViewer/Viewer>
41 #include <osg/ShapeDrawable>
49 #include <agxCallable/CallableAccessor.h>
51 #if AGX_USE_WEBSOCKETS()
56 class ExampleApplication;
65 ExampleApplicationController(ExampleApplication *application);
67 void signalAutoStepping(
bool flag);
68 void pushParameters();
70 void setEnableTickSignals(
bool flag);
73 void updateMouseSpringRenderer();
79 bool needSceneLoading();
80 void performSceneLoading();
83 void updateRemoteViewers(
const RenderFrame& frame);
90 void initPlotCallbacks();
94 void initRemoteConnection();
96 agxNet::WebSocket::ControlChannel *getControlChannel();
99 virtual ~ExampleApplicationController();
102 bool httpGetCurrentState(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
103 bool httpStartSimulation(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
104 bool httpStopSimulation(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
105 bool httpStepForward(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
106 bool httpStepBackward(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
107 bool httpListLuaScenes(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
108 bool httpLoadScene(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
110 bool httpListJournals(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
112 bool httpAddJournalBinding(agxIO::RemoteCommandServer* server, mg_connection* connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
113 bool httpRemoveJournalBinding(agxIO::RemoteCommandServer* server, mg_connection* connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
114 bool httpListJournalBindings(agxIO::RemoteCommandServer* server, mg_connection* connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
115 bool httpSaveCurrentJournalBindings(agxIO::RemoteCommandServer* server, mg_connection* connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
117 bool httpEnableRecording(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
118 bool httpDisableRecording(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
120 bool httpLoadJournal(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
121 bool httpUnloadJournal(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
124 #if AGX_USE_WEBSOCKETS()
125 void webSocketClientConnected(agxNet::WebSocket* client);
126 void webSocketClientDisconnected(agxNet::WebSocket* client);
129 void simulationTickCallback(
agx::Clock *clock);
132 bool httpEnablePlaybackLoop(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
133 bool httpDisablePlaybackLoop(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
135 bool httpEnableRealTimeSync(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
136 bool httpDisableRealTimeSync(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
138 bool httpSetPlaybackPosition(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
140 bool httpAttachVideoStream(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
144 bool httpAttachRemoteSolver(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
145 bool httpDetachRemoteSolver(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
146 bool httpAttachStatisticsViewer(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
147 bool httpGetControlChannel(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
149 bool httpGetRemoteViewChannel(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
151 void buildStateMessage(agxJson::Value& eRoot);
153 #if AGX_USE_WEBSOCKETS()
156 void ccAttachRemoteViewer(agxNet::WebSocket::ControlChannel *channel, agxNet::WebSocket *socket,
agxNet::StructuredMessage *message);
157 void ccRegisterUser(agxNet::WebSocket::ControlChannel *channel, agxNet::WebSocket *socket,
agxNet::StructuredMessage *message);
158 void ccRegisterAttributeListener(agxNet::WebSocket::ControlChannel *channel, agxNet::WebSocket *socket,
agxNet::StructuredMessage *message);
159 void ccUnregisterAttributeListener(agxNet::WebSocket::ControlChannel *channel, agxNet::WebSocket *socket,
agxNet::StructuredMessage *message);
160 void ccRegisterCallableDataStream(agxNet::WebSocket::ControlChannel *channel, agxNet::WebSocket *socket,
agxNet::StructuredMessage *message);
161 void ccFunctionCall(agxNet::WebSocket::ControlChannel *channel, agxNet::WebSocket *socket,
agxNet::StructuredMessage *message);
162 void ccHeartbeat(agxNet::WebSocket::ControlChannel *channel, agxNet::WebSocket *socket,
agxNet::StructuredMessage *message);
163 void ccEnableStructureSynchronization(agxNet::WebSocket::ControlChannel *channel, agxNet::WebSocket *socket,
agxNet::StructuredMessage *message);
164 void ccGetTreeStructure(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
165 void ccGetTrimeshData(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
166 void ccGetShapeRenderData(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
167 void ccGetState(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
168 void ccExitApplication(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
169 void ccReloadScene(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
170 void ccLoadSceneCallback(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message, std::function<
void ()> callback);
172 std::function<
void ()> callback);
173 void ccLoadScene(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
174 void ccKeyDown(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
176 void ccMousePickStart(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
177 void ccMousePickDrag(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
178 void ccMousePickEnd(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
184 void djangoMessage(agxNet::WebSocket *,
agx::UInt8* data,
size_t numBytes);
187 void getConstraintData(
agx::Constraint *constraint, agxJson::Value& eConstraint);
188 void getRigidBodyData(
agx::RigidBody *body, agxJson::Value& eBody);
190 agxJson::Value& createArrayElement( agxJson::Value& eParent,
const char* groupName );
198 void addSocket(agxNet::WebSocket *socket);
201 virtual ~StructureSynchronization();
213 agxNet::WebSocketRefVector m_sockets;
217 StructureSynchronizationTable m_structureSynchronizationTable;
225 MouseSpringRenderer(ExampleApplication *application);
227 void frameCallback(agxData::Frame *frame);
228 virtual void writeFrame(
const agxData::Frame *frame)
override;
229 virtual void endOfStream()
override;
236 virtual ~MouseSpringRenderer();
243 ExampleApplication *m_application;
244 osg::PositionAttitudeTransform *m_mouseAttachTransform;
245 osg::PositionAttitudeTransform *m_mousePositionTransform;
246 osg::Geometry *m_mouseSpringLine;
247 osg::ref_ptr< osg::Switch > m_mouseSpringSwitch;
258 OrbitCamera() : enabled(false), mode(0) {}
268 OrbitCamera m_orbitCamera;
270 ExampleApplication *m_application;
274 bool m_enableTickCallback;
275 MouseSpringRendererRef m_mouseSpringRenderer;
280 bool m_remoteSimulationReceived;
281 std::string loadSceneFormat;
283 agx::Real m_interactiveClientHeartbeatTimeOut;
285 RemoteViewerThreadRef m_remoteViewerThread;
291 bool m_needSceneLoading;
295 class ControlChannelTCP :
public Referenced
298 ControlChannelTCP(ExampleApplicationController *controller,
agx::UInt16 port);
308 void registerMessageHandler(
const agx::String& uri, MessageHandler callback);
311 virtual ~ControlChannelTCP();
322 MessageHandlerTable m_messageHandlerTable;
326 bool m_setupComplete;
328 #if AGX_USE_WEBSOCKETS()
329 agxNet::WebSocket::ControlChannelRef m_controlChannel;
330 agxNet::WebSocketRef m_djangoSocket;
331 void controlChannelSocketConnectedCallback(agxNet::WebSocket::ControlChannel *channel, agxNet::WebSocket *socket);
332 void controlChannelSocketDisconnectedCallback(agxNet::WebSocket::ControlChannel *channel, agxNet::WebSocket *socket);
333 agxNet::WebSocket::ControlChannel::SocketEvent::CallbackType m_controlChannelSocketConnectedCallback;
334 agxNet::WebSocket::ControlChannel::SocketEvent::CallbackType m_controlChannelSocketDisconnectedCallback;
336 #if AGX_USE_WEBPLOT()
349 UserData& getUserData(agxNet::WebSocket *);
352 UserTable m_userTable;
354 typedef std::queue<agxNet::StructuredMessageRef> FrameMessageQueue;
355 FrameMessageQueue m_frameMessageQueue;
358 ControlChannelTCPRef m_controlChannelTCP;
359 agx::JournalReaderRef m_journalReader;
362 VideoStreamRef m_videoStream;
367 bool m_djangoProxyResponse;
368 agxCallable::CallableAccessorRefVector m_dataStreamCallables;
372 struct ExampleApplicationController::RenderFrame
374 osg::ref_ptr<osg::Image> image;
382 RemoteViewerThread(ExampleApplication *application);
386 void pushFrame(
const RenderFrame& frame);
391 virtual ~RemoteViewerThread();
394 virtual void run()
override;
396 void connectCallback(agxNet::WebSocket *client);
397 void disconnectCallback(agxNet::WebSocket *client);
398 void messageReceiveCallback(agxNet::WebSocket *,
agx::UInt8 *data,
size_t numBytes);
399 void updatePickHandlers();
402 agxNet::WebSocketServerRef m_serverSocket;
406 RenderFrame m_currentFrame;
407 ExampleApplication *m_application;
410 PickHandlerTable m_pickHandlerTable;