Subversion Repositories HomeAutomation

Rev

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

Rev 1989 Rev 2107
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"
44
#include "net/GetFile.h"
45
#include "net/Url.h"
45
#include "net/Url.h"
46
 
46
 
47
#include "vm/plugin/System.h"
47
#include "vm/plugin/System.h"
48
#include "vm/plugin/Timer.h"
48
#include "vm/plugin/Timer.h"
49
#include "vm/plugin/Module.h"
49
#include "vm/plugin/Module.h"
50
#include "vm/plugin/Node.h"
50
#include "vm/plugin/Node.h"
51
#include "vm/plugin/Console.h"
51
#include "vm/plugin/Console.h"
52
#include "vm/plugin/Storage.h"
52
#include "vm/plugin/Storage.h"
53
#include "vm/plugin/Socket.h"
53
#include "vm/plugin/Socket.h"
-
 
54
#include "vm/plugin/JsPipe.h"
54
 
55
 
55
#ifdef USE_PLUGIN_XORG
56
#ifdef USE_PLUGIN_XORG
56
#include "vm/plugin/Xorg.h"
57
#include "vm/plugin/Xorg.h"
57
#endif // USE_PLUGIN_XORG
58
#endif // USE_PLUGIN_XORG
58
 
59
 
59
#ifdef USE_PLUGIN_MYSQL
60
#ifdef USE_PLUGIN_MYSQL
60
#include "vm/plugin/MySql.h"
61
#include "vm/plugin/MySql.h"
61
#endif // USE_PLUGIN_MYSQL
62
#endif // USE_PLUGIN_MYSQL
62
 
63
 
63
using namespace atom;
64
using namespace atom;
64
 
65
 
65
logging::Logger LOG("Main"); // Deprecated
66
logging::Logger LOG("Main"); // Deprecated
66
static const std::string log_module_ = "main";
67
static const std::string log_module_ = "main";
67
std::vector<broker::Subscriber::Pointer> subscribers;
68
std::vector<broker::Subscriber::Pointer> subscribers;
68
boost::condition on_message_condition;
69
boost::condition on_message_condition;
69
boost::mutex guard_mutex;
70
boost::mutex guard_mutex;
70
net::GetFile::Pointer downloader;
71
net::GetFile::Pointer downloader;
71
 
72
 
72
void ContinueInitialization(std::string protocol_filename);
73
void ContinueInitialization(std::string protocol_filename);
73
void Handler(int status);
74
void Handler(int status);
74
void CleanUp();
75
void CleanUp();
75
 
76
 
76
 
77
 
77
 
78
 
78
 
79
 
79
void HandleProtocolFileDownload(bool success, net::Url url, std::string temporary_filepath)
80
void HandleProtocolFileDownload(bool success, net::Url url, std::string temporary_filepath)
80
{
81
{
81
  if (success)
82
  if (success)
82
  {
83
  {
83
    log::Info(log_module_, "Successfully downloaded protocol file!");
84
    log::Info(log_module_, "Successfully downloaded protocol file!");
84
    ContinueInitialization(temporary_filepath);
85
    ContinueInitialization(temporary_filepath);
85
  }
86
  }
86
  else
87
  else
87
  {
88
  {
88
    ContinueInitialization(url.GetRaw());
89
    ContinueInitialization(url.GetRaw());
89
  }
90
  }
90
}
91
}
91
 
92
 
92
int main(int argc, char **argv)
93
int main(int argc, char **argv)
93
{
94
{
94
  /* Set core dump parameters */
95
  /* Set core dump parameters */
95
  rlimit core_limit = { RLIM_INFINITY, RLIM_INFINITY };
96
  rlimit core_limit = { RLIM_INFINITY, RLIM_INFINITY };
96
  setrlimit( RLIMIT_CORE, &core_limit ); // enable core dumps
97
  setrlimit( RLIMIT_CORE, &core_limit ); // enable core dumps
97
 
98
 
98
 
99
 
99
  /* Print startup information */
100
  /* Print startup information */
100
  log::Info(log_module_, "Atom daemon, version %s starting...", VERSION);
101
  log::Info(log_module_, "Atom daemon, version %s starting...", VERSION);
101
  log::Info(log_module_, "Released under %s.", LICENSE);
102
  log::Info(log_module_, "Released under %s.", LICENSE);
102
  log::Info(log_module_, "Written by Mattias Runge 2010-2012.");
103
  log::Info(log_module_, "Written by Mattias Runge 2010-2012.");
103
 
104
 
104
 
105
 
105
  /* Register signal handlers */
106
  /* Register signal handlers */
106
  signal(SIGTERM, Handler);
107
  signal(SIGTERM, Handler);
107
  signal(SIGINT,  Handler);
108
  signal(SIGINT,  Handler);
108
  signal(SIGQUIT, Handler);
109
  signal(SIGQUIT, Handler);
109
  signal(SIGABRT, Handler);
110
  signal(SIGABRT, Handler);
110
  signal(SIGPIPE, Handler);
111
  signal(SIGPIPE, Handler);
111
 
112
 
112
 
113
 
113
  /* Check configuration */
114
  /* Check configuration */
114
  config::Manager::Create();
115
  config::Manager::Create();
115
 
116
 
116
  if (!config::Manager::Instance()->Set(argc, argv))
117
  if (!config::Manager::Instance()->Set(argc, argv))
117
  {
118
  {
118
    CleanUp();
119
    CleanUp();
119
    return EXIT_SUCCESS;
120
    return EXIT_SUCCESS;
120
  }
121
  }
121
 
122
 
122
 
123
 
123
  /* Set up logging */
124
  /* Set up logging */
124
  if (config::Manager::Instance()->Exist("LogFile"))
125
  if (config::Manager::Instance()->Exist("LogFile"))
125
  {
126
  {
126
    log::OpenFile(config::Manager::Instance()->GetAsString("LogFile"));
127
    log::OpenFile(config::Manager::Instance()->GetAsString("LogFile"));
127
  }
128
  }
128
 
129
 
129
  if (config::Manager::Instance()->Exist("LogLevelMask"))
130
  if (config::Manager::Instance()->Exist("LogLevelMask"))
130
  {
131
  {
131
    log::SetLevelByString(config::Manager::Instance()->GetAsString("LogLevelMask"));
132
    log::SetLevelByString(config::Manager::Instance()->GetAsString("LogLevelMask"));
132
  }
133
  }
133
 
134
 
134
 
135
 
135
  /* Enter daemon mode if requested */
136
  /* Enter daemon mode if requested */
136
  if (config::Manager::Instance()->Exist("daemon"))
137
  if (config::Manager::Instance()->Exist("daemon"))
137
  {
138
  {
138
    LOG.Info("Entering daemon mode...");
139
    LOG.Info("Entering daemon mode...");
139
 
140
 
140
    if (daemon(0, 0) == -1)
141
    if (daemon(0, 0) == -1)
141
    {
142
    {
142
      log::Error(log_module_, "Could not enter daemon mode. Exiting...");
143
      log::Error(log_module_, "Could not enter daemon mode. Exiting...");
143
      CleanUp();
144
      CleanUp();
144
      return EXIT_FAILURE;
145
      return EXIT_FAILURE;
145
    }
146
    }
146
 
147
 
147
    log::Info(log_module_, "Deamon mode entered successfully!");
148
    log::Info(log_module_, "Deamon mode entered successfully!");
148
  }
149
  }
149
 
150
 
150
 
151
 
151
  /* Create singletons */
152
  /* Create singletons */
152
  storage::Manager::Create();
153
  storage::Manager::Create();
153
  timer::Manager::Create();
154
  timer::Manager::Create();
154
  broker::Manager::Create();
155
  broker::Manager::Create();
155
  net::Manager::Create();
156
  net::Manager::Create();
156
  can::Protocol::Create();
157
  can::Protocol::Create();
157
  control::Manager::Create();
158
  control::Manager::Create();
158
  vm::Manager::Create();
159
  vm::Manager::Create();
159
 
160
 
160
 
161
 
161
 
162
 
162
  std::string protocol_filename = config::Manager::Instance()->GetAsString("ProtocolFile");
163
  std::string protocol_filename = config::Manager::Instance()->GetAsString("ProtocolFile");
163
 
164
 
164
  try
165
  try
165
  {
166
  {
166
    net::Url url(protocol_filename);
167
    net::Url url(protocol_filename);
167
   
168
   
168
    downloader = net::GetFile::Pointer(new net::GetFile());
169
    downloader = net::GetFile::Pointer(new net::GetFile());
169
   
170
   
170
    log::Info(log_module_, "Will try to download protocol file from \"%s\"...", protocol_filename.data());
171
    log::Info(log_module_, "Will try to download protocol file from \"%s\"...", protocol_filename.data());
171
   
172
   
172
    downloader->Start(url, HandleProtocolFileDownload);
173
    downloader->Start(url, HandleProtocolFileDownload);
173
  }
174
  }
174
  catch (std::exception& exception)
175
  catch (std::exception& exception)
175
  {
176
  {
176
    log::Debug(log_module_, "Could not parse ProtocolFile as a URL, \"%s\".", protocol_filename.data());
177
    log::Debug(log_module_, "Could not parse ProtocolFile as a URL, \"%s\".", protocol_filename.data());
177
    ContinueInitialization(protocol_filename);
178
    ContinueInitialization(protocol_filename);
178
  }
179
  }
179
 
180
 
180
 
181
 
181
  /* Lock main thread and wait for exit */
182
  /* Lock main thread and wait for exit */
182
  boost::mutex::scoped_lock guard(guard_mutex);
183
  boost::mutex::scoped_lock guard(guard_mutex);
183
 
184
 
184
  on_message_condition.wait(guard);
185
  on_message_condition.wait(guard);
185
 
186
 
186
 
187
 
187
  /* Cleanup */
188
  /* Cleanup */
188
  log::Info(log_module_, "Cleaning up...");
189
  log::Info(log_module_, "Cleaning up...");
189
 
190
 
190
  CleanUp();
191
  CleanUp();
191
 
192
 
192
  log::Info(log_module_, "Thank you for using Atom. Goodbye!");
193
  log::Info(log_module_, "Thank you for using Atom. Goodbye!");
193
 
194
 
194
  return EXIT_SUCCESS;
195
  return EXIT_SUCCESS;
195
}
196
}
196
 
197
 
197
void ContinueInitialization(std::string protocol_filename)
198
void ContinueInitialization(std::string protocol_filename)
198
{  
199
{  
199
  /* Load protocol file */
200
  /* Load protocol file */
200
  if (!can::Protocol::Instance()->Load(protocol_filename))
201
  if (!can::Protocol::Instance()->Load(protocol_filename))
201
  {
202
  {
202
    log::Error(log_module_, "Failed to load %s!", protocol_filename.data());
203
    log::Error(log_module_, "Failed to load %s!", protocol_filename.data());
203
 
204
 
204
    on_message_condition.notify_all();
205
    on_message_condition.notify_all();
205
  }
206
  }
206
 
207
 
207
 
208
 
208
  /* Set up storage path */
209
  /* Set up storage path */
209
  storage::Manager::Instance()->SetRootPath(config::Manager::Instance()->GetAsString("StoragePath"));
210
  storage::Manager::Instance()->SetRootPath(config::Manager::Instance()->GetAsString("StoragePath"));
210
 
211
 
211
 
212
 
212
  /* Start monitor server */
213
  /* Start monitor server */
213
  if (config::Manager::Instance()->Exist("MonitorPort"))
214
  if (config::Manager::Instance()->Exist("MonitorPort"))
214
  {
215
  {
215
    subscribers.push_back(can::Monitor::Pointer(new can::Monitor(config::Manager::Instance()->GetAsInt("MonitorPort"))));
216
    subscribers.push_back(can::Monitor::Pointer(new can::Monitor(config::Manager::Instance()->GetAsInt("MonitorPort"))));
216
  }
217
  }
217
 
218
 
218
 
219
 
219
  /* Start CanDaemon server */
220
  /* Start CanDaemon server */
220
  if (config::Manager::Instance()->Exist("DaemonPort"))
221
  if (config::Manager::Instance()->Exist("DaemonPort"))
221
  {
222
  {
222
    subscribers.push_back(can::CanDaemon::Pointer(new can::CanDaemon(config::Manager::Instance()->GetAsInt("DaemonPort"))));
223
    subscribers.push_back(can::CanDaemon::Pointer(new can::CanDaemon(config::Manager::Instance()->GetAsInt("DaemonPort"))));
223
  }
224
  }
224
 
225
 
225
 
226
 
226
  /* Subscribe Can control manager to data */
227
  /* Subscribe Can control manager to data */
227
  subscribers.push_back(control::Manager::Instance());
228
  subscribers.push_back(control::Manager::Instance());
228
 
229
 
229
 
230
 
230
  /* Start VM plugins */
231
  /* Start VM plugins */
231
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::System(vm::Manager::Instance()->GetIoService())));
232
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::System(vm::Manager::Instance()->GetIoService())));
232
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Console(vm::Manager::Instance()->GetIoService(), config::Manager::Instance()->GetAsInt("CommandPort"))));
233
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Console(vm::Manager::Instance()->GetIoService(), config::Manager::Instance()->GetAsInt("CommandPort"))));
-
 
234
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::JsPipe(vm::Manager::Instance()->GetIoService(), config::Manager::Instance()->GetAsInt("JsPipePort"))));
233
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Storage(vm::Manager::Instance()->GetIoService())));
235
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Storage(vm::Manager::Instance()->GetIoService())));
234
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Timer(vm::Manager::Instance()->GetIoService())));
236
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Timer(vm::Manager::Instance()->GetIoService())));
235
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Module(vm::Manager::Instance()->GetIoService())));
237
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Module(vm::Manager::Instance()->GetIoService())));
236
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Node(vm::Manager::Instance()->GetIoService())));
238
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Node(vm::Manager::Instance()->GetIoService())));
237
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Socket(vm::Manager::Instance()->GetIoService())));
239
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Socket(vm::Manager::Instance()->GetIoService())));
238
 
240
 
239
#ifdef USE_PLUGIN_XORG
241
#ifdef USE_PLUGIN_XORG
240
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Xorg(vm::Manager::Instance()->GetIoService())));
242
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Xorg(vm::Manager::Instance()->GetIoService())));
241
#endif // USE_PLUGIN_XORG
243
#endif // USE_PLUGIN_XORG
242
 
244
 
243
#ifdef USE_PLUGIN_MYSQL
245
#ifdef USE_PLUGIN_MYSQL
244
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::MySql(vm::Manager::Instance()->GetIoService())));
246
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::MySql(vm::Manager::Instance()->GetIoService())));
245
#endif // USE_PLUGIN_MYSQL
247
#endif // USE_PLUGIN_MYSQL
246
 
248
 
247
 
249
 
248
  /* Start VM */
250
  /* Start VM */
249
  vm::Manager::Instance()->Start(config::Manager::Instance()->GetAsString("ScriptPath"), config::Manager::Instance()->GetAsString("UserScriptPath"));
251
  vm::Manager::Instance()->Start(config::Manager::Instance()->GetAsString("ScriptPath"), config::Manager::Instance()->GetAsString("UserScriptPath"));
250
 
252
 
251
 
253
 
252
  /* Connect to Can network */
254
  /* Connect to Can network */
253
  if (config::Manager::Instance()->Exist("CanNet"))
255
  if (config::Manager::Instance()->Exist("CanNet"))
254
  {
256
  {
255
    common::StringList cannetworks = config::Manager::Instance()->GetAsStringVector("CanNet");
257
    common::StringList cannetworks = config::Manager::Instance()->GetAsStringVector("CanNet");
256
 
258
 
257
    for (uint n = 0; n < cannetworks.size(); n++)
259
    for (uint n = 0; n < cannetworks.size(); n++)
258
    {
260
    {
259
      subscribers.push_back(can::Network::Pointer(new can::Network(cannetworks[n])));
261
      subscribers.push_back(can::Network::Pointer(new can::Network(cannetworks[n])));
260
    }
262
    }
261
  }
263
  }
262
}
264
}
263
 
265
 
264
void CleanUp()
266
void CleanUp()
265
{
267
{
266
  downloader.reset();
268
  downloader.reset();
267
 
269
 
268
  if (net::Manager::Instance().use_count() > 0)
270
  if (net::Manager::Instance().use_count() > 0)
269
  {
271
  {
270
    net::Manager::Instance()->Stop();
272
    net::Manager::Instance()->Stop();
271
  }
273
  }
272
 
274
 
273
  subscribers.clear();
275
  subscribers.clear();
274
  config::Manager::Delete();
276
  config::Manager::Delete();
275
  timer::Manager::Delete();
277
  timer::Manager::Delete();
276
  control::Manager::Delete();
278
  control::Manager::Delete();
277
  can::Protocol::Delete();
279
  can::Protocol::Delete();
278
  broker::Manager::Delete();
280
  broker::Manager::Delete();
279
  vm::Manager::Delete();
281
  vm::Manager::Delete();
280
  net::Manager::Delete();
282
  net::Manager::Delete();
281
  storage::Manager::Delete();
283
  storage::Manager::Delete();
282
 
284
 
283
  log::CloseFile();
285
  log::CloseFile();
284
}
286
}
285
 
287
 
286
void Handler(int status)
288
void Handler(int status)
287
{
289
{
288
  std::string signal_name = "Unknown";
290
  std::string signal_name = "Unknown";
289
 
291
 
290
  switch (status)
292
  switch (status)
291
  {
293
  {
292
    case SIGTERM:
294
    case SIGTERM:
293
    {
295
    {
294
      signal_name = "Terminate";
296
      signal_name = "Terminate";
295
      break;
297
      break;
296
    }
298
    }
297
 
299
 
298
    case SIGINT:
300
    case SIGINT:
299
    {
301
    {
300
      signal_name = "Interrupt";
302
      signal_name = "Interrupt";
301
      break;
303
      break;
302
    }
304
    }
303
 
305
 
304
    case SIGQUIT:
306
    case SIGQUIT:
305
    {
307
    {
306
      signal_name = "Quit";
308
      signal_name = "Quit";
307
      break;
309
      break;
308
    }
310
    }
309
 
311
 
310
    case SIGABRT:
312
    case SIGABRT:
311
    {
313
    {
312
      signal_name = "Abort";
314
      signal_name = "Abort";
313
      break;
315
      break;
314
    }
316
    }
315
 
317
 
316
    case SIGIO:
318
    case SIGIO:
317
    {
319
    {
318
      signal_name = "I/O";
320
      signal_name = "I/O";
319
      break;
321
      break;
320
    }
322
    }
321
 
323
 
322
    case SIGPIPE:
324
    case SIGPIPE:
323
    {
325
    {
324
      signal_name = "Pipe";
326
      signal_name = "Pipe";
325
      break;
327
      break;
326
    }
328
    }
327
  }
329
  }
328
 
330
 
329
  log::Info(log_module_, "Received signal %s (%d).", signal_name.c_str(), status);
331
  log::Info(log_module_, "Received signal %s (%d).", signal_name.c_str(), status);
330
 
332
 
331
  if (status != SIGPIPE)
333
  if (status != SIGPIPE)
332
  {
334
  {
333
    on_message_condition.notify_all();
335
    on_message_condition.notify_all();
334
  }
336
  }
335
}
337
}
336
 
338