92 #ifdef CHECK_MEMORY_LEAKS
94 #endif // CHECK_MEMORY_LEAKS
108 : mySocket(0), myTargetTime(begin), myDoingSimStep(false), myAmEmbedded(port == 0), myLaneTree(0) {
150 WRITE_WARNING(
"Starting TraCI without using internal lanes!");
174 for (std::map<int, NamedRTree*>::const_iterator i =
myObjects.begin(); i !=
myObjects.end(); ++i) {
188 for (std::map<int, CmdExecutor>::const_iterator i = execs.begin(); i != execs.end(); ++i) {
222 (*i).second->getInfluencer().postProcessVTD((*i).second);
224 WRITE_WARNING(
"Vehicle '" + (*i).first +
"' was removed though being controlled by VTD");
300 }
catch (std::invalid_argument& e) {
322 traciemb_execute(PyObject* , PyObject* args) {
325 if (!PyArg_ParseTuple(args,
"s#", &msg, &size)) {
328 std::string result = TraCIServer::execute(std::string(msg, size));
329 return Py_BuildValue(
"s#", result.c_str(), result.size());
332 static PyMethodDef EmbMethods[] = {
334 "execute", traciemb_execute, METH_VARARGS,
335 "Execute the given TraCI command and return the result."
337 {NULL, NULL, 0, NULL}
342 TraCIServer::execute(std::string cmd) {
353 for (std::string::iterator i = cmd.begin(); i != cmd.end(); ++i) {
358 }
catch (std::invalid_argument& e) {
369 TraCIServer::runEmbedded(std::string pyFile) {
370 PyObject* pName, *pModule;
372 Py_InitModule(
"traciemb", EmbMethods);
373 if (pyFile.length() > 3 && !pyFile.compare(pyFile.length() - 3, 3,
".py")) {
374 PyObject* sys_path, *path;
375 char pathstr[] =
"path";
376 sys_path = PySys_GetObject(pathstr);
377 if (sys_path == NULL || !PyList_Check(sys_path)) {
378 throw ProcessError(
"Could not access python sys.path!");
381 PyList_Insert(sys_path, 0, path);
383 FILE* pFile = fopen(pyFile.c_str(),
"r");
385 throw ProcessError(
"Failed to load \"" + pyFile +
"\"!");
387 PyRun_SimpleFile(pFile, pyFile.c_str());
390 pName = PyString_FromString(pyFile.c_str());
392 pModule = PyImport_Import(pName);
394 if (pModule == NULL) {
396 throw ProcessError(
"Failed to load \"" + pyFile +
"\"!");
408 if (commandLength == 0) {
413 bool success =
false;
482 std::ostringstream msg;
483 msg <<
"Wrong position in requestMessage after dispatching command.";
484 msg <<
" Expected command length was " << commandLength;
500 answerTmp.
writeString(std::string(
"SUMO ") + sumoVersion);
531 if ((s.
endTime < t) || isArrivedVehicle) {
572 WRITE_ERROR(
"Requested command not implemented (" +
toString(commandId) +
"): " + description);
574 outputStorage.
writeUnsignedByte(1 + 1 + 1 + 4 + static_cast<int>(description.length()));
610 if ((*j).id ==
id && (*j).commandId == commandId && (domain < 0 || (*j).contextDomain == domain)) {
726 myObjects[domain]->Search(cmin, cmax, sv);
744 std::string& errors) {
748 std::set<std::string> objIDs;
759 for (std::set<std::string>::iterator j = objIDs.begin(); j != objIDs.end(); ++j) {
764 for (std::vector<int>::const_iterator i = s.
variables.begin(); i != s.
variables.end(); ++i) {
770 ok &=
myExecutors[getCommandId](*
this, message, tmpOutput);
778 while (--length > 0) {
781 int lengthLength = 1;
793 length -= (lengthLength + 1 + 4 + (
int)
id.length());
794 while (--length > 0) {
809 errors = errors + msg;
814 unsigned int length = (1 + 4) + 1 + (4 + (
int)(s.
id.length())) + 1 + (
int)outputStorage.
size();
827 writeInto.
writeInt((
int)objIDs.size());
842 std::vector<int> variables;
843 for (
int i = 0; i < no; ++i) {
847 if (variables.size() == 0) {
852 Subscription s(commandId,
id, variables, beginTime, endTime,
false, 0, 0);
866 std::vector<int> variables;
867 for (
int i = 0; i < no; ++i) {
871 if (variables.size() == 0) {
875 Subscription s(commandId,
id, variables, beginTime, endTime,
true, domain, range);
883 if (tempMsg.
size() < 254) {
938 unsigned char r =
static_cast<unsigned char>(inputStorage.
readUnsignedByte());
939 unsigned char g =
static_cast<unsigned char>(inputStorage.
readUnsignedByte());
940 unsigned char b =
static_cast<unsigned char>(inputStorage.
readUnsignedByte());
941 unsigned char a =
static_cast<unsigned char>(inputStorage.
readUnsignedByte());
942 into.
set(r, g, b, a);
968 into.
set(xmin, ymin, xmax, ymax);
1001 for (
unsigned int i = 0; i < noEntries; ++i) {