Subversion Repositories HomeAutomation

Rev

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