Subversion Repositories HomeAutomation

Rev

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

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