Rev 1940 | Rev 1947 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1940 | Rev 1945 | ||
|---|---|---|---|
| Line 1... | Line 1... | ||
| 1 | /* |
1 | /* |
| 2 | * |
2 | * |
| 3 | * Copyright |
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 | * |
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. |
| Line 50... | Line 50... | ||
| 50 | #include "vm/plugin/Socket.h" |
50 | #include "vm/plugin/Socket.h" |
| 51 | 51 | ||
| 52 | #ifdef USE_PLUGIN_XORG |
52 | #ifdef USE_PLUGIN_XORG |
| 53 | #include "vm/plugin/Xorg.h" |
53 | #include "vm/plugin/Xorg.h" |
| 54 | #endif // USE_PLUGIN_XORG |
54 | #endif // USE_PLUGIN_XORG |
| - | 55 | ||
| - | 56 | #ifdef USE_PLUGIN_MYSQL |
|
| - | 57 | #include "vm/plugin/MySql.h" |
|
| - | 58 | #endif // USE_PLUGIN_MYSQL |
|
| 55 | 59 | ||
| 56 | using namespace atom; |
60 | using namespace atom; |
| 57 | 61 | ||
| 58 | logging::Logger LOG |
62 | logging::Logger LOG("Main"); |
| 59 | std::vector<broker::Subscriber::Pointer> subscribers; |
63 | std::vector<broker::Subscriber::Pointer> subscribers; |
| 60 | boost::condition on_message_condition; |
64 | boost::condition on_message_condition; |
| 61 | boost::mutex guard_mutex; |
65 | boost::mutex guard_mutex; |
| 62 | 66 | ||
| 63 | void Handler |
67 | void Handler(int status); |
| 64 | void CleanUp(); |
68 | void CleanUp(); |
| 65 | 69 | ||
| 66 | int main |
70 | int main(int argc, char **argv) |
| 67 | { |
71 | { |
| 68 | LOG.Info |
72 | LOG.Info("\033[29;1mAtom Daemon, version " + std::string(VERSION) + " starting...\033[0m"); |
| 69 | LOG.Info |
73 | LOG.Info("\033[29;1mReleased under " + std::string(LICENSE) + ".\033[0m"); |
| 70 | LOG.Info |
74 | LOG.Info("Written by Mattias Runge 2010-2012."); |
| 71 | 75 | ||
| 72 | signal |
76 | signal(SIGTERM, Handler); |
| 73 | signal |
77 | signal(SIGINT, Handler); |
| 74 | signal |
78 | signal(SIGQUIT, Handler); |
| 75 | signal |
79 | signal(SIGABRT, Handler); |
| 76 | signal |
80 | signal(SIGPIPE, Handler); |
| 77 | 81 | ||
| 78 | config::Manager::Create(); |
82 | config::Manager::Create(); |
| 79 | 83 | ||
| 80 | if (!config::Manager::Instance()->Set |
84 | if (!config::Manager::Instance()->Set(argc, argv)) |
| 81 | { |
85 | { |
| 82 | CleanUp(); |
86 | CleanUp(); |
| 83 | return EXIT_SUCCESS; |
87 | return EXIT_SUCCESS; |
| 84 | } |
88 | } |
| 85 | 89 | ||
| 86 | if (config::Manager::Instance()->Exist |
90 | if (config::Manager::Instance()->Exist("LogFile")) |
| 87 | { |
91 | { |
| 88 | logging::Logger::OpenFile |
92 | logging::Logger::OpenFile(config::Manager::Instance()->GetAsString("LogFile")); |
| 89 | } |
93 | } |
| 90 | 94 | ||
| 91 | if (config::Manager::Instance()->Exist |
95 | if (config::Manager::Instance()->Exist("LogLevel")) |
| 92 | { |
96 | { |
| 93 | logging::Logger::SetLevel |
97 | logging::Logger::SetLevel((logging::Logger::Level) config::Manager::Instance()->GetAsInt("LogLevel")); |
| 94 | } |
98 | } |
| 95 | 99 | ||
| 96 | if (config::Manager::Instance()->Exist |
100 | if (config::Manager::Instance()->Exist("daemon")) |
| 97 | { |
101 | { |
| 98 | LOG.Info |
102 | LOG.Info("Entering daemon mode..."); |
| 99 | 103 | ||
| 100 | if (daemon |
104 | if (daemon(0, 0) == -1) |
| 101 | { |
105 | { |
| 102 | LOG.Error |
106 | LOG.Error("Could not enter daemon mode. Exiting..."); |
| 103 | CleanUp(); |
107 | CleanUp(); |
| 104 | return EXIT_FAILURE; |
108 | return EXIT_FAILURE; |
| 105 | } |
109 | } |
| 106 | 110 | ||
| 107 | LOG.Info |
111 | LOG.Info("Deamon mode entered successfully!"); |
| 108 | } |
112 | } |
| 109 | 113 | ||
| 110 | storage::Manager::Create(); |
114 | storage::Manager::Create(); |
| 111 | 115 | ||
| 112 | timer::Manager::Create(); |
116 | timer::Manager::Create(); |
| Line 114... | Line 118... | ||
| 114 | net::Manager::Create(); |
118 | net::Manager::Create(); |
| 115 | can::Protocol::Create(); |
119 | can::Protocol::Create(); |
| 116 | control::Manager::Create(); |
120 | control::Manager::Create(); |
| 117 | vm::Manager::Create(); |
121 | vm::Manager::Create(); |
| 118 | 122 | ||
| 119 | if (config::Manager::Instance()->Exist |
123 | if (config::Manager::Instance()->Exist("MbbPort")) |
| 120 | { |
124 | { |
| 121 | mbb::Manager::Create |
125 | mbb::Manager::Create(config::Manager::Instance()->GetAsInt("MbbPort")); |
| 122 | } |
126 | } |
| 123 | 127 | ||
| 124 | storage::Manager::Instance()->SetRootPath |
128 | storage::Manager::Instance()->SetRootPath(config::Manager::Instance()->GetAsString("StoragePath")); |
| 125 | 129 | ||
| 126 | can::Protocol::Instance()->Load |
130 | can::Protocol::Instance()->Load(config::Manager::Instance()->GetAsString("ProtocolFile")); |
| 127 | 131 | ||
| 128 | if (config::Manager::Instance()->Exist |
132 | if (config::Manager::Instance()->Exist("MonitorPort")) |
| 129 | { |
133 | { |
| 130 | subscribers.push_back |
134 | subscribers.push_back(can::Monitor::Pointer(new can::Monitor(config::Manager::Instance()->GetAsInt("MonitorPort")))); |
| 131 | } |
135 | } |
| 132 | 136 | ||
| 133 | if (config::Manager::Instance()->Exist |
137 | if (config::Manager::Instance()->Exist("DaemonPort")) |
| 134 | { |
138 | { |
| 135 | subscribers.push_back |
139 | subscribers.push_back(can::CanDaemon::Pointer(new can::CanDaemon(config::Manager::Instance()->GetAsInt("DaemonPort")))); |
| 136 | } |
140 | } |
| 137 | 141 | ||
| 138 | subscribers.push_back |
142 | subscribers.push_back(control::Manager::Instance()); |
| 139 | 143 | ||
| 140 | vm::Manager::Instance()->AddPlugin |
144 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::System(vm::Manager::Instance()->GetIoService()))); |
| 141 | vm::Manager::Instance()->AddPlugin |
145 | 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 |
146 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Storage(vm::Manager::Instance()->GetIoService()))); |
| 143 | vm::Manager::Instance()->AddPlugin |
147 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Timer(vm::Manager::Instance()->GetIoService()))); |
| 144 | vm::Manager::Instance()->AddPlugin |
148 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Module(vm::Manager::Instance()->GetIoService()))); |
| 145 | vm::Manager::Instance()->AddPlugin |
149 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Node(vm::Manager::Instance()->GetIoService()))); |
| 146 | vm::Manager::Instance()->AddPlugin |
150 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Socket(vm::Manager::Instance()->GetIoService()))); |
| 147 | 151 | ||
| 148 | #ifdef USE_PLUGIN_XORG |
152 | #ifdef USE_PLUGIN_XORG |
| 149 | vm::Manager::Instance()->AddPlugin |
153 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Xorg(vm::Manager::Instance()->GetIoService()))); |
| 150 | #endif // USE_PLUGIN_XORG |
154 | #endif // USE_PLUGIN_XORG |
| - | 155 | ||
| - | 156 | #ifdef USE_PLUGIN_MYSQL |
|
| - | 157 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::MySql(vm::Manager::Instance()->GetIoService()))); |
|
| - | 158 | #endif // USE_PLUGIN_MYSQL |
|
| 151 | 159 | ||
| 152 | vm::Manager::Instance()->Start |
160 | vm::Manager::Instance()->Start(config::Manager::Instance()->GetAsString("ScriptPath"), config::Manager::Instance()->GetAsString("UserScriptPath")); |
| 153 | 161 | ||
| 154 | if (config::Manager::Instance()->Exist |
162 | if (config::Manager::Instance()->Exist("CanNet")) |
| 155 | { |
163 | { |
| 156 | common::StringList cannetworks = config::Manager::Instance()->GetAsStringVector |
164 | common::StringList cannetworks = config::Manager::Instance()->GetAsStringVector("CanNet"); |
| 157 | 165 | ||
| 158 | for (uint n = 0; n < cannetworks.size(); n++) |
166 | for (uint n = 0; n < cannetworks.size(); n++) |
| 159 | { |
167 | { |
| 160 | subscribers.push_back |
168 | subscribers.push_back(can::Network::Pointer(new can::Network(cannetworks[n]))); |
| 161 | } |
169 | } |
| 162 | } |
170 | } |
| 163 | 171 | ||
| 164 | boost::mutex::scoped_lock guard |
172 | boost::mutex::scoped_lock guard(guard_mutex); |
| 165 | 173 | ||
| 166 | on_message_condition.wait |
174 | on_message_condition.wait(guard); |
| 167 | 175 | ||
| 168 | LOG.Info |
176 | LOG.Info("Cleaning up..."); |
| 169 | 177 | ||
| 170 | CleanUp(); |
178 | CleanUp(); |
| 171 | 179 | ||
| 172 | LOG.Info |
180 | LOG.Info("Thank you for using Atom. Goodbye!"); |
| 173 | 181 | ||
| 174 | return EXIT_SUCCESS; |
182 | return EXIT_SUCCESS; |
| 175 | } |
183 | } |
| 176 | 184 | ||
| 177 | void CleanUp() |
185 | void CleanUp() |
| Line 188... | Line 196... | ||
| 188 | vm::Manager::Delete(); |
196 | vm::Manager::Delete(); |
| 189 | net::Manager::Delete(); |
197 | net::Manager::Delete(); |
| 190 | storage::Manager::Delete(); |
198 | storage::Manager::Delete(); |
| 191 | } |
199 | } |
| 192 | 200 | ||
| 193 | void Handler |
201 | void Handler(int status) |
| 194 | { |
202 | { |
| 195 | std::string signal_name = "Unknown"; |
203 | std::string signal_name = "Unknown"; |
| 196 | 204 | ||
| 197 | switch (status) |
205 | switch (status) |
| 198 | { |
206 | { |
| Line 231... | Line 239... | ||
| 231 | signal_name = "Pipe"; |
239 | signal_name = "Pipe"; |
| 232 | break; |
240 | break; |
| 233 | } |
241 | } |
| 234 | } |
242 | } |
| 235 | 243 | ||
| 236 | LOG.Debug |
244 | LOG.Debug("Received signal " + signal_name + "(" + boost::lexical_cast<std::string>(status) + ")."); |
| 237 | 245 | ||
| 238 | if (status != SIGPIPE) |
246 | if (status != SIGPIPE) |
| 239 | { |
247 | { |
| 240 | on_message_condition.notify_all(); |
248 | on_message_condition.notify_all(); |
| 241 | } |
249 | } |