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