Rev 1914 | Rev 1940 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1914 | Rev 1939 | ||
|---|---|---|---|
| Line 1... | Line 1... | ||
| 1 | /* |
1 | /* |
| 2 | * |
2 | * |
| 3 | * Copyright (C) 2010 Mattias Runge |
3 | * Copyright (C) 2010 Mattias Runge |
| 4 | * |
4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
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 |
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 |
7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
8 | * (at your option) any later version. |
| 9 | * |
9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
13 | * GNU General Public License for more details. |
| 14 | * |
14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
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., |
16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
18 | * |
| 19 | */ |
19 | */ |
| 20 | 20 | ||
| 21 | #include <signal.h> |
21 | #include <signal.h> |
| 22 | 22 | ||
| 23 | #include <boost/program_options.hpp> |
23 | #include <boost/program_options.hpp> |
| Line 37... | Line 37... | ||
| 37 | #include "can/Network.h" |
37 | #include "can/Network.h" |
| 38 | #include "can/Monitor.h" |
38 | #include "can/Monitor.h" |
| 39 | #include "can/CanDaemon.h" |
39 | #include "can/CanDaemon.h" |
| 40 | #include "vm/Manager.h" |
40 | #include "vm/Manager.h" |
| 41 | #include "storage/Manager.h" |
41 | #include "storage/Manager.h" |
| - | 42 | #include "mbb/Manager.h" |
|
| 42 | 43 | ||
| 43 | #include "vm/plugin/System.h" |
44 | #include "vm/plugin/System.h" |
| 44 | #include "vm/plugin/Timer.h" |
45 | #include "vm/plugin/Timer.h" |
| 45 | #include "vm/plugin/Module.h" |
46 | #include "vm/plugin/Module.h" |
| 46 | #include "vm/plugin/Node.h" |
47 | #include "vm/plugin/Node.h" |
| Line 51... | Line 52... | ||
| 51 | #ifdef USE_PLUGIN_XORG |
52 | #ifdef USE_PLUGIN_XORG |
| 52 | #include "vm/plugin/Xorg.h" |
53 | #include "vm/plugin/Xorg.h" |
| 53 | #endif // USE_PLUGIN_XORG |
54 | #endif // USE_PLUGIN_XORG |
| 54 | 55 | ||
| 55 | using namespace atom; |
56 | using namespace atom; |
| 56 | 57 | ||
| 57 | logging::Logger LOG("Main"); |
58 | logging::Logger LOG ("Main"); |
| 58 | std::vector<broker::Subscriber::Pointer> subscribers; |
59 | std::vector<broker::Subscriber::Pointer> subscribers; |
| 59 | boost::condition on_message_condition; |
60 | boost::condition on_message_condition; |
| 60 | boost::mutex guard_mutex; |
61 | boost::mutex guard_mutex; |
| 61 | 62 | ||
| 62 | void Handler(int status); |
63 | void Handler (int status); |
| 63 | void CleanUp(); |
64 | void CleanUp(); |
| 64 | 65 | ||
| 65 | int main(int argc, char **argv) |
66 | int main (int argc, char **argv) |
| 66 | { |
67 | { |
| 67 |
|
68 | LOG.Info ("\033[29;1mAtom Daemon, version " + std::string (VERSION) + " starting...\033[0m"); |
| 68 |
|
69 | LOG.Info ("\033[29;1mReleased under " + std::string (LICENSE) + ".\033[0m"); |
| 69 |
|
70 | LOG.Info ("Written by Mattias Runge 2010-2012."); |
| 70 | 71 | ||
| 71 |
|
72 | signal (SIGTERM, Handler); |
| 72 |
|
73 | signal (SIGINT, Handler); |
| 73 |
|
74 | signal (SIGQUIT, Handler); |
| 74 |
|
75 | signal (SIGABRT, Handler); |
| 75 |
|
76 | signal (SIGPIPE, Handler); |
| 76 | - | ||
| 77 | config::Manager::Create(); |
- | |
| 78 | - | ||
| 79 | if (!config::Manager::Instance()->Set(argc, argv)) |
- | |
| 80 | { |
- | |
| 81 | CleanUp(); |
- | |
| 82 | return EXIT_SUCCESS; |
- | |
| 83 | } |
- | |
| 84 | - | ||
| 85 | if (config::Manager::Instance()->Exist("LogFile")) |
- | |
| 86 | { |
- | |
| 87 | logging::Logger::OpenFile(config::Manager::Instance()->GetAsString("LogFile")); |
- | |
| 88 | } |
- | |
| 89 | - | ||
| 90 | if (config::Manager::Instance()->Exist("LogLevel")) |
- | |
| 91 | { |
- | |
| 92 | logging::Logger::SetLevel((logging::Logger::Level)config::Manager::Instance()->GetAsInt("LogLevel")); |
- | |
| 93 | } |
- | |
| 94 | - | ||
| 95 | if (config::Manager::Instance()->Exist("daemon")) |
- | |
| 96 | { |
- | |
| 97 | LOG.Info("Entering daemon mode..."); |
- | |
| 98 | - | ||
| 99 | if (daemon(0, 0) == -1) |
- | |
| 100 | { |
- | |
| 101 | LOG.Error("Could not enter daemon mode. Exiting..."); |
- | |
| 102 | CleanUp(); |
- | |
| 103 | return EXIT_FAILURE; |
- | |
| 104 | } |
- | |
| 105 | - | ||
| 106 | LOG.Info("Deamon mode entered successfully!"); |
- | |
| 107 | } |
- | |
| 108 | 77 | ||
| 109 | storage::Manager::Create(); |
- | |
| 110 | timer::Manager::Create(); |
- | |
| 111 | broker::Manager::Create(); |
- | |
| 112 |
|
78 | config::Manager::Create(); |
| 113 | can::Protocol::Create(); |
- | |
| 114 | control::Manager::Create(); |
- | |
| 115 | vm::Manager::Create(); |
- | |
| 116 | - | ||
| 117 | storage::Manager::Instance()->SetRootPath(config::Manager::Instance()->GetAsString("StoragePath")); |
- | |
| 118 | - | ||
| 119 | can::Protocol::Instance()->Load(config::Manager::Instance()->GetAsString("ProtocolFile")); |
- | |
| 120 | - | ||
| 121 | if (config::Manager::Instance()->Exist("MonitorPort")) |
- | |
| 122 | { |
- | |
| 123 | subscribers.push_back(can::Monitor::Pointer(new can::Monitor(config::Manager::Instance()->GetAsInt("MonitorPort")))); |
- | |
| 124 | } |
- | |
| 125 | 79 | ||
| 126 |
|
80 | if (!config::Manager::Instance()->Set (argc, argv)) |
| 127 | { |
- | |
| 128 | subscribers.push_back(can::CanDaemon::Pointer(new can::CanDaemon(config::Manager::Instance()->GetAsInt("DaemonPort")))); |
- | |
| 129 | } |
- | |
| 130 | - | ||
| 131 | subscribers.push_back(control::Manager::Instance()); |
- | |
| 132 | - | ||
| 133 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::System(vm::Manager::Instance()->GetIoService()))); |
- | |
| 134 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Console(vm::Manager::Instance()->GetIoService(), config::Manager::Instance()->GetAsInt("CommandPort")))); |
- | |
| 135 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Storage(vm::Manager::Instance()->GetIoService()))); |
- | |
| 136 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Timer(vm::Manager::Instance()->GetIoService()))); |
- | |
| 137 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Module(vm::Manager::Instance()->GetIoService()))); |
- | |
| 138 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Node(vm::Manager::Instance()->GetIoService()))); |
- | |
| 139 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Socket(vm::Manager::Instance()->GetIoService()))); |
- | |
| 140 | - | ||
| 141 | #ifdef USE_PLUGIN_XORG |
- | |
| 142 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Xorg(vm::Manager::Instance()->GetIoService()))); |
- | |
| 143 | #endif // USE_PLUGIN_XORG |
- | |
| 144 | - | ||
| 145 | vm::Manager::Instance()->Start(config::Manager::Instance()->GetAsString("ScriptPath"), config::Manager::Instance()->GetAsString("UserScriptPath")); |
- | |
| 146 | - | ||
| 147 | if (config::Manager::Instance()->Exist("CanNet")) |
- | |
| 148 |
|
81 | { |
| 149 | common::StringList cannetworks = config::Manager::Instance()->GetAsStringVector("CanNet"); |
- | |
| 150 | - | ||
| 151 | for (uint n = 0; n < cannetworks.size(); n++) |
- | |
| 152 | { |
- | |
| 153 | subscribers.push_back(can::Network::Pointer(new can::Network(cannetworks[n]))); |
- | |
| 154 | } |
- | |
| 155 | } |
- | |
| 156 | - | ||
| 157 | boost::mutex::scoped_lock guard(guard_mutex); |
- | |
| 158 | on_message_condition.wait(guard); |
- | |
| 159 | - | ||
| 160 | LOG.Info("Cleaning up..."); |
- | |
| 161 | - | ||
| 162 | CleanUp(); |
82 | CleanUp(); |
| 163 | - | ||
| 164 | LOG.Info("Thank you for using Atom. Goodbye!"); |
- | |
| 165 | - | ||
| 166 | return EXIT_SUCCESS; |
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) |
|
| - | 101 | { |
|
| - | 102 | LOG.Error ("Could not enter daemon mode. Exiting..."); |
|
| - | 103 | CleanUp(); |
|
| - | 104 | return EXIT_FAILURE; |
|
| - | 105 | } |
|
| - | 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++) |
|
| - | 159 | { |
|
| - | 160 | subscribers.push_back (can::Network::Pointer (new can::Network (cannetworks[n]))); |
|
| - | 161 | } |
|
| - | 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; |
|
| 167 | } |
175 | } |
| 168 | 176 | ||
| 169 | void CleanUp() |
177 | void CleanUp() |
| 170 | { |
178 | { |
| - | 179 | net::Manager::Instance()->Stop(); |
|
| - | 180 | ||
| 171 |
|
181 | subscribers.clear(); |
| 172 |
|
182 | config::Manager::Delete(); |
| 173 |
|
183 | timer::Manager::Delete(); |
| 174 |
|
184 | control::Manager::Delete(); |
| 175 |
|
185 | mbb::Manager::Delete(); |
| 176 |
|
186 | can::Protocol::Delete(); |
| 177 |
|
187 | broker::Manager::Delete(); |
| - | 188 | vm::Manager::Delete(); |
|
| 178 |
|
189 | net::Manager::Delete(); |
| 179 |
|
190 | storage::Manager::Delete(); |
| 180 | } |
191 | } |
| 181 | 192 | ||
| 182 | void Handler(int status) |
193 | void Handler (int status) |
| 183 | { |
194 | { |
| 184 |
|
195 | std::string signal_name = "Unknown"; |
| - | 196 | ||
| - | 197 | switch (status) |
|
| - | 198 | { |
|
| - | 199 | case SIGTERM: |
|
| - | 200 | { |
|
| - | 201 | signal_name = "Terminate"; |
|
| - | 202 | break; |
|
| - | 203 | } |
|
| - | 204 | ||
| - | 205 | case SIGINT: |
|
| - | 206 | { |
|
| - | 207 | signal_name = "Interupt"; |
|
| - | 208 | break; |
|
| - | 209 | } |
|
| - | 210 | ||
| - | 211 | case SIGQUIT: |
|
| - | 212 | { |
|
| - | 213 | signal_name = "Quit"; |
|
| - | 214 | break; |
|
| - | 215 | } |
|
| - | 216 | ||
| - | 217 | case SIGABRT: |
|
| - | 218 | { |
|
| - | 219 | signal_name = "Abort"; |
|
| - | 220 | break; |
|
| - | 221 | } |
|
| - | 222 | ||
| - | 223 | case SIGIO: |
|
| - | 224 | { |
|
| - | 225 | signal_name = "I/O"; |
|
| - | 226 | break; |
|
| 185 | 227 | } |
|
| - | 228 | ||
| 186 |
|
229 | case SIGPIPE: |
| 187 | { |
230 | { |
| 188 | case SIGTERM: |
- | |
| 189 | { |
- | |
| 190 | signal_name = "Terminate"; |
- | |
| 191 | break; |
- | |
| 192 | } |
- | |
| 193 | case SIGINT: |
- | |
| 194 | { |
- | |
| 195 | signal_name = "Interupt"; |
- | |
| 196 | break; |
- | |
| 197 | } |
- | |
| 198 | case SIGQUIT: |
- | |
| 199 | { |
- | |
| 200 | signal_name = "Quit"; |
- | |
| 201 | break; |
- | |
| 202 | } |
- | |
| 203 | case SIGABRT: |
- | |
| 204 | { |
- | |
| 205 | signal_name = "Abort"; |
- | |
| 206 | break; |
- | |
| 207 | } |
- | |
| 208 | case SIGIO: |
- | |
| 209 | { |
- | |
| 210 | signal_name = "I/O"; |
- | |
| 211 | break; |
- | |
| 212 | } |
- | |
| 213 | case SIGPIPE: |
- | |
| 214 | { |
- | |
| 215 |
|
231 | signal_name = "Pipe"; |
| 216 |
|
232 | break; |
| 217 | } |
- | |
| 218 | } |
- | |
| 219 | - | ||
| 220 | LOG.Debug("Received signal " + signal_name + "(" + boost::lexical_cast<std::string>(status) + ")."); |
- | |
| 221 | - | ||
| 222 | if (status != SIGPIPE) |
- | |
| 223 | { |
- | |
| 224 | on_message_condition.notify_all(); |
- | |
| 225 | } |
233 | } |
| - | 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 | } |
|
| 226 | } |
242 | } |
| 227 | - | ||