43 #pragma warning(disable: 4503)
45 Connection::Connection(
const std::string& host,
int port,
int numRetries,
const std::string& label, FILE*
const pipe) :
46 myLabel(label), myProcessPipe(pipe), mySocket(host, port) {
47 for (
int i = 0; i <= numRetries; i++) {
52 if (i == numRetries) {
55 std::this_thread::sleep_for(std::chrono::seconds(1));
74 std::string acknowledgement;
78 std::array<char, 128> buffer;
79 std::stringstream result;
80 while (fgets(buffer.data(), (
int)buffer.size(),
myProcessPipe) !=
nullptr) {
81 result << buffer.data();
84 while (std::getline(result, line)) {
85 if (line.compare(0, 6,
"Error:") == 0) {
86 std::cerr << line << std::endl;
88 std::cout << line << std::endl;
116 while (numSubs > 0) {
148 length += 1 + 4 + (int) objID.length();
150 if (add !=
nullptr) {
151 length += (int)add->
size();
165 if (add !=
nullptr) {
175 int length = 1 + 1 + 1;
176 if (add !=
nullptr) {
177 length += (int)add->
size();
188 if (add !=
nullptr) {
202 const int numVars = (int) vars.
size();
204 outMsg.
writeInt(5 + 1 + 8 + 8 + 4 + (
int) objID.length() + 1 + numVars);
213 if (numVars == 1 && vars.front() == -1) {
227 for (
int i = 0; i < numVars; ++i) {
229 const auto& paramEntry = params.find(vars[i]);
230 if (paramEntry != params.end()) {
232 outMsg.
writePacket(paramEntry->second->toPacket());
256 int varNo = (int) vars.
size();
258 outMsg.
writeInt(5 + 1 + 8 + 8 + 4 + (
int) objID.length() + 1 + 8 + 1 + varNo);
271 for (
int i = 0; i < varNo; ++i) {
273 const auto& paramEntry = params.find(vars[i]);
274 if (paramEntry != params.end()) {
276 outMsg.
writePacket(paramEntry->second->toPacket());
301 if (command != cmdId && !ignoreCommandId) {
306 }
catch (std::invalid_argument&) {
309 switch (resultType) {
315 if (acknowledgement !=
nullptr) {
316 (*acknowledgement) =
".. Command acknowledged (" +
toString(command) +
"), [description: " + msg +
"]";
322 if ((cmdStart + cmdLength) != (
int) inMsg.
position()) {
336 if (!ignoreCommandId && cmdId != (command + 0x10)) {
339 if (expectedType >= 0) {
344 if (valueDataType != expectedType) {
379 while (variableCount > 0) {
388 into[objectID][variableID] = std::make_shared<libsumo::TraCIDouble>(inMsg.
readDouble());
391 into[objectID][variableID] = std::make_shared<libsumo::TraCIString>(inMsg.
readString());
394 auto p = std::make_shared<libsumo::TraCIPosition>();
398 into[objectID][variableID] = p;
402 auto p = std::make_shared<libsumo::TraCIPosition>();
406 into[objectID][variableID] = p;
410 auto c = std::make_shared<libsumo::TraCIColor>();
415 into[objectID][variableID] = c;
419 into[objectID][variableID] = std::make_shared<libsumo::TraCIInt>(inMsg.
readInt());
422 auto sl = std::make_shared<libsumo::TraCIStringList>();
424 for (
int i = 0; i < n; ++i) {
427 into[objectID][variableID] = sl;
447 const std::string objectID = inMsg.
readString();
455 const std::string contextID = inMsg.
readString();
458 int numObjects = inMsg.
readInt();
460 while (numObjects > 0) {
void simulationStep(double time)
Sends a SimulationStep command.
void subscribeObjectVariable(int domID, const std::string &objID, double beginTime, double endTime, const std::vector< int > &vars, const libsumo::TraCIResults ¶ms)
Sends a SubscribeVariable request.
Connection(const std::string &host, int port, int numRetries, const std::string &label, FILE *const pipe)
Constructor, connects to the specified SUMO server.
void close()
ends the simulation and closes the connection
int check_commandGetResult(tcpip::Storage &inMsg, int command, int expectedType=-1, bool ignoreCommandId=false) const
Validates the result state of a command.
bool processGet(int command, int expectedType, bool ignoreCommandId=false)
void readVariableSubscription(int responseID, tcpip::Storage &inMsg)
tcpip::Socket mySocket
The socket.
std::map< int, libsumo::SubscriptionResults > mySubscriptionResults
void check_resultState(tcpip::Storage &inMsg, int command, bool ignoreCommandId=false, std::string *acknowledgement=0)
Validates the result state of a command.
tcpip::Storage myInput
The reusable input storage.
FILE *const myProcessPipe
void createFilterCommand(int cmdID, int varID, tcpip::Storage *add=nullptr) const
void readVariables(tcpip::Storage &inMsg, const std::string &objectID, int variableCount, libsumo::SubscriptionResults &into)
std::map< int, libsumo::ContextSubscriptionResults > myContextSubscriptionResults
tcpip::Storage myOutput
The reusable output storage.
void send_commandSetOrder(int order)
Sends a SetOrder command.
tcpip::Storage & doCommand(int command, int var, const std::string &id, tcpip::Storage *add=nullptr)
static std::map< const std::string, Connection * > myConnections
void createCommand(int cmdID, int varID, const std::string &objID, tcpip::Storage *add=nullptr) const
Sends a GetVariable / SetVariable request if mySocket is connected. Otherwise writes to myOutput only...
void readContextSubscription(int responseID, tcpip::Storage &inMsg)
static Connection * myActive
static std::string toString(const T &t, std::streamsize accuracy=PRECISION)
void subscribeObjectContext(int domID, const std::string &objID, double beginTime, double endTime, int domain, double range, const std::vector< int > &vars, const libsumo::TraCIResults ¶ms)
Sends a SubscribeContext request.
bool receiveExact(Storage &)
Receive a complete TraCI message from Socket::socket_.
void sendExact(const Storage &)
bool has_client_connection() const
void connect()
Connects to host_:port_.
virtual void writePacket(unsigned char *packet, int length)
virtual std::string readString()
virtual void writeString(const std::string &s)
virtual unsigned int position() const
virtual void writeInt(int)
virtual void writeDouble(double)
virtual int readUnsignedByte()
virtual void writeUnsignedByte(int)
StorageType::size_type size() const
virtual void writeStorage(tcpip::Storage &store)
virtual double readDouble()
TRACI_CONST int TYPE_COLOR
TRACI_CONST int LAST_STEP_VEHICLE_NUMBER
TRACI_CONST int POSITION_3D
TRACI_CONST int RTYPE_NOTIMPLEMENTED
TRACI_CONST int TRACI_ID_LIST
std::map< int, std::shared_ptr< TraCIResult > > TraCIResults
{variable->value}
TRACI_CONST int VAR_ROAD_ID
TRACI_CONST int RESPONSE_SUBSCRIBE_PARKINGAREA_VARIABLE
TRACI_CONST int RESPONSE_SUBSCRIBE_INDUCTIONLOOP_VARIABLE
TRACI_CONST int POSITION_2D
TRACI_CONST int RESPONSE_SUBSCRIBE_OVERHEADWIRE_VARIABLE
TRACI_CONST int CMD_CLOSE
TRACI_CONST int CMD_SETORDER
TRACI_CONST int TYPE_STRINGLIST
TRACI_CONST int TYPE_INTEGER
TRACI_CONST int RESPONSE_SUBSCRIBE_BUSSTOP_VARIABLE
std::map< std::string, TraCIResults > SubscriptionResults
{object->{variable->value}}
TRACI_CONST int VAR_LANEPOSITION
TRACI_CONST int CMD_SUBSCRIBE_VEHICLE_VARIABLE
TRACI_CONST int TYPE_DOUBLE
TRACI_CONST int CMD_SUBSCRIBE_LANEAREA_VARIABLE
TRACI_CONST int CMD_SUBSCRIBE_INDUCTIONLOOP_VARIABLE
TRACI_CONST int CMD_SUBSCRIBE_MULTIENTRYEXIT_VARIABLE
TRACI_CONST int RTYPE_ERR
TRACI_CONST int CMD_SIMSTEP
TRACI_CONST int TYPE_STRING