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