SUMO - Simulation of Urban MObility
TraCITestClient.cpp
Go to the documentation of this file.
1 /****************************************************************************/
13 /****************************************************************************/
14 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
15 // Copyright (C) 2008-2016 DLR (http://www.dlr.de/) and contributors
16 /****************************************************************************/
17 //
18 // This file is part of SUMO.
19 // SUMO is free software: you can redistribute it and/or modify
20 // it under the terms of the GNU General Public License as published by
21 // the Free Software Foundation, either version 3 of the License, or
22 // (at your option) any later version.
23 //
24 /****************************************************************************/
25 /* =========================================================================
26  * included modules
27  * ======================================================================= */
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <vector>
35 #include <iostream>
36 #include <iomanip>
37 #include <fstream>
38 #include <sstream>
39 #include <ctime>
40 #include <cstdlib>
41 
42 #define BUILD_TCPIP
43 #include <foreign/tcpip/storage.h>
44 #include <foreign/tcpip/socket.h>
45 
47 #include <utils/common/SUMOTime.h>
48 #include <utils/common/ToString.h>
49 #include "TraCITestClient.h"
50 
51 #ifdef CHECK_MEMORY_LEAKS
52 #include <foreign/nvwa/debug_new.h>
53 #endif // CHECK_MEMORY_LEAKS
54 
55 
56 // ===========================================================================
57 // method definitions
58 // ===========================================================================
59 TraCITestClient::TraCITestClient(std::string outputFileName)
60  : outputFileName(outputFileName), answerLog("") {
61  answerLog.setf(std::ios::fixed , std::ios::floatfield); // use decimal format
62  answerLog.setf(std::ios::showpoint); // print decimal point
63  answerLog << std::setprecision(2);
64 }
65 
66 
68  writeResult();
69 }
70 
71 
72 bool
73 TraCITestClient::run(std::string fileName, int port, std::string host) {
74  std::ifstream defFile;
75  std::string fileContentStr;
76  std::stringstream fileContent;
77  std::string lineCommand;
78  std::stringstream msg;
79  int repNo = 1;
80  bool commentRead = false;
81 
82  // try to connect
83  try {
84  TraCIAPI::connect(host, port);
85  } catch (tcpip::SocketException& e) {
86  std::stringstream msg;
87  msg << "#Error while connecting: " << e.what();
88  errorMsg(msg);
89  return false;
90  }
91 
92  // read definition file and trigger commands according to it
93  defFile.open(fileName.c_str());
94  if (!defFile) {
95  msg << "Can not open definition file " << fileName << std::endl;
96  errorMsg(msg);
97  return false;
98  }
99  defFile.unsetf(std::ios::dec);
100 
101  while (defFile >> lineCommand) {
102  repNo = 1;
103  if (lineCommand.compare("%") == 0) {
104  // a comment was read
105  commentRead = !commentRead;
106  continue;
107  }
108  if (commentRead) {
109  // wait until end of comment is reached
110  continue;
111  }
112  if (lineCommand.compare("repeat") == 0) {
113  defFile >> repNo;
114  defFile >> lineCommand;
115  }
116  if (lineCommand.compare("simstep2") == 0) {
117  // read parameter for command simulation step and trigger command
118  std::string time;
119  defFile >> time;
120  for (int i = 0; i < repNo; i++) {
122  }
123  } else if (lineCommand.compare("getvariable") == 0) {
124  // trigger command GetXXXVariable
125  int domID, varID;
126  std::string objID;
127  defFile >> domID >> varID >> objID;
128  commandGetVariable(domID, varID, objID);
129  } else if (lineCommand.compare("getvariable_plus") == 0) {
130  // trigger command GetXXXVariable with one parameter
131  int domID, varID;
132  std::string objID;
133  defFile >> domID >> varID >> objID;
134  std::stringstream msg;
135  tcpip::Storage tmp;
136  setValueTypeDependant(tmp, defFile, msg);
137  std::string msgS = msg.str();
138  if (msgS != "") {
139  errorMsg(msg);
140  }
141  commandGetVariable(domID, varID, objID, &tmp);
142  } else if (lineCommand.compare("subscribevariable") == 0) {
143  // trigger command SubscribeXXXVariable
144  int domID, varNo;
145  std::string beginTime, endTime;
146  std::string objID;
147  defFile >> domID >> objID >> beginTime >> endTime >> varNo;
148  commandSubscribeObjectVariable(domID, objID, string2time(beginTime), string2time(endTime), varNo, defFile);
149  } else if (lineCommand.compare("subscribecontext") == 0) {
150  // trigger command SubscribeXXXVariable
151  int domID, varNo, domain;
152  SUMOReal range;
153  std::string beginTime, endTime;
154  std::string objID;
155  defFile >> domID >> objID >> beginTime >> endTime >> domain >> range >> varNo;
156  commandSubscribeContextVariable(domID, objID, string2time(beginTime), string2time(endTime), domain, range, varNo, defFile);
157  } else if (lineCommand.compare("setvalue") == 0) {
158  // trigger command SetXXXValue
159  int domID, varID;
160  std::string objID;
161  defFile >> domID >> varID >> objID;
162  commandSetValue(domID, varID, objID, defFile);
163  } else if (lineCommand.compare("testAPI") == 0) {
164  // call all native API methods
165  testAPI();
166  } else {
167  msg << "Error in definition file: " << lineCommand << " is not a valid command";
168  errorMsg(msg);
169  commandClose();
170  close();
171  return false;
172  }
173  }
174  defFile.close();
175  commandClose();
176  close();
177  return true;
178 }
179 
180 
181 // ---------- Commands handling
182 void
185  answerLog << std::endl << "-> Command sent: <SimulationStep2>:" << std::endl;
186  tcpip::Storage inMsg;
187  try {
188  std::string acknowledgement;
189  check_resultState(inMsg, CMD_SIMSTEP2, false, &acknowledgement);
190  answerLog << acknowledgement << std::endl;
192  } catch (tcpip::SocketException& e) {
193  answerLog << e.what() << std::endl;
194  }
195 }
196 
197 
198 void
201  answerLog << std::endl << "-> Command sent: <Close>:" << std::endl;
202  try {
203  tcpip::Storage inMsg;
204  std::string acknowledgement;
205  check_resultState(inMsg, CMD_CLOSE, false, &acknowledgement);
206  answerLog << acknowledgement << std::endl;
207  } catch (tcpip::SocketException& e) {
208  answerLog << e.what() << std::endl;
209  }
210 }
211 
212 
213 void
214 TraCITestClient::commandGetVariable(int domID, int varID, const std::string& objID, tcpip::Storage* addData) {
215  send_commandGetVariable(domID, varID, objID, addData);
216  answerLog << std::endl << "-> Command sent: <GetVariable>:" << std::endl
217  << " domID=" << domID << " varID=" << varID
218  << " objID=" << objID << std::endl;
219  tcpip::Storage inMsg;
220  try {
221  std::string acknowledgement;
222  check_resultState(inMsg, domID, false, &acknowledgement);
223  answerLog << acknowledgement << std::endl;
224  } catch (tcpip::SocketException& e) {
225  answerLog << e.what() << std::endl;
226  return;
227  }
228  check_commandGetResult(inMsg, domID, -1, false);
229  // report result state
230  try {
231  int variableID = inMsg.readUnsignedByte();
232  std::string objectID = inMsg.readString();
233  answerLog << " CommandID=" << (domID + 0x10) << " VariableID=" << variableID << " ObjectID=" << objectID;
234  int valueDataType = inMsg.readUnsignedByte();
235  answerLog << " valueDataType=" << valueDataType;
236  readAndReportTypeDependent(inMsg, valueDataType);
237  } catch (tcpip::SocketException& e) {
238  std::stringstream msg;
239  msg << "Error while receiving command: " << e.what();
240  errorMsg(msg);
241  return;
242  }
243 }
244 
245 
246 void
247 TraCITestClient::commandSetValue(int domID, int varID, const std::string& objID, std::ifstream& defFile) {
248  std::stringstream msg;
249  tcpip::Storage inMsg, tmp;
250  setValueTypeDependant(tmp, defFile, msg);
251  std::string msgS = msg.str();
252  if (msgS != "") {
253  errorMsg(msg);
254  }
255  send_commandSetValue(domID, varID, objID, tmp);
256  answerLog << std::endl << "-> Command sent: <SetValue>:" << std::endl
257  << " domID=" << domID << " varID=" << varID
258  << " objID=" << objID << std::endl;
259  try {
260  std::string acknowledgement;
261  check_resultState(inMsg, domID, false, &acknowledgement);
262  answerLog << acknowledgement << std::endl;
263  } catch (tcpip::SocketException& e) {
264  answerLog << e.what() << std::endl;
265  }
266 }
267 
268 
269 void
270 TraCITestClient::commandSubscribeObjectVariable(int domID, const std::string& objID, SUMOTime beginTime, SUMOTime endTime, int varNo, std::ifstream& defFile) {
271  std::vector<int> vars;
272  for (int i = 0; i < varNo; ++i) {
273  int var;
274  defFile >> var;
275  // variable id
276  vars.push_back(var);
277  }
278  send_commandSubscribeObjectVariable(domID, objID, beginTime, endTime, vars);
279  answerLog << std::endl << "-> Command sent: <SubscribeVariable>:" << std::endl
280  << " domID=" << domID << " objID=" << objID << " with " << varNo << " variables" << std::endl;
281  tcpip::Storage inMsg;
282  try {
283  std::string acknowledgement;
284  check_resultState(inMsg, domID, false, &acknowledgement);
285  answerLog << acknowledgement << std::endl;
286  validateSubscription(inMsg);
287  } catch (tcpip::SocketException& e) {
288  answerLog << e.what() << std::endl;
289  }
290 }
291 
292 
293 void
294 TraCITestClient::commandSubscribeContextVariable(int domID, const std::string& objID, SUMOTime beginTime, SUMOTime endTime,
295  int domain, SUMOReal range, int varNo, std::ifstream& defFile) {
296  std::vector<int> vars;
297  for (int i = 0; i < varNo; ++i) {
298  int var;
299  defFile >> var;
300  // variable id
301  vars.push_back(var);
302  }
303  send_commandSubscribeObjectContext(domID, objID, beginTime, endTime, domain, range, vars);
304  answerLog << std::endl << "-> Command sent: <SubscribeContext>:" << std::endl
305  << " domID=" << domID << " objID=" << objID << " domain=" << domain << " range=" << range
306  << " with " << varNo << " variables" << std::endl;
307  tcpip::Storage inMsg;
308  try {
309  std::string acknowledgement;
310  check_resultState(inMsg, domID, false, &acknowledgement);
311  answerLog << acknowledgement << std::endl;
312  validateSubscription(inMsg);
313  } catch (tcpip::SocketException& e) {
314  answerLog << e.what() << std::endl;
315  }
316 }
317 
318 
319 // ---------- Report helper
320 void
322  time_t seconds;
323  tm* locTime;
324  std::ofstream outFile(outputFileName.c_str());
325  if (!outFile) {
326  std::cerr << "Unable to write result file" << std::endl;
327  }
328  time(&seconds);
329  locTime = localtime(&seconds);
330  outFile << "TraCITestClient output file. Date: " << asctime(locTime) << std::endl;
331  outFile << answerLog.str();
332  outFile.close();
333 }
334 
335 
336 void
337 TraCITestClient::errorMsg(std::stringstream& msg) {
338  std::cerr << msg.str() << std::endl;
339  answerLog << "----" << std::endl << msg.str() << std::endl;
340 }
341 
342 
343 
344 
345 
346 
347 bool
349  try {
350  int noSubscriptions = inMsg.readInt();
351  for (int s = 0; s < noSubscriptions; ++s) {
352  if (!validateSubscription(inMsg)) {
353  return false;
354  }
355  }
356  } catch (std::invalid_argument& e) {
357  answerLog << "#Error while reading message:" << e.what() << std::endl;
358  return false;
359  }
360  return true;
361 }
362 
363 
364 bool
366  try {
367  int length = inMsg.readUnsignedByte();
368  if (length == 0) {
369  length = inMsg.readInt();
370  }
371  int cmdId = inMsg.readUnsignedByte();
373  answerLog << " CommandID=" << cmdId;
374  answerLog << " ObjectID=" << inMsg.readString();
375  int varNo = inMsg.readUnsignedByte();
376  answerLog << " #variables=" << varNo << std::endl;
377  for (int i = 0; i < varNo; ++i) {
378  answerLog << " VariableID=" << inMsg.readUnsignedByte();
379  bool ok = inMsg.readUnsignedByte() == RTYPE_OK;
380  answerLog << " ok=" << ok;
381  int valueDataType = inMsg.readUnsignedByte();
382  answerLog << " valueDataType=" << valueDataType;
383  readAndReportTypeDependent(inMsg, valueDataType);
384  }
386  answerLog << " CommandID=" << cmdId;
387  answerLog << " ObjectID=" << inMsg.readString();
388  answerLog << " Domain=" << inMsg.readUnsignedByte();
389  int varNo = inMsg.readUnsignedByte();
390  answerLog << " #variables=" << varNo << std::endl;
391  int objNo = inMsg.readInt();
392  answerLog << " #objects=" << objNo << std::endl;
393  for (int j = 0; j < objNo; ++j) {
394  answerLog << " ObjectID=" << inMsg.readString() << std::endl;
395  for (int i = 0; i < varNo; ++i) {
396  answerLog << " VariableID=" << inMsg.readUnsignedByte();
397  bool ok = inMsg.readUnsignedByte() == RTYPE_OK;
398  answerLog << " ok=" << ok;
399  int valueDataType = inMsg.readUnsignedByte();
400  answerLog << " valueDataType=" << valueDataType;
401  readAndReportTypeDependent(inMsg, valueDataType);
402  }
403  }
404  } else {
405  answerLog << "#Error: received response with command id: " << cmdId << " but expected a subscription response (0xe0-0xef / 0x90-0x9f)" << std::endl;
406  return false;
407  }
408  } catch (std::invalid_argument& e) {
409  answerLog << "#Error while reading message:" << e.what() << std::endl;
410  return false;
411  }
412  return true;
413 }
414 
415 
416 
417 
418 
419 
420 
421 // ---------- Conversion helper
422 int
423 TraCITestClient::setValueTypeDependant(tcpip::Storage& into, std::ifstream& defFile, std::stringstream& msg) {
424  std::string dataTypeS;
425  defFile >> dataTypeS;
426  if (dataTypeS == "<airDist>") {
428  return 1;
429  } else if (dataTypeS == "<drivingDist>") {
431  return 1;
432  } else if (dataTypeS == "<objSubscription>") {
433  int beginTime, endTime, numVars;
434  defFile >> beginTime >> endTime >> numVars;
435  into.writeInt(beginTime);
436  into.writeInt(endTime);
437  into.writeInt(numVars);
438  for (int i = 0; i < numVars; ++i) {
439  int var;
440  defFile >> var;
441  into.writeUnsignedByte(var);
442  }
443  return 4 + 4 + 4 + numVars;
444  }
445  int valI;
446  double valF;
447  if (dataTypeS == "<int>") {
448  defFile >> valI;
450  into.writeInt(valI);
451  return 4 + 1;
452  } else if (dataTypeS == "<byte>") {
453  defFile >> valI;
455  into.writeByte(valI);
456  return 1 + 1;
457  } else if (dataTypeS == "<ubyte>") {
458  defFile >> valI;
460  into.writeUnsignedByte(valI);
461  return 1 + 1;
462  } else if (dataTypeS == "<float>") {
463  defFile >> valF;
465  into.writeFloat(float(valF));
466  return 4 + 1;
467  } else if (dataTypeS == "<double>") {
468  defFile >> valF;
470  into.writeDouble(valF);
471  return 8 + 1;
472  } else if (dataTypeS == "<string>") {
473  std::string valueS;
474  defFile >> valueS;
475  if (valueS == "\"\"") {
476  valueS = "";
477  }
479  into.writeString(valueS);
480  return 4 + 1 + (int) valueS.length();
481  } else if (dataTypeS == "<string*>") {
482  std::vector<std::string> slValue;
483  defFile >> valI;
484  int length = 1 + 4;
485  for (int i = 0; i < valI; ++i) {
486  std::string tmp;
487  defFile >> tmp;
488  slValue.push_back(tmp);
489  length += 4 + int(tmp.length());
490  }
492  into.writeStringList(slValue);
493  return length;
494  } else if (dataTypeS == "<compound>") {
495  defFile >> valI;
497  into.writeInt(valI);
498  int length = 1 + 4;
499  for (int i = 0; i < valI; ++i) {
500  length += setValueTypeDependant(into, defFile, msg);
501  }
502  return length;
503  } else if (dataTypeS == "<color>") {
504  defFile >> valI;
506  into.writeUnsignedByte(valI);
507  for (int i = 0; i < 3; ++i) {
508  defFile >> valI;
509  into.writeUnsignedByte(valI);
510  }
511  return 1 + 4;
512  } else if (dataTypeS == "<position2D>") {
513  defFile >> valF;
515  into.writeDouble(valF);
516  defFile >> valF;
517  into.writeDouble(valF);
518  return 1 + 8 + 8;
519  } else if (dataTypeS == "<position3D>") {
520  defFile >> valF;
522  into.writeDouble(valF);
523  defFile >> valF;
524  into.writeDouble(valF);
525  defFile >> valF;
526  into.writeDouble(valF);
527  return 1 + 8 + 8 + 8;
528  } else if (dataTypeS == "<positionRoadmap>") {
529  std::string valueS;
530  defFile >> valueS;
532  into.writeString(valueS);
533  int length = 1 + 8 + (int) valueS.length();
534  defFile >> valF;
535  into.writeDouble(valF);
536  defFile >> valI;
537  into.writeUnsignedByte(valI);
538  return length + 4 + 1;
539  } else if (dataTypeS == "<shape>") {
540  defFile >> valI;
542  into.writeUnsignedByte(valI);
543  int length = 1 + 1;
544  for (int i = 0; i < valI; ++i) {
545  double x, y;
546  defFile >> x >> y;
547  into.writeDouble(x);
548  into.writeDouble(y);
549  length += 8 + 8;
550  }
551  return length;
552  }
553  msg << "## Unknown data type: " << dataTypeS;
554  return 0;
555 }
556 
557 
558 void
560  if (valueDataType == TYPE_UBYTE) {
561  int ubyte = inMsg.readUnsignedByte();
562  answerLog << " Unsigned Byte Value: " << ubyte << std::endl;
563  } else if (valueDataType == TYPE_BYTE) {
564  int byte = inMsg.readByte();
565  answerLog << " Byte value: " << byte << std::endl;
566  } else if (valueDataType == TYPE_INTEGER) {
567  int integer = inMsg.readInt();
568  answerLog << " Int value: " << integer << std::endl;
569  } else if (valueDataType == TYPE_FLOAT) {
570  float floatv = inMsg.readFloat();
571  if (floatv < 0.1 && floatv > 0) {
572  answerLog.setf(std::ios::scientific, std::ios::floatfield);
573  }
574  answerLog << " float value: " << floatv << std::endl;
575  answerLog.setf(std::ios::fixed , std::ios::floatfield); // use decimal format
576  answerLog.setf(std::ios::showpoint); // print decimal point
577  answerLog << std::setprecision(2);
578  } else if (valueDataType == TYPE_DOUBLE) {
579  double doublev = inMsg.readDouble();
580  answerLog << " Double value: " << doublev << std::endl;
581  } else if (valueDataType == TYPE_BOUNDINGBOX) {
582  SUMOReal lowerLeftX = inMsg.readDouble();
583  SUMOReal lowerLeftY = inMsg.readDouble();
584  SUMOReal upperRightX = inMsg.readDouble();
585  SUMOReal upperRightY = inMsg.readDouble();
586  answerLog << " BoundaryBoxValue: lowerLeft x=" << lowerLeftX
587  << " y=" << lowerLeftY << " upperRight x=" << upperRightX
588  << " y=" << upperRightY << std::endl;
589  } else if (valueDataType == TYPE_POLYGON) {
590  int length = inMsg.readUnsignedByte();
591  answerLog << " PolygonValue: ";
592  for (int i = 0; i < length; i++) {
593  SUMOReal x = inMsg.readDouble();
594  SUMOReal y = inMsg.readDouble();
595  answerLog << "(" << x << "," << y << ") ";
596  }
597  answerLog << std::endl;
598  } else if (valueDataType == POSITION_3D) {
599  SUMOReal x = inMsg.readDouble();
600  SUMOReal y = inMsg.readDouble();
601  SUMOReal z = inMsg.readDouble();
602  answerLog << " Position3DValue: " << std::endl;
603  answerLog << " x: " << x << " y: " << y
604  << " z: " << z << std::endl;
605  } else if (valueDataType == POSITION_ROADMAP) {
606  std::string roadId = inMsg.readString();
607  SUMOReal pos = inMsg.readDouble();
608  int laneId = inMsg.readUnsignedByte();
609  answerLog << " RoadMapPositionValue: roadId=" << roadId
610  << " pos=" << pos
611  << " laneId=" << laneId << std::endl;
612  } else if (valueDataType == TYPE_TLPHASELIST) {
613  int length = inMsg.readUnsignedByte();
614  answerLog << " TLPhaseListValue: length=" << length << std::endl;
615  for (int i = 0; i < length; i++) {
616  std::string pred = inMsg.readString();
617  std::string succ = inMsg.readString();
618  int phase = inMsg.readUnsignedByte();
619  answerLog << " precRoad=" << pred << " succRoad=" << succ
620  << " phase=";
621  switch (phase) {
622  case TLPHASE_RED:
623  answerLog << "red" << std::endl;
624  break;
625  case TLPHASE_YELLOW:
626  answerLog << "yellow" << std::endl;
627  break;
628  case TLPHASE_GREEN:
629  answerLog << "green" << std::endl;
630  break;
631  default:
632  answerLog << "#Error: unknown phase value" << (int)phase << std::endl;
633  return;
634  }
635  }
636  } else if (valueDataType == TYPE_STRING) {
637  std::string s = inMsg.readString();
638  answerLog << " string value: " << s << std::endl;
639  } else if (valueDataType == TYPE_STRINGLIST) {
640  std::vector<std::string> s = inMsg.readStringList();
641  answerLog << " string list value: [ " << std::endl;
642  for (std::vector<std::string>::iterator i = s.begin(); i != s.end(); ++i) {
643  if (i != s.begin()) {
644  answerLog << ", ";
645  }
646  answerLog << '"' << *i << '"';
647  }
648  answerLog << " ]" << std::endl;
649  } else if (valueDataType == TYPE_COMPOUND) {
650  int no = inMsg.readInt();
651  answerLog << " compound value with " << no << " members: [ " << std::endl;
652  for (int i = 0; i < no; ++i) {
653  int currentValueDataType = inMsg.readUnsignedByte();
654  answerLog << " valueDataType=" << currentValueDataType;
655  readAndReportTypeDependent(inMsg, currentValueDataType);
656  }
657  answerLog << " ]" << std::endl;
658  } else if (valueDataType == POSITION_2D) {
659  SUMOReal xv = inMsg.readDouble();
660  SUMOReal yv = inMsg.readDouble();
661  answerLog << " position value: (" << xv << "," << yv << ")" << std::endl;
662  } else if (valueDataType == TYPE_COLOR) {
663  int r = inMsg.readUnsignedByte();
664  int g = inMsg.readUnsignedByte();
665  int b = inMsg.readUnsignedByte();
666  int a = inMsg.readUnsignedByte();
667  answerLog << " color value: (" << r << "," << g << "," << b << "," << a << ")" << std::endl;
668  } else {
669  answerLog << "#Error: unknown valueDataType!" << std::endl;
670  }
671 }
672 
673 
674 void
676  answerLog << "testAPI:\n";
677 
678  answerLog << " edge:\n";
679  answerLog << " getIDList: " << joinToString(edge.getIDList(), " ") << "\n";
680  answerLog << " getIDCount: " << edge.getIDCount() << "\n";
681  const std::string edgeID = "e_m0";
682  edge.adaptTraveltime(edgeID, 42, 0, 10);
683  edge.setEffort(edgeID, 420, 0, 10);
684  answerLog << " currentTraveltime: " << edge.getTraveltime(edgeID) << "\n";
685  answerLog << " adaptedTravelTime: " << edge.getAdaptedTraveltime(edgeID, 0) << "\n";
686  answerLog << " effort: " << edge.getEffort(edgeID, 0) << "\n";
687  answerLog << " route:\n";
688  answerLog << " add:\n";
689  std::vector<std::string> edges;
690  edges.push_back("e_u1");
691  route.add("e_u1", edges);
692  answerLog << " getIDList: " << joinToString(route.getIDList(), " ") << "\n";
693  answerLog << " vehicleType:\n";
694  answerLog << " getIDList: " << joinToString(vehicletype.getIDList(), " ") << "\n";
695  vehicletype.setWidth("t1", 1.9);
696  answerLog << " getWidth: " << vehicletype.getWidth("t1") << "\n";
697  vehicletype.setHeight("t1", 1.8);
698  answerLog << " getHeight: " << vehicletype.getHeight("t1") << "\n";
699  answerLog << " vehicle:\n";
700  answerLog << " getRoadID: " << vehicle.getRoadID("0") << "\n";
701  answerLog << " getLaneID: " << vehicle.getLaneID("0") << "\n";
702  answerLog << " getSpeedMode: " << vehicle.getSpeedMode("0") << "\n";
703  answerLog << " getSlope: " << vehicle.getSlope("0") << "\n";
704  TraCIColor col1;
705  col1.r = 255;
706  col1.g = 255;
707  col1.b = 0;
708  col1.a = 128;
709  vehicle.setColor("0", col1);
710  TraCIColor col2 = vehicle.getColor("0");
711  answerLog << " getColor: " << col2.r << "r=" << col2.r << " g=" << col2.g << " b=" << col2.b << " a=" << col2.a << "\n";
712  answerLog << " getNextTLS:\n";
713  std::vector<VehicleScope::NextTLSData> result = vehicle.getNextTLS("0");
714  for (int i = 0; i < (int)result.size(); ++i) {
715  const VehicleScope::NextTLSData& d = result[i];
716  answerLog << " tls=" << d.id << " tlIndex=" << d.tlIndex << " dist=" << d.dist << " state=" << d.state << "\n";
717  }
718  answerLog << " moveToXY, simStep:\n";
719  vehicle.moveToXY("0", "dummy", 0, 2231.61, 498.29, 90, 1);
720  simulationStep();
721  answerLog << " getRoadID: " << vehicle.getRoadID("0") << "\n";
722  answerLog << " getLaneID: " << vehicle.getLaneID("0") << "\n";
723  answerLog << " add:\n";
724  vehicle.add("1", "e_u1");
725  simulationStep();
726  answerLog << " getIDList: " << joinToString(vehicle.getIDList(), " ") << "\n";
727  answerLog << " getWaitingTime: " << vehicle.getWaitingTime("0") << "\n";
728  answerLog << " remove:\n";
729  vehicle.remove("0");
730  answerLog << " getIDCount: " << vehicle.getIDCount() << "\n";
731 
732  answerLog << " inductionloop:\n";
733  answerLog << " getIDList: " << joinToString(inductionloop.getIDList(), " ") << "\n";
734  answerLog << " getVehicleData:\n";
735  std::vector<InductionLoopScope::VehicleData> result2 = inductionloop.getVehicleData("det1");
736  for (int i = 0; i < (int)result2.size(); ++i) {
737  const InductionLoopScope::VehicleData& vd = result2[i];
738  answerLog << " veh=" << vd.id << " length=" << vd.length << " entered=" << vd.entryTime << " left=" << vd.leaveTime << " type=" << vd.typeID << "\n";
739  }
740 
741  answerLog << " simulation:\n";
742  answerLog << " getCurrentTime: " << simulation.getCurrentTime() << "\n";
743  answerLog << " subscribe to road and pos of vehicle '1':\n";
744  std::vector<int> vars;
745  vars.push_back(VAR_ROAD_ID);
746  vars.push_back(VAR_LANEPOSITION);
748  simulationStep();
749  answerLog << " subscription results:\n";
751  answerLog << " roadID=" << result3[VAR_ROAD_ID].string << " pos=" << result3[VAR_LANEPOSITION].scalar << "\n";
752 
753  answerLog << " subscribe to vehicles around edge 'e_u1':\n";
754  std::vector<int> vars2;
755  vars2.push_back(VAR_LANEPOSITION);
757  simulationStep();
758  answerLog << " context subscription results:\n";
760  for (SubscribedValues::iterator it = result4.begin(); it != result4.end(); ++it) {
761  answerLog << " vehicle=" << it->first << " pos=" << it->second[VAR_LANEPOSITION].scalar << "\n";
762  }
763 
764  answerLog << " person:\n";
765  answerLog << " getIDList: " << joinToString(person.getIDList(), " ") << "\n";
766  answerLog << " getRoadID: " << person.getRoadID("p0") << "\n";
767  answerLog << " getTypeID: " << person.getTypeID("p0") << "\n";
768  answerLog << " getWaitingTime: " << person.getWaitingTime("p0") << "\n";
769  answerLog << " getNextEdge: " << person.getNextEdge("p0") << "\n";
770 
771  answerLog << " gui:\n";
772  try {
773  answerLog << " setScheme: \n";
774  gui.setSchema("View #0", "real world");
775  answerLog << " getScheme: " << gui.getSchema("View #0") << "\n";
776  answerLog << " take screenshot: \n";
777  gui.screenshot("View #0", "image.png");
778  } catch (tcpip::SocketException&) {
779  answerLog << " no support for gui commands\n";
780  }
781 }
EdgeScope edge
Scope for interaction with edges.
Definition: TraCIAPI.h:837
#define VAR_ROAD_ID
#define RESPONSE_SUBSCRIBE_INDUCTIONLOOP_CONTEXT
std::vector< std::string > getIDList() const
Definition: TraCIAPI.cpp:1295
void send_commandSetValue(int domID, int varID, const std::string &objID, tcpip::Storage &content) const
Sends a SetVariable request.
Definition: TraCIAPI.cpp:142
SubscribedContextValues getContextSubscriptionResults()
Definition: TraCIAPI.cpp:1428
void remove(const std::string &vehicleID, char reason=REMOVE_VAPORIZED) const
Definition: TraCIAPI.cpp:2070
std::vector< NextTLSData > getNextTLS(const std::string &vehID) const
Definition: TraCIAPI.cpp:1979
void readAndReportTypeDependent(tcpip::Storage &inMsg, int valueDataType)
Reads a value of the given type from the given storage and reports it.
void screenshot(const std::string &viewID, const std::string &filename) const
Definition: TraCIAPI.cpp:764
long long int SUMOTime
Definition: SUMOTime.h:43
#define REQUEST_DRIVINGDIST
void close()
Closes the connection.
Definition: TraCIAPI.cpp:80
void check_resultState(tcpip::Storage &inMsg, int command, bool ignoreCommandId=false, std::string *acknowledgement=0) const
Validates the result state of a command.
Definition: TraCIAPI.cpp:241
void connect(const std::string &host, int port)
Connects to the specified SUMO server.
Definition: TraCIAPI.cpp:67
void moveToXY(const std::string &vehicleID, const std::string &edgeID, const int lane, const SUMOReal x, const SUMOReal y, const SUMOReal angle, const int keepRoute) const
Definition: TraCIAPI.cpp:2096
#define CMD_GET_VEHICLE_VARIABLE
#define TYPE_COMPOUND
SUMOTime getCurrentTime() const
Definition: TraCIAPI.cpp:1323
std::stringstream answerLog
Stream containing the log.
#define CMD_CLOSE
virtual std::vector< std::string > readStringList()
#define RESPONSE_SUBSCRIBE_GUI_VARIABLE
#define POSITION_2D
void commandSubscribeObjectVariable(int domID, const std::string &objID, SUMOTime beginTime, SUMOTime endTime, int varNo, std::ifstream &defFile)
Sends and validates a SubscribeVariable command.
void testAPI()
call all API methods once
std::map< int, TraCIValue > TraCIValues
{object->{variable->value}}
Definition: TraCIAPI.h:560
std::string getTypeID(const std::string &typeID) const
Definition: TraCIAPI.cpp:2171
int setValueTypeDependant(tcpip::Storage &into, std::ifstream &defFile, std::stringstream &msg)
Parses the next value type / value pair from the stream and inserts it into the storage.
void setEffort(const std::string &edgeID, SUMOReal effort, SUMOTime begin=0, SUMOTime end=SUMOTime_MAX) const
Definition: TraCIAPI.cpp:664
#define TYPE_UBYTE
#define RTYPE_OK
SUMOReal getWaitingTime(const std::string &typeID) const
Definition: TraCIAPI.cpp:2176
void send_commandGetVariable(int domID, int varID, const std::string &objID, tcpip::Storage *add=0) const
Sends a GetVariable request.
Definition: TraCIAPI.cpp:115
#define POSITION_ROADMAP
virtual double readDouble()
#define TYPE_POLYGON
std::string getRoadID(const std::string &typeID) const
Definition: TraCIAPI.cpp:2166
PersonScope person
Scope for interaction with persons.
Definition: TraCIAPI.h:863
int getIDCount() const
Definition: TraCIAPI.cpp:1856
bool validateSubscription(tcpip::Storage &inMsg)
Validates whether the given message is a valid subscription return message.
std::string getRoadID(const std::string &vehicleID) const
Definition: TraCIAPI.cpp:1876
#define TYPE_COLOR
TraCIColor getColor(const std::string &vehicleID) const
Definition: TraCIAPI.cpp:1911
#define TYPE_STRINGLIST
SUMOReal getSlope(const std::string &vehicleID) const
Definition: TraCIAPI.cpp:1973
#define POSITION_3D
void simulationStep(SUMOTime time=0)
Advances by one step (or up to the given time)
Definition: TraCIAPI.cpp:520
std::string outputFileName
The name of the file to write the results log into.
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
virtual void writeUnsignedByte(int)
InductionLoopScope inductionloop
Scope for interaction with inductive loops.
Definition: TraCIAPI.h:841
virtual void writeInt(int)
#define TLPHASE_RED
#define TYPE_STRING
virtual int readUnsignedByte()
void subscribeContext(int domID, const std::string &objID, SUMOTime beginTime, SUMOTime endTime, int domain, SUMOReal range, const std::vector< int > &vars) const
Definition: TraCIAPI.cpp:1402
SUMOReal getWaitingTime(const std::string &vehicleID) const
Definition: TraCIAPI.cpp:1961
#define RESPONSE_SUBSCRIBE_INDUCTIONLOOP_VARIABLE
bool validateSimulationStep2(tcpip::Storage &inMsg)
Validates whether the given message is a valid answer to CMD_SIMSTEP2.
void setColor(const std::string &vehicleID, const TraCIColor &c) const
Definition: TraCIAPI.cpp:2128
void commandSetValue(int domID, int varID, const std::string &objID, std::ifstream &defFile)
Sends and validates a SetVariable command.
#define TLPHASE_YELLOW
SUMOReal getTraveltime(const std::string &edgeID) const
Definition: TraCIAPI.cpp:626
#define TYPE_FLOAT
#define TYPE_TLPHASELIST
std::vector< std::string > getIDList() const
Definition: TraCIAPI.cpp:790
std::map< std::string, TraCIValues > SubscribedValues
Definition: TraCIAPI.h:561
virtual int readInt()
std::string getNextEdge(const std::string &typeID) const
Definition: TraCIAPI.cpp:2181
#define VAR_LANEPOSITION
void writeResult()
Writes the results file.
void send_commandSubscribeObjectVariable(int domID, const std::string &objID, SUMOTime beginTime, SUMOTime endTime, const std::vector< int > &vars) const
Sends a SubscribeVariable request.
Definition: TraCIAPI.cpp:163
virtual void writeByte(int)
RouteScope route
Scope for interaction with routes.
Definition: TraCIAPI.h:853
#define TYPE_BOUNDINGBOX
void send_commandSimulationStep(SUMOTime time) const
Sends a SimulationStep command.
Definition: TraCIAPI.cpp:91
std::vector< VehicleData > getVehicleData(const std::string &loopID) const
Definition: TraCIAPI.cpp:835
virtual const char * what() const
Definition: socket.h:70
bool run(std::string fileName, int port, std::string host="localhost")
Runs a test.
virtual void writeStringList(const std::vector< std::string > &s)
int getIDCount() const
Definition: TraCIAPI.cpp:549
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:46
virtual std::string readString()
SUMOReal getWidth(const std::string &typeID) const
Definition: TraCIAPI.cpp:1674
void setWidth(const std::string &typeID, SUMOReal width) const
Definition: TraCIAPI.cpp:1751
void add(const std::string &routeID, const std::vector< std::string > &edges) const
Definition: TraCIAPI.cpp:1306
#define CMD_SUBSCRIBE_EDGE_CONTEXT
TraCITestClient(std::string outputFileName="testclient_result.out")
Constructor.
virtual void writeFloat(float)
void errorMsg(std::stringstream &msg)
Writes an error message.
#define TLPHASE_GREEN
void send_commandSubscribeObjectContext(int domID, const std::string &objID, SUMOTime beginTime, SUMOTime endTime, int domain, SUMOReal range, const std::vector< int > &vars) const
Sends a SubscribeContext request.
Definition: TraCIAPI.cpp:191
#define RESPONSE_SUBSCRIBE_GUI_CONTEXT
SimulationScope simulation
Scope for interaction with the simulation.
Definition: TraCIAPI.h:855
virtual void writeString(const std::string &s)
SubscribedValues getSubscriptionResults()
Definition: TraCIAPI.cpp:1412
#define REQUEST_AIRDIST
VehicleScope vehicle
Scope for interaction with vehicles.
Definition: TraCIAPI.h:861
#define TYPE_DOUBLE
#define CMD_SUBSCRIBE_VEHICLE_VARIABLE
virtual float readFloat()
#define TYPE_BYTE
void commandClose()
Sends and validates a Close command.
void send_commandClose() const
Sends a Close command.
Definition: TraCIAPI.cpp:104
int check_commandGetResult(tcpip::Storage &inMsg, int command, int expectedType=-1, bool ignoreCommandId=false) const
Validates the result state of a command.
Definition: TraCIAPI.cpp:280
int getSpeedMode(const std::string &vehicleID) const
Definition: TraCIAPI.cpp:1967
void setSchema(const std::string &viewID, const std::string &schemeName) const
Definition: TraCIAPI.cpp:741
std::vector< std::string > getIDList() const
Definition: TraCIAPI.cpp:1609
virtual void writeDouble(double)
GUIScope gui
Scope for interaction with the gui.
Definition: TraCIAPI.h:839
void subscribe(int domID, const std::string &objID, SUMOTime beginTime, SUMOTime endTime, const std::vector< int > &vars) const
Definition: TraCIAPI.cpp:1393
std::string joinToString(const std::vector< T > &v, const T_BETWEEN &between, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:188
std::vector< std::string > getIDList() const
Definition: TraCIAPI.cpp:544
#define SUMOReal
Definition: config.h:213
void commandSubscribeContextVariable(int domID, const std::string &objID, SUMOTime beginTime, SUMOTime endTime, int domain, SUMOReal range, int varNo, std::ifstream &defFile)
Sends and validates a SubscribeContext command.
std::string getLaneID(const std::string &vehicleID) const
Definition: TraCIAPI.cpp:1881
void adaptTraveltime(const std::string &edgeID, SUMOReal time, SUMOTime begin=0, SUMOTime end=SUMOTime_MAX) const
Definition: TraCIAPI.cpp:648
void commandGetVariable(int domID, int varID, const std::string &objID, tcpip::Storage *addData=0)
Sends and validates a GetVariable command.
void add(const std::string &vehicleID, const std::string &routeID, const std::string &typeID="DEFAULT_VEHTYPE", std::string depart="-1", const std::string &departLane="first", const std::string &departPos="base", const std::string &departSpeed="0", const std::string &arrivalLane="current", const std::string &arrivalPos="max", const std::string &arrivalSpeed="current", const std::string &fromTaz="", const std::string &toTaz="", const std::string &line="", int personCapacity=0, int personNumber=0) const
Definition: TraCIAPI.cpp:2009
SUMOReal getEffort(const std::string &edgeID, SUMOTime time) const
Definition: TraCIAPI.cpp:562
void commandSimulationStep(SUMOTime time)
Sends and validates a simulation step command.
SUMOReal getHeight(const std::string &typeID) const
Definition: TraCIAPI.cpp:1679
SUMOReal getAdaptedTraveltime(const std::string &edgeID, SUMOTime time) const
Definition: TraCIAPI.cpp:554
#define TYPE_INTEGER
#define CMD_SIMSTEP2
void setHeight(const std::string &typeID, SUMOReal height) const
Definition: TraCIAPI.cpp:1761
virtual int readByte()
~TraCITestClient()
Destructor.
std::vector< std::string > getIDList() const
Definition: TraCIAPI.cpp:2146
std::vector< std::string > getIDList() const
Definition: TraCIAPI.cpp:1851
std::string getSchema(const std::string &viewID=DEFAULT_VIEW) const
Definition: TraCIAPI.cpp:710
VehicleTypeScope vehicletype
Scope for interaction with vehicle types.
Definition: TraCIAPI.h:859