Rev 1939 | Rev 1945 | 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 | * |
| 1598 | 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 |
||
| 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> |
| 22 | |||
| 1594 | runge | 23 | #include <boost/program_options.hpp> |
| 1599 | runge | 24 | #include <boost/thread/mutex.hpp> |
| 25 | #include <boost/thread/condition.hpp> |
||
| 26 | #include <boost/thread/locks.hpp> |
||
| 1592 | runge | 27 | |
| 1652 | runge | 28 | #include "config.h" |
| 29 | |||
| 1599 | runge | 30 | #include "logging/Logger.h" |
| 1592 | runge | 31 | #include "net/Manager.h" |
| 1593 | runge | 32 | #include "timer/Manager.h" |
| 1594 | runge | 33 | #include "config/Manager.h" |
| 1599 | runge | 34 | #include "can/Protocol.h" |
| 35 | #include "broker/Manager.h" |
||
| 1642 | runge | 36 | #include "control/Manager.h" |
| 1596 | runge | 37 | #include "can/Network.h" |
| 38 | #include "can/Monitor.h" |
||
| 1914 | linlun | 39 | #include "can/CanDaemon.h" |
| 1601 | runge | 40 | #include "vm/Manager.h" |
| 1619 | runge | 41 | #include "storage/Manager.h" |
| 1939 | runge | 42 | #include "mbb/Manager.h" |
| 1596 | runge | 43 | |
| 1601 | runge | 44 | #include "vm/plugin/System.h" |
| 45 | #include "vm/plugin/Timer.h" |
||
| 1602 | runge | 46 | #include "vm/plugin/Module.h" |
| 1657 | runge | 47 | #include "vm/plugin/Node.h" |
| 1608 | runge | 48 | #include "vm/plugin/Console.h" |
| 1619 | runge | 49 | #include "vm/plugin/Storage.h" |
| 1651 | runge | 50 | #include "vm/plugin/Socket.h" |
| 1601 | runge | 51 | |
| 1660 | runge | 52 | #ifdef USE_PLUGIN_XORG |
| 53 | #include "vm/plugin/Xorg.h" |
||
| 54 | #endif // USE_PLUGIN_XORG |
||
| 55 | |||
| 1592 | runge | 56 | using namespace atom; |
| 1939 | runge | 57 | |
| 58 | logging::Logger LOG ("Main"); |
||
| 1599 | runge | 59 | std::vector<broker::Subscriber::Pointer> subscribers; |
| 60 | boost::condition on_message_condition; |
||
| 61 | boost::mutex guard_mutex; |
||
| 1592 | runge | 62 | |
| 1939 | runge | 63 | void Handler (int status); |
| 1598 | runge | 64 | void CleanUp(); |
| 1592 | runge | 65 | |
| 1939 | runge | 66 | int main (int argc, char **argv) |
| 1592 | runge | 67 | { |
| 1939 | runge | 68 | LOG.Info ("\033[29;1mAtom Daemon, version " + std::string (VERSION) + " starting...\033[0m"); |
| 69 | LOG.Info ("\033[29;1mReleased under " + std::string (LICENSE) + ".\033[0m"); |
||
| 70 | LOG.Info ("Written by Mattias Runge 2010-2012."); |
||
| 71 | |||
| 72 | signal (SIGTERM, Handler); |
||
| 73 | signal (SIGINT, Handler); |
||
| 74 | signal (SIGQUIT, Handler); |
||
| 75 | signal (SIGABRT, Handler); |
||
| 76 | signal (SIGPIPE, Handler); |
||
| 77 | |||
| 78 | config::Manager::Create(); |
||
| 79 | |||
| 80 | if (!config::Manager::Instance()->Set (argc, argv)) |
||
| 81 | { |
||
| 82 | CleanUp(); |
||
| 83 | return EXIT_SUCCESS; |
||
| 84 | } |
||
| 85 | |||
| 86 | if (config::Manager::Instance()->Exist ("LogFile")) |
||
| 87 | { |
||
| 88 | logging::Logger::OpenFile (config::Manager::Instance()->GetAsString ("LogFile")); |
||
| 89 | } |
||
| 90 | |||
| 91 | if (config::Manager::Instance()->Exist ("LogLevel")) |
||
| 92 | { |
||
| 93 | logging::Logger::SetLevel ( (logging::Logger::Level) config::Manager::Instance()->GetAsInt ("LogLevel")); |
||
| 94 | } |
||
| 95 | |||
| 96 | if (config::Manager::Instance()->Exist ("daemon")) |
||
| 97 | { |
||
| 98 | LOG.Info ("Entering daemon mode..."); |
||
| 99 | |||
| 100 | if (daemon (0, 0) == -1) |
||
| 1594 | runge | 101 | { |
| 1939 | runge | 102 | LOG.Error ("Could not enter daemon mode. Exiting..."); |
| 103 | CleanUp(); |
||
| 104 | return EXIT_FAILURE; |
||
| 1594 | runge | 105 | } |
| 1939 | runge | 106 | |
| 107 | LOG.Info ("Deamon mode entered successfully!"); |
||
| 108 | } |
||
| 109 | |||
| 110 | storage::Manager::Create(); |
||
| 111 | |||
| 112 | timer::Manager::Create(); |
||
| 113 | broker::Manager::Create(); |
||
| 114 | net::Manager::Create(); |
||
| 115 | can::Protocol::Create(); |
||
| 116 | control::Manager::Create(); |
||
| 117 | vm::Manager::Create(); |
||
| 118 | |||
| 119 | if (config::Manager::Instance()->Exist ("MbbPort")) |
||
| 120 | { |
||
| 121 | mbb::Manager::Create (config::Manager::Instance()->GetAsInt ("MbbPort")); |
||
| 122 | } |
||
| 123 | |||
| 124 | storage::Manager::Instance()->SetRootPath (config::Manager::Instance()->GetAsString ("StoragePath")); |
||
| 125 | |||
| 126 | can::Protocol::Instance()->Load (config::Manager::Instance()->GetAsString ("ProtocolFile")); |
||
| 127 | |||
| 128 | if (config::Manager::Instance()->Exist ("MonitorPort")) |
||
| 129 | { |
||
| 130 | subscribers.push_back (can::Monitor::Pointer (new can::Monitor (config::Manager::Instance()->GetAsInt ("MonitorPort")))); |
||
| 131 | } |
||
| 132 | |||
| 133 | if (config::Manager::Instance()->Exist ("DaemonPort")) |
||
| 134 | { |
||
| 135 | subscribers.push_back (can::CanDaemon::Pointer (new can::CanDaemon (config::Manager::Instance()->GetAsInt ("DaemonPort")))); |
||
| 136 | } |
||
| 137 | |||
| 138 | subscribers.push_back (control::Manager::Instance()); |
||
| 139 | |||
| 140 | vm::Manager::Instance()->AddPlugin (vm::Plugin::Pointer (new vm::plugin::System (vm::Manager::Instance()->GetIoService()))); |
||
| 141 | vm::Manager::Instance()->AddPlugin (vm::Plugin::Pointer (new vm::plugin::Console (vm::Manager::Instance()->GetIoService(), config::Manager::Instance()->GetAsInt ("CommandPort")))); |
||
| 142 | vm::Manager::Instance()->AddPlugin (vm::Plugin::Pointer (new vm::plugin::Storage (vm::Manager::Instance()->GetIoService()))); |
||
| 143 | vm::Manager::Instance()->AddPlugin (vm::Plugin::Pointer (new vm::plugin::Timer (vm::Manager::Instance()->GetIoService()))); |
||
| 144 | vm::Manager::Instance()->AddPlugin (vm::Plugin::Pointer (new vm::plugin::Module (vm::Manager::Instance()->GetIoService()))); |
||
| 145 | vm::Manager::Instance()->AddPlugin (vm::Plugin::Pointer (new vm::plugin::Node (vm::Manager::Instance()->GetIoService()))); |
||
| 146 | vm::Manager::Instance()->AddPlugin (vm::Plugin::Pointer (new vm::plugin::Socket (vm::Manager::Instance()->GetIoService()))); |
||
| 147 | |||
| 148 | #ifdef USE_PLUGIN_XORG |
||
| 149 | vm::Manager::Instance()->AddPlugin (vm::Plugin::Pointer (new vm::plugin::Xorg (vm::Manager::Instance()->GetIoService()))); |
||
| 150 | #endif // USE_PLUGIN_XORG |
||
| 151 | |||
| 152 | vm::Manager::Instance()->Start (config::Manager::Instance()->GetAsString ("ScriptPath"), config::Manager::Instance()->GetAsString ("UserScriptPath")); |
||
| 153 | |||
| 154 | if (config::Manager::Instance()->Exist ("CanNet")) |
||
| 155 | { |
||
| 156 | common::StringList cannetworks = config::Manager::Instance()->GetAsStringVector ("CanNet"); |
||
| 157 | |||
| 158 | for (uint n = 0; n < cannetworks.size(); n++) |
||
| 1599 | runge | 159 | { |
| 1939 | runge | 160 | subscribers.push_back (can::Network::Pointer (new can::Network (cannetworks[n]))); |
| 1599 | runge | 161 | } |
| 1939 | runge | 162 | } |
| 163 | |||
| 164 | boost::mutex::scoped_lock guard (guard_mutex); |
||
| 165 | |||
| 166 | on_message_condition.wait (guard); |
||
| 167 | |||
| 168 | LOG.Info ("Cleaning up..."); |
||
| 169 | |||
| 170 | CleanUp(); |
||
| 171 | |||
| 172 | LOG.Info ("Thank you for using Atom. Goodbye!"); |
||
| 173 | |||
| 174 | return EXIT_SUCCESS; |
||
| 175 | } |
||
| 176 | |||
| 177 | void CleanUp() |
||
| 178 | { |
||
| 179 | net::Manager::Instance()->Stop(); |
||
| 180 | |||
| 181 | subscribers.clear(); |
||
| 182 | config::Manager::Delete(); |
||
| 183 | timer::Manager::Delete(); |
||
| 184 | control::Manager::Delete(); |
||
| 185 | mbb::Manager::Delete(); |
||
| 186 | can::Protocol::Delete(); |
||
| 187 | broker::Manager::Delete(); |
||
| 188 | vm::Manager::Delete(); |
||
| 189 | net::Manager::Delete(); |
||
| 190 | storage::Manager::Delete(); |
||
| 191 | } |
||
| 192 | |||
| 193 | void Handler (int status) |
||
| 194 | { |
||
| 195 | std::string signal_name = "Unknown"; |
||
| 196 | |||
| 197 | switch (status) |
||
| 198 | { |
||
| 199 | case SIGTERM: |
||
| 1599 | runge | 200 | { |
| 1939 | runge | 201 | signal_name = "Terminate"; |
| 202 | break; |
||
| 1599 | runge | 203 | } |
| 1939 | runge | 204 | |
| 205 | case SIGINT: |
||
| 1599 | runge | 206 | { |
| 1940 | runge | 207 | signal_name = "Interrupt"; |
| 1939 | runge | 208 | break; |
| 1599 | runge | 209 | } |
| 210 | |||
| 1939 | runge | 211 | case SIGQUIT: |
| 1594 | runge | 212 | { |
| 1939 | runge | 213 | signal_name = "Quit"; |
| 214 | break; |
||
| 1594 | runge | 215 | } |
| 1914 | linlun | 216 | |
| 1939 | runge | 217 | case SIGABRT: |
| 1914 | linlun | 218 | { |
| 1939 | runge | 219 | signal_name = "Abort"; |
| 220 | break; |
||
| 1914 | linlun | 221 | } |
| 1939 | runge | 222 | |
| 223 | case SIGIO: |
||
| 1594 | runge | 224 | { |
| 1939 | runge | 225 | signal_name = "I/O"; |
| 226 | break; |
||
| 1594 | runge | 227 | } |
| 1598 | runge | 228 | |
| 1939 | runge | 229 | case SIGPIPE: |
| 1599 | runge | 230 | { |
| 1939 | runge | 231 | signal_name = "Pipe"; |
| 232 | break; |
||
| 1599 | runge | 233 | } |
| 1939 | runge | 234 | } |
| 235 | |||
| 236 | LOG.Debug ("Received signal " + signal_name + "(" + boost::lexical_cast<std::string> (status) + ")."); |
||
| 237 | |||
| 238 | if (status != SIGPIPE) |
||
| 239 | { |
||
| 240 | on_message_condition.notify_all(); |
||
| 241 | } |
||
| 1675 | arune | 242 | } |