90 #ifdef CHECK_MEMORY_LEAKS
92 #endif // CHECK_MEMORY_LEAKS
111 : mySocket(0), myTargetTime(0), myDoingSimStep(false), myHaveWarnedDeprecation(false), myAmEmbedded(port == 0) {
146 WRITE_WARNING(
"Starting TraCI without using internal lanes!");
170 for (std::map<int, TraCIRTree*>::const_iterator i =
myObjects.begin(); i !=
myObjects.end(); ++i) {
182 for (std::map<int, CmdExecutor>::const_iterator i = execs.begin(); i != execs.end(); ++i) {
216 (*i).second->getInfluencer().postProcessVTD((*i).second);
218 WRITE_WARNING(
"Vehicle '" + (*i).first +
"' was removed though being controlled by VTD");
294 }
catch (std::invalid_argument& e) {
316 traciemb_execute(PyObject* , PyObject* args) {
319 if (!PyArg_ParseTuple(args,
"s#", &msg, &size)) {
322 std::string result = traci::TraCIServer::execute(std::string(msg, size));
323 return Py_BuildValue(
"s#", result.c_str(), result.size());
326 static PyMethodDef EmbMethods[] = {
328 "execute", traciemb_execute, METH_VARARGS,
329 "Execute the given TraCI command and return the result."
331 {NULL, NULL, 0, NULL}
336 TraCIServer::execute(std::string cmd) {
347 for (std::string::iterator i = cmd.begin(); i != cmd.end(); ++i) {
352 }
catch (std::invalid_argument& e) {
363 TraCIServer::runEmbedded(std::string pyFile) {
364 PyObject* pName, *pModule;
366 Py_InitModule(
"traciemb", EmbMethods);
367 if (pyFile.length() > 3 && !pyFile.compare(pyFile.length() - 3, 3,
".py")) {
368 FILE* pFile = fopen(pyFile.c_str(),
"r");
369 PyRun_SimpleFile(pFile, pyFile.c_str());
372 pName = PyString_FromString(pyFile.c_str());
374 pModule = PyImport_Import(pName);
376 if (pModule == NULL) {
378 throw ProcessError(
"Failed to load \"" + pyFile +
"\"!");
390 if (commandLength == 0) {
395 bool success =
false;
423 WRITE_WARNING(
"Using old TraCI API, please update your client!");
468 std::ostringstream msg;
469 msg <<
"Wrong position in requestMessage after dispatching command.";
470 msg <<
" Expected command length was " << commandLength;
486 answerTmp.
writeString(std::string(
"SUMO ") + sumoVersion);
516 if ((s.
endTime < t) || isArrivedVehicle) {
571 lane = route->
getEdges()[0]->getLanes()[0];
583 vehicleParams->
id = vehicleId;
586 if (vehicle == NULL) {
591 float clippedInsertionSpeed;
592 if (insertionSpeed < 0) {
604 if (!lane->
isInsertionSuccess(vehicle, clippedInsertionSpeed, insertionPosition,
true)) {
630 WRITE_ERROR(
"Requested command not implemented (" +
toString(commandId) +
"): " + description);
632 outputStorage.
writeUnsignedByte(1 + 1 + 1 + 4 + static_cast<int>(description.length()));
668 if ((*j).id ==
id && (*j).commandId == commandId && (domain < 0 || (*j).contextDomain == domain)) {
805 if (shape.size() == 1) {
806 for (std::set<std::string>::iterator i = into.begin(); i != into.end();) {
818 std::set<std::string> tmp;
821 for (std::set<std::string>::const_iterator i = tmp.begin(); i != tmp.end(); ++i) {
825 for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
826 if (shape.
distance((*j)->getPosition()) <= range) {
827 into.insert((*j)->getID());
871 std::string& errors) {
875 std::set<std::string> objIDs;
888 for (std::set<std::string>::iterator j = objIDs.begin(); j != objIDs.end(); ++j) {
892 for (std::vector<int>::const_iterator i = s.
variables.begin(); i != s.
variables.end(); ++i) {
898 ok &=
myExecutors[getCommandId](*
this, message, tmpOutput);
906 while (--length > 0) {
909 int lengthLength = 1;
921 length -= (lengthLength + 1 + 4 + (
int)
id.length());
922 while (--length > 0) {
937 errors = errors + msg;
941 unsigned int length = (1 + 4) + 1 + (4 + (
int)(s.
id.length())) + 1 + (
int)outputStorage.
size();
954 writeInto.
writeInt((
int)objIDs.size());
969 std::vector<int> variables;
970 for (
int i = 0; i < no; ++i) {
974 if (variables.size() == 0) {
979 Subscription s(commandId,
id, variables, beginTime, endTime,
false, 0, 0);
993 std::vector<int> variables;
994 for (
int i = 0; i < no; ++i) {
998 if (variables.size() == 0) {
1002 Subscription s(commandId,
id, variables, beginTime, endTime,
true, domain, range);
1010 if (tempMsg.
size() < 254) {
1025 into = inputStorage.
readInt();
1065 unsigned char r =
static_cast<unsigned char>(inputStorage.
readUnsignedByte());
1066 unsigned char g =
static_cast<unsigned char>(inputStorage.
readUnsignedByte());
1067 unsigned char b =
static_cast<unsigned char>(inputStorage.
readUnsignedByte());
1068 unsigned char a =
static_cast<unsigned char>(inputStorage.
readUnsignedByte());
1069 into.
set(r, g, b, a);
1095 into.
set(xmin, ymin, xmax, ymax);
1128 for (
unsigned int i = 0; i < noEntries; ++i) {
The vehicle has departed (was inserted into the network)
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
std::map< MSNet::VehicleState, std::vector< std::string > > myVehicleStateChanges
Changes in the states of simulated vehicles.
static TraCIRTree * getTree()
Returns a tree filled with inductive loop instances.
static bool processGet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa0: Get Induction Loop Variable)
#define CMD_SUBSCRIBE_VEHICLE_CONTEXT
#define CMD_SUBSCRIBE_LANE_VARIABLE
MSEdge & getEdge() const
Returns the lane's edge.
Representation of a vehicle in the micro simulation.
virtual void deleteVehicle(SUMOVehicle *v, bool discard=false)
Deletes the vehicle.
#define CMD_GET_TL_VARIABLE
#define CMD_SUBSCRIBE_JUNCTION_CONTEXT
static bool processGet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa2: Get Traffic Lights Variable)
void removeVehicleStateListener(VehicleStateListener *listener)
Removes a vehicle states listener.
static bool processSet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc6: Change Route State)
static bool processGet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa1: Get MeMeDetector Variable)
#define CMD_GET_VEHICLE_VARIABLE
#define CMD_SUBSCRIBE_SIM_CONTEXT
virtual void releaseVehicles() const
Allows to use the container for microsimulation again.
#define CMD_SUBSCRIBE_VEHICLETYPE_CONTEXT
tcpip::Storage myOutputStorage
The storage to writeto.
virtual std::vector< std::string > readStringList()
bool myDoingSimStep
Whether a step is currently done.
SUMOReal getMaxSpeed() const
Returns the maximum speed.
static void openSocket(const std::map< int, CmdExecutor > &execs)
Initialises the server.
void initialiseSubscription(const Subscription &s)
std::vector< Subscription > mySubscriptions
The list of known, still valid subscriptions.
bool receiveExact(Storage &)
Receive a complete TraCI message from Socket::socket_.
#define CMD_GET_INDUCTIONLOOP_VARIABLE
static bool dictionary(std::string id, MSLane *lane)
Inserts a MSLane into the static dictionary Returns true if the key id isn't already in the dictionar...
SUMOReal ymin() const
Returns minimum y-coordinate.
#define CMD_SUBSCRIBE_INDUCTIONLOOP_VARIABLE
virtual unsigned int position() const
static bool processGet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa4: Get Vehicle Variable)
virtual double readDouble()
MSVehicleType * getVType(const std::string &id=DEFAULT_VTYPE_ID)
Returns the named vehicle type or a sample from the named distribution.
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
bool contextVars
Whether the subscription is a context subscription (variable subscription otherwise) ...
std::map< int, CmdExecutor > myExecutors
Map of commandIds -> their executors; applicable if the executor applies to the method footprint...
void accept()
Wait for a incoming connection to port_.
std::vector< MSVehicle * > VehCont
Container for vehicles.
SUMOReal xmin() const
Returns minimum x-coordinate.
StorageType::size_type size() const
const MSEdgeVector & getEdges() const
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
static bool processSet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc8: Change Polygon State)
static bool getPosition(const std::string &id, Position &p)
Returns the named vehicle's position.
bool readTypeCheckingStringList(tcpip::Storage &inputStorage, std::vector< std::string > &into)
Reads the value type and a string list, verifying the type.
static TraCIRTree * getTree()
Returns a tree filled with edge instances.
static bool processGet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xaa: Get Edge Variable)
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
std::string id
The id of the object that is subscribed.
static bool processSet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc3: Change Lane State)
#define CMD_SUBSCRIBE_POLYGON_CONTEXT
virtual bool addVehicle(const std::string &id, SUMOVehicle *v)
Tries to insert the vehicle into the internal vehicle container.
#define CMD_GET_POLYGON_VARIABLE
#define CMD_SUBSCRIBE_JUNCTION_VARIABLE
#define CMD_SUBSCRIBE_ROUTE_CONTEXT
virtual void writeUnsignedByte(int)
#define CMD_SET_EDGE_VARIABLE
void postProcessSimulationStep2()
Handles subscriptions to send after a simstep2 command.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step (in s)
Representation of a subscription.
#define CMD_SUBSCRIBE_EDGE_VARIABLE
virtual unsigned char readChar()
SUMOReal xmax() const
Returns maximum x-coordinate.
void addVehicleStateListener(VehicleStateListener *listener)
Adds a vehicle states listener.
#define CMD_GET_ROUTE_VARIABLE
A class that stores a 2D geometrical boundary.
#define CMD_SUBSCRIBE_INDUCTIONLOOP_CONTEXT
virtual void writeInt(int)
#define WRITE_WARNING(msg)
The car-following model and parameter.
static OptionsCont & getOptions()
Retrieves the options.
bool processSingleSubscription(const TraCIServer::Subscription &s, tcpip::Storage &writeInto, std::string &errors)
virtual int readUnsignedByte()
virtual const VehCont & getVehiclesSecure() const
Returns the vehicles container; locks it for microsimulation.
virtual void writeChar(unsigned char)
#define CMD_SUBSCRIBE_POI_VARIABLE
static bool getPosition(const std::string &id, Position &p)
Returns the named inductive loop's position.
SUMOReal range
The range of the context.
#define CMD_SET_TL_VARIABLE
virtual SUMOVehicle * buildVehicle(SUMOVehicleParameter *defs, const MSRoute *route, const MSVehicleType *type)
Builds a vehicle, increases the number of built vehicles.
bool readTypeCheckingBoundary(tcpip::Storage &inputStorage, Boundary &into)
Reads the value type and a 2D bounding box, verifying the type.
void set(SUMOReal xmin, SUMOReal ymin, SUMOReal xmax, SUMOReal ymax)
Sets the boundary to the given values.
static bool processGet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa7: Get PoI Variable)
static bool processGet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xaa: Get Edge Variable)
void vehicleStateChanged(const SUMOVehicle *const vehicle, MSNet::VehicleState to)
Called if a vehicle changes its state.
TraCI server used to control sumo by a remote TraCI client.
#define CMD_SUBSCRIBE_LANE_CONTEXT
#define CMD_SUBSCRIBE_SIM_VARIABLE
#define CMD_GET_VEHICLETYPE_VARIABLE
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
tcpip::Storage myInputStorage
The storage to read from.
virtual bool isInsertionSuccess(MSVehicle *vehicle, SUMOReal speed, SUMOReal pos, bool recheckNextLanes, MSMoveReminder::Notification notification=MSMoveReminder::NOTIFICATION_DEPARTED)
Tries to insert the given vehicle with the given state (speed and pos)
#define CMD_SET_ROUTE_VARIABLE
#define CMD_SUBSCRIBE_GUI_CONTEXT
static bool wasClosed()
check whether close was requested
The vehicle got a new route.
const bool myAmEmbedded
Whether the server runs in embedded mode.
The vehicle arrived at his destination (is deleted)
StorageType::const_iterator end() const
Representation of a vehicle.
std::vector< const MSEdge * > MSEdgeVector
static bool processSet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc4: Change Vehicle State)
A point in 2D or 3D with translation and scaling methods.
#define CMD_GET_POI_VARIABLE
void set(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
assigns new values
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
#define CMD_SET_VEHICLETYPE_VARIABLE
static bool processGet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa5: Get Vehicle Type Variable)
#define CMD_SUBSCRIBE_MULTI_ENTRY_EXIT_DETECTOR_VARIABLE
void setVTDControlled(MSVehicle *v, MSLane *l, SUMOReal pos, int edgeOffset, MSEdgeVector route)
void setVTDControlled(bool c, MSLane *l, SUMOReal pos, int edgeOffset, const MSEdgeVector &route)
SUMOReal distance(const Position &p) const
SUMOTime depart
The vehicle's departure time.
#define CMD_SUBSCRIBE_GUI_VARIABLE
#define CMD_GET_LANE_VARIABLE
bool addObjectVariableSubscription(int commandId)
#define CMD_SET_VEHICLE_VARIABLE
SUMOTime beginTime
The begin time of the subscription.
std::map< std::string, MSVehicle * > myVTDControlledVehicles
The vehicle started to teleport.
#define CMD_GET_SIM_VARIABLE
virtual std::string readString()
#define CMD_GET_EDGE_VARIABLE
#define CMD_GET_GUI_VARIABLE
static void close()
request termination of connection
static bool gUsingInternalLanes
Information whether the simulation regards internal lanes.
#define CMD_SET_POI_VARIABLE
#define CMD_SUBSCRIBE_POLYGON_VARIABLE
static bool getPosition(const std::string &id, Position &p)
Returns the named PoI's position.
#define CMD_SUBSCRIBE_TL_CONTEXT
static bool processGet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa9: Get Junction Variable)
#define CMD_SUBSCRIBE_EDGE_CONTEXT
SUMOTime endTime
The end time of the subscription.
#define CMD_GET_JUNCTION_VARIABLE
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
virtual void writeStorage(tcpip::Storage &store)
static TraCIServer * myInstance
Singleton instance of the server.
virtual ~TraCIServer()
Destructor.
static bool processGet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa3: Get Lane Variable)
tcpip::Socket * mySocket
The socket on which server is listening on.
#define CMD_SUBSCRIBE_VEHICLETYPE_VARIABLE
VehicleState
Definition of a vehicle state.
The vehicle was built, but has not yet departed.
SUMOTime myTargetTime
The time step to reach until processing the next commands.
void push_back(const PositionVector &p)
Appends all positions from the given vector.
Allows to store the object; used as context while traveling the rtree in TraCI.
static TraCIRTree * getTree()
Returns a tree filled with inductive loop instances.
#define CMD_SET_POLYGON_VARIABLE
void removeSubscription(int commandId, const std::string &identity, int domain)
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
Boundary & grow(SUMOReal by)
extends the boundary by the given amount
static bool getShape(const std::string &id, PositionVector &shape)
Returns the named edge's shape.
virtual void writeString(const std::string &s)
#define RTYPE_NOTIMPLEMENTED
Influencer & getInfluencer()
Returns the velocity/lane influencer.
Structure representing possible vehicle parameter.
#define CMD_GET_MULTI_ENTRY_EXIT_DETECTOR_VARIABLE
virtual const char * what() const
static bool myDoCloseConnection
Whether the connection was set to be to close.
static bool getPosition(const std::string &id, Position &p)
Returns the named junction's position.
#define CMD_SUBSCRIBE_VEHICLE_VARIABLE
std::map< int, TraCIRTree * > myObjects
A storage of objects.
TraCIServer(int port=0)
Constructor.
static bool processGet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa6: Get Route Variable)
bool commandCloseConnection()
Indicates the connection as being closed.
static void processCommandsUntilSimStep(SUMOTime step)
process all commands until a simulation step is wanted
bool readTypeCheckingByte(tcpip::Storage &inputStorage, int &into)
Reads the value type and a byte, verifying the type.
void sendExact(const Storage &)
virtual float readFloat()
static bool processSet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc5: Change Vehicle Type State)
static TraCIRTree * getTree()
Returns a tree filled with inductive loop instances.
#define CMD_SET_LANE_VARIABLE
void onDepart()
Called when the vehicle is inserted into the network.
void inform(std::string msg, bool addType=true)
adds a new error to the list
void set(SUMOReal x, SUMOReal y)
bool addObjectContextSubscription(int commandId)
int commandId
commandIdArg The command id of the subscription
StorageType::const_iterator begin() const
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
const PositionVector & getShape() const
Returns this lane's shape.
#define CMD_SUBSCRIBE_MULTI_ENTRY_EXIT_DETECTOR_CONTEXT
#define CMD_SUBSCRIBE_TL_VARIABLE
static bool processSet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc2: Change Traffic Lights State)
SUMOReal ymax() const
Returns maximum y-coordinate.
static TraCIRTree * getTree()
Returns a tree filled with inductive loop instances.
int contextDomain
The domain ID of the context.
void simulationStep()
Performs a single simulation step.
bool commandGetVersion()
Returns the TraCI-version.
static bool processSet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc7: Change PoI State)
bool readTypeCheckingUnsignedByte(tcpip::Storage &inputStorage, int &into)
Reads the value type and an unsigned byte, verifying the type.
SUMOReal getVehicleMaxSpeed(const SUMOVehicle *const veh) const
Returns the lane's maximum speed, given a vehicle's speed limit adaptation.
void collectObjectsInRange(int domain, const PositionVector &shape, SUMOReal range, std::set< std::string > &into)
#define WRITE_MESSAGE(msg)
bool readTypeCheckingColor(tcpip::Storage &inputStorage, RGBColor &into)
Reads the value type and a color, verifying the type.
bool commandAddVehicle()
Adds a vehicle.
#define CMD_SUBSCRIBE_POI_CONTEXT
Boundary getBoxBoundary() const
Returns a boundary enclosing this list of lines.
Representation of a lane in the micro simulation.
bool readTypeCheckingInt(tcpip::Storage &inputStorage, int &into)
Reads the value type and an int, verifying the type.
The vehicle ended being teleported.
bool findObjectShape(int domain, const std::string &id, PositionVector &shape)
bool readTypeCheckingPolygon(tcpip::Storage &inputStorage, PositionVector &into)
Reads the value type and a polygon, verifying the type.
static TraCIRTree * getTree()
Returns a tree filled with inductive loop instances.
virtual const std::string & getID() const =0
Get the vehicle's ID.
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
bool readTypeCheckingPosition2D(tcpip::Storage &inputStorage, Position &into)
Reads the value type and a 2D position, verifying the type.
bool myHaveWarnedDeprecation
Whether the usage of deprecated methods was already reported.
static bool getShape(const std::string &id, PositionVector &shape)
Returns the named polygons's shape.
static bool processGet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa8: Get Polygon Variable)
MSRouteIterator begin() const
Returns the begin of the list of edges to pass.
static bool processSet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xca: Change Edge State)
std::vector< int > variables
The subscribed variables.
#define CMD_SUBSCRIBE_ROUTE_VARIABLE
std::string id
The vehicle's id.
static bool getShape(const std::string &id, PositionVector &shape)
Returns the named lane's shape.
const std::string & getID() const
Returns the name of the vehicle.
static bool dictionary(const std::string &id, const MSRoute *route)
Adds a route to the dictionary.