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