LCOV - code coverage report
Current view: top level - src/libtraci - Domain.h (source / functions) Coverage Total Hit
Test: lcov.info Lines: 99.1 % 106 105
Test Date: 2024-11-21 15:56:26 Functions: 92.8 % 167 155

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2012-2024 German Aerospace Center (DLR) and others.
       4              : // This program and the accompanying materials are made available under the
       5              : // terms of the Eclipse Public License 2.0 which is available at
       6              : // https://www.eclipse.org/legal/epl-2.0/
       7              : // This Source Code may also be made available under the following Secondary
       8              : // Licenses when the conditions for such availability set forth in the Eclipse
       9              : // Public License 2.0 are satisfied: GNU General Public License, version 2
      10              : // or later which is available at
      11              : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
      12              : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
      13              : /****************************************************************************/
      14              : /// @file    Domain.h
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Mario Krumnow
      17              : /// @author  Michael Behrisch
      18              : /// @author  Robert Hilbrich
      19              : /// @date    30.05.2012
      20              : ///
      21              : // C++ TraCI client API implementation
      22              : /****************************************************************************/
      23              : #pragma once
      24              : #include <config.h>
      25              : 
      26              : #include <vector>
      27              : #include <limits>
      28              : #include <map>
      29              : #include <string>
      30              : #include <stdexcept>
      31              : #include <sstream>
      32              : #include <memory>
      33              : #include <foreign/tcpip/storage.h>
      34              : #include <libtraci/Connection.h>
      35              : #include <libsumo/StorageHelper.h>
      36              : 
      37              : 
      38              : #define LIBTRACI_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN) \
      39              : const int CLASS::DOMAIN_ID(libsumo::CMD_GET_##DOMAIN##_VARIABLE); \
      40              : void CLASS::subscribe(const std::string& objectID, const std::vector<int>& varIDs, double begin, double end, const libsumo::TraCIResults& params) { \
      41              :     libtraci::Connection::getActive().subscribe(libsumo::CMD_SUBSCRIBE_##DOMAIN##_VARIABLE, objectID, begin, end, -1, -1, varIDs, params); \
      42              : } \
      43              : \
      44              : void CLASS::unsubscribe(const std::string& objectID) { \
      45              :     subscribe(objectID, std::vector<int>()); \
      46              : } \
      47              : \
      48              : void CLASS::subscribeContext(const std::string& objectID, int domain, double dist, const std::vector<int>& varIDs, double begin, double end, const libsumo::TraCIResults& params) { \
      49              :     libtraci::Connection::getActive().subscribe(libsumo::CMD_SUBSCRIBE_##DOMAIN##_CONTEXT, objectID, begin, end, domain, dist, varIDs, params); \
      50              : } \
      51              : \
      52              : void CLASS::unsubscribeContext(const std::string& objectID, int domain, double dist) { \
      53              :     subscribeContext(objectID, domain, dist, std::vector<int>()); \
      54              : } \
      55              : \
      56              : const libsumo::SubscriptionResults CLASS::getAllSubscriptionResults() { \
      57              :     return libtraci::Connection::getActive().getAllSubscriptionResults(libsumo::RESPONSE_SUBSCRIBE_##DOMAIN##_VARIABLE); \
      58              : } \
      59              : \
      60              : const libsumo::TraCIResults CLASS::getSubscriptionResults(const std::string& objectID) { \
      61              :     return libtraci::Connection::getActive().getAllSubscriptionResults(libsumo::RESPONSE_SUBSCRIBE_##DOMAIN##_VARIABLE)[objectID]; \
      62              : } \
      63              : \
      64              : const libsumo::ContextSubscriptionResults CLASS::getAllContextSubscriptionResults() { \
      65              :     return libtraci::Connection::getActive().getAllContextSubscriptionResults(libsumo::RESPONSE_SUBSCRIBE_##DOMAIN##_CONTEXT); \
      66              : } \
      67              : \
      68              : const libsumo::SubscriptionResults CLASS::getContextSubscriptionResults(const std::string& objectID) { \
      69              :     return libtraci::Connection::getActive().getAllContextSubscriptionResults(libsumo::RESPONSE_SUBSCRIBE_##DOMAIN##_CONTEXT)[objectID]; \
      70              : } \
      71              : \
      72              : void CLASS::subscribeParameterWithKey(const std::string& objectID, const std::string& key, double beginTime, double endTime) { \
      73              :     subscribe(objectID, std::vector<int>({libsumo::VAR_PARAMETER_WITH_KEY}), beginTime, endTime, libsumo::TraCIResults {{libsumo::VAR_PARAMETER_WITH_KEY, std::make_shared<libsumo::TraCIString>(key)}}); \
      74              : }
      75              : 
      76              : 
      77              : #define LIBTRACI_PARAMETER_IMPLEMENTATION(CLASS, DOMAIN) \
      78              : std::string \
      79              : CLASS::getParameter(const std::string& objectID, const std::string& param) { \
      80              :     tcpip::Storage content; \
      81              :     content.writeByte(libsumo::TYPE_STRING); \
      82              :     content.writeString(param); \
      83              :     return Dom::getString(libsumo::VAR_PARAMETER, objectID, &content); \
      84              : } \
      85              : \
      86              : void \
      87              : CLASS::setParameter(const std::string& objectID, const std::string& key, const std::string& value) { \
      88              :     tcpip::Storage content; \
      89              :     content.writeUnsignedByte(libsumo::TYPE_COMPOUND); \
      90              :     content.writeInt(2); \
      91              :     content.writeUnsignedByte(libsumo::TYPE_STRING); \
      92              :     content.writeString(key); \
      93              :     content.writeUnsignedByte(libsumo::TYPE_STRING); \
      94              :     content.writeString(value); \
      95              :     Connection::getActive().doCommand(libsumo::CMD_SET_##DOMAIN##_VARIABLE, libsumo::VAR_PARAMETER, objectID, &content); \
      96              : } \
      97              : \
      98              : const std::pair<std::string, std::string> \
      99              : CLASS::getParameterWithKey(const std::string& objectID, const std::string& key) { \
     100              :     return std::make_pair(key, getParameter(objectID, key)); \
     101              : }
     102              : 
     103              : 
     104              : // ===========================================================================
     105              : // class and type definitions
     106              : // ===========================================================================
     107              : namespace libtraci {
     108              : template<int GET, int SET>
     109              : class Domain {
     110              : public:
     111              :     static inline tcpip::Storage& get(int var, const std::string& id, tcpip::Storage* add = nullptr, int expectedType = libsumo::TYPE_COMPOUND) {
     112       627573 :         return libtraci::Connection::getActive().doCommand(GET, var, id, add, expectedType);
     113              :     }
     114              : 
     115              :     static int getUnsignedByte(int var, const std::string& id, tcpip::Storage* add = nullptr) {
     116              :         std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
     117              :         return get(var, id, add, libsumo::TYPE_UBYTE).readUnsignedByte();
     118              :     }
     119              : 
     120              :     static int getByte(int var, const std::string& id, tcpip::Storage* add = nullptr) {
     121              :         std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
     122              :         return get(var, id, add, libsumo::TYPE_BYTE).readByte();
     123              :     }
     124              : 
     125        66882 :     static int getInt(int var, const std::string& id, tcpip::Storage* add = nullptr) {
     126        66882 :         std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
     127       133760 :         return get(var, id, add, libsumo::TYPE_INTEGER).readInt();
     128              :     }
     129              : 
     130        86413 :     static double getDouble(int var, const std::string& id, tcpip::Storage* add = nullptr) {
     131        86413 :         std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
     132       172800 :         return get(var, id, add, libsumo::TYPE_DOUBLE).readDouble();
     133              :     }
     134              : 
     135         6034 :     static libsumo::TraCIPositionVector getPolygon(int var, const std::string& id, tcpip::Storage* add = nullptr) {
     136         6034 :         std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
     137              :         tcpip::Storage& result = get(var, id, add, libsumo::TYPE_POLYGON);
     138              :         libsumo::TraCIPositionVector ret;
     139         6034 :         int size = result.readUnsignedByte();
     140         6034 :         if (size == 0) {
     141            1 :             size = result.readInt();
     142              :         }
     143        21049 :         for (int i = 0; i < size; ++i) {
     144        15015 :             libsumo::TraCIPosition p;
     145        15015 :             p.x = result.readDouble();
     146        15015 :             p.y = result.readDouble();
     147        15015 :             p.z = 0.;
     148        15015 :             ret.value.push_back(p);
     149              :         }
     150         6034 :         return ret;
     151              :     }
     152              : 
     153       359961 :     static libsumo::TraCIPosition getPos(int var, const std::string& id, tcpip::Storage* add = nullptr, const bool isGeo = false) {
     154       359961 :         std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
     155       359961 :         tcpip::Storage& result = get(var, id, add, isGeo ? libsumo::POSITION_LON_LAT : libsumo::POSITION_2D);
     156       359960 :         libsumo::TraCIPosition p;
     157       359960 :         p.x = result.readDouble();
     158       359960 :         p.y = result.readDouble();
     159       359960 :         return p;
     160              :     }
     161              : 
     162          404 :     static libsumo::TraCIPosition getPos3D(int var, const std::string& id, tcpip::Storage* add = nullptr, const bool isGeo = false) {
     163          404 :         std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
     164          404 :         tcpip::Storage& result = get(var, id, add, isGeo ? libsumo::POSITION_LON_LAT_ALT : libsumo::POSITION_3D);
     165          404 :         libsumo::TraCIPosition p;
     166          404 :         p.x = result.readDouble();
     167          404 :         p.y = result.readDouble();
     168          404 :         p.z = result.readDouble();
     169          404 :         return p;
     170              :     }
     171              : 
     172        21777 :     static std::string getString(int var, const std::string& id, tcpip::Storage* add = nullptr) {
     173        21777 :         std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
     174        43276 :         return get(var, id, add, libsumo::TYPE_STRING).readString();
     175              :     }
     176              : 
     177        55978 :     static std::vector<std::string> getStringVector(int var, const std::string& id, tcpip::Storage* add = nullptr) {
     178        55978 :         std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
     179       111862 :         return get(var, id, add, libsumo::TYPE_STRINGLIST).readStringList();
     180              :     }
     181              : 
     182            2 :     static std::vector<double> getDoubleVector(int var, const std::string& id, tcpip::Storage* add = nullptr) {
     183            2 :         std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
     184            4 :         return get(var, id, add, libsumo::TYPE_DOUBLELIST).readDoubleList();
     185              :     }
     186              : 
     187           72 :     static libsumo::TraCIColor getCol(int var, const std::string& id, tcpip::Storage* add = nullptr) {
     188           72 :         std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
     189              :         tcpip::Storage& result = get(var, id, add, libsumo::TYPE_COLOR);
     190              :         libsumo::TraCIColor c;
     191           72 :         c.r = (unsigned char)result.readUnsignedByte();
     192           72 :         c.g = (unsigned char)result.readUnsignedByte();
     193           72 :         c.b = (unsigned char)result.readUnsignedByte();
     194           72 :         c.a = (unsigned char)result.readUnsignedByte();
     195           72 :         return c;
     196              :     }
     197              : 
     198          193 :     static libsumo::TraCIStage getTraCIStage(int var, const std::string& id, tcpip::Storage* add = nullptr) {
     199          193 :         std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
     200              :         tcpip::Storage& result = get(var, id, add);
     201          369 :         libsumo::TraCIStage s;
     202          176 :         result.readInt(); // components
     203          176 :         s.type = StoHelp::readTypedInt(result);
     204          176 :         s.vType = StoHelp::readTypedString(result);
     205          176 :         s.line = StoHelp::readTypedString(result);
     206          176 :         s.destStop = StoHelp::readTypedString(result);
     207          176 :         s.edges = StoHelp::readTypedStringList(result);
     208          176 :         s.travelTime = StoHelp::readTypedDouble(result);
     209          176 :         s.cost = StoHelp::readTypedDouble(result);
     210          176 :         s.length = StoHelp::readTypedDouble(result);
     211          176 :         s.intended = StoHelp::readTypedString(result);
     212          176 :         s.depart = StoHelp::readTypedDouble(result);
     213          176 :         s.departPos = StoHelp::readTypedDouble(result);
     214          176 :         s.arrivalPos = StoHelp::readTypedDouble(result);
     215          352 :         s.description = StoHelp::readTypedString(result);
     216          176 :         return s;
     217            0 :     }
     218              : 
     219        15618 :     static void set(int var, const std::string& id, tcpip::Storage* add) {
     220        15618 :         std::unique_lock<std::mutex> lock{ libtraci::Connection::getActive().getMutex() };
     221        15618 :         libtraci::Connection::getActive().doCommand(SET, var, id, add);
     222        15578 :     }
     223              : 
     224          204 :     static void setInt(int var, const std::string& id, int value) {
     225          204 :         tcpip::Storage content;
     226          204 :         content.writeUnsignedByte(libsumo::TYPE_INTEGER);
     227          204 :         content.writeInt(value);
     228          204 :         set(var, id, &content);
     229          204 :     }
     230              : 
     231         2733 :     static void setDouble(int var, const std::string& id, double value) {
     232         2733 :         tcpip::Storage content;
     233         2733 :         content.writeUnsignedByte(libsumo::TYPE_DOUBLE);
     234         2733 :         content.writeDouble(value);
     235         2733 :         set(var, id, &content);
     236         2733 :     }
     237              : 
     238          182 :     static void setString(int var, const std::string& id, const std::string& value) {
     239          182 :         tcpip::Storage content;
     240          182 :         content.writeUnsignedByte(libsumo::TYPE_STRING);
     241          182 :         content.writeString(value);
     242          182 :         set(var, id, &content);
     243          182 :     }
     244              : 
     245           92 :     static void setStringVector(int var, const std::string& id, const std::vector<std::string>& value) {
     246           92 :         tcpip::Storage content;
     247           92 :         content.writeUnsignedByte(libsumo::TYPE_STRINGLIST);
     248           92 :         content.writeStringList(value);
     249           92 :         set(var, id, &content);
     250           92 :     }
     251              : 
     252           49 :     static void setCol(int var, const std::string& id, const libsumo::TraCIColor value) {
     253           49 :         tcpip::Storage content;
     254           49 :         content.writeUnsignedByte(libsumo::TYPE_COLOR);
     255           49 :         content.writeUnsignedByte(value.r);
     256           49 :         content.writeUnsignedByte(value.g);
     257           49 :         content.writeUnsignedByte(value.b);
     258           49 :         content.writeUnsignedByte(value.a);
     259           49 :         set(var, id, &content);
     260           49 :     }
     261              : 
     262              : };
     263              : 
     264              : }
        

Generated by: LCOV version 2.0-1