openETCS
case study for the European Train Control System developed for the authors dissertation
DF/PS/SIM/Main.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010-2012
3  Johannes Feuser <feuser@uni-bremen.de>
4  This file is part of the openETCS library.
5 
6  The openETCS library is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  any later version.
10 
11  The openETCS library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with the openETCS library. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 
21 #include <QtCore>
22 #include <QtDBus>
23 #include <thread>
24 #include "Simulation.h"
25 #include "oETCS/DF/OdometerAdaptor.h"
26 #include "oETCS/DF/EmergencyBrakeAdaptor.h"
27 #include "oETCS/DF/ServiceBrakeAdaptor.h"
28 #include "oETCS/DF/BaliseDeviceInAdaptor.h"
29 #include "oETCS/DF/BaliseDeviceOutAdaptor.h"
30 
31 
32 
33 
34 void InputThread(QCoreApplication* pApplication)
35 {
36  char cInput(0);
37 
38 
39 
40  // wait for user pressing a key
41  ::std::cin.get(&cInput, 1);
42 
43  // tell application core to terminate
44  pApplication->exit(0);
45 
46 } // void InputThread(QCoreApplication* pApplication)
47 
48 
49 
50 
51 int main(int argc, char* argv[])
52 {
53  int iResult(1);
54  bool bConnected(true);
55  QCoreApplication Application(argc, argv);
56  QDBusConnection Connection = QDBusConnection::sessionBus();
59  ::oETCS::DF::PS::SIM::CServiceBrake ServiceBrake(&Odometer, &EmergencyBrake);
62  ::AOdometer* pOdometer(nullptr);
63  ::AEmergencyBrake* pEmergencyBrake(nullptr);
64  ::AServiceBrake* pServiceBrake(nullptr);
65  ::ABaliseDeviceIn* pBaliseIn(nullptr);
66  ::ABaliseDeviceOut* pBaliseOut(nullptr);
67  ::std::thread* pEvent(nullptr);
68 
69 
70 
71  try
72  {
73  // create all apdaptor objects
74  pOdometer = new ::AOdometer(&Odometer);
75  pEmergencyBrake = new ::AEmergencyBrake(&EmergencyBrake);
76  pServiceBrake = new ::AServiceBrake(&ServiceBrake);
77  pBaliseOut = new ::ABaliseDeviceOut(&BaliseOut);
78  pBaliseIn = new ::ABaliseDeviceIn(&BaliseIn);
79 
80 
81  // register all adaptors
82  bConnected &= Connection.registerService("oETCS.DF.IOdometer");
83  bConnected &= Connection.registerObject("/oETCS/Odometer", &Odometer);
84  bConnected &= Connection.registerService("oETCS.DF.IEmergencyBrake");
85  bConnected &= Connection.registerObject("/oETCS/EmergencyBrake", &EmergencyBrake);
86  bConnected &= Connection.registerService("oETCS.DF.IServiceBrake");
87  bConnected &= Connection.registerObject("/oETCS/ServiceBrake", &ServiceBrake);
88  bConnected &= Connection.registerService("oETCS.DF.IBaliseDeviceIn");
89  bConnected &= Connection.registerObject("/oETCS/BaliseDeviceIn", &BaliseIn);
90  bConnected &= Connection.registerService("oETCS.DF.IBaliseDeviceOut");
91  bConnected &= Connection.registerObject("/oETCS/BaliseDeviceOut", &BaliseOut);
92 
93 
94  // check, if all adaptors where registered successfully
95  if (not bConnected)
96  {
97  // print warning to stderr
98  ::std::cerr << "Could not register all adaptors!" << ::std::endl;
99 
100  } // if (not bConnected)
101 
102 
103  // print status to stdout
104  ::std::cout << "Executing adaptor event loop (<ENTER> to abort)..." << ::std::flush;
105 
106  // create input thread
107  pEvent = new ::std::thread(&::InputThread, &Application);
108 
109  // execute core application and get result after termination
110  iResult = Application.exec();
111 
112 
113  // check, if thread is joinable
114  if (pEvent->joinable())
115  {
116  // wait for thread to terminate
117  pEvent->join();
118 
119  } // if (pEvent->joinable())
120 
121 
122  // print status to stdout
123  ::std::cout << "terminated" << ::std::endl;
124 
125  } // try
126  catch (const ::std::bad_alloc& Exception)
127  {
128  // print error to stderr
129  ::std::cerr << "Could not allocate all objects!" << Connection.lastError().message().toStdString() << "Aborting..." << ::std::endl;
130 
131  } // catch (const ::std::bad_alloc& Exception)
132 
133  // delete all apdaptor objects
134  delete pServiceBrake;
135  delete pOdometer;
136  delete pEmergencyBrake;
137  delete pBaliseIn;
138  delete pBaliseOut;
139 
140 
141  // return execution result
142  return (iResult);
143 
144 } // int main(int argc, char* argv[]) char* argv[])

Copyright (C) 2010-2012 Johannes Feuser (feuser@uni-bremen.de)
The openETCS library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.
The openETCS library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with the openETCS library. If not, see "http://www.gnu.org/licenses/.