Rev 1964 | Rev 2107 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1598 | runge | 1 | /* |
| 1939 | runge | 2 | * |
| 1945 | runge | 3 | * Copyright(C) 2010 Mattias Runge |
| 1939 | runge | 4 | * |
| 1598 | runge | 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
||
| 7 | * the Free Software Foundation; either version 2 of the License, or |
||
| 1945 | runge | 8 | * (at your option) any later version. |
| 1939 | runge | 9 | * |
| 1598 | runge | 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 13 | * GNU General Public License for more details. |
||
| 1939 | runge | 14 | * |
| 1598 | runge | 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
||
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
||
| 1939 | runge | 18 | * |
| 1598 | runge | 19 | */ |
| 1592 | runge | 20 | |
| 1599 | runge | 21 | #include <signal.h> |
| 1956 | runge | 22 | #include <sys/resource.h> |
| 1599 | runge | 23 | |
| 1594 | runge | 24 | #include <boost/program_options.hpp> |
| 1599 | runge | 25 | #include <boost/thread/mutex.hpp> |
| 26 | #include <boost/thread/condition.hpp> |
||
| 27 | #include <boost/thread/locks.hpp> |
||
| 1592 | runge | 28 | |
| 1652 | runge | 29 | #include "config.h" |
| 30 | |||
| 1961 | runge | 31 | #include "logging/Logger.h" // Deprecated |
| 32 | #include "common/log.h" |
||
| 1592 | runge | 33 | #include "net/Manager.h" |
| 1593 | runge | 34 | #include "timer/Manager.h" |
| 1594 | runge | 35 | #include "config/Manager.h" |
| 1599 | runge | 36 | #include "can/Protocol.h" |
| 37 | #include "broker/Manager.h" |
||
| 1642 | runge | 38 | #include "control/Manager.h" |
| 1596 | runge | 39 | #include "can/Network.h" |
| 40 | #include "can/Monitor.h" |
||
| 1914 | linlun | 41 | #include "can/CanDaemon.h" |
| 1601 | runge | 42 | #include "vm/Manager.h" |
| 1619 | runge | 43 | #include "storage/Manager.h" |
| 1989 | runge | 44 | #include "net/GetFile.h" |
| 45 | #include "net/Url.h" |
||
| 1596 | runge | 46 | |
| 1601 | runge | 47 | #include "vm/plugin/System.h" |
| 48 | #include "vm/plugin/Timer.h" |
||
| 1602 | runge | 49 | #include "vm/plugin/Module.h" |
| 1657 | runge | 50 | #include "vm/plugin/Node.h" |
| 1608 | runge | 51 | #include "vm/plugin/Console.h" |
| 1619 | runge | 52 | #include "vm/plugin/Storage.h" |
| 1651 | runge | 53 | #include "vm/plugin/Socket.h" |
| 1601 | runge | 54 | |
| 1660 | runge | 55 | #ifdef USE_PLUGIN_XORG |
| 56 | #include "vm/plugin/Xorg.h" |
||
| 57 | #endif // USE_PLUGIN_XORG |
||
| 58 | |||
| 1945 | runge | 59 | #ifdef USE_PLUGIN_MYSQL |
| 60 | #include "vm/plugin/MySql.h" |
||
| 61 | #endif // USE_PLUGIN_MYSQL |
||
| 62 | |||
| 1592 | runge | 63 | using namespace atom; |
| 1939 | runge | 64 | |
| 1961 | runge | 65 | logging::Logger LOG("Main"); // Deprecated |
| 66 | static const std::string log_module_ = "main"; |
||
| 1599 | runge | 67 | std::vector<broker::Subscriber::Pointer> subscribers; |
| 68 | boost::condition on_message_condition; |
||
| 69 | boost::mutex guard_mutex; |
||
| 1989 | runge | 70 | net::GetFile::Pointer downloader; |
| 1592 | runge | 71 | |
| 1989 | runge | 72 | void ContinueInitialization(std::string protocol_filename); |
| 1945 | runge | 73 | void Handler(int status); |
| 1598 | runge | 74 | void CleanUp(); |
| 1592 | runge | 75 | |
| 1989 | runge | 76 | |
| 77 | |||
| 78 | |||
| 79 | void HandleProtocolFileDownload(bool success, net::Url url, std::string temporary_filepath) |
||
| 80 | { |
||
| 81 | if (success) |
||
| 82 | { |
||
| 83 | log::Info(log_module_, "Successfully downloaded protocol file!"); |
||
| 84 | ContinueInitialization(temporary_filepath); |
||
| 85 | } |
||
| 86 | else |
||
| 87 | { |
||
| 88 | ContinueInitialization(url.GetRaw()); |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 1945 | runge | 92 | int main(int argc, char **argv) |
| 1592 | runge | 93 | { |
| 1989 | runge | 94 | /* Set core dump parameters */ |
| 1956 | runge | 95 | rlimit core_limit = { RLIM_INFINITY, RLIM_INFINITY }; |
| 96 | setrlimit( RLIMIT_CORE, &core_limit ); // enable core dumps |
||
| 1989 | runge | 97 | |
| 98 | |||
| 99 | /* Print startup information */ |
||
| 1961 | runge | 100 | log::Info(log_module_, "Atom daemon, version %s starting...", VERSION); |
| 101 | log::Info(log_module_, "Released under %s.", LICENSE); |
||
| 102 | log::Info(log_module_, "Written by Mattias Runge 2010-2012."); |
||
| 1939 | runge | 103 | |
| 1989 | runge | 104 | |
| 105 | /* Register signal handlers */ |
||
| 1945 | runge | 106 | signal(SIGTERM, Handler); |
| 1961 | runge | 107 | signal(SIGINT, Handler); |
| 1945 | runge | 108 | signal(SIGQUIT, Handler); |
| 109 | signal(SIGABRT, Handler); |
||
| 110 | signal(SIGPIPE, Handler); |
||
| 1939 | runge | 111 | |
| 1989 | runge | 112 | |
| 113 | /* Check configuration */ |
||
| 1939 | runge | 114 | config::Manager::Create(); |
| 115 | |||
| 1945 | runge | 116 | if (!config::Manager::Instance()->Set(argc, argv)) |
| 1939 | runge | 117 | { |
| 118 | CleanUp(); |
||
| 119 | return EXIT_SUCCESS; |
||
| 120 | } |
||
| 121 | |||
| 1989 | runge | 122 | |
| 123 | /* Set up logging */ |
||
| 1945 | runge | 124 | if (config::Manager::Instance()->Exist("LogFile")) |
| 1939 | runge | 125 | { |
| 1964 | runge | 126 | log::OpenFile(config::Manager::Instance()->GetAsString("LogFile")); |
| 1939 | runge | 127 | } |
| 128 | |||
| 1961 | runge | 129 | if (config::Manager::Instance()->Exist("LogLevelMask")) |
| 1939 | runge | 130 | { |
| 1964 | runge | 131 | log::SetLevelByString(config::Manager::Instance()->GetAsString("LogLevelMask")); |
| 1961 | runge | 132 | } |
| 1939 | runge | 133 | |
| 1989 | runge | 134 | |
| 135 | /* Enter daemon mode if requested */ |
||
| 1945 | runge | 136 | if (config::Manager::Instance()->Exist("daemon")) |
| 1939 | runge | 137 | { |
| 1945 | runge | 138 | LOG.Info("Entering daemon mode..."); |
| 1939 | runge | 139 | |
| 1945 | runge | 140 | if (daemon(0, 0) == -1) |
| 1594 | runge | 141 | { |
| 1961 | runge | 142 | log::Error(log_module_, "Could not enter daemon mode. Exiting..."); |
| 1939 | runge | 143 | CleanUp(); |
| 144 | return EXIT_FAILURE; |
||
| 1594 | runge | 145 | } |
| 1939 | runge | 146 | |
| 1961 | runge | 147 | log::Info(log_module_, "Deamon mode entered successfully!"); |
| 1939 | runge | 148 | } |
| 149 | |||
| 1989 | runge | 150 | |
| 151 | /* Create singletons */ |
||
| 1939 | runge | 152 | storage::Manager::Create(); |
| 153 | timer::Manager::Create(); |
||
| 154 | broker::Manager::Create(); |
||
| 155 | net::Manager::Create(); |
||
| 156 | can::Protocol::Create(); |
||
| 157 | control::Manager::Create(); |
||
| 158 | vm::Manager::Create(); |
||
| 159 | |||
| 1989 | runge | 160 | |
| 161 | |||
| 162 | std::string protocol_filename = config::Manager::Instance()->GetAsString("ProtocolFile"); |
||
| 163 | |||
| 164 | try |
||
| 165 | { |
||
| 166 | net::Url url(protocol_filename); |
||
| 167 | |||
| 168 | downloader = net::GetFile::Pointer(new net::GetFile()); |
||
| 169 | |||
| 170 | log::Info(log_module_, "Will try to download protocol file from \"%s\"...", protocol_filename.data()); |
||
| 171 | |||
| 172 | downloader->Start(url, HandleProtocolFileDownload); |
||
| 173 | } |
||
| 174 | catch (std::exception& exception) |
||
| 175 | { |
||
| 176 | log::Debug(log_module_, "Could not parse ProtocolFile as a URL, \"%s\".", protocol_filename.data()); |
||
| 177 | ContinueInitialization(protocol_filename); |
||
| 178 | } |
||
| 179 | |||
| 180 | |||
| 181 | /* Lock main thread and wait for exit */ |
||
| 182 | boost::mutex::scoped_lock guard(guard_mutex); |
||
| 183 | |||
| 184 | on_message_condition.wait(guard); |
||
| 185 | |||
| 186 | |||
| 187 | /* Cleanup */ |
||
| 188 | log::Info(log_module_, "Cleaning up..."); |
||
| 189 | |||
| 190 | CleanUp(); |
||
| 191 | |||
| 192 | log::Info(log_module_, "Thank you for using Atom. Goodbye!"); |
||
| 193 | |||
| 194 | return EXIT_SUCCESS; |
||
| 195 | } |
||
| 196 | |||
| 197 | void ContinueInitialization(std::string protocol_filename) |
||
| 198 | { |
||
| 199 | /* Load protocol file */ |
||
| 200 | if (!can::Protocol::Instance()->Load(protocol_filename)) |
||
| 201 | { |
||
| 202 | log::Error(log_module_, "Failed to load %s!", protocol_filename.data()); |
||
| 203 | |||
| 204 | on_message_condition.notify_all(); |
||
| 205 | } |
||
| 206 | |||
| 207 | |||
| 208 | /* Set up storage path */ |
||
| 1945 | runge | 209 | storage::Manager::Instance()->SetRootPath(config::Manager::Instance()->GetAsString("StoragePath")); |
| 1939 | runge | 210 | |
| 1989 | runge | 211 | |
| 212 | /* Start monitor server */ |
||
| 1945 | runge | 213 | if (config::Manager::Instance()->Exist("MonitorPort")) |
| 1939 | runge | 214 | { |
| 1945 | runge | 215 | subscribers.push_back(can::Monitor::Pointer(new can::Monitor(config::Manager::Instance()->GetAsInt("MonitorPort")))); |
| 1939 | runge | 216 | } |
| 217 | |||
| 1989 | runge | 218 | |
| 219 | /* Start CanDaemon server */ |
||
| 1945 | runge | 220 | if (config::Manager::Instance()->Exist("DaemonPort")) |
| 1939 | runge | 221 | { |
| 1945 | runge | 222 | subscribers.push_back(can::CanDaemon::Pointer(new can::CanDaemon(config::Manager::Instance()->GetAsInt("DaemonPort")))); |
| 1939 | runge | 223 | } |
| 224 | |||
| 1989 | runge | 225 | |
| 226 | /* Subscribe Can control manager to data */ |
||
| 1945 | runge | 227 | subscribers.push_back(control::Manager::Instance()); |
| 1939 | runge | 228 | |
| 1989 | runge | 229 | |
| 230 | /* Start VM plugins */ |
||
| 1945 | runge | 231 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::System(vm::Manager::Instance()->GetIoService()))); |
| 232 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Console(vm::Manager::Instance()->GetIoService(), config::Manager::Instance()->GetAsInt("CommandPort")))); |
||
| 233 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Storage(vm::Manager::Instance()->GetIoService()))); |
||
| 234 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Timer(vm::Manager::Instance()->GetIoService()))); |
||
| 235 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Module(vm::Manager::Instance()->GetIoService()))); |
||
| 236 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Node(vm::Manager::Instance()->GetIoService()))); |
||
| 237 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Socket(vm::Manager::Instance()->GetIoService()))); |
||
| 1939 | runge | 238 | |
| 239 | #ifdef USE_PLUGIN_XORG |
||
| 1945 | runge | 240 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Xorg(vm::Manager::Instance()->GetIoService()))); |
| 1939 | runge | 241 | #endif // USE_PLUGIN_XORG |
| 1945 | runge | 242 | |
| 243 | #ifdef USE_PLUGIN_MYSQL |
||
| 244 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::MySql(vm::Manager::Instance()->GetIoService()))); |
||
| 245 | #endif // USE_PLUGIN_MYSQL |
||
| 1939 | runge | 246 | |
| 1989 | runge | 247 | |
| 248 | /* Start VM */ |
||
| 1945 | runge | 249 | vm::Manager::Instance()->Start(config::Manager::Instance()->GetAsString("ScriptPath"), config::Manager::Instance()->GetAsString("UserScriptPath")); |
| 1939 | runge | 250 | |
| 1989 | runge | 251 | |
| 252 | /* Connect to Can network */ |
||
| 1945 | runge | 253 | if (config::Manager::Instance()->Exist("CanNet")) |
| 1939 | runge | 254 | { |
| 1945 | runge | 255 | common::StringList cannetworks = config::Manager::Instance()->GetAsStringVector("CanNet"); |
| 1939 | runge | 256 | |
| 257 | for (uint n = 0; n < cannetworks.size(); n++) |
||
| 1599 | runge | 258 | { |
| 1945 | runge | 259 | subscribers.push_back(can::Network::Pointer(new can::Network(cannetworks[n]))); |
| 1599 | runge | 260 | } |
| 1939 | runge | 261 | } |
| 262 | } |
||
| 263 | |||
| 264 | void CleanUp() |
||
| 265 | { |
||
| 1989 | runge | 266 | downloader.reset(); |
| 267 | |||
| 1961 | runge | 268 | if (net::Manager::Instance().use_count() > 0) |
| 269 | { |
||
| 270 | net::Manager::Instance()->Stop(); |
||
| 271 | } |
||
| 1939 | runge | 272 | |
| 273 | subscribers.clear(); |
||
| 274 | config::Manager::Delete(); |
||
| 275 | timer::Manager::Delete(); |
||
| 276 | control::Manager::Delete(); |
||
| 277 | can::Protocol::Delete(); |
||
| 278 | broker::Manager::Delete(); |
||
| 279 | vm::Manager::Delete(); |
||
| 280 | net::Manager::Delete(); |
||
| 281 | storage::Manager::Delete(); |
||
| 1964 | runge | 282 | |
| 283 | log::CloseFile(); |
||
| 1939 | runge | 284 | } |
| 285 | |||
| 1945 | runge | 286 | void Handler(int status) |
| 1939 | runge | 287 | { |
| 288 | std::string signal_name = "Unknown"; |
||
| 289 | |||
| 290 | switch (status) |
||
| 291 | { |
||
| 292 | case SIGTERM: |
||
| 1599 | runge | 293 | { |
| 1939 | runge | 294 | signal_name = "Terminate"; |
| 295 | break; |
||
| 1599 | runge | 296 | } |
| 1939 | runge | 297 | |
| 298 | case SIGINT: |
||
| 1599 | runge | 299 | { |
| 1940 | runge | 300 | signal_name = "Interrupt"; |
| 1939 | runge | 301 | break; |
| 1599 | runge | 302 | } |
| 303 | |||
| 1939 | runge | 304 | case SIGQUIT: |
| 1594 | runge | 305 | { |
| 1939 | runge | 306 | signal_name = "Quit"; |
| 307 | break; |
||
| 1594 | runge | 308 | } |
| 1914 | linlun | 309 | |
| 1939 | runge | 310 | case SIGABRT: |
| 1914 | linlun | 311 | { |
| 1939 | runge | 312 | signal_name = "Abort"; |
| 313 | break; |
||
| 1914 | linlun | 314 | } |
| 1939 | runge | 315 | |
| 316 | case SIGIO: |
||
| 1594 | runge | 317 | { |
| 1939 | runge | 318 | signal_name = "I/O"; |
| 319 | break; |
||
| 1594 | runge | 320 | } |
| 1598 | runge | 321 | |
| 1939 | runge | 322 | case SIGPIPE: |
| 1599 | runge | 323 | { |
| 1939 | runge | 324 | signal_name = "Pipe"; |
| 325 | break; |
||
| 1599 | runge | 326 | } |
| 1939 | runge | 327 | } |
| 328 | |||
| 1961 | runge | 329 | log::Info(log_module_, "Received signal %s (%d).", signal_name.c_str(), status); |
| 1939 | runge | 330 | |
| 331 | if (status != SIGPIPE) |
||
| 332 | { |
||
| 333 | on_message_condition.notify_all(); |
||
| 334 | } |
||
| 1675 | arune | 335 | } |