92 #ifdef CHECK_MEMORY_LEAKS 94 #endif // CHECK_MEMORY_LEAKS 108 : mySocket(0), myTargetTime(begin), myDoingSimStep(false), myAmEmbedded(port == 0), myLaneTree(0) {
154 WRITE_WARNING(
"Starting TraCI without using internal lanes!");
178 for (std::map<int, NamedRTree*>::const_iterator i =
myObjects.begin(); i !=
myObjects.end(); ++i) {
194 for (std::map<int, CmdExecutor>::const_iterator i = execs.begin(); i != execs.end(); ++i) {
234 (*i).second->getInfluencer().postProcessVTD((*i).second);
236 WRITE_WARNING(
"Vehicle '" + (*i).first +
"' was removed though being controlled by VTD");
301 }
catch (std::invalid_argument& e) {
318 traciemb_execute(PyObject* , PyObject* args) {
321 if (!PyArg_ParseTuple(args,
"s#", &msg, &size)) {
324 std::string result = TraCIServer::execute(std::string(msg, size));
325 return Py_BuildValue(
"s#", result.c_str(), result.size());
328 static PyMethodDef EmbMethods[] = {
330 "execute", traciemb_execute, METH_VARARGS,
331 "Execute the given TraCI command and return the result." 333 {NULL, NULL, 0, NULL}
338 TraCIServer::execute(std::string cmd) {
349 for (std::string::iterator i = cmd.begin(); i != cmd.end(); ++i) {
354 }
catch (std::invalid_argument& e) {
365 TraCIServer::runEmbedded(std::string pyFile) {
366 PyObject* pName, *pModule;
368 Py_InitModule(
"traciemb", EmbMethods);
369 if (pyFile.length() > 3 && !pyFile.compare(pyFile.length() - 3, 3,
".py")) {
370 PyObject* sys_path, *path;
371 char pathstr[] =
"path";
372 sys_path = PySys_GetObject(pathstr);
373 if (sys_path == NULL || !PyList_Check(sys_path)) {
374 throw ProcessError(
"Could not access python sys.path!");
377 PyList_Insert(sys_path, 0, path);
379 FILE* pFile = fopen(pyFile.c_str(),
"r");
381 throw ProcessError(
"Failed to load \"" + pyFile +
"\"!");
383 PyRun_SimpleFile(pFile, pyFile.c_str());
386 pName = PyString_FromString(pyFile.c_str());
388 pModule = PyImport_Import(pName);
390 if (pModule == NULL) {
392 throw ProcessError(
"Failed to load \"" + pyFile +
"\"!");
404 if (commandLength == 0) {
409 bool success =
false;
483 std::ostringstream msg;
484 msg <<
"Wrong position in requestMessage after dispatching command.";
485 msg <<
" Expected command length was " << commandLength;
501 answerTmp.
writeString(std::string(
"SUMO ") + sumoVersion);
523 if ((s.
endTime < t) || isArrivedVehicle) {
562 WRITE_ERROR(
"Answered with error to command " +
toHex(commandId, 2) +
": " + description);
564 WRITE_ERROR(
"Requested command not implemented (" +
toHex(commandId, 2) +
"): " + description);
566 outputStorage.
writeUnsignedByte(1 + 1 + 1 + 4 + static_cast<int>(description.length()));
588 bool needNewSubscription =
true;
590 if (s.
commandId == i->commandId && s.
id == i->id &&
593 std::vector<std::vector<unsigned char> >::const_iterator k = s.
parameters.begin();
594 for (std::vector<int>::const_iterator j = s.
variables.begin(); j != s.
variables.end(); ++j, ++k) {
595 const int offset = (int)(std::find(i->variables.begin(), i->variables.end(), *j) - i->variables.begin());
596 if (offset == (
int)i->variables.size() || i->parameters[offset] != *k) {
597 i->variables.push_back(*j);
598 i->parameters.push_back(*k);
601 needNewSubscription =
false;
605 if (needNewSubscription) {
621 if ((*j).id ==
id && (*j).commandId == commandId && (domain < 0 || (*j).contextDomain == domain)) {
735 const float cmin[2] = {(float) b.
xmin(), (float) b.
ymin()};
736 const float cmax[2] = {(float) b.
xmax(), (float) b.
ymax()};
743 myObjects[domain]->Search(cmin, cmax, sv);
761 std::string& errors) {
765 std::set<std::string> objIDs;
776 for (std::set<std::string>::iterator j = objIDs.begin(); j != objIDs.end(); ++j) {
781 std::vector<std::vector<unsigned char> >::const_iterator k = s.
parameters.begin();
782 for (std::vector<int>::const_iterator i = s.
variables.begin(); i != s.
variables.end(); ++i, ++k) {
789 ok &=
myExecutors[getCommandId](*
this, message, tmpOutput);
797 while (--length > 0) {
800 int lengthLength = 1;
812 length -= (lengthLength + 1 + 4 + (int)
id.length());
813 while (--length > 0) {
828 errors = errors + msg;
833 int length = (1 + 4) + 1 + (4 + (
int)(s.
id.length())) + 1 + (int)outputStorage.
size();
846 writeInto.
writeInt((
int)objIDs.size());
863 std::vector<int> variables;
864 std::vector<std::vector<unsigned char> > parameters;
865 for (
int i = 0; i < num; ++i) {
867 variables.push_back(varID);
868 parameters.push_back(std::vector<unsigned char>());
874 if (variables.size() == 0) {
879 Subscription s(commandId,
id, variables, parameters, beginTime, endTime, hasContext, domain, range);
887 if (tempMsg.
size() < 254) {
942 unsigned char r =
static_cast<unsigned char>(inputStorage.
readUnsignedByte());
943 unsigned char g =
static_cast<unsigned char>(inputStorage.
readUnsignedByte());
944 unsigned char b =
static_cast<unsigned char>(inputStorage.
readUnsignedByte());
945 unsigned char a =
static_cast<unsigned char>(inputStorage.
readUnsignedByte());
946 into.
set(r, g, b, a);
972 into.
set(xmin, ymin, xmax, ymax);
1005 for (
int i = 0; i < noEntries; ++i) {
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa3: Get Lane Variable)
bool processSingleSubscription(const TraCIServer::Subscription &s, tcpip::Storage &writeInto, std::string &errors)
The vehicle has departed (was inserted into the network)
bool myDoingSimStep
Whether a step is currently done.
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
tcpip::Socket * mySocket
The socket on which server is listening on.
#define CMD_SUBSCRIBE_VEHICLE_CONTEXT
#define CMD_SUBSCRIBE_LANE_VARIABLE
bool findObjectShape(int domain, const std::string &id, PositionVector &shape)
Representation of a vehicle in the micro simulation.
bool contextVars
Whether the subscription is a context subscription (variable subscription otherwise) ...
#define CMD_GET_TL_VARIABLE
void collectObjectsInRange(int domain, const PositionVector &shape, SUMOReal range, std::set< std::string > &into)
#define CMD_SUBSCRIBE_JUNCTION_CONTEXT
StorageType::const_iterator end() const
void removeVehicleStateListener(VehicleStateListener *listener)
Removes a vehicle states listener.
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc3: Change Lane State)
#define CMD_GET_VEHICLE_VARIABLE
virtual ~TraCIServer()
Destructor.
#define CMD_SUBSCRIBE_SIM_CONTEXT
bool commandGetVersion()
Returns the TraCI-version.
#define CMD_SUBSCRIBE_VEHICLETYPE_CONTEXT
virtual std::vector< std::string > readStringList()
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc4: Change Vehicle State)
std::vector< std::vector< unsigned char > > parameters
The parameters for the subscribed variables.
void setVTDControlled(MSVehicle *v, Position xyPos, MSLane *l, SUMOReal pos, SUMOReal posLat, SUMOReal angle, int edgeOffset, ConstMSEdgeVector route, SUMOTime t)
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc7: Change PoI State)
bool receiveExact(Storage &)
Receive a complete TraCI message from Socket::socket_.
#define CMD_GET_INDUCTIONLOOP_VARIABLE
bool readTypeCheckingColor(tcpip::Storage &inputStorage, RGBColor &into)
Reads the value type and a color, verifying the type.
static NamedRTree * getTree()
Returns a tree filled with junction instances.
static NamedRTree * getTree()
Returns a tree filled with polygon instances.
static void fill(RTREE &into)
Fills the given RTree with lane instances.
#define CMD_SUBSCRIBE_INDUCTIONLOOP_VARIABLE
SUMOTime beginTime
The begin time of the subscription.
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa9: Get Junction Variable)
#define CMD_GET_PERSON_VARIABLE
virtual double readDouble()
tcpip::Storage myOutputStorage
The storage to writeto.
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
void accept()
Wait for a incoming connection to port_.
SUMOReal ymin() const
Returns minimum y-coordinate.
static bool getPosition(const std::string &id, Position &p)
Returns the named vehicle's position.
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xab: Get Simulation Variable)
bool readTypeCheckingInt(tcpip::Storage &inputStorage, int &into)
Reads the value type and an int, verifying the type.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc6: Change Route State)
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
#define CMD_SUBSCRIBE_POLYGON_CONTEXT
tcpip::Storage myInputStorage
The storage to read from.
#define CMD_GET_POLYGON_VARIABLE
virtual void writePacket(unsigned char *packet, int length)
#define CMD_SUBSCRIBE_JUNCTION_VARIABLE
Representation of a subscription.
std::vector< const MSEdge * > ConstMSEdgeVector
#define CMD_SUBSCRIBE_ROUTE_CONTEXT
SUMOReal ymax() const
Returns maximum y-coordinate.
bool readTypeCheckingPolygon(tcpip::Storage &inputStorage, PositionVector &into)
Reads the value type and a polygon, verifying the type.
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xca: Change Edge State)
virtual void writeUnsignedByte(int)
#define CMD_SET_EDGE_VARIABLE
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
#define CMD_SUBSCRIBE_EDGE_VARIABLE
virtual unsigned char readChar()
void addVehicleStateListener(VehicleStateListener *listener)
Adds a vehicle states listener.
#define CMD_GET_ROUTE_VARIABLE
A class that stores a 2D geometrical boundary.
SUMOReal xmin() const
Returns minimum x-coordinate.
#define CMD_SUBSCRIBE_INDUCTIONLOOP_CONTEXT
virtual void writeInt(int)
#define WRITE_WARNING(msg)
static OptionsCont & getOptions()
Retrieves the options.
virtual int readUnsignedByte()
virtual void writeChar(unsigned char)
#define CMD_SUBSCRIBE_POI_VARIABLE
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa4: Get Vehicle Variable)
static bool getPosition(const std::string &id, Position &p)
Returns the named inductive loop's position.
static bool getPosition(const std::string &id, Position &p)
Returns the named persons's position.
static bool myDoCloseConnection
Whether the connection was set to be to close.
The vehicles starts to stop.
std::map< int, CmdExecutor > myExecutors
Map of commandIds -> their executors; applicable if the executor applies to the method footprint...
#define CMD_SET_TL_VARIABLE
void set(SUMOReal xmin, SUMOReal ymin, SUMOReal xmax, SUMOReal ymax)
Sets the boundary to the given values.
void vehicleStateChanged(const SUMOVehicle *const vehicle, MSNet::VehicleState to)
Called if a vehicle changes its state.
bool readTypeCheckingBoundary(tcpip::Storage &inputStorage, Boundary &into)
Reads the value type and a 2D bounding box, verifying the type.
static NamedRTree * getTree()
Returns a tree filled with PoI instances.
#define CMD_SUBSCRIBE_LANE_CONTEXT
#define CMD_SUBSCRIBE_SIM_VARIABLE
void removeSubscription(int commandId, const std::string &identity, int domain)
#define CMD_GET_VEHICLETYPE_VARIABLE
static void close()
request termination of connection
bool addObjectVariableSubscription(const int commandId, const bool hasContext)
#define CMD_SET_ROUTE_VARIABLE
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xcb: Set Simulation Variable)
#define CMD_GET_AREAL_DETECTOR_VARIABLE
#define CMD_SUBSCRIBE_GUI_CONTEXT
Allows to store the object; used as context while traveling the rtree in TraCI.
The vehicle got a new route.
The vehicle arrived at his destination (is deleted)
The vehicles starts to park.
int commandId
commandIdArg The command id of the subscription
std::vector< Subscription > mySubscriptions
The list of known, still valid subscriptions.
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xce: Change Person State)
Representation of a vehicle.
SUMOReal range
The range of the context.
A point in 2D or 3D with translation and scaling methods.
std::string id
The id of the object that is subscribed.
#define CMD_GET_POI_VARIABLE
void set(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
assigns new values
std::vector< int > variables
The subscribed variables.
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
#define CMD_SET_VEHICLETYPE_VARIABLE
#define CMD_SUBSCRIBE_MULTI_ENTRY_EXIT_DETECTOR_VARIABLE
virtual const char * what() const
bool readTypeCheckingStringList(tcpip::Storage &inputStorage, std::vector< std::string > &into)
Reads the value type and a string list, verifying the type.
#define CMD_SUBSCRIBE_GUI_VARIABLE
#define CMD_GET_LANE_VARIABLE
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa5: Get Vehicle Type Variable)
#define CMD_SET_VEHICLE_VARIABLE
SUMOTime string2time(const std::string &r)
void postProcessSimulationStep2()
Handles subscriptions to send after a simstep2 command.
The vehicle started to teleport.
#define CMD_GET_SIM_VARIABLE
virtual std::string readString()
#define CMD_GET_EDGE_VARIABLE
virtual unsigned int position() const
static bool gUsingInternalLanes
Information whether the simulation regards internal lanes.
#define CMD_SET_POI_VARIABLE
#define CMD_SUBSCRIBE_POLYGON_VARIABLE
static TraCIServer * myInstance
Singleton instance of the server.
static bool getPosition(const std::string &id, Position &p)
Returns the named PoI's position.
TraCIServer(const SUMOTime begin, const int port=0)
Constructor.
#define CMD_SUBSCRIBE_TL_CONTEXT
The vehicle ends to park.
#define CMD_SUBSCRIBE_EDGE_CONTEXT
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa0: Get Induction Loop Variable)
#define CMD_GET_JUNCTION_VARIABLE
#define CMD_SUBSCRIBE_PERSON_VARIABLE
bool readTypeCheckingUnsignedByte(tcpip::Storage &inputStorage, int &into)
Reads the value type and an unsigned byte, verifying the type.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
TraCI server used to control sumo by a remote TraCI client.
virtual void writeStorage(tcpip::Storage &store)
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xae: Get Person Variable)
#define CMD_SET_SIM_VARIABLE
void setVTDControlled(Position xyPos, MSLane *l, SUMOReal pos, SUMOReal posLat, SUMOReal angle, int edgeOffset, const ConstMSEdgeVector &route, SUMOTime t)
#define CMD_SUBSCRIBE_AREAL_DETECTOR_VARIABLE
StorageType::size_type size() const
#define CMD_SUBSCRIBE_AREAL_DETECTOR_CONTEXT
#define CMD_SUBSCRIBE_VEHICLETYPE_VARIABLE
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
VehicleState
Definition of a vehicle state.
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa7: Get PoI Variable)
const bool myAmEmbedded
Whether the server runs in embedded mode.
bool readTypeCheckingPosition2D(tcpip::Storage &inputStorage, Position &into)
Reads the value type and a 2D position, verifying the type.
The vehicle was built, but has not yet departed.
static void processCommandsUntilSimStep(SUMOTime step)
process all commands until a simulation step is wanted
LANE_RTREE_QUAL * myLaneTree
A storage of lanes.
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc5: Change Vehicle Type State)
Allows to store the object; used as context while traveling the rtree in TraCI.
#define CMD_SET_POLYGON_VARIABLE
static void openSocket(const std::map< int, CmdExecutor > &execs)
Initialises the server.
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.
#define CMD_GET_MULTI_ENTRY_EXIT_DETECTOR_VARIABLE
SUMOReal xmax() const
Returns maximum x-coordinate.
std::string toHex(const T i, std::streamsize numDigits=0)
static bool getPosition(const std::string &id, Position &p)
Returns the named junction's position.
#define CMD_SUBSCRIBE_VEHICLE_VARIABLE
#define CMD_SUBSCRIBE_PERSON_CONTEXT
void sendExact(const Storage &)
void visit(const TraCIServerAPI_Lane::StoringVisitor &cont) const
Callback for visiting the lane when traversing an RTree.
StorageType::const_iterator begin() const
#define CMD_SET_LANE_VARIABLE
void inform(std::string msg, bool addType=true)
adds a new error to the list
void set(SUMOReal x, SUMOReal y)
SUMOTime myTargetTime
The time step to reach until processing the next commands.
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa6: Get Route Variable)
static NamedRTree * getTree()
Returns a tree filled with inductive loop instances.
The vehicle ends to stop.
std::map< int, NamedRTree * > myObjects
A storage of objects.
std::map< std::string, MSVehicle * > myVTDControlledVehicles
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa8: Get Polygon Variable)
#define CMD_SUBSCRIBE_MULTI_ENTRY_EXIT_DETECTOR_CONTEXT
#define CMD_SUBSCRIBE_TL_VARIABLE
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc2: Change Traffic Lights State)
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
Boundary getBoxBoundary() const
Returns a boundary enclosing this list of lines.
static std::string getFilePath(const std::string &path)
Removes the file information from the given path.
#define CMD_SET_PERSON_VARIABLE
void simulationStep()
Performs a single simulation step.
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa1: Get MeMeDetector Variable)
int contextDomain
The domain ID of the context.
#define WRITE_MESSAGE(msg)
void initialiseSubscription(const Subscription &s)
const std::string & getID() const
Returns the name of the vehicle.
#define CMD_SUBSCRIBE_POI_CONTEXT
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xaa: Get Edge Variable)
Representation of a lane in the micro simulation.
static bool wasClosed()
check whether close was requested
std::map< MSNet::VehicleState, std::vector< std::string > > myVehicleStateChanges
Changes in the states of simulated vehicles.
The vehicle ended being teleported.
virtual const std::string & getID() const =0
Get the vehicle's ID.
static bool getShape(const std::string &id, PositionVector &shape)
Returns the named polygons's shape.
bool readTypeCheckingByte(tcpip::Storage &inputStorage, int &into)
Reads the value type and a byte, verifying the type.
SUMOTime endTime
The end time of the subscription.
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa2: Get Traffic Lights Variable)
#define CMD_SUBSCRIBE_ROUTE_VARIABLE
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc8: Change Polygon State)
static bool getShape(const std::string &id, PositionVector &shape)
Returns the named lane's shape.
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa1: Get AreaDetector Variable)
std::map< int, int > myParameterSizes
Map of variable ids to the size of the parameter in bytes.