![]() |
User Manual, Developers Guide and API Documentation |
![]() |
00001 /******************************************************************************* 00002 * This file is part of openWNS (open Wireless Network Simulator) 00003 * _____________________________________________________________________________ 00004 * 00005 * Copyright (C) 2004-2007 00006 * Chair of Communication Networks (ComNets) 00007 * Kopernikusstr. 16, D-52074 Aachen, Germany 00008 * phone: ++49-241-80-27910, 00009 * fax: ++49-241-80-22242 00010 * email: info@openwns.org 00011 * www: http://www.openwns.org 00012 * _____________________________________________________________________________ 00013 * 00014 * openWNS is free software; you can redistribute it and/or modify it under the 00015 * terms of the GNU Lesser General Public License version 2 as published by the 00016 * Free Software Foundation; 00017 * 00018 * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY 00019 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 00020 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 00021 * details. 00022 * 00023 * You should have received a copy of the GNU Lesser General Public License 00024 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00025 * 00026 ******************************************************************************/ 00027 00028 #ifndef WNS_SIMULATOR_MAIN_HPP 00029 #define WNS_SIMULATOR_MAIN_HPP 00030 00031 #include <WNS/simulator/IApplication.hpp> 00032 #include <WNS/simulator/ISimulator.hpp> 00033 #include <WNS/logger/Master.hpp> 00034 00035 namespace wns { namespace simulator { 00036 00051 template <typename APPLICATION> 00052 class Main : 00053 public IApplication 00054 { 00055 public: 00056 typedef APPLICATION Application; 00057 00058 Main(); 00059 00060 private: 00064 //{@ 00065 virtual void 00066 doReadCommandLine(int argc, char* argv[]); 00067 00068 virtual void 00069 doInit(); 00070 00071 virtual void 00072 doRun(); 00073 00074 virtual void 00075 doShutdown(); 00076 00077 virtual int 00078 doStatus() const; 00080 00084 //{@ 00085 void 00086 handleWNSException(bool showLoggerBacktrace, const wns::Exception& exception) const; 00087 00088 void 00089 handleSTDException(bool showLoggerBacktrace, const std::exception& exception) const; 00090 00091 void 00092 handleAnyException(bool showLoggerBacktrace) const; 00094 00098 Application application_; 00099 }; 00100 00101 00102 template <typename APPLICATION> 00103 Main<APPLICATION>::Main() : 00104 IApplication(), 00105 application_() 00106 { 00107 } 00108 00109 00110 template <typename APPLICATION> 00111 void 00112 Main<APPLICATION>::doReadCommandLine(int argc, char* argv[]) 00113 { 00114 try 00115 { 00116 return application_.readCommandLine(argc, argv); 00117 } 00118 catch (const wns::Exception& exception) 00119 { 00120 handleWNSException(false, exception); 00121 } 00122 catch (const std::exception& exception) 00123 { 00124 handleSTDException(false, exception); 00125 } 00126 catch (...) 00127 { 00128 handleAnyException(false); 00129 } 00130 } 00131 00132 template <typename APPLICATION> 00133 void 00134 Main<APPLICATION>::doInit() 00135 { 00136 try 00137 { 00138 return application_.init(); 00139 } 00140 catch (const wns::Exception& exception) 00141 { 00142 handleWNSException(false, exception); 00143 } 00144 catch (const std::exception& exception) 00145 { 00146 handleSTDException(false, exception); 00147 } 00148 catch (...) 00149 { 00150 handleAnyException(false); 00151 } 00152 } 00153 00154 template <typename APPLICATION> 00155 void 00156 Main<APPLICATION>::doRun() 00157 { 00158 try 00159 { 00160 return application_.run(); 00161 } 00162 catch (const wns::Exception& exception) 00163 { 00164 handleWNSException(true, exception); 00165 } 00166 catch (const std::exception& exception) 00167 { 00168 handleSTDException(true, exception); 00169 } 00170 catch (...) 00171 { 00172 handleAnyException(true); 00173 } 00174 } 00175 00176 template <typename APPLICATION> 00177 void 00178 Main<APPLICATION>::doShutdown() 00179 { 00180 try 00181 { 00182 return application_.shutdown(); 00183 } 00184 catch (const wns::Exception& exception) 00185 { 00186 handleWNSException(false, exception); 00187 } 00188 catch (const std::exception& exception) 00189 { 00190 handleSTDException(false, exception); 00191 } 00192 catch (...) 00193 { 00194 handleAnyException(false); 00195 } 00196 } 00197 00198 template <typename APPLICATION> 00199 int 00200 Main<APPLICATION>::doStatus() const 00201 { 00202 try 00203 { 00204 return application_.status(); 00205 } 00206 catch (const wns::Exception& exception) 00207 { 00208 handleWNSException(false, exception); 00209 } 00210 catch (const std::exception& exception) 00211 { 00212 handleSTDException(false, exception); 00213 } 00214 catch (...) 00215 { 00216 handleAnyException(false); 00217 } 00218 // if reach this something went wrong 00219 return 1; 00220 } 00221 00222 00223 template <typename APPLICATION> 00224 void 00225 Main<APPLICATION>::handleWNSException(bool showLoggerBacktrace, const wns::Exception& exception) const 00226 { 00227 if (showLoggerBacktrace) 00228 { 00229 wns::simulator::getMasterLogger()->outputBacktrace(); 00230 } 00231 std::stringstream message; 00232 message << exception.getBacktrace() 00233 << "openWNS: Caught " << wns::TypeInfo::create(exception) << ":\n\n" 00234 << exception.what(); 00235 std::cerr << message.str() << "\n\n"; 00236 exit(1); 00237 } 00238 00239 00240 template <typename APPLICATION> 00241 void 00242 Main<APPLICATION>::handleSTDException(bool showLoggerBacktrace, const std::exception& exception) const 00243 { 00244 if (showLoggerBacktrace) 00245 { 00246 wns::simulator::getMasterLogger()->outputBacktrace(); 00247 } 00248 std::stringstream message; 00249 message << "openWNS: Caught " << wns::TypeInfo::create(exception) << ":\n\n" 00250 << exception.what(); 00251 std::cerr << message.str() << "\n\n"; 00252 exit(1); 00253 } 00254 00255 00256 template <typename APPLICATION> 00257 void 00258 Main<APPLICATION>::handleAnyException(bool showLoggerBacktrace) const 00259 { 00260 if (showLoggerBacktrace) 00261 { 00262 wns::simulator::getMasterLogger()->outputBacktrace(); 00263 } 00264 std::cerr << "openWNS: An unknown exception occurred.\n"; 00265 exit(1); 00266 } 00267 00268 } // simulator 00269 } // wns 00270 00271 #endif // NOT defined WNS_SIMULATOR_MAIN_HPP
1.5.5