Subversion Repositories HomeAutomation

Rev

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

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