Rev 1989 | 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" |
| 2107 | runge | 54 | #include "vm/plugin/JsPipe.h" |
| 1601 | runge | 55 | |
| 1660 | runge | 56 | #ifdef USE_PLUGIN_XORG |
| 57 | #include "vm/plugin/Xorg.h" |
||
| 58 | #endif // USE_PLUGIN_XORG |
||
| 59 | |||
| 1945 | runge | 60 | #ifdef USE_PLUGIN_MYSQL |
| 61 | #include "vm/plugin/MySql.h" |
||
| 62 | #endif // USE_PLUGIN_MYSQL |
||
| 63 | |||
| 1592 | runge | 64 | using namespace atom; |
| 1939 | runge | 65 | |
| 1961 | runge | 66 | logging::Logger LOG("Main"); // Deprecated |
| 67 | static const std::string log_module_ = "main"; |
||
| 1599 | runge | 68 | std::vector<broker::Subscriber::Pointer> subscribers; |
| 69 | boost::condition on_message_condition; |
||
| 70 | boost::mutex guard_mutex; |
||
| 1989 | runge | 71 | net::GetFile::Pointer downloader; |
| 1592 | runge | 72 | |
| 1989 | runge | 73 | void ContinueInitialization(std::string protocol_filename); |
| 1945 | runge | 74 | void Handler(int status); |
| 1598 | runge | 75 | void CleanUp(); |
| 1592 | runge | 76 | |
| 1989 | runge | 77 | |
| 78 | |||
| 79 | |||
| 80 | void HandleProtocolFileDownload(bool success, net::Url url, std::string temporary_filepath) |
||
| 81 | { |
||
| 82 | if (success) |
||
| 83 | { |
||
| 84 | log::Info(log_module_, "Successfully downloaded protocol file!"); |
||
| 85 | ContinueInitialization(temporary_filepath); |
||
| 86 | } |
||
| 87 | else |
||
| 88 | { |
||
| 89 | ContinueInitialization(url.GetRaw()); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 1945 | runge | 93 | int main(int argc, char **argv) |
| 1592 | runge | 94 | { |
| 1989 | runge | 95 | /* Set core dump parameters */ |
| 1956 | runge | 96 | rlimit core_limit = { RLIM_INFINITY, RLIM_INFINITY }; |
| 97 | setrlimit( RLIMIT_CORE, &core_limit ); // enable core dumps |
||
| 1989 | runge | 98 | |
| 99 | |||
| 100 | /* Print startup information */ |
||
| 1961 | runge | 101 | log::Info(log_module_, "Atom daemon, version %s starting...", VERSION); |
| 102 | log::Info(log_module_, "Released under %s.", LICENSE); |
||
| 103 | log::Info(log_module_, "Written by Mattias Runge 2010-2012."); |
||
| 1939 | runge | 104 | |
| 1989 | runge | 105 | |
| 106 | /* Register signal handlers */ |
||
| 1945 | runge | 107 | signal(SIGTERM, Handler); |
| 1961 | runge | 108 | signal(SIGINT, Handler); |
| 1945 | runge | 109 | signal(SIGQUIT, Handler); |
| 110 | signal(SIGABRT, Handler); |
||
| 111 | signal(SIGPIPE, Handler); |
||
| 1939 | runge | 112 | |
| 1989 | runge | 113 | |
| 114 | /* Check configuration */ |
||
| 1939 | runge | 115 | config::Manager::Create(); |
| 116 | |||
| 1945 | runge | 117 | if (!config::Manager::Instance()->Set(argc, argv)) |
| 1939 | runge | 118 | { |
| 119 | CleanUp(); |
||
| 120 | return EXIT_SUCCESS; |
||
| 121 | } |
||
| 122 | |||
| 1989 | runge | 123 | |
| 124 | /* Set up logging */ |
||
| 1945 | runge | 125 | if (config::Manager::Instance()->Exist("LogFile")) |
| 1939 | runge | 126 | { |
| 1964 | runge | 127 | log::OpenFile(config::Manager::Instance()->GetAsString("LogFile")); |
| 1939 | runge | 128 | } |
| 129 | |||
| 1961 | runge | 130 | if (config::Manager::Instance()->Exist("LogLevelMask")) |
| 1939 | runge | 131 | { |
| 1964 | runge | 132 | log::SetLevelByString(config::Manager::Instance()->GetAsString("LogLevelMask")); |
| 1961 | runge | 133 | } |
| 1939 | runge | 134 | |
| 1989 | runge | 135 | |
| 136 | /* Enter daemon mode if requested */ |
||
| 1945 | runge | 137 | if (config::Manager::Instance()->Exist("daemon")) |
| 1939 | runge | 138 | { |
| 1945 | runge | 139 | LOG.Info("Entering daemon mode..."); |
| 1939 | runge | 140 | |
| 1945 | runge | 141 | if (daemon(0, 0) == -1) |
| 1594 | runge | 142 | { |
| 1961 | runge | 143 | log::Error(log_module_, "Could not enter daemon mode. Exiting..."); |
| 1939 | runge | 144 | CleanUp(); |
| 145 | return EXIT_FAILURE; |
||
| 1594 | runge | 146 | } |
| 1939 | runge | 147 | |
| 1961 | runge | 148 | log::Info(log_module_, "Deamon mode entered successfully!"); |
| 1939 | runge | 149 | } |
| 150 | |||
| 1989 | runge | 151 | |
| 152 | /* Create singletons */ |
||
| 1939 | runge | 153 | storage::Manager::Create(); |
| 154 | timer::Manager::Create(); |
||
| 155 | broker::Manager::Create(); |
||
| 156 | net::Manager::Create(); |
||
| 157 | can::Protocol::Create(); |
||
| 158 | control::Manager::Create(); |
||
| 159 | vm::Manager::Create(); |
||
| 160 | |||
| 1989 | runge | 161 | |
| 162 | |||
| 163 | std::string protocol_filename = config::Manager::Instance()->GetAsString("ProtocolFile"); |
||
| 164 | |||
| 165 | try |
||
| 166 | { |
||
| 167 | net::Url url(protocol_filename); |
||
| 168 | |||
| 169 | downloader = net::GetFile::Pointer(new net::GetFile()); |
||
| 170 | |||
| 171 | log::Info(log_module_, "Will try to download protocol file from \"%s\"...", protocol_filename.data()); |
||
| 172 | |||
| 173 | downloader->Start(url, HandleProtocolFileDownload); |
||
| 174 | } |
||
| 175 | catch (std::exception& exception) |
||
| 176 | { |
||
| 177 | log::Debug(log_module_, "Could not parse ProtocolFile as a URL, \"%s\".", protocol_filename.data()); |
||
| 178 | ContinueInitialization(protocol_filename); |
||
| 179 | } |
||
| 180 | |||
| 181 | |||
| 182 | /* Lock main thread and wait for exit */ |
||
| 183 | boost::mutex::scoped_lock guard(guard_mutex); |
||
| 184 | |||
| 185 | on_message_condition.wait(guard); |
||
| 186 | |||
| 187 | |||
| 188 | /* Cleanup */ |
||
| 189 | log::Info(log_module_, "Cleaning up..."); |
||
| 190 | |||
| 191 | CleanUp(); |
||
| 192 | |||
| 193 | log::Info(log_module_, "Thank you for using Atom. Goodbye!"); |
||
| 194 | |||
| 195 | return EXIT_SUCCESS; |
||
| 196 | } |
||
| 197 | |||
| 198 | void ContinueInitialization(std::string protocol_filename) |
||
| 199 | { |
||
| 200 | /* Load protocol file */ |
||
| 201 | if (!can::Protocol::Instance()->Load(protocol_filename)) |
||
| 202 | { |
||
| 203 | log::Error(log_module_, "Failed to load %s!", protocol_filename.data()); |
||
| 204 | |||
| 205 | on_message_condition.notify_all(); |
||
| 206 | } |
||
| 207 | |||
| 208 | |||
| 209 | /* Set up storage path */ |
||
| 1945 | runge | 210 | storage::Manager::Instance()->SetRootPath(config::Manager::Instance()->GetAsString("StoragePath")); |
| 1939 | runge | 211 | |
| 1989 | runge | 212 | |
| 213 | /* Start monitor server */ |
||
| 1945 | runge | 214 | if (config::Manager::Instance()->Exist("MonitorPort")) |
| 1939 | runge | 215 | { |
| 1945 | runge | 216 | subscribers.push_back(can::Monitor::Pointer(new can::Monitor(config::Manager::Instance()->GetAsInt("MonitorPort")))); |
| 1939 | runge | 217 | } |
| 218 | |||
| 1989 | runge | 219 | |
| 220 | /* Start CanDaemon server */ |
||
| 1945 | runge | 221 | if (config::Manager::Instance()->Exist("DaemonPort")) |
| 1939 | runge | 222 | { |
| 1945 | runge | 223 | subscribers.push_back(can::CanDaemon::Pointer(new can::CanDaemon(config::Manager::Instance()->GetAsInt("DaemonPort")))); |
| 1939 | runge | 224 | } |
| 225 | |||
| 1989 | runge | 226 | |
| 227 | /* Subscribe Can control manager to data */ |
||
| 1945 | runge | 228 | subscribers.push_back(control::Manager::Instance()); |
| 1939 | runge | 229 | |
| 1989 | runge | 230 | |
| 231 | /* Start VM plugins */ |
||
| 1945 | runge | 232 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::System(vm::Manager::Instance()->GetIoService()))); |
| 233 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Console(vm::Manager::Instance()->GetIoService(), config::Manager::Instance()->GetAsInt("CommandPort")))); |
||
| 2107 | runge | 234 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::JsPipe(vm::Manager::Instance()->GetIoService(), config::Manager::Instance()->GetAsInt("JsPipePort")))); |
| 1945 | runge | 235 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Storage(vm::Manager::Instance()->GetIoService()))); |
| 236 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Timer(vm::Manager::Instance()->GetIoService()))); |
||
| 237 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Module(vm::Manager::Instance()->GetIoService()))); |
||
| 238 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Node(vm::Manager::Instance()->GetIoService()))); |
||
| 239 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Socket(vm::Manager::Instance()->GetIoService()))); |
||
| 1939 | runge | 240 | |
| 241 | #ifdef USE_PLUGIN_XORG |
||
| 1945 | runge | 242 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Xorg(vm::Manager::Instance()->GetIoService()))); |
| 1939 | runge | 243 | #endif // USE_PLUGIN_XORG |
| 1945 | runge | 244 | |
| 245 | #ifdef USE_PLUGIN_MYSQL |
||
| 246 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::MySql(vm::Manager::Instance()->GetIoService()))); |
||
| 247 | #endif // USE_PLUGIN_MYSQL |
||
| 1939 | runge | 248 | |
| 1989 | runge | 249 | |
| 250 | /* Start VM */ |
||
| 1945 | runge | 251 | vm::Manager::Instance()->Start(config::Manager::Instance()->GetAsString("ScriptPath"), config::Manager::Instance()->GetAsString("UserScriptPath")); |
| 1939 | runge | 252 | |
| 1989 | runge | 253 | |
| 254 | /* Connect to Can network */ |
||
| 1945 | runge | 255 | if (config::Manager::Instance()->Exist("CanNet")) |
| 1939 | runge | 256 | { |
| 1945 | runge | 257 | common::StringList cannetworks = config::Manager::Instance()->GetAsStringVector("CanNet"); |
| 1939 | runge | 258 | |
| 259 | for (uint n = 0; n < cannetworks.size(); n++) |
||
| 1599 | runge | 260 | { |
| 1945 | runge | 261 | subscribers.push_back(can::Network::Pointer(new can::Network(cannetworks[n]))); |
| 1599 | runge | 262 | } |
| 1939 | runge | 263 | } |
| 264 | } |
||
| 265 | |||
| 266 | void CleanUp() |
||
| 267 | { |
||
| 1989 | runge | 268 | downloader.reset(); |
| 269 | |||
| 1961 | runge | 270 | if (net::Manager::Instance().use_count() > 0) |
| 271 | { |
||
| 272 | net::Manager::Instance()->Stop(); |
||
| 273 | } |
||
| 1939 | runge | 274 | |
| 275 | subscribers.clear(); |
||
| 276 | config::Manager::Delete(); |
||
| 277 | timer::Manager::Delete(); |
||
| 278 | control::Manager::Delete(); |
||
| 279 | can::Protocol::Delete(); |
||
| 280 | broker::Manager::Delete(); |
||
| 281 | vm::Manager::Delete(); |
||
| 282 | net::Manager::Delete(); |
||
| 283 | storage::Manager::Delete(); |
||
| 1964 | runge | 284 | |
| 285 | log::CloseFile(); |
||
| 1939 | runge | 286 | } |
| 287 | |||
| 1945 | runge | 288 | void Handler(int status) |
| 1939 | runge | 289 | { |
| 290 | std::string signal_name = "Unknown"; |
||
| 291 | |||
| 292 | switch (status) |
||
| 293 | { |
||
| 294 | case SIGTERM: |
||
| 1599 | runge | 295 | { |
| 1939 | runge | 296 | signal_name = "Terminate"; |
| 297 | break; |
||
| 1599 | runge | 298 | } |
| 1939 | runge | 299 | |
| 300 | case SIGINT: |
||
| 1599 | runge | 301 | { |
| 1940 | runge | 302 | signal_name = "Interrupt"; |
| 1939 | runge | 303 | break; |
| 1599 | runge | 304 | } |
| 305 | |||
| 1939 | runge | 306 | case SIGQUIT: |
| 1594 | runge | 307 | { |
| 1939 | runge | 308 | signal_name = "Quit"; |
| 309 | break; |
||
| 1594 | runge | 310 | } |
| 1914 | linlun | 311 | |
| 1939 | runge | 312 | case SIGABRT: |
| 1914 | linlun | 313 | { |
| 1939 | runge | 314 | signal_name = "Abort"; |
| 315 | break; |
||
| 1914 | linlun | 316 | } |
| 1939 | runge | 317 | |
| 318 | case SIGIO: |
||
| 1594 | runge | 319 | { |
| 1939 | runge | 320 | signal_name = "I/O"; |
| 321 | break; |
||
| 1594 | runge | 322 | } |
| 1598 | runge | 323 | |
| 1939 | runge | 324 | case SIGPIPE: |
| 1599 | runge | 325 | { |
| 1939 | runge | 326 | signal_name = "Pipe"; |
| 327 | break; |
||
| 1599 | runge | 328 | } |
| 1939 | runge | 329 | } |
| 330 | |||
| 1961 | runge | 331 | log::Info(log_module_, "Received signal %s (%d).", signal_name.c_str(), status); |
| 1939 | runge | 332 | |
| 333 | if (status != SIGPIPE) |
||
| 334 | { |
||
| 335 | on_message_condition.notify_all(); |
||
| 336 | } |
||
| 1675 | arune | 337 | } |