VR-Engage  2.3
Loading...
Searching...
No Matches
vreBackendConnector.h
Go to the documentation of this file.
1/******************************************************************************
2** Copyright (c) 2026 MAK Technologies
3** All rights reserved.
4******************************************************************************/
5
6//! \file vreBackendConnector.h
7//! \brief Defines the DtVreBackendConnector class for VR-Engage backend lifecycle management
8//!
9//! This connector manages the VR-Engage simulation backend (vrEngageSim) process
10//! lifecycle: configuration, start/stop, scenario loading, backend status monitoring,
11//! chat bridging, and wind state queries.
12//!
13//! It receives shared infrastructure from DtConnectorAccessory and does not own any
14//! infrastructure components. Cross-connector state (session terrain, SMS list, play
15//! state) is accessed via a non-owning pointer to DtSessionStatusConnector.
16
17#pragma once
18
21
23
24#include <vlpi/simulationAddress.h>
25
26#include <boost/signals2/connection.hpp>
27
28#include <chrono>
29#include <set>
30#include <string>
31#include <vector>
34
35//! \brief Forward declarations
36class DtExerciseConnInitializer;
37class DtFilename;
38class DtScenario;
39class DtSimMessage;
40class DtBackendMap;
41
42namespace makVrfEvents
43{
44class DtEvent;
45} // namespace makVrfEvents
46
47namespace makVrf
48{
49class DtVrlinkVrfRemoteController;
50} // namespace makVrf
51
52namespace makVre
53{
55
56//! \brief Connector for VR-Engage backend lifecycle management and process launching
57//!
58//! DtVreBackendConnector manages the full lifecycle of the VR-Engage simulation backend:
59//! configuration of the backend's simulation address, building and launching the backend
60//! process, loading scenarios, stopping the backend, and monitoring backend status changes.
61//!
62//! In addition, it bridges chat events between the VRF event manager and the VR-Engage
63//! message bus, and handles wind state queries via the VRF remote environment controller.
64//!
65//! \ingroup vrePlayerStationConnector
67{
68public:
69 //! \brief Default constructor. Initializes all state to default values.
71
72 //! \brief Virtual destructor
73 virtual ~DtVreBackendConnector() override;
74
75 //! \brief Installs the backend connector with shared infrastructure
76 //!
77 //! \param connection Non-owning pointer to the protocol-specific VR-Link connection.
78 //! \param vrfRemoteController Non-owning pointer to the shared VRF remote controller
79 //! owned by DtConnectorAccessory.
80 //!
81 //! Also binds handles from the global App attribute tree and locks write-protected handles
82 //! (myBackendIsHostAttr, myBackendStatusAttr) to prevent
83 //! unintended writes from other connectors. These handles are read-only for this connector.
84 virtual void install(
85 makVrv::DT_PROTOCOL_NAMESPACE::DtVrlinkConnection* connection,
86 makVrf::DtVrlinkVrfRemoteController* vrfRemoteController);
87
88 //! \brief Performs periodic backend status evaluation
89 //!
90 //! Calls updateBackendStatus() each tick to detect backend status changes
91 //! and broadcast updates to the frontend.
92 //!
93 //! \param dt The elapsed time in seconds since the last tick
94 virtual void tick(double dt) override;
95
96 //! \brief Shuts down the connector
97 //!
98 //! Stops any running VRE backend process (sends DtIfExit), disconnects the
99 //! chat boost signal, unregisters all VRE message handlers, and removes the
100 //! DtLoadCompleteMessageType VRF callback.
101 virtual void shutdown() override;
102
103 //! \brief Backend lifecycle message handlers
104 //! @{
105
106 //! \brief Handles backend configuration (site ID and application ID for starting address)
107 //! \param message Pointer to the SetBackendConfigMessage
108 //! \return HANDLED
110
111 //! \brief Handles a request to launch the VRE backend process
112 //! \param message Pointer to the StartBackendMessage
113 //! \return HANDLED
114 //!
115 //! If a backend is already running, queues a StopBackend + restart sequence.
116 //! Otherwise calls launchVrfBackend() to spawn the sim process and updates the backend address.
117 //! Publishes myBackendIsHostAttr to the global App attribute tree.
119
120 //! \brief Handles a request to stop the VRE backend process
121 //! \param message Pointer to the StopBackendMessage (may be nullptr when called internally)
122 //! \return HANDLED
123 //!
124 //! Sends DtIfExit to the backend, clears backend state via clearBackendState(),
125 //! and resets snapshot state.
127
128 //! \brief Handles a request for the current backend status
129 //! \param message Pointer to the RequestBackendStatusMessage
130 //! \return HANDLED
132
133 //! \brief Handles a request to load a scenario on the VRE backend
134 //! \param message Pointer to the LoadBackendMessage
135 //! \return HANDLED
136 //!
137 //! Validates backend readiness, then loads either a session terrain (if a session
138 //! is running), a full scenario file (if this backend can host), or a terrain-only
139 //! scenario. Reads session state from DtSessionStatusConnector.
141
142 //! @}
143
144 //! \brief Chat and environmental message handlers
145 //! @{
146
147 //! \brief Handles chat send messages from the frontend → VRF backend
148 //! \param message Pointer to the ChatMessage
149 //! \return HANDLED if a send-type message, IGNORED for receive-type
151
152 //! \brief Handles wind state request messages
153 //! \param message Pointer to the WindStateRequestMessage
154 //! \return HANDLED
155 //!
156 //! Queries the VRF remote environment controller for wind speed and direction
157 //! at the requested geocentric location and queues a WindStateResponseMessage.
159
160 //! @}
161
162 //! \brief Static callbacks for VRF backend events
163 //! @{
164
165 //! \brief Static callback invoked when a backend simulation address is discovered
166 static void BackendDiscovered(const DtSimulationAddress& addr, void* usr);
167
168 //! \brief Static callback invoked when a backend simulation address is removed
169 static void BackendRemoved(const DtSimulationAddress& addr, void* usr);
170
171 //! \brief Static callback invoked when a VRE backend reports scenario load complete
172 //! \param msg Pointer to the DtLoadComplete simulation message
173 //! \param usr Pointer to the connector instance
174 static void ifLoadCompleteMessageCallback(DtSimMessage* msg, void* usr);
175
176 //! \brief Static callback for remapping backend addresses during scenario load
177 //! \param map Backend address map to remap
178 //! \param expectedBackends Set of expected backend addresses
179 //! \param usr Pointer to the connector instance
180 //! \return true if remapping succeeded
181 static bool backendRemapper(DtBackendMap* map, const std::set<DtSimulationAddress>& expectedBackends, void* usr);
182
183 //! @}
184
185 //! \brief Returns true if this connector has a pending or active VRE backend address
186 //!
187 //! True from the moment handleStartBackend() sets the address, even before the
188 //! backend process connects. Use for lifecycle guards (stop, shutdown, timeout).
189 bool hasBackendAddress() const;
190
191 //! \brief Returns the current VRE backend simulation address
192 //!
193 //! Returns DtSimulationAddress::nullId() when no backend address is configured.
194 //! The address is stored as the vreBackendStatus.SimAddress child attribute.
195 DtSimulationAddress backendAddress() const;
196
197 //! \brief Sets the startup timeout used to detect backends that fail to connect
198 //!
199 //! If the backend process does not appear in the backend listener within this
200 //! many seconds of being launched, the connector clears the backend state.
201 //! Defaults to 30 seconds. A process-exit check (Windows only) takes priority:
202 //! if the launched process is detected as exited before the timeout elapses,
203 //! the state is cleared immediately.
204 //!
205 //! \param seconds Timeout in seconds; must be positive
206 void setStartupTimeoutSeconds(int seconds);
207
208protected:
209 //! \brief Handle to the launched backend process (Windows only; nullptr on other platforms)
210 //!
211 //! Populated by launchVrfBackend() and cleared by clearBackendState(). Used to
212 //! detect early process exit before the startup timeout fires.
213 void* myBackendProcessHandle = nullptr;
214
215 //! \brief Processes a backend discovery notification
216 //! \param addr Simulation address of the discovered backend
217 virtual void processBackendDiscovered(const DtSimulationAddress& addr);
218
219 //! \brief Processes a backend removal notification
220 //! \param addr Simulation address of the removed backend
221 virtual void processBackendRemoved(const DtSimulationAddress& addr);
222
223 //! \brief Processes a load-complete message from the VRE backend
224 //! \param msg Pointer to the load-complete simulation message
225 //!
226 //! Calls loadedScenario() on the VRF remote controller, then takes a base snapshot.
227 virtual void processLoadCompleteMessage(DtSimMessage* msg);
228
229 //! \brief Processes a chat event received from the VRF event manager
230 //! \param evt The VRF chat event; ignored if not a DtChatMessageEvent
231 //!
232 //! Connected via boost signal in install(). Converts the VRF chat event to a
233 //! ChatMessage and queues it in the VRE message manager.
234 virtual void processChatEvent(const makVrfEvents::DtEvent& evt);
235
236 //! \brief Evaluates the current backend status and broadcasts changes to the frontend
237 //!
238 //! Inspects the backend referenced by backendAddress() via the backend listener.
239 //! Sends a late-join Run command if the session is already playing but the backend
240 //! just became loaded. Broadcasts BackendStatusMessage when any field changes.
241 virtual void updateBackendStatus();
242
243 //! \brief Sends the current cached backend status to the frontend
245
246 //! \brief Remaps all backend addresses in a scenario to the current VRE backend address
247 //! \param map Backend address map to remap in-place
248 //! \param expectedBackends Set of expected backend addresses (unused; provided by VRF API)
249 //! \return true if remapping succeeded
250 virtual bool remapBackends(DtBackendMap* map, const std::set<DtSimulationAddress>& expectedBackends);
251
252 //! \brief Configures a scenario with terrain, SMS, and component attachment settings, then sends it to the backend
253 //! \param scenario The scenario object to configure and send
254 //! \param loadMsg The load backend message supplying exercise start time and component attachment table
255 //! \param sessionSmsList The set of simulation model set filenames to add to the scenario
256 //! \param terrain The terrain database filename
257 //! \param guiTerrain The GUI terrain database filename; uses terrain if empty
258 virtual void sendTerrainScenario(DtScenario& scenario,
259 const LoadBackendMessage* loadMsg,
260 const std::set<std::string>& sessionSmsList,
261 const DtFilename& terrain,
262 const DtFilename& guiTerrain);
263
264protected:
265 virtual void sendSessionSmsUpdate();
266
267protected:
268 //! \brief Launches a VR-Forces backend process with the specified parameters
269 //!
270 //! Builds a command line via vrfSimEngineCommand() and spawns the process
271 //! via the platform process API.
272 //!
273 //! \param address Site/application ID to use for the new backend
274 //! \param sessionId VR-Engage session ID to pass to the backend
275 //! \param simType Simulation mode: full VR-Forces, ownship-only, or limited
276 //! \param pluginBaseNames List of plugin base names to load
277 //! \param showConsole Whether to open a console window for the backend process
278 //! \param additionalArguments Additional command-line arguments (may be modified)
279 //! \return On Windows, the process handle for the launched backend process;
280 //! nullptr on other platforms or if the process was not launched.
281 virtual void* launchVrfBackend(const DtSimulationAddress& address,
282 int sessionId,
283 int simType,
284 const std::vector<std::string>& pluginBaseNames,
285 bool showConsole,
286 std::string& additionalArguments);
287
288 //! \brief Builds the full command-line string for the sim engine process
289 //!
290 //! \param address Site/application ID to use
291 //! \param sessionId VR-Engage session ID
292 //! \param simType Simulation mode (full, ownship, limited)
293 //! \param pluginBaseNames Plugin base names to include
294 //! \param additionalArguments Additional arguments (may be modified to remove duplicates)
295 //! \return Full command string ready for process creation
296 std::string vrfSimEngineCommand(const DtSimulationAddress& address,
297 int sessionId,
298 int simType,
299 const std::vector<std::string>& pluginBaseNames,
300 std::string& additionalArguments);
301
302 //! \brief Returns the sim engine executable name (protocol- and build-variant-aware)
303 //! \return Executable filename, e.g. "vrEngageSimDIS.exe"
304 virtual std::string application() const;
305
306 //! \brief Builds protocol-specific connection parameters for the sim engine command line
307 //!
308 //! \param address Site/application ID
309 //! \param additionalArguments Additional arguments (duplicates are removed and warned)
310 //! \return Connection parameter string
311 virtual std::string connectionParameters(const DtSimulationAddress& address, std::string& additionalArguments) const;
312
313 //! \brief Returns the protocol name string used in command-line arguments
314 //! \return Protocol string, e.g. "DIS", "HLA1516e"
315 virtual std::string protocol() const;
316
317protected:
318 //! \brief Infrastructure pointers (NON-OWNING — owned by DtConnectorAccessory)
319 //! @{
320
321 //! \brief Shared VRF remote controller (NON-OWNING)
322 //!
323 //! Provides access to the VRF message interface, backend listener, remote control
324 //! APIs, and remote environment controller. Lifetime managed by DtConnectorAccessory,
325 //! guaranteed to outlive this connector.
326 makVrf::DtVrlinkVrfRemoteController* myVrfRemoteController = nullptr;
327
328 //! \brief Connection for chat event forwarding callback
329 boost::signals2::scoped_connection myChatEventConnection;
330
331 //! @}
332
333 //! \brief Launch configuration
334 //! @{
335
336 //! \brief Exercise connection initializer used to build protocol-specific connection parameters
337 //!
338 //! Non-owning pointer sourced from the VR-Link driver during install(). Lifetime is
339 //! guaranteed to outlive this connector.
340 const DtExerciseConnInitializer* myExerciseConnInitializer = nullptr;
341
342 //! \brief VR-Engage session ID passed to the backend process on launch
343 int mySessionId = 1;
344
345 //! \brief List of plugin base names to load in the backend process
346 std::vector<std::string> myPlugins;
347
348 //! \brief Whether to open a console window for the backend process
349 bool myShowConsole = false;
350
351 //! \brief Additional command-line arguments for the backend process
352 std::string myArguments;
353
354 //! @}
355
356 //! \brief Clears all backend-owned state atomically
357 //!
358 //! Sets SimEngineAddress to nullId, Enabled to false, and resets snapshot state.
359 //! Called from handleStopBackend, processBackendRemoved, startup timeout, and shutdown.
361
362 //! \brief Backend state
363 //! @{
364
365 //! \brief Starting simulation address used when launching a new backend process
366 //!
367 //! Incremented if already in use (from mySimulationAddresses).
368 DtSimulationAddress myBackendStartingAddress;
369
370 //! \brief True if a TakeBaseSnapshot save was issued after the scenario loaded
371 bool myTookSnapshot = false;
372
373 //! \brief Simulation time at which the base snapshot was taken
374 double mySnapshotSimTime = 0.0;
375
376 //! \brief Time point at which the backend process was launched
377 //!
378 //! Used to detect backends that fail to start: if the backend has not
379 //! appeared in the backend listener within a timeout period after launch,
380 //! the connector clears the stale address.
381 std::chrono::steady_clock::time_point myBackendLaunchTime;
382
383 //! \brief Startup timeout in seconds for detecting backends that fail to connect
384 //!
385 //! If the backend process has not appeared in the backend listener within this
386 //! many seconds of being launched, the connector clears the stale backend address.
387 //! Configurable via setStartupTimeoutSeconds(). Defaults to 30 seconds.
389
390 //! \brief Set of all discovered backend simulation addresses
391 //!
392 //! Used to avoid starting a new backend at an address already in use.
393 std::set<DtSimulationAddress> mySimulationAddresses;
394
395 //! \brief True if the session is currently playing (not paused)
396 //! \vreAttrKey{sessionPlayState}
398
399 //! \brief Current set of simulation model set filenames for this session
400 //! \vreAttrKey{sessionSmsList}
402
403 //! \brief Indicates whether this station is the backend host; true if started in full-forces (host) mode
404 //! \vreAttrKey{vreBackendIsHost}
406
407 //! \brief Published backend status data
408 //! \vreAttrKey{vreBackendStatus}
410
411 //! \brief Current cached session status data
412 //! \vreAttrKey{sessionStatus}
414
415 //! @}
416};
417
418} // namespace makVre
Provides handle classes for safe access to attributes.
Base class for all simulation event connectors in VREngage.
Definition simEventConnector.h:53
Type-specific handle class for attributes.
Definition attributeHandle.h:229
DtAttributeHandleType< std::set< std::string > > mySessionSmsListAttr
Current set of simulation model set filenames for this session.
Definition vreBackendConnector.h:401
static bool backendRemapper(DtBackendMap *map, const std::set< DtSimulationAddress > &expectedBackends, void *usr)
Static callback for remapping backend addresses during scenario load.
virtual void processBackendDiscovered(const DtSimulationAddress &addr)
Processes a backend discovery notification.
std::string vrfSimEngineCommand(const DtSimulationAddress &address, int sessionId, int simType, const std::vector< std::string > &pluginBaseNames, std::string &additionalArguments)
Builds the full command-line string for the sim engine process.
boost::signals2::scoped_connection myChatEventConnection
Connection for chat event forwarding callback.
Definition vreBackendConnector.h:329
virtual void updateBackendStatus()
Evaluates the current backend status and broadcasts changes to the frontend.
virtual std::string connectionParameters(const DtSimulationAddress &address, std::string &additionalArguments) const
Builds protocol-specific connection parameters for the sim engine command line.
virtual DtVreMessageResult handleSetBackendConfig(DtVreMessage *message)
Backend lifecycle message handlers.
virtual void processLoadCompleteMessage(DtSimMessage *msg)
Processes a load-complete message from the VRE backend.
virtual ~DtVreBackendConnector() override
Virtual destructor.
virtual void tick(double dt) override
Performs periodic backend status evaluation.
static void BackendDiscovered(const DtSimulationAddress &addr, void *usr)
Static callbacks for VRF backend events.
virtual std::string application() const
Returns the sim engine executable name (protocol- and build-variant-aware)
DtVreBackendConnector()
Default constructor. Initializes all state to default values.
virtual std::string protocol() const
Returns the protocol name string used in command-line arguments.
virtual void install(makVrv::DT_PROTOCOL_NAMESPACE::DtVrlinkConnection *connection, makVrf::DtVrlinkVrfRemoteController *vrfRemoteController)
Installs the backend connector with shared infrastructure.
virtual void shutdown() override
Shuts down the connector.
virtual DtVreMessageResult handleRequestBackendStatus(DtVreMessage *message)
Handles a request for the current backend status.
DtAttributeHandleType< BackendStatusMessage::BackendStatusData > myBackendStatusAttr
Published backend status data.
Definition vreBackendConnector.h:409
std::string myArguments
Additional command-line arguments for the backend process.
Definition vreBackendConnector.h:352
std::set< DtSimulationAddress > mySimulationAddresses
Set of all discovered backend simulation addresses.
Definition vreBackendConnector.h:393
virtual void processBackendRemoved(const DtSimulationAddress &addr)
Processes a backend removal notification.
virtual void * launchVrfBackend(const DtSimulationAddress &address, int sessionId, int simType, const std::vector< std::string > &pluginBaseNames, bool showConsole, std::string &additionalArguments)
Launches a VR-Forces backend process with the specified parameters.
virtual DtVreMessageResult handleWindRequest(DtVreMessage *message)
Handles wind state request messages.
virtual bool remapBackends(DtBackendMap *map, const std::set< DtSimulationAddress > &expectedBackends)
Remaps all backend addresses in a scenario to the current VRE backend address.
virtual void sendBackendStatusMessage()
Sends the current cached backend status to the frontend.
std::chrono::steady_clock::time_point myBackendLaunchTime
Time point at which the backend process was launched.
Definition vreBackendConnector.h:381
const DtExerciseConnInitializer * myExerciseConnInitializer
Launch configuration.
Definition vreBackendConnector.h:340
DtSimulationAddress myBackendStartingAddress
Backend state.
Definition vreBackendConnector.h:368
int myStartupTimeoutSeconds
Startup timeout in seconds for detecting backends that fail to connect.
Definition vreBackendConnector.h:388
void clearBackendState()
Clears all backend-owned state atomically.
DtSimulationAddress backendAddress() const
Returns the current VRE backend simulation address.
makVrf::DtVrlinkVrfRemoteController * myVrfRemoteController
Infrastructure pointers (NON-OWNING — owned by DtConnectorAccessory)
Definition vreBackendConnector.h:326
std::vector< std::string > myPlugins
List of plugin base names to load in the backend process.
Definition vreBackendConnector.h:346
bool myTookSnapshot
True if a TakeBaseSnapshot save was issued after the scenario loaded.
Definition vreBackendConnector.h:371
DtAttributeHandleType< bool > mySessionPlayStateAttr
True if the session is currently playing (not paused)
Definition vreBackendConnector.h:397
virtual DtVreMessageResult handleChatEvent(DtVreMessage *message)
Chat and environmental message handlers.
void * myBackendProcessHandle
Handle to the launched backend process (Windows only; nullptr on other platforms)
Definition vreBackendConnector.h:213
virtual void sendTerrainScenario(DtScenario &scenario, const LoadBackendMessage *loadMsg, const std::set< std::string > &sessionSmsList, const DtFilename &terrain, const DtFilename &guiTerrain)
Configures a scenario with terrain, SMS, and component attachment settings, then sends it to the back...
bool hasBackendAddress() const
Returns true if this connector has a pending or active VRE backend address.
virtual void processChatEvent(const makVrfEvents::DtEvent &evt)
Processes a chat event received from the VRF event manager.
virtual void sendSessionSmsUpdate()
void setStartupTimeoutSeconds(int seconds)
Sets the startup timeout used to detect backends that fail to connect.
virtual DtVreMessageResult handleStopBackend(DtVreMessage *message)
Handles a request to stop the VRE backend process.
double mySnapshotSimTime
Simulation time at which the base snapshot was taken.
Definition vreBackendConnector.h:374
virtual DtVreMessageResult handleLoadBackend(DtVreMessage *message)
Handles a request to load a scenario on the VRE backend.
DtAttributeHandleType< bool > myBackendIsHostAttr
Indicates whether this station is the backend host; true if started in full-forces (host) mode.
Definition vreBackendConnector.h:405
static void BackendRemoved(const DtSimulationAddress &addr, void *usr)
Static callback invoked when a backend simulation address is removed.
DtAttributeHandleType< VrfSessionStatusMessage::VrfSessionStatusData > mySessionStatusAttr
Current cached session status data.
Definition vreBackendConnector.h:413
virtual DtVreMessageResult handleStartBackend(DtVreMessage *message)
Handles a request to launch the VRE backend process.
bool myShowConsole
Whether to open a console window for the backend process.
Definition vreBackendConnector.h:349
static void ifLoadCompleteMessageCallback(DtSimMessage *msg, void *usr)
Static callback invoked when a VRE backend reports scenario load complete.
int mySessionId
VR-Engage session ID passed to the backend process on launch.
Definition vreBackendConnector.h:343
Abstract base class for all VREngage messages.
Definition vreMessage.h:50
Send to create a new scenario or load an existing scenario on this Vr-Engage's sim engine.
Definition loadBackend.h:33
Defines export macros and protocol namespace for vrePlayerStationConnector.
#define VRL_DLL
Empty definition for non-Windows platforms.
Definition export.h:52
!
Definition glsVreMessageUtil.h:50
DtVreMessageResult
Enumeration of possible message handling results.
Definition vreMessage.h:33
Definition vreBackendConnector.h:43
Definition connectorAccessory.h:38
Defines the base DtSimEventConnector class for VREngage simulation events.