Subversion Repositories HomeAutomation

Rev

Rev 2107 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

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