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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
19 // Main for SUMO
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #ifdef HAVE_VERSION_H
29 #include <version.h>
30 #endif
31 
32 #include <ctime>
33 #include <csignal>
34 #include <string>
35 #include <iostream>
36 #include <netload/NLBuilder.h>
43 #include <utils/common/ToString.h>
44 #include <utils/common/StdDefs.h>
45 #include <utils/xml/XMLSubSys.h>
47 
48 
49 // ===========================================================================
50 // functions
51 // ===========================================================================
52 void
53 signalHandler(int signum) {
54  if (MSNet::hasInstance()) {
55  switch (signum) {
56  case SIGINT:
57  case SIGTERM:
58  if (MSNet::getInstance()->isInterrupted()) {
59  std::cout << "Another interrupt signal received, hard exit." << std::endl;
60  exit(signum);
61  }
62  std::cout << "Interrupt signal received, trying to exit gracefully." << std::endl;
64  break;
65 #ifndef _MSC_VER
66  case SIGUSR1:
67  std::cout << "Step #" << SIMSTEP << std::endl;
68  std::cout << MSNet::getInstance()->generateStatistics(string2time(OptionsCont::getOptions().getString("begin"))) << std::endl;
69  break;
70  case SIGUSR2:
71  //TODO reload sim
72  break;
73 #endif
74  default:
75  break;
76  }
77  }
78 }
79 
80 
81 /* -------------------------------------------------------------------------
82  * main
83  * ----------------------------------------------------------------------- */
84 int
85 main(int argc, char** argv) {
86  signal(SIGINT, signalHandler);
87  signal(SIGTERM, signalHandler);
88 #ifndef _MSC_VER
89  signal(SIGUSR1, signalHandler);
90  signal(SIGUSR2, signalHandler);
91 #endif
92 
94  // give some application descriptions
95  oc.setApplicationDescription("A microscopic, multi-modal traffic simulation.");
96  oc.setApplicationName("sumo", "Eclipse SUMO Version " VERSION_STRING);
97  gSimulation = true;
98  int ret = 0;
99  MSNet* net = nullptr;
100  try {
101  // initialise subsystems
102  XMLSubSys::init();
103  OptionsIO::setArgs(argc, argv);
104  // load the net
106  while (state == MSNet::SIMSTATE_LOADING) {
107  net = NLBuilder::init();
108  if (net != nullptr) {
109  state = net->simulate(string2time(oc.getString("begin")), string2time(oc.getString("end")));
110  delete net;
111  } else {
112  break;
113  }
114  }
115  } catch (const ProcessError& e) {
116  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
117  WRITE_ERROR(e.what());
118  }
119  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
120  // we need to delete the network explicitly to trigger the cleanup in the correct order
121  delete net;
122  ret = 1;
123 #ifndef _DEBUG
124  } catch (const std::exception& e) {
125  if (std::string(e.what()) != std::string("")) {
126  WRITE_ERROR(e.what());
127  }
128  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
129  ret = 1;
130  } catch (...) {
131  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
132  ret = 1;
133 #endif
134  }
137  return ret;
138 }
139 
140 
141 /****************************************************************************/
ToString.h
SystemFrame::close
static void close()
Closes all of an applications subsystems.
Definition: SystemFrame.cpp:131
NLBuilder.h
OptionsCont.h
MsgHandler.h
MSNet
The simulated network and simulation perfomer.
Definition: MSNet.h:92
OptionsCont::getString
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
Definition: OptionsCont.cpp:202
MsgHandler::inform
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:118
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
MSNet::simulate
SimulationState simulate(SUMOTime start, SUMOTime stop)
Simulates from timestep start to stop.
Definition: MSNet.cpp:334
TraCIServer::close
static void close()
request termination of connection
Definition: TraCIServer.cpp:302
gSimulation
bool gSimulation
Definition: StdDefs.cpp:30
MSNet::SimulationState
SimulationState
Possible states of a simulation - running or stopped with different reasons.
Definition: MSNet.h:97
OptionsCont::setApplicationName
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.
Definition: OptionsCont.cpp:485
SystemFrame.h
OutputDevice.h
ProcessError
Definition: UtilExceptions.h:40
MSNet::SIMSTATE_LOADING
The simulation is loading.
Definition: MSNet.h:99
UtilExceptions.h
NLBuilder::init
static MSNet * init()
Definition: NLBuilder.cpp:220
XMLSubSys::init
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:48
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:90
SIMSTEP
#define SIMSTEP
Definition: SUMOTime.h:63
signalHandler
void signalHandler(int signum)
Definition: sumo_main.cpp:53
string2time
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:42
OptionsCont::setApplicationDescription
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
Definition: OptionsCont.cpp:493
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:168
OptionsIO::setArgs
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:55
TraCIServer.h
MSNet::generateStatistics
const std::string generateStatistics(SUMOTime start)
Writes performance output and running vehicle stats.
Definition: MSNet.cpp:381
MSNet::hasInstance
static bool hasInstance()
Returns whether the network was already constructed.
Definition: MSNet.h:144
config.h
StdDefs.h
main
int main(int argc, char **argv)
Definition: sumo_main.cpp:85
MsgHandler::getErrorInstance
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:81
MSNet::interrupt
void interrupt()
Definition: MSNet.h:674
VERSION_STRING
#define VERSION_STRING
Definition: config.h:207
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:245
OptionsIO.h
XMLSubSys.h