Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
TraCIServer.h
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
26// TraCI server used to control sumo by a remote TraCI client
27/****************************************************************************/
28#pragma once
29#include <config.h>
30
31#include <map>
32#include <string>
33#include <set>
34
35#define BUILD_TCPIP
41#include <utils/geom/Boundary.h>
42#include <utils/geom/Position.h>
45#include <microsim/MSNet.h>
49#include <libsumo/TraCIDefs.h>
50#include "TraCIServerAPI_Lane.h"
51
52
53// ===========================================================================
54// class definitions
55// ===========================================================================
60public:
62 typedef bool(*CmdExecutor)(TraCIServer& server, tcpip::Storage& inputStorage, tcpip::Storage& outputStorage);
63
65 return myTargetTime;
66 }
67
69 return myInstance;
70 }
71
74
78 static void openSocket(const std::map<int, CmdExecutor>& execs);
79
80
82 static void close();
83
84
88 static bool wasClosed();
90
91
95 int processCommands(const SUMOTime step, const bool afterMove = false);
96
103 bool processGet(const int commandID, tcpip::Storage& inputStorage, tcpip::Storage& outputStorage);
104
106 void cleanup();
107
108
109 void vehicleStateChanged(const SUMOVehicle* const vehicle, MSNet::VehicleState to, const std::string& info = "");
110
111 void transportableStateChanged(const MSTransportable* const transportable, MSNet::TransportableState to, const std::string& info = "");
112
115
122 void writeStatusCmd(int commandId, int status, const std::string& description, tcpip::Storage& outputStorage);
123
124
130 void writeStatusCmd(int commandId, int status, const std::string& description);
131
132
138 bool writeErrorStatusCmd(int commandId, const std::string& description, tcpip::Storage& outputStorage);
140
141
142
143 const std::map<MSNet::VehicleState, std::vector<std::string> >& getVehicleStateChanges() const {
144 if (myCurrentSocket == mySockets.end()) {
145 // Requested in context of a subscription update
147 } else {
148 // Requested in the context of a custom query by active client
149 return myCurrentSocket->second->vehicleStateChanges;
150 }
151 }
152
153 const std::map<MSNet::TransportableState, std::vector<std::string> >& getTransportableStateChanges() const {
154 if (myCurrentSocket == mySockets.end()) {
155 // Requested in context of a subscription update
157 } else {
158 // Requested in the context of a custom query by active client
159 return myCurrentSocket->second->transportableStateChanges;
160 }
161 }
162
163 void writeResponseWithLength(tcpip::Storage& outputStorage, tcpip::Storage& tempMsg);
164
167 void stateLoaded(SUMOTime targetTime);
168
169 std::vector<std::string>& getLoadArgs() {
170 return myLoadArgs;
171 }
172
175 void initWrapper(const int domainID, const int variable, const std::string& objID);
176 bool wrapConnectionVector(const std::string& objID, const int variable, const std::vector<libsumo::TraCIConnection>& value);
177 bool wrapDouble(const std::string& objID, const int variable, const double value);
178 bool wrapInt(const std::string& objID, const int variable, const int value);
179 bool wrapString(const std::string& objID, const int variable, const std::string& value);
180 bool wrapStringList(const std::string& objID, const int variable, const std::vector<std::string>& value);
181 bool wrapDoubleList(const std::string& objID, const int variable, const std::vector<double>& value);
182 bool wrapPosition(const std::string& objID, const int variable, const libsumo::TraCIPosition& value);
183 bool wrapPositionVector(const std::string& objID, const int variable, const libsumo::TraCIPositionVector& value);
184 bool wrapColor(const std::string& objID, const int variable, const libsumo::TraCIColor& value);
185 bool wrapStringDoublePair(const std::string& objID, const int variable, const std::pair<std::string, double>& value);
186 bool wrapStringDoublePairList(const std::string& objID, const int variable, const std::vector<std::pair<std::string, double> >& value);
187 bool wrapStringPair(const std::string& objID, const int variable, const std::pair<std::string, std::string>& value);
188 bool wrapIntPair(const std::string& objID, const int variable, const std::pair<int, int>& value);
189 bool wrapStage(const std::string& objID, const int variable, const libsumo::TraCIStage& value);
190 bool wrapReservationVector(const std::string& objID, const int variable, const std::vector<libsumo::TraCIReservation>& value);
191 bool wrapLogicVector(const std::string& objID, const int variable, const std::vector<libsumo::TraCILogic>& value);
192 bool wrapLinkVectorVector(const std::string& objID, const int variable, const std::vector<std::vector<libsumo::TraCILink> >& value);
193 bool wrapSignalConstraintVector(const std::string& objID, const int variable, const std::vector<libsumo::TraCISignalConstraint>& value);
194 bool wrapJunctionFoeVector(const std::string& objID, const int variable, const std::vector<libsumo::TraCIJunctionFoe>& value);
195 bool wrapNextStopDataVector(const std::string& objID, const int variable, const std::vector<libsumo::TraCINextStopData>& value);
196 bool wrapVehicleDataVector(const std::string& objID, const int variable, const std::vector<libsumo::TraCIVehicleData>& value);
197 bool wrapBestLanesDataVector(const std::string& objID, const int variable, const std::vector<libsumo::TraCIBestLanesData>& value);
198 bool wrapNextTLSDataVector(const std::string& objID, const int variable, const std::vector<libsumo::TraCINextTLSData>& value);
201
202
203private:
207 TraCIServer(const SUMOTime begin, const int port, const int numClients);
208
209
211 virtual ~TraCIServer();
212
213
214
215 struct SocketInfo {
216 public:
222 delete socket;
223 }
227 bool executeMove = false;
231 std::map<MSNet::VehicleState, std::vector<std::string> > vehicleStateChanges;
233 std::map<MSNet::TransportableState, std::vector<std::string> > transportableStateChanges;
234 private:
236 };
237
240
244 bool commandGetVersion();
245
250
251
255 int readCommandID(int& commandStart, int& commandLength);
256
258 int dispatchCommand();
259
262 void checkClientOrdering();
263
266
268 SUMOTime nextTargetTime() const;
269
271 void sendOutputToAll() const;
272
275
277 std::map<int, SocketInfo*>::iterator removeCurrentSocket();
278
279
280private:
283
286
289 std::map<int, SocketInfo*> mySockets;
290
292 std::map<int, SocketInfo*> mySocketReorderRequests;
293
295 std::map<int, SocketInfo*>::iterator myCurrentSocket;
296
299
302
305
308
311
313 std::map<int, CmdExecutor> myExecutors;
314
316 std::set<std::pair<int, int>> myParameterized;
317
318 std::vector<std::string> myLoadArgs;
319
321 std::vector<libsumo::Subscription> mySubscriptions;
322
325
333 std::map<MSNet::VehicleState, std::vector<std::string> > myVehicleStateChanges;
334
342 std::map<MSNet::TransportableState, std::vector<std::string> > myTransportableStateChanges;
343
344private:
345 bool addObjectVariableSubscription(const int commandId, const bool hasContext);
347 void removeSubscription(int commandId, const std::string& identity, int domain);
349 std::string& errors);
350
351
353 void removeFilters();
354 void addSubscriptionFilterLanes(std::vector<int> lanes);
359 // TODO: for libsumo, implement convenience definitions present in python client:
360 // void addSubscriptionFilterCF();
361 // void addSubscriptionFilterLC(int direction);
362 void addSubscriptionFilterTurn(double dist);
364 void addSubscriptionFilterVType(std::set<std::string> vTypes);
369 void addSubscriptionFilterFieldOfVision(double openingAngle);
374 void addSubscriptionFilterLateralDistance(double dist);
375
377 bool centralObject(const libsumo::Subscription& s, const std::string& objID);
378
379
380private:
382 TraCIServer& operator=(const TraCIServer& s) = delete;
383
384};
long long int SUMOTime
Definition GUI.h:36
long long int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
Interface for objects listening to transportable state changes.
Definition MSNet.h:718
Interface for objects listening to vehicle state changes.
Definition MSNet.h:659
VehicleState
Definition of a vehicle state.
Definition MSNet.h:626
TransportableState
Definition of a transportable state.
Definition MSNet.h:703
Representation of a vehicle.
Definition SUMOVehicle.h:62
TraCI server used to control sumo by a remote TraCI client.
Definition TraCIServer.h:59
bool(* CmdExecutor)(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Definition of a method to be called for serving an associated commandID.
Definition TraCIServer.h:62
void addSubscriptionFilterDownstreamDistance(double dist)
static bool myDoCloseConnection
Whether the connection was set to be to close.
bool wrapStringDoublePair(const std::string &objID, const int variable, const std::pair< std::string, double > &value)
tcpip::Storage myWrapperStorage
A temporary storage to let the wrapper write to.
TraCIServer & operator=(const TraCIServer &s)=delete
Invalidated assignment operator.
std::map< int, SocketInfo * > mySockets
The socket connections to the clients the first component (index) determines the client's order (lowe...
void addSubscriptionFilterLateralDistance(double dist)
Filter only vehicles within the given lateral distance.
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
static bool wasClosed()
check whether close was requested
void addSubscriptionFilterTurn(double dist)
bool wrapIntPair(const std::string &objID, const int variable, const std::pair< int, int > &value)
bool wrapReservationVector(const std::string &objID, const int variable, const std::vector< libsumo::TraCIReservation > &value)
bool centralObject(const libsumo::Subscription &s, const std::string &objID)
check whether a found objID refers to the central object of a context subscription
bool wrapPositionVector(const std::string &objID, const int variable, const libsumo::TraCIPositionVector &value)
void addSubscriptionFilterVType(std::set< std::string > vTypes)
bool wrapInt(const std::string &objID, const int variable, const int value)
std::map< int, SocketInfo * >::iterator removeCurrentSocket()
removes myCurrentSocket from mySockets and returns an iterator pointing to the next member according ...
tcpip::Storage myOutputStorage
The storage to write to.
bool addObjectVariableSubscription(const int commandId, const bool hasContext)
void addSubscriptionFilterUpstreamDistance(double dist)
SUMOTime getTargetTime() const
Definition TraCIServer.h:64
void stateLoaded(SUMOTime targetTime)
updates myTargetTime and resets vehicle state changes after loading a simulation state
void addSubscriptionFilterLeadFollow()
void addSubscriptionFilterNoOpposite()
tcpip::Storage & getWrapperStorage()
SUMOTime nextTargetTime() const
get the minimal next target time among all clients
std::map< MSNet::TransportableState, std::vector< std::string > > myTransportableStateChanges
Changes in the states of simulated transportables.
void removeSubscription(int commandId, const std::string &identity, int domain)
bool wrapStage(const std::string &objID, const int variable, const libsumo::TraCIStage &value)
bool wrapDouble(const std::string &objID, const int variable, const double value)
tcpip::Storage mySubscriptionCache
The last timestep's subscription results.
bool wrapColor(const std::string &objID, const int variable, const libsumo::TraCIColor &value)
bool wrapString(const std::string &objID, const int variable, const std::string &value)
int dispatchCommand()
Handles command, writes response to myOutputStorage.
bool wrapConnectionVector(const std::string &objID, const int variable, const std::vector< libsumo::TraCIConnection > &value)
void initWrapper(const int domainID, const int variable, const std::string &objID)
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
static TraCIServer * getInstance()
Definition TraCIServer.h:68
bool wrapDoubleList(const std::string &objID, const int variable, const std::vector< double > &value)
void checkClientOrdering()
Called once after connection of all clients for executing SET_ORDER (and possibly prior GET_VERSION) ...
void processReorderingRequests()
checks for and processes reordering requests (relevant for multiple clients)
void sendOutputToAll() const
send out subscription results (actually just the content of myOutputStorage) to clients which will ac...
std::vector< std::string > & getLoadArgs()
bool wrapSignalConstraintVector(const std::string &objID, const int variable, const std::vector< libsumo::TraCISignalConstraint > &value)
void cleanup()
clean up subscriptions
static void close()
request termination of connection
void postProcessSimulationStep()
Handles subscriptions to send after a simstep2 command.
bool commandGetVersion()
Returns the TraCI-version.
bool wrapPosition(const std::string &objID, const int variable, const libsumo::TraCIPosition &value)
static void openSocket(const std::map< int, CmdExecutor > &execs)
Initialises the server.
std::vector< std::string > myLoadArgs
void removeFilters()
void addSubscriptionFilterLanes(std::vector< int > lanes)
bool processGet(const int commandID, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command.
int processCommands(const SUMOTime step, const bool afterMove=false)
process all commands until the next SUMO simulation step. It is guaranteed that t->getTargetTime() >=...
SUMOTime myTargetTime
The time step to reach until processing the next commands.
virtual ~TraCIServer()
Destructor.
bool wrapNextStopDataVector(const std::string &objID, const int variable, const std::vector< libsumo::TraCINextStopData > &value)
bool wrapStringPair(const std::string &objID, const int variable, const std::pair< std::string, std::string > &value)
std::map< MSNet::VehicleState, std::vector< std::string > > myVehicleStateChanges
Changes in the states of simulated vehicles.
int readCommandID(int &commandStart, int &commandLength)
Reads the next command ID from the input storage.
void sendSingleSimStepResponse()
sends an empty response to a simstep command to the current client. (This applies to a situation wher...
std::map< int, CmdExecutor > myExecutors
Map of commandIds -> their executors; applicable if the executor applies to the method footprint.
std::vector< libsumo::Subscription > mySubscriptions
The list of known, still valid subscriptions.
void vehicleStateChanged(const SUMOVehicle *const vehicle, MSNet::VehicleState to, const std::string &info="")
Called if a vehicle changes its state.
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
bool wrapStringDoublePairList(const std::string &objID, const int variable, const std::vector< std::pair< std::string, double > > &value)
const std::map< MSNet::VehicleState, std::vector< std::string > > & getVehicleStateChanges() const
tcpip::Storage myInputStorage
The storage to read from.
bool wrapLogicVector(const std::string &objID, const int variable, const std::vector< libsumo::TraCILogic > &value)
bool wrapNextTLSDataVector(const std::string &objID, const int variable, const std::vector< libsumo::TraCINextTLSData > &value)
libsumo::Subscription * myLastContextSubscription
The last modified context subscription (the one to add a filter to, see @addSubscriptionFilter(),...
std::map< int, SocketInfo * >::iterator myCurrentSocket
The currently active client socket.
bool wrapVehicleDataVector(const std::string &objID, const int variable, const std::vector< libsumo::TraCIVehicleData > &value)
void addSubscriptionFilterVClass(SVCPermissions vClasses)
static TraCIServer * myInstance
Singleton instance of the server.
std::map< int, SocketInfo * > mySocketReorderRequests
This stores the setOrder(int) requests of the clients.
bool addSubscriptionFilter()
std::set< std::pair< int, int > > myParameterized
Set of variables which have parameters.
void transportableStateChanged(const MSTransportable *const transportable, MSNet::TransportableState to, const std::string &info="")
Called if a transportable changes its state.
bool processSingleSubscription(const libsumo::Subscription &s, tcpip::Storage &writeInto, std::string &errors)
void initialiseSubscription(libsumo::Subscription &s)
void addSubscriptionFilterFieldOfVision(double openingAngle)
Filter only vehicles within field of vision.
bool wrapStringList(const std::string &objID, const int variable, const std::vector< std::string > &value)
bool wrapBestLanesDataVector(const std::string &objID, const int variable, const std::vector< libsumo::TraCIBestLanesData > &value)
bool wrapLinkVectorVector(const std::string &objID, const int variable, const std::vector< std::vector< libsumo::TraCILink > > &value)
bool wrapJunctionFoeVector(const std::string &objID, const int variable, const std::vector< libsumo::TraCIJunctionFoe > &value)
const std::map< MSNet::TransportableState, std::vector< std::string > > & getTransportableStateChanges() const
Representation of a subscription.
SocketInfo(const SocketInfo &)
std::map< MSNet::VehicleState, std::vector< std::string > > vehicleStateChanges
container for vehicle state changes since last step taken by this client
bool executeMove
whether a "half step" has been done, executing only the move
SocketInfo(tcpip::Socket *socket, SUMOTime t)
constructor
tcpip::Socket * socket
Socket object for this client.
SUMOTime targetTime
next point of action for the client
std::map< MSNet::TransportableState, std::vector< std::string > > transportableStateChanges
container for transportable state changes since last step taken by this client
A 2D or 3D-position, for 2D positions z == INVALID_DOUBLE_VALUE.
Definition TraCIDefs.h:179
A list of positions.
Definition TraCIDefs.h:240