Subversion Repositories HomeAutomation

Rev

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

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