Rev 1964 | Rev 2107 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1964 | Rev 1989 | ||
|---|---|---|---|
| Line 39... | Line 39... | ||
| 39 | #include "can/Network.h" |
39 | #include "can/Network.h" |
| 40 | #include "can/Monitor.h" |
40 | #include "can/Monitor.h" |
| 41 | #include "can/CanDaemon.h" |
41 | #include "can/CanDaemon.h" |
| 42 | #include "vm/Manager.h" |
42 | #include "vm/Manager.h" |
| 43 | #include "storage/Manager.h" |
43 | #include "storage/Manager.h" |
| - | 44 | #include "net/GetFile.h" |
|
| - | 45 | #include "net/Url.h" |
|
| 44 | 46 | ||
| 45 | #include "vm/plugin/System.h" |
47 | #include "vm/plugin/System.h" |
| 46 | #include "vm/plugin/Timer.h" |
48 | #include "vm/plugin/Timer.h" |
| 47 | #include "vm/plugin/Module.h" |
49 | #include "vm/plugin/Module.h" |
| 48 | #include "vm/plugin/Node.h" |
50 | #include "vm/plugin/Node.h" |
| Line 63... | Line 65... | ||
| 63 | logging::Logger LOG("Main"); // Deprecated |
65 | logging::Logger LOG("Main"); // Deprecated |
| 64 | static const std::string log_module_ = "main"; |
66 | static const std::string log_module_ = "main"; |
| 65 | std::vector<broker::Subscriber::Pointer> subscribers; |
67 | std::vector<broker::Subscriber::Pointer> subscribers; |
| 66 | boost::condition on_message_condition; |
68 | boost::condition on_message_condition; |
| 67 | boost::mutex guard_mutex; |
69 | boost::mutex guard_mutex; |
| - | 70 | net::GetFile::Pointer downloader; |
|
| 68 | 71 | ||
| - | 72 | void ContinueInitialization(std::string protocol_filename); |
|
| 69 | void Handler(int status); |
73 | void Handler(int status); |
| 70 | void CleanUp(); |
74 | void CleanUp(); |
| - | 75 | ||
| - | 76 | ||
| - | 77 | ||
| - | 78 | ||
| - | 79 | void HandleProtocolFileDownload(bool success, net::Url url, std::string temporary_filepath) |
|
| - | 80 | { |
|
| - | 81 | if (success) |
|
| - | 82 | { |
|
| - | 83 | log::Info(log_module_, "Successfully downloaded protocol file!"); |
|
| - | 84 | ContinueInitialization(temporary_filepath); |
|
| - | 85 | } |
|
| - | 86 | else |
|
| - | 87 | { |
|
| - | 88 | ContinueInitialization(url.GetRaw()); |
|
| - | 89 | } |
|
| - | 90 | } |
|
| 71 | 91 | ||
| 72 | int main(int argc, char **argv) |
92 | int main(int argc, char **argv) |
| 73 | { |
93 | { |
| - | 94 | /* Set core dump parameters */ |
|
| 74 | rlimit core_limit = { RLIM_INFINITY, RLIM_INFINITY }; |
95 | rlimit core_limit = { RLIM_INFINITY, RLIM_INFINITY }; |
| 75 | setrlimit( RLIMIT_CORE, &core_limit ); // enable core dumps |
96 | setrlimit( RLIMIT_CORE, &core_limit ); // enable core dumps |
| 76 | 97 | ||
| - | 98 | ||
| - | 99 | /* Print startup information */ |
|
| 77 | log::Info(log_module_, "Atom daemon, version %s starting...", VERSION); |
100 | log::Info(log_module_, "Atom daemon, version %s starting...", VERSION); |
| 78 | log::Info(log_module_, "Released under %s.", LICENSE); |
101 | log::Info(log_module_, "Released under %s.", LICENSE); |
| 79 | log::Info(log_module_, "Written by Mattias Runge 2010-2012."); |
102 | log::Info(log_module_, "Written by Mattias Runge 2010-2012."); |
| 80 | 103 | ||
| - | 104 | ||
| - | 105 | /* Register signal handlers */ |
|
| 81 | signal(SIGTERM, Handler); |
106 | signal(SIGTERM, Handler); |
| 82 | signal(SIGINT, Handler); |
107 | signal(SIGINT, Handler); |
| 83 | signal(SIGQUIT, Handler); |
108 | signal(SIGQUIT, Handler); |
| 84 | signal(SIGABRT, Handler); |
109 | signal(SIGABRT, Handler); |
| 85 | signal(SIGPIPE, Handler); |
110 | signal(SIGPIPE, Handler); |
| 86 | 111 | ||
| - | 112 | ||
| - | 113 | /* Check configuration */ |
|
| 87 | config::Manager::Create(); |
114 | config::Manager::Create(); |
| 88 | 115 | ||
| 89 | if (!config::Manager::Instance()->Set(argc, argv)) |
116 | if (!config::Manager::Instance()->Set(argc, argv)) |
| 90 | { |
117 | { |
| 91 | CleanUp(); |
118 | CleanUp(); |
| 92 | return EXIT_SUCCESS; |
119 | return EXIT_SUCCESS; |
| 93 | } |
120 | } |
| - | 121 | ||
| 94 | 122 | ||
| - | 123 | /* Set up logging */ |
|
| 95 | if (config::Manager::Instance()->Exist("LogFile")) |
124 | if (config::Manager::Instance()->Exist("LogFile")) |
| 96 | { |
125 | { |
| 97 | log::OpenFile(config::Manager::Instance()->GetAsString("LogFile")); |
126 | log::OpenFile(config::Manager::Instance()->GetAsString("LogFile")); |
| 98 | } |
127 | } |
| 99 | 128 | ||
| 100 | if (config::Manager::Instance()->Exist("LogLevelMask")) |
129 | if (config::Manager::Instance()->Exist("LogLevelMask")) |
| 101 | { |
130 | { |
| 102 | log::SetLevelByString(config::Manager::Instance()->GetAsString("LogLevelMask")); |
131 | log::SetLevelByString(config::Manager::Instance()->GetAsString("LogLevelMask")); |
| 103 | } |
132 | } |
| 104 | 133 | ||
| - | 134 | ||
| - | 135 | /* Enter daemon mode if requested */ |
|
| 105 | if (config::Manager::Instance()->Exist("daemon")) |
136 | if (config::Manager::Instance()->Exist("daemon")) |
| 106 | { |
137 | { |
| 107 | LOG.Info("Entering daemon mode..."); |
138 | LOG.Info("Entering daemon mode..."); |
| 108 | 139 | ||
| 109 | if (daemon(0, 0) == -1) |
140 | if (daemon(0, 0) == -1) |
| 110 | { |
141 | { |
| 111 | log::Error(log_module_, "Could not enter daemon mode. Exiting..."); |
142 | log::Error(log_module_, "Could not enter daemon mode. Exiting..."); |
| 112 | CleanUp(); |
143 | CleanUp(); |
| 113 | return EXIT_FAILURE; |
144 | return EXIT_FAILURE; |
| 114 | } |
145 | } |
| 115 | 146 | ||
| 116 | log::Info(log_module_, "Deamon mode entered successfully!"); |
147 | log::Info(log_module_, "Deamon mode entered successfully!"); |
| 117 | } |
148 | } |
| 118 | 149 | ||
| - | 150 | ||
| - | 151 | /* Create singletons */ |
|
| 119 | storage::Manager::Create(); |
152 | storage::Manager::Create(); |
| 120 | timer::Manager::Create(); |
153 | timer::Manager::Create(); |
| 121 | broker::Manager::Create(); |
154 | broker::Manager::Create(); |
| 122 | net::Manager::Create(); |
155 | net::Manager::Create(); |
| 123 | can::Protocol::Create(); |
156 | can::Protocol::Create(); |
| 124 | control::Manager::Create(); |
157 | control::Manager::Create(); |
| 125 | vm::Manager::Create(); |
158 | vm::Manager::Create(); |
| 126 | 159 | ||
| - | 160 | ||
| - | 161 | ||
| - | 162 | std::string protocol_filename = config::Manager::Instance()->GetAsString("ProtocolFile"); |
|
| - | 163 | ||
| - | 164 | try |
|
| - | 165 | { |
|
| - | 166 | net::Url url(protocol_filename); |
|
| - | 167 | ||
| - | 168 | downloader = net::GetFile::Pointer(new net::GetFile()); |
|
| - | 169 | ||
| - | 170 | log::Info(log_module_, "Will try to download protocol file from \"%s\"...", protocol_filename.data()); |
|
| - | 171 | ||
| - | 172 | downloader->Start(url, HandleProtocolFileDownload); |
|
| - | 173 | } |
|
| - | 174 | catch (std::exception& exception) |
|
| - | 175 | { |
|
| - | 176 | log::Debug(log_module_, "Could not parse ProtocolFile as a URL, \"%s\".", protocol_filename.data()); |
|
| - | 177 | ContinueInitialization(protocol_filename); |
|
| - | 178 | } |
|
| - | 179 | ||
| - | 180 | ||
| - | 181 | /* Lock main thread and wait for exit */ |
|
| - | 182 | boost::mutex::scoped_lock guard(guard_mutex); |
|
| - | 183 | ||
| - | 184 | on_message_condition.wait(guard); |
|
| - | 185 | ||
| - | 186 | ||
| - | 187 | /* Cleanup */ |
|
| - | 188 | log::Info(log_module_, "Cleaning up..."); |
|
| - | 189 | ||
| - | 190 | CleanUp(); |
|
| - | 191 | ||
| - | 192 | log::Info(log_module_, "Thank you for using Atom. Goodbye!"); |
|
| - | 193 | ||
| - | 194 | return EXIT_SUCCESS; |
|
| - | 195 | } |
|
| - | 196 | ||
| - | 197 | void ContinueInitialization(std::string protocol_filename) |
|
| - | 198 | { |
|
| - | 199 | /* Load protocol file */ |
|
| - | 200 | if (!can::Protocol::Instance()->Load(protocol_filename)) |
|
| - | 201 | { |
|
| - | 202 | log::Error(log_module_, "Failed to load %s!", protocol_filename.data()); |
|
| - | 203 | ||
| - | 204 | on_message_condition.notify_all(); |
|
| - | 205 | } |
|
| - | 206 | ||
| - | 207 | ||
| - | 208 | /* Set up storage path */ |
|
| 127 | storage::Manager::Instance()->SetRootPath(config::Manager::Instance()->GetAsString("StoragePath")); |
209 | storage::Manager::Instance()->SetRootPath(config::Manager::Instance()->GetAsString("StoragePath")); |
| 128 | 210 | ||
| 129 | can::Protocol::Instance()->Load(config::Manager::Instance()->GetAsString("ProtocolFile")); |
- | |
| 130 | 211 | ||
| - | 212 | /* Start monitor server */ |
|
| 131 | if (config::Manager::Instance()->Exist("MonitorPort")) |
213 | if (config::Manager::Instance()->Exist("MonitorPort")) |
| 132 | { |
214 | { |
| 133 | subscribers.push_back(can::Monitor::Pointer(new can::Monitor(config::Manager::Instance()->GetAsInt("MonitorPort")))); |
215 | subscribers.push_back(can::Monitor::Pointer(new can::Monitor(config::Manager::Instance()->GetAsInt("MonitorPort")))); |
| 134 | } |
216 | } |
| 135 | 217 | ||
| - | 218 | ||
| - | 219 | /* Start CanDaemon server */ |
|
| 136 | if (config::Manager::Instance()->Exist("DaemonPort")) |
220 | if (config::Manager::Instance()->Exist("DaemonPort")) |
| 137 | { |
221 | { |
| 138 | subscribers.push_back(can::CanDaemon::Pointer(new can::CanDaemon(config::Manager::Instance()->GetAsInt("DaemonPort")))); |
222 | subscribers.push_back(can::CanDaemon::Pointer(new can::CanDaemon(config::Manager::Instance()->GetAsInt("DaemonPort")))); |
| 139 | } |
223 | } |
| 140 | 224 | ||
| - | 225 | ||
| - | 226 | /* Subscribe Can control manager to data */ |
|
| 141 | subscribers.push_back(control::Manager::Instance()); |
227 | subscribers.push_back(control::Manager::Instance()); |
| 142 | 228 | ||
| - | 229 | ||
| - | 230 | /* Start VM plugins */ |
|
| 143 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::System(vm::Manager::Instance()->GetIoService()))); |
231 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::System(vm::Manager::Instance()->GetIoService()))); |
| 144 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Console(vm::Manager::Instance()->GetIoService(), config::Manager::Instance()->GetAsInt("CommandPort")))); |
232 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Console(vm::Manager::Instance()->GetIoService(), config::Manager::Instance()->GetAsInt("CommandPort")))); |
| 145 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Storage(vm::Manager::Instance()->GetIoService()))); |
233 | 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::Timer(vm::Manager::Instance()->GetIoService()))); |
234 | 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::Module(vm::Manager::Instance()->GetIoService()))); |
235 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Module(vm::Manager::Instance()->GetIoService()))); |
| Line 154... | Line 242... | ||
| 154 | 242 | ||
| 155 | #ifdef USE_PLUGIN_MYSQL |
243 | #ifdef USE_PLUGIN_MYSQL |
| 156 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::MySql(vm::Manager::Instance()->GetIoService()))); |
244 | vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::MySql(vm::Manager::Instance()->GetIoService()))); |
| 157 | #endif // USE_PLUGIN_MYSQL |
245 | #endif // USE_PLUGIN_MYSQL |
| 158 | 246 | ||
| 159 | vm::Manager::Instance()->Start(config::Manager::Instance()->GetAsString("ScriptPath"), config::Manager::Instance()->GetAsString("UserScriptPath")); |
- | |
| 160 | 247 | ||
| - | 248 | /* Start VM */ |
|
| - | 249 | vm::Manager::Instance()->Start(config::Manager::Instance()->GetAsString("ScriptPath"), config::Manager::Instance()->GetAsString("UserScriptPath")); |
|
| - | 250 | ||
| - | 251 | ||
| - | 252 | /* Connect to Can network */ |
|
| 161 | if (config::Manager::Instance()->Exist("CanNet")) |
253 | if (config::Manager::Instance()->Exist("CanNet")) |
| 162 | { |
254 | { |
| 163 | common::StringList cannetworks = config::Manager::Instance()->GetAsStringVector("CanNet"); |
255 | common::StringList cannetworks = config::Manager::Instance()->GetAsStringVector("CanNet"); |
| 164 | 256 | ||
| 165 | for (uint n = 0; n < cannetworks.size(); n++) |
257 | for (uint n = 0; n < cannetworks.size(); n++) |
| 166 | { |
258 | { |
| 167 | subscribers.push_back(can::Network::Pointer(new can::Network(cannetworks[n]))); |
259 | subscribers.push_back(can::Network::Pointer(new can::Network(cannetworks[n]))); |
| 168 | } |
260 | } |
| 169 | } |
261 | } |
| 170 | - | ||
| 171 | boost::mutex::scoped_lock guard(guard_mutex); |
- | |
| 172 | - | ||
| 173 | on_message_condition.wait(guard); |
- | |
| 174 | - | ||
| 175 | log::Info(log_module_, "Cleaning up..."); |
- | |
| 176 | - | ||
| 177 | CleanUp(); |
- | |
| 178 | - | ||
| 179 | log::Info(log_module_, "Thank you for using Atom. Goodbye!"); |
- | |
| 180 | - | ||
| 181 | return EXIT_SUCCESS; |
- | |
| 182 | } |
262 | } |
| 183 | 263 | ||
| 184 | void CleanUp() |
264 | void CleanUp() |
| 185 | { |
265 | { |
| - | 266 | downloader.reset(); |
|
| - | 267 | ||
| 186 | if (net::Manager::Instance().use_count() > 0) |
268 | if (net::Manager::Instance().use_count() > 0) |
| 187 | { |
269 | { |
| 188 | net::Manager::Instance()->Stop(); |
270 | net::Manager::Instance()->Stop(); |
| 189 | } |
271 | } |
| 190 | 272 | ||