Subversion Repositories HomeAutomation

Rev

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

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