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.org/sumo
3 // Copyright (C) 2001-2022 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>
34 #include <utils/xml/XMLSubSys.h>
36 
37 
38 // ===========================================================================
39 // functions
40 // ===========================================================================
41 void
42 signalHandler(int signum) {
43  if (MSNet::hasInstance()) {
44  switch (signum) {
45  case SIGINT:
46  case SIGTERM:
47  if (MSNet::getInstance()->isInterrupted()) {
48  std::cout << "Another interrupt signal received, hard exit." << std::endl;
49  exit(signum);
50  }
51  std::cout << "Interrupt signal received, trying to exit gracefully." << std::endl;
53  break;
54 #ifndef WIN32
55  case SIGUSR1:
56  std::cout << "Step #" << SIMSTEP << std::endl;
57  std::cout << MSNet::getInstance()->generateStatistics(string2time(OptionsCont::getOptions().getString("begin"))) << std::endl;
58  break;
59  case SIGUSR2:
60  //TODO reload sim
61  break;
62 #endif
63  default:
64  break;
65  }
66  }
67 }
68 
69 
70 /* -------------------------------------------------------------------------
71  * main
72  * ----------------------------------------------------------------------- */
73 int
74 main(int argc, char** argv) {
75  signal(SIGINT, signalHandler);
76  signal(SIGTERM, signalHandler);
77 #ifndef WIN32
78  signal(SIGUSR1, signalHandler);
79  signal(SIGUSR2, signalHandler);
80 #endif
81 
83  // give some application descriptions
84  oc.setApplicationDescription("A microscopic, multi-modal traffic simulation.");
85  oc.setApplicationName("sumo", "Eclipse SUMO sumo Version " VERSION_STRING);
86  gSimulation = true;
87  int ret = 0;
88  MSNet* net = nullptr;
89  try {
90  // initialise subsystems
92  OptionsIO::setArgs(argc, argv);
93  // load the net
95  while (state == MSNet::SIMSTATE_LOADING) {
96  net = NLBuilder::init();
97  if (net != nullptr) {
98  state = net->simulate(string2time(oc.getString("begin")), string2time(oc.getString("end")));
99  delete net;
100  } else {
101  break;
102  }
103  }
104  } catch (const ProcessError& e) {
105  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
106  WRITE_ERROR(e.what());
107  }
108  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
109  // we need to delete the network explicitly to trigger the cleanup in the correct order
110  delete net;
111  ret = 1;
112 #ifndef _DEBUG
113  } catch (const std::exception& e) {
114  if (std::string(e.what()) != std::string("")) {
115  WRITE_ERROR(e.what());
116  }
117  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
118  ret = 1;
119  } catch (...) {
120  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
121  ret = 1;
122 #endif
123  }
126  return ret;
127 }
128 
129 
130 /****************************************************************************/
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:288
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition: SUMOTime.cpp:45
#define SIMSTEP
Definition: SUMOTime.h:59
bool gSimulation
Definition: StdDefs.cpp:28
The simulated network and simulation perfomer.
Definition: MSNet.h:88
const std::string generateStatistics(SUMOTime start)
Writes performance output and running vehicle stats.
Definition: MSNet.cpp:423
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:174
SimulationState
Possible states of a simulation - running or stopped with different reasons.
Definition: MSNet.h:93
@ SIMSTATE_LOADING
The simulation is loading.
Definition: MSNet.h:95
void interrupt()
Definition: MSNet.h:800
static bool hasInstance()
Returns whether the network was already constructed.
Definition: MSNet.h:154
SimulationState simulate(SUMOTime start, SUMOTime stop)
Simulates from timestep start to stop.
Definition: MSNet.cpp:375
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:117
static MSNet * init(const bool isLibsumo=false)
Definition: NLBuilder.cpp:270
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:58
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:58
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:54
int main(int argc, char **argv)
Definition: sumo_main.cpp:74
void signalHandler(int signum)
Definition: sumo_main.cpp:42