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 httpLoadScene(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
109 bool httpListJournals(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
111 bool httpAddJournalBinding(agxIO::RemoteCommandServer* server, mg_connection* connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
112 bool httpRemoveJournalBinding(agxIO::RemoteCommandServer* server, mg_connection* connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
113 bool httpListJournalBindings(agxIO::RemoteCommandServer* server, mg_connection* connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
114 bool httpSaveCurrentJournalBindings(agxIO::RemoteCommandServer* server, mg_connection* connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
116 bool httpEnableRecording(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
117 bool httpDisableRecording(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
119 bool httpLoadJournal(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
120 bool httpUnloadJournal(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
123 #if AGX_USE_WEBSOCKETS()
124 void webSocketClientConnected(agxNet::WebSocket* client);
125 void webSocketClientDisconnected(agxNet::WebSocket* client);
128 void simulationTickCallback(
agx::Clock *clock);
131 bool httpEnablePlaybackLoop(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
132 bool httpDisablePlaybackLoop(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
134 bool httpEnableRealTimeSync(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
135 bool httpDisableRealTimeSync(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
137 bool httpSetPlaybackPosition(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
139 bool httpAttachVideoStream(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
143 bool httpAttachRemoteSolver(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
144 bool httpDetachRemoteSolver(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
145 bool httpAttachStatisticsViewer(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
146 bool httpGetControlChannel(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
148 bool httpGetRemoteViewChannel(agxIO::RemoteCommandServer *server, mg_connection *connection,
const agxIO::RemoteCommandServer::QueryArgumentTable& arguments);
150 void buildStateMessage(agxJson::Value& eRoot);
152 #if AGX_USE_WEBSOCKETS()
155 void ccAttachRemoteViewer(agxNet::WebSocket::ControlChannel *channel, agxNet::WebSocket *socket,
agxNet::StructuredMessage *message);
156 void ccRegisterUser(agxNet::WebSocket::ControlChannel *channel, agxNet::WebSocket *socket,
agxNet::StructuredMessage *message);
157 void ccRegisterAttributeListener(agxNet::WebSocket::ControlChannel *channel, agxNet::WebSocket *socket,
agxNet::StructuredMessage *message);
158 void ccUnregisterAttributeListener(agxNet::WebSocket::ControlChannel *channel, agxNet::WebSocket *socket,
agxNet::StructuredMessage *message);
159 void ccRegisterCallableDataStream(agxNet::WebSocket::ControlChannel *channel, agxNet::WebSocket *socket,
agxNet::StructuredMessage *message);
160 void ccFunctionCall(agxNet::WebSocket::ControlChannel *channel, agxNet::WebSocket *socket,
agxNet::StructuredMessage *message);
161 void ccHeartbeat(agxNet::WebSocket::ControlChannel *channel, agxNet::WebSocket *socket,
agxNet::StructuredMessage *message);
162 void ccEnableStructureSynchronization(agxNet::WebSocket::ControlChannel *channel, agxNet::WebSocket *socket,
agxNet::StructuredMessage *message);
163 void ccGetTreeStructure(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
164 void ccGetTrimeshData(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
165 void ccGetShapeRenderData(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
166 void ccGetState(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
167 void ccExitApplication(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
168 void ccReloadScene(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
169 void ccLoadSceneCallback(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message, std::function<
void ()> callback);
171 std::function<
void ()> callback);
172 void ccLoadScene(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
173 void ccKeyDown(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
175 void ccMousePickStart(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
176 void ccMousePickDrag(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
177 void ccMousePickEnd(agxNet::WebSocket::ControlChannel* channel, agxNet::WebSocket* socket,
agxNet::StructuredMessage* message);
183 void djangoMessage(agxNet::WebSocket *,
agx::UInt8* data,
size_t numBytes);
186 void getConstraintData(
agx::Constraint *constraint, agxJson::Value& eConstraint);
187 void getRigidBodyData(
agx::RigidBody *body, agxJson::Value& eBody);
189 agxJson::Value& createArrayElement( agxJson::Value& eParent,
const char* groupName );
197 void addSocket(agxNet::WebSocket *socket);
200 virtual ~StructureSynchronization();
212 agxNet::WebSocketRefVector m_sockets;
216 StructureSynchronizationTable m_structureSynchronizationTable;
224 MouseSpringRenderer(ExampleApplication *application);
226 void frameCallback(agxData::Frame *frame);
227 virtual void writeFrame(
const agxData::Frame *frame)
override;
228 virtual void endOfStream()
override;
235 virtual ~MouseSpringRenderer();
242 ExampleApplication *m_application;
243 osg::PositionAttitudeTransform *m_mouseAttachTransform;
244 osg::PositionAttitudeTransform *m_mousePositionTransform;
245 osg::Geometry *m_mouseSpringLine;
246 osg::ref_ptr< osg::Switch > m_mouseSpringSwitch;
257 OrbitCamera() : enabled(false), mode(0) {}
267 OrbitCamera m_orbitCamera;
269 ExampleApplication *m_application;
273 bool m_enableTickCallback;
274 MouseSpringRendererRef m_mouseSpringRenderer;
279 bool m_remoteSimulationReceived;
280 std::string loadSceneFormat;
282 agx::Real m_interactiveClientHeartbeatTimeOut;
284 RemoteViewerThreadRef m_remoteViewerThread;
290 bool m_needSceneLoading;
294 class ControlChannelTCP :
public Referenced
297 ControlChannelTCP(ExampleApplicationController *controller,
agx::UInt16 port);
307 void registerMessageHandler(
const agx::String& uri, MessageHandler callback);
310 virtual ~ControlChannelTCP();
321 MessageHandlerTable m_messageHandlerTable;
325 bool m_setupComplete;
327 #if AGX_USE_WEBSOCKETS()
328 agxNet::WebSocket::ControlChannelRef m_controlChannel;
329 agxNet::WebSocketRef m_djangoSocket;
330 void controlChannelSocketConnectedCallback(agxNet::WebSocket::ControlChannel *channel, agxNet::WebSocket *socket);
331 void controlChannelSocketDisconnectedCallback(agxNet::WebSocket::ControlChannel *channel, agxNet::WebSocket *socket);
332 agxNet::WebSocket::ControlChannel::SocketEvent::CallbackType m_controlChannelSocketConnectedCallback;
333 agxNet::WebSocket::ControlChannel::SocketEvent::CallbackType m_controlChannelSocketDisconnectedCallback;
335 #if AGX_USE_WEBPLOT()
348 UserData& getUserData(agxNet::WebSocket *);
351 UserTable m_userTable;
353 typedef std::queue<agxNet::StructuredMessageRef> FrameMessageQueue;
354 FrameMessageQueue m_frameMessageQueue;
357 ControlChannelTCPRef m_controlChannelTCP;
358 agx::JournalReaderRef m_journalReader;
361 VideoStreamRef m_videoStream;
366 bool m_djangoProxyResponse;
367 agxCallable::CallableAccessorRefVector m_dataStreamCallables;
371 struct ExampleApplicationController::RenderFrame
373 osg::ref_ptr<osg::Image> image;
381 RemoteViewerThread(ExampleApplication *application);
385 void pushFrame(
const RenderFrame& frame);
390 virtual ~RemoteViewerThread();
393 virtual void run()
override;
395 void connectCallback(agxNet::WebSocket *client);
396 void disconnectCallback(agxNet::WebSocket *client);
397 void messageReceiveCallback(agxNet::WebSocket *,
agx::UInt8 *data,
size_t numBytes);
398 void updatePickHandlers();
401 agxNet::WebSocketServerRef m_serverSocket;
405 RenderFrame m_currentFrame;
406 ExampleApplication *m_application;
409 PickHandlerTable m_pickHandlerTable;