91 #ifdef CHECK_MEMORY_LEAKS
93 #endif // CHECK_MEMORY_LEAKS
107 : mySocket(0), myTargetTime(begin), myDoingSimStep(false), myAmEmbedded(port == 0), myLaneTree(0) {
151 WRITE_WARNING(
"Starting TraCI without using internal lanes!");
175 for (std::map<int, NamedRTree*>::const_iterator i =
myObjects.begin(); i !=
myObjects.end(); ++i) {
189 for (std::map<int, CmdExecutor>::const_iterator i = execs.begin(); i != execs.end(); ++i) {
223 (*i).second->getInfluencer().postProcessVTD((*i).second);
225 WRITE_WARNING(
"Vehicle '" + (*i).first +
"' was removed though being controlled by VTD");
296 }
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;
478 std::ostringstream msg;
479 msg <<
"Wrong position in requestMessage after dispatching command.";
480 msg <<
" Expected command length was " << commandLength;
496 answerTmp.
writeString(std::string(
"SUMO ") + sumoVersion);
527 if ((s.
endTime < t) || isArrivedVehicle) {
566 WRITE_ERROR(
"Answered with error to command " +
toHex(commandId, 2) +
": " + description);
568 WRITE_ERROR(
"Requested command not implemented (" +
toHex(commandId, 2) +
"): " + description);
570 outputStorage.
writeUnsignedByte(1 + 1 + 1 + 4 + static_cast<int>(description.length()));
606 if ((*j).id ==
id && (*j).commandId == commandId && (domain < 0 || (*j).contextDomain == domain)) {
722 myObjects[domain]->Search(cmin, cmax, sv);
740 std::string& errors) {
744 std::set<std::string> objIDs;
755 for (std::set<std::string>::iterator j = objIDs.begin(); j != objIDs.end(); ++j) {
760 std::vector<std::vector<unsigned char> >::const_iterator k = s.
parameters.begin();
761 for (std::vector<int>::const_iterator i = s.
variables.begin(); i != s.
variables.end(); ++i, ++k) {
768 ok &=
myExecutors[getCommandId](*
this, message, tmpOutput);
776 while (--length > 0) {
779 int lengthLength = 1;
791 length -= (lengthLength + 1 + 4 + (
int)
id.length());
792 while (--length > 0) {
807 errors = errors + msg;
812 unsigned int length = (1 + 4) + 1 + (4 + (
int)(s.
id.length())) + 1 + (
int)outputStorage.
size();
825 writeInto.
writeInt((
int)objIDs.size());
842 std::vector<int> variables;
843 std::vector<std::vector<unsigned char> > parameters;
844 for (
int i = 0; i < num; ++i) {
846 variables.push_back(varID);
847 parameters.push_back(std::vector<unsigned char>());
853 if (variables.size() == 0) {
858 Subscription s(commandId,
id, variables, parameters, beginTime, endTime, hasContext, domain, range);
866 if (tempMsg.
size() < 254) {
921 unsigned char r =
static_cast<unsigned char>(inputStorage.
readUnsignedByte());
922 unsigned char g =
static_cast<unsigned char>(inputStorage.
readUnsignedByte());
923 unsigned char b =
static_cast<unsigned char>(inputStorage.
readUnsignedByte());
924 unsigned char a =
static_cast<unsigned char>(inputStorage.
readUnsignedByte());
925 into.
set(r, g, b, a);
951 into.
set(xmin, ymin, xmax, ymax);
984 for (
unsigned int i = 0; i < noEntries; ++i) {