Eclipse SUMO - Simulation of Urban MObility
sumo_main.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 // Copyright (C) 2001-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 /****************************************************************************/
22 // Main for SUMO
23 /****************************************************************************/
24 #include <config.h>
25 
26 #ifdef HAVE_VERSION_H
27 #include <version.h>
28 #endif
29 
30 #include <csignal>
31 #include <netload/NLBuilder.h>
35 #include <utils/xml/XMLSubSys.h>
37 
38 
39 // ===========================================================================
40 // functions
41 // ===========================================================================
42 void
43 signalHandler(int signum) {
44  if (MSNet::hasInstance()) {
45  switch (signum) {
46  case SIGINT:
47  case SIGTERM:
48  if (MSNet::getInstance()->isInterrupted()) {
49  std::cout << TL("Another interrupt signal received, hard exit.") << std::endl;
50  exit(signum);
51  }
52  std::cout << TL("Interrupt signal received, trying to exit gracefully.") << std::endl;
54  break;
55 #ifndef WIN32
56  case SIGUSR1:
57  std::cout << "Step #" << SIMSTEP << std::endl;
58  std::cout << MSNet::getInstance()->generateStatistics(string2time(OptionsCont::getOptions().getString("begin")),
59  SysUtils::getCurrentMillis()) << std::endl;
60  break;
61  case SIGUSR2:
62  //TODO reload sim
63  break;
64 #endif
65  default:
66  break;
67  }
68  }
69 }
70 
71 
72 /* -------------------------------------------------------------------------
73  * main
74  * ----------------------------------------------------------------------- */
75 int
76 main(int argc, char** argv) {
77  signal(SIGINT, signalHandler);
78  signal(SIGTERM, signalHandler);
79 #ifndef WIN32
80  signal(SIGUSR1, signalHandler);
81  signal(SIGUSR2, signalHandler);
82 #endif
83 
85  // give some application descriptions
86  oc.setApplicationDescription(TL("A microscopic, multi-modal traffic simulation."));
87  oc.setApplicationName("sumo", "Eclipse SUMO sumo Version " VERSION_STRING);
88  gSimulation = true;
89  int ret = 0;
90  MSNet* net = nullptr;
91  try {
92  // initialise subsystems
94  OptionsIO::setArgs(argc, argv);
95  // load the net
97  while (state == MSNet::SIMSTATE_LOADING) {
98  net = NLBuilder::init();
99  if (net != nullptr) {
100  state = net->simulate(string2time(oc.getString("begin")), string2time(oc.getString("end")));
101  delete net;
102  } else {
103  break;
104  }
105  // flush warnings and prepare reinit of all outputs
109  }
110  } catch (const ProcessError& e) {
111  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
112  WRITE_ERROR(e.what());
113  }
114  MsgHandler::getErrorInstance()->inform(TL("Quitting (on error)."), false);
115  // we need to delete the network explicitly to trigger the cleanup in the correct order
116  delete net;
117  ret = 1;
118 #ifndef _DEBUG
119  } catch (const std::exception& e) {
120  if (std::string(e.what()) != std::string("")) {
121  WRITE_ERROR(e.what());
122  }
123  MsgHandler::getErrorInstance()->inform(TL("Quitting (on error)."), false);
124  ret = 1;
125  } catch (...) {
126  MsgHandler::getErrorInstance()->inform(TL("Quitting (on unknown error)."), false);
127  ret = 1;
128 #endif
129  }
132  return ret;
133 }
134 
135 
136 /****************************************************************************/
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:304
#define TL(string)
Definition: MsgHandler.h:315
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition: SUMOTime.cpp:46
#define SIMSTEP
Definition: SUMOTime.h:61
bool gSimulation
Definition: StdDefs.cpp:30
The simulated network and simulation perfomer.
Definition: MSNet.h:89
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:184
SimulationState
Possible states of a simulation - running or stopped with different reasons.
Definition: MSNet.h:94
@ SIMSTATE_LOADING
The simulation is loading.
Definition: MSNet.h:96
void interrupt()
Definition: MSNet.h:812
static bool hasInstance()
Returns whether the network was already constructed.
Definition: MSNet.h:152
const std::string generateStatistics(const SUMOTime start, const long now)
Writes performance output and running vehicle stats.
Definition: MSNet.cpp:444
SimulationState simulate(SUMOTime start, SUMOTime stop)
Simulates from timestep start to stop.
Definition: MSNet.cpp:396
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:92
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:154
static MsgHandler * getWarningInstance()
Returns the instance to add warnings to.
Definition: MsgHandler.cpp:79
virtual void clear(bool resetInformed=true)
Clears information whether an error occurred previously and print aggregated message summary.
Definition: MsgHandler.cpp:213
static void cleanupOnEnd()
Removes pending handler.
Definition: MsgHandler.cpp:359
static MSNet * init(const bool isLibsumo=false)
Definition: NLBuilder.cpp:296
A storage for options typed value containers)
Definition: OptionsCont.h:89
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:60
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:58
static void closeAll(bool keepErrorRetrievers=false)
static long getCurrentMillis()
Returns the current time in milliseconds.
Definition: SysUtils.cpp:44
static void close()
Closes all of an applications subsystems.
static void close()
request termination of connection
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:56
int main(int argc, char **argv)
Definition: sumo_main.cpp:76
void signalHandler(int signum)
Definition: sumo_main.cpp:43