LCOV - code coverage report
Current view: top level - src/traci-server - TraCIServerAPI_Lane.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 96.2 % 78 75
Test Date: 2024-12-21 15:45:41 Functions: 100.0 % 2 2

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2009-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    TraCIServerAPI_Lane.cpp
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Jakob Erdmann
      17              : /// @author  Michael Behrisch
      18              : /// @author  Laura Bieker
      19              : /// @author  Mario Krumnow
      20              : /// @author  Leonhard Luecken
      21              : /// @author  Mirko Barthauer
      22              : /// @date    07.05.2009
      23              : ///
      24              : // APIs for getting/setting lane values via TraCI
      25              : /****************************************************************************/
      26              : #include <config.h>
      27              : 
      28              : #include <microsim/MSEdge.h>
      29              : #include <microsim/MSEdgeControl.h>
      30              : #include <microsim/MSLane.h>
      31              : #include <microsim/MSNet.h>
      32              : #include <microsim/MSVehicle.h>
      33              : #include <microsim/transportables/MSTransportable.h>
      34              : #include <libsumo/Lane.h>
      35              : #include <libsumo/TraCIConstants.h>
      36              : #include <libsumo/StorageHelper.h>
      37              : #include "TraCIServer.h"
      38              : #include "TraCIServerAPI_Lane.h"
      39              : 
      40              : 
      41              : // ===========================================================================
      42              : // method definitions
      43              : // ===========================================================================
      44              : bool
      45        16034 : TraCIServerAPI_Lane::processGet(TraCIServer& server, tcpip::Storage& inputStorage,
      46              :                                 tcpip::Storage& outputStorage) {
      47        16034 :     const int variable = inputStorage.readUnsignedByte();
      48        16034 :     const std::string id = inputStorage.readString();
      49        16034 :     server.initWrapper(libsumo::RESPONSE_GET_LANE_VARIABLE, variable, id);
      50              :     try {
      51        16034 :         if (!libsumo::Lane::handleVariable(id, variable, &server, &inputStorage)) {
      52           44 :             switch (variable) {
      53           24 :                 case libsumo::LANE_LINKS: {
      54           24 :                     server.getWrapperStorage().writeUnsignedByte(libsumo::TYPE_COMPOUND);
      55           24 :                     const std::vector<libsumo::TraCIConnection> links = libsumo::Lane::getLinks(id);
      56           24 :                     tcpip::Storage tempContent;
      57              :                     int cnt = 0;
      58           24 :                     tempContent.writeUnsignedByte(libsumo::TYPE_INTEGER);
      59           24 :                     tempContent.writeInt((int) links.size());
      60              :                     ++cnt;
      61           76 :                     for (std::vector<libsumo::TraCIConnection>::const_iterator i = links.begin(); i != links.end(); ++i) {
      62              :                         // approached non-internal lane (if any)
      63           52 :                         tempContent.writeUnsignedByte(libsumo::TYPE_STRING);
      64           52 :                         tempContent.writeString(i->approachedLane);
      65              :                         ++cnt;
      66              :                         // approached "via", internal lane (if any)
      67           52 :                         tempContent.writeUnsignedByte(libsumo::TYPE_STRING);
      68           52 :                         tempContent.writeString(i->approachedInternal);
      69              :                         ++cnt;
      70              :                         // priority
      71           52 :                         tempContent.writeUnsignedByte(libsumo::TYPE_UBYTE);
      72           52 :                         tempContent.writeUnsignedByte(i->hasPrio);
      73              :                         ++cnt;
      74              :                         // opened
      75           52 :                         tempContent.writeUnsignedByte(libsumo::TYPE_UBYTE);
      76           52 :                         tempContent.writeUnsignedByte(i->isOpen);
      77              :                         ++cnt;
      78              :                         // approaching foe
      79           52 :                         tempContent.writeUnsignedByte(libsumo::TYPE_UBYTE);
      80           52 :                         tempContent.writeUnsignedByte(i->hasFoe);
      81              :                         ++cnt;
      82              :                         // state (not implemented, yet)
      83           52 :                         tempContent.writeUnsignedByte(libsumo::TYPE_STRING);
      84           52 :                         tempContent.writeString(i->state);
      85              :                         ++cnt;
      86              :                         // direction
      87           52 :                         tempContent.writeUnsignedByte(libsumo::TYPE_STRING);
      88           52 :                         tempContent.writeString(i->direction);
      89              :                         ++cnt;
      90              :                         // length
      91           52 :                         tempContent.writeUnsignedByte(libsumo::TYPE_DOUBLE);
      92           52 :                         tempContent.writeDouble(i->length);
      93           52 :                         ++cnt;
      94              :                     }
      95           24 :                     server.getWrapperStorage().writeInt(cnt);
      96           24 :                     server.getWrapperStorage().writeStorage(tempContent);
      97              :                     break;
      98           24 :                 }
      99              :                 case libsumo::VAR_FOES: {
     100           18 :                     const std::string toLane = StoHelp::readTypedString(inputStorage, "Foe retrieval requires a string.");
     101           33 :                     StoHelp::writeTypedStringList(server.getWrapperStorage(), toLane == "" ? libsumo::Lane::getInternalFoes(id) : libsumo::Lane::getFoes(id, toLane));
     102              :                     break;
     103              :                 }
     104            2 :                 default:
     105            6 :                     return server.writeErrorStatusCmd(libsumo::CMD_GET_LANE_VARIABLE, "Get Lane Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
     106              :             }
     107              :         }
     108            5 :     } catch (libsumo::TraCIException& e) {
     109            5 :         return server.writeErrorStatusCmd(libsumo::CMD_GET_LANE_VARIABLE, e.what(), outputStorage);
     110            5 :     }
     111        16027 :     server.writeStatusCmd(libsumo::CMD_GET_LANE_VARIABLE, libsumo::RTYPE_OK, "", outputStorage);
     112        16027 :     server.writeResponseWithLength(outputStorage, server.getWrapperStorage());
     113              :     return true;
     114              : }
     115              : 
     116              : 
     117              : bool
     118          117 : TraCIServerAPI_Lane::processSet(TraCIServer& server, tcpip::Storage& inputStorage,
     119              :                                 tcpip::Storage& outputStorage) {
     120          117 :     std::string warning = ""; // additional description for response
     121              :     // variable
     122          117 :     int variable = inputStorage.readUnsignedByte();
     123          117 :     if (variable != libsumo::VAR_MAXSPEED && variable != libsumo::VAR_LENGTH && variable != libsumo::LANE_ALLOWED && variable != libsumo::LANE_DISALLOWED
     124           45 :             && variable != libsumo::VAR_PARAMETER && variable != libsumo::LANE_CHANGES) {
     125            0 :         return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "Change Lane State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
     126              :     }
     127              :     // id
     128          117 :     std::string id = inputStorage.readString();
     129          117 :     MSLane* l = MSLane::dictionary(id);
     130          117 :     if (l == nullptr) {
     131           12 :         return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, "Lane '" + id + "' is not known", outputStorage);
     132              :     }
     133              :     try {
     134              :         // process
     135          113 :         switch (variable) {
     136              :             case libsumo::VAR_MAXSPEED: {
     137           23 :                 const double value = StoHelp::readTypedDouble(inputStorage, "The speed must be given as a double.");
     138           22 :                 libsumo::Lane::setMaxSpeed(id, value);
     139              :                 break;
     140              :             }
     141              :             case libsumo::VAR_FRICTION: {
     142            0 :                 const double value = StoHelp::readTypedDouble(inputStorage, "The friction must be given as a double.");
     143            0 :                 libsumo::Lane::setFriction(id, value);
     144              :                 break;
     145              :             }
     146              :             case libsumo::VAR_LENGTH: {
     147           10 :                 const double value = StoHelp::readTypedDouble(inputStorage, "The length must be given as a double.");
     148            9 :                 libsumo::Lane::setLength(id, value);
     149              :                 break;
     150              :             }
     151              :             case libsumo::LANE_ALLOWED: {
     152           22 :                 const std::vector<std::string> classes = StoHelp::readTypedStringList(inputStorage, "Allowed vehicle classes must be given as a list of strings.");
     153           21 :                 libsumo::Lane::setAllowed(id, classes);
     154              :                 break;
     155           21 :             }
     156              :             case libsumo::LANE_DISALLOWED: {
     157           13 :                 const std::vector<std::string> classes = StoHelp::readTypedStringList(inputStorage, "Not allowed vehicle classes must be given as a list of strings.");
     158           12 :                 libsumo::Lane::setDisallowed(id, classes);
     159              :                 break;
     160           12 :             }
     161              :             case libsumo::LANE_CHANGES: {
     162           10 :                 StoHelp::readCompound(inputStorage, 2, "A compound object of size 2 is needed for setting lane change permissions.");
     163           10 :                 const std::vector<std::string> classes = StoHelp::readTypedStringList(inputStorage, "Vehicle classes allowed to change lane must be given as a list of strings.");
     164           10 :                 const int direction = StoHelp::readTypedByte(inputStorage, "The lane change direction must be given as an integer.");
     165           10 :                 libsumo::Lane::setChangePermissions(id, classes, direction);
     166              :                 break;
     167           10 :             }
     168              :             case libsumo::VAR_PARAMETER: {
     169           35 :                 StoHelp::readCompound(inputStorage, 2, "A compound object of size 2 is needed for setting a parameter.");
     170           35 :                 const std::string name = StoHelp::readTypedString(inputStorage, "The name of the parameter must be given as a string.");
     171           35 :                 const std::string value = StoHelp::readTypedString(inputStorage, "The value of the parameter must be given as a string.");
     172           35 :                 libsumo::Lane::setParameter(id, name, value);
     173              :                 break;
     174              :             }
     175              :             default:
     176              :                 break;
     177              :         }
     178            4 :     } catch (libsumo::TraCIException& e) {
     179            4 :         return server.writeErrorStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, e.what(), outputStorage);
     180            4 :     }
     181          109 :     server.writeStatusCmd(libsumo::CMD_SET_LANE_VARIABLE, libsumo::RTYPE_OK, warning, outputStorage);
     182              :     return true;
     183              : }
     184              : 
     185              : 
     186              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1