Subversion Repositories HomeAutomation

Rev

Rev 1964 | Rev 2107 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1964 Rev 1989
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
#include <sys/resource.h>
23
 
23
 
24
#include <boost/program_options.hpp>
24
#include <boost/program_options.hpp>
25
#include <boost/thread/mutex.hpp>
25
#include <boost/thread/mutex.hpp>
26
#include <boost/thread/condition.hpp>
26
#include <boost/thread/condition.hpp>
27
#include <boost/thread/locks.hpp>
27
#include <boost/thread/locks.hpp>
28
 
28
 
29
#include "config.h"
29
#include "config.h"
30
 
30
 
31
#include "logging/Logger.h" // Deprecated
31
#include "logging/Logger.h" // Deprecated
32
#include "common/log.h"
32
#include "common/log.h"
33
#include "net/Manager.h"
33
#include "net/Manager.h"
34
#include "timer/Manager.h"
34
#include "timer/Manager.h"
35
#include "config/Manager.h"
35
#include "config/Manager.h"
36
#include "can/Protocol.h"
36
#include "can/Protocol.h"
37
#include "broker/Manager.h"
37
#include "broker/Manager.h"
38
#include "control/Manager.h"
38
#include "control/Manager.h"
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"
49
#include "vm/plugin/Console.h"
51
#include "vm/plugin/Console.h"
50
#include "vm/plugin/Storage.h"
52
#include "vm/plugin/Storage.h"
51
#include "vm/plugin/Socket.h"
53
#include "vm/plugin/Socket.h"
52
 
54
 
53
#ifdef USE_PLUGIN_XORG
55
#ifdef USE_PLUGIN_XORG
54
#include "vm/plugin/Xorg.h"
56
#include "vm/plugin/Xorg.h"
55
#endif // USE_PLUGIN_XORG
57
#endif // USE_PLUGIN_XORG
56
 
58
 
57
#ifdef USE_PLUGIN_MYSQL
59
#ifdef USE_PLUGIN_MYSQL
58
#include "vm/plugin/MySql.h"
60
#include "vm/plugin/MySql.h"
59
#endif // USE_PLUGIN_MYSQL
61
#endif // USE_PLUGIN_MYSQL
60
 
62
 
61
using namespace atom;
63
using namespace atom;
62
 
64
 
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())));
148
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Node(vm::Manager::Instance()->GetIoService())));
236
  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::Socket(vm::Manager::Instance()->GetIoService())));
237
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Socket(vm::Manager::Instance()->GetIoService())));
150
 
238
 
151
#ifdef USE_PLUGIN_XORG
239
#ifdef USE_PLUGIN_XORG
152
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Xorg(vm::Manager::Instance()->GetIoService())));
240
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Xorg(vm::Manager::Instance()->GetIoService())));
153
#endif // USE_PLUGIN_XORG
241
#endif // USE_PLUGIN_XORG
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
 
191
  subscribers.clear();
273
  subscribers.clear();
192
  config::Manager::Delete();
274
  config::Manager::Delete();
193
  timer::Manager::Delete();
275
  timer::Manager::Delete();
194
  control::Manager::Delete();
276
  control::Manager::Delete();
195
  can::Protocol::Delete();
277
  can::Protocol::Delete();
196
  broker::Manager::Delete();
278
  broker::Manager::Delete();
197
  vm::Manager::Delete();
279
  vm::Manager::Delete();
198
  net::Manager::Delete();
280
  net::Manager::Delete();
199
  storage::Manager::Delete();
281
  storage::Manager::Delete();
200
 
282
 
201
  log::CloseFile();
283
  log::CloseFile();
202
}
284
}
203
 
285
 
204
void Handler(int status)
286
void Handler(int status)
205
{
287
{
206
  std::string signal_name = "Unknown";
288
  std::string signal_name = "Unknown";
207
 
289
 
208
  switch (status)
290
  switch (status)
209
  {
291
  {
210
    case SIGTERM:
292
    case SIGTERM:
211
    {
293
    {
212
      signal_name = "Terminate";
294
      signal_name = "Terminate";
213
      break;
295
      break;
214
    }
296
    }
215
 
297
 
216
    case SIGINT:
298
    case SIGINT:
217
    {
299
    {
218
      signal_name = "Interrupt";
300
      signal_name = "Interrupt";
219
      break;
301
      break;
220
    }
302
    }
221
 
303
 
222
    case SIGQUIT:
304
    case SIGQUIT:
223
    {
305
    {
224
      signal_name = "Quit";
306
      signal_name = "Quit";
225
      break;
307
      break;
226
    }
308
    }
227
 
309
 
228
    case SIGABRT:
310
    case SIGABRT:
229
    {
311
    {
230
      signal_name = "Abort";
312
      signal_name = "Abort";
231
      break;
313
      break;
232
    }
314
    }
233
 
315
 
234
    case SIGIO:
316
    case SIGIO:
235
    {
317
    {
236
      signal_name = "I/O";
318
      signal_name = "I/O";
237
      break;
319
      break;
238
    }
320
    }
239
 
321
 
240
    case SIGPIPE:
322
    case SIGPIPE:
241
    {
323
    {
242
      signal_name = "Pipe";
324
      signal_name = "Pipe";
243
      break;
325
      break;
244
    }
326
    }
245
  }
327
  }
246
 
328
 
247
  log::Info(log_module_, "Received signal %s (%d).", signal_name.c_str(), status);
329
  log::Info(log_module_, "Received signal %s (%d).", signal_name.c_str(), status);
248
 
330
 
249
  if (status != SIGPIPE)
331
  if (status != SIGPIPE)
250
  {
332
  {
251
    on_message_condition.notify_all();
333
    on_message_condition.notify_all();
252
  }
334
  }
253
}
335
}
254
 
336