AGX Dynamics  2.35.0.1
RemoteJournalConnector.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 //#include <signal.h>
18 #pragma once
19 
20 #include <agx/config/AGX_USE_WEBSOCKETS.h>
21 
22 #if AGX_USE_WEBSOCKETS()
23 
24 #include <agxNet/Socket.h>
25 #include <agxNet/WebSocket.h>
26 #include <agxNet/FramePacket.h>
27 #include <agxData/Track.h>
28 #include <agxNet/WebSocketTrack.h>
30 #include <agx/Thread.h>
31 
32 //#include <agx/Interrupt.h>
33 //#include <agxIO/ArgumentParser.h>
34 
35 #include <agxSDK/Simulation.h>
36 
37 using namespace agx;
38 using namespace agxSDK;
39 
40 namespace agxNet{
41  AGX_DECLARE_POINTER_TYPES(RemoteJournalConnector);
42  AGX_DECLARE_VECTOR_TYPES(RemoteJournalConnector);
43 
47  class AGXPHYSICS_EXPORT RemoteJournalConnector {
48  public:
49  /*
50  * Initialize a RemoteJournalConnector entity that expects a
51  * control channel server at host:port
52  */
53  RemoteJournalConnector(String host, UInt16 port);
54  virtual ~RemoteJournalConnector();
55 
56  /*
57  * Attempts to connect to a control channel at host:port
58  */
59  void connectToControlChannel();
60  agxSDK::Simulation* getSimulation();
61 
62  /*
63  * Sends a "Simulation.Play" message to the control server
64  */
65  void sendPlayMessage();
66 
67  /*
68  * Mutex that gets locked during manipulation of the Simulation
69  */
70  virtual agx::Mutex& getFrameMutex()
71  {
72  static agx::Mutex s_defaultMutex;
73  return s_defaultMutex;
74  };
75 
76  /*
77  \return True as long as the connection should be kept
78  */
79  virtual bool isRunning() = 0;
80 
81  /*
82  * Called after the control channel have recieved and parsed a "Simulation.LoadScene" message
83  */
84  virtual void onLoadSimulation(Simulation*) {};
85 
86  /*
87  * Called after the connection to the Journal Socket is established
88  */
89  virtual void onRemoteViewerAttached() {};
90 
91  /*
92  * Called when the control channel receives a "Simulation.Exit" message
93  */
94  virtual void onSimulationExit() {};
95 
96  private:
97  void connectToJournalSocket();
98  void handleSimulationLoadScene(agxNet::StructuredMessage* message);
99  void onControlChannelMessage(agxNet::WebSocket *, UInt8 *data, size_t numBytes);
100  void onDisconnected(agxNet::WebSocket* disconnectedSocket);
101  std::stringstream readAgxSceneDataFromLoadSceneMessage(agxNet::StructuredMessage* message);
102  void runServerSocketThread();
103  bool service();
104 
105  agxSDK::SimulationRef m_simulation;
106  agxNet::WebSocketServerRef m_serverSocket;
107  agxNet::WebSocketRef m_controlChannelSocket;
108  agxNet::WebSocketRef m_journalTrackSocket;
109  agxNet::WebSocketFrameReaderRef m_journalTrackReader;
110  agxSDK::SimulationFrameWriterRef m_simulationWriter;
111  agxData::TrackRef m_socketToSimulationTrack;
112  String m_host;
113  UInt16 m_controlChannelPort;
114  UInt16 m_journalSocketPort;
115  std::atomic<bool> m_serverSocketShouldStop;
116  std::thread m_serverSocketThread;
117  };
118 }
119 
120 #endif
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
#define AGXPHYSICS_EXPORT
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: agx/Vector.h:34
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:127
Containins classes for sending/reading data over sockets as well as compression functionality.
Definition: MacUtil.h:23
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:31
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
uint16_t UInt16
Definition: Integer.h:39
std::mutex Mutex
Definition: Referenced.h:93
uint8_t UInt8
Definition: Integer.h:38