Subversion Repositories HomeAutomation

Rev

Rev 1961 | Rev 1989 | 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
  {
1964 runge 97
    log::OpenFile(config::Manager::Instance()->GetAsString("LogFile"));
1939 runge 98
  }
99
 
1961 runge 100
  if (config::Manager::Instance()->Exist("LogLevelMask"))
1939 runge 101
  {
1964 runge 102
    log::SetLevelByString(config::Manager::Instance()->GetAsString("LogLevelMask"));
1961 runge 103
  }
1939 runge 104
 
1945 runge 105
  if (config::Manager::Instance()->Exist("daemon"))
1939 runge 106
  {
1945 runge 107
    LOG.Info("Entering daemon mode...");
1939 runge 108
 
1945 runge 109
    if (daemon(0, 0) == -1)
1594 runge 110
    {
1961 runge 111
      log::Error(log_module_, "Could not enter daemon mode. Exiting...");
1939 runge 112
      CleanUp();
113
      return EXIT_FAILURE;
1594 runge 114
    }
1939 runge 115
 
1961 runge 116
    log::Info(log_module_, "Deamon mode entered successfully!");
1939 runge 117
  }
118
 
119
  storage::Manager::Create();
120
  timer::Manager::Create();
121
  broker::Manager::Create();
122
  net::Manager::Create();
123
  can::Protocol::Create();
124
  control::Manager::Create();
125
  vm::Manager::Create();
126
 
1945 runge 127
  storage::Manager::Instance()->SetRootPath(config::Manager::Instance()->GetAsString("StoragePath"));
1939 runge 128
 
1945 runge 129
  can::Protocol::Instance()->Load(config::Manager::Instance()->GetAsString("ProtocolFile"));
1939 runge 130
 
1945 runge 131
  if (config::Manager::Instance()->Exist("MonitorPort"))
1939 runge 132
  {
1945 runge 133
    subscribers.push_back(can::Monitor::Pointer(new can::Monitor(config::Manager::Instance()->GetAsInt("MonitorPort"))));
1939 runge 134
  }
135
 
1945 runge 136
  if (config::Manager::Instance()->Exist("DaemonPort"))
1939 runge 137
  {
1945 runge 138
    subscribers.push_back(can::CanDaemon::Pointer(new can::CanDaemon(config::Manager::Instance()->GetAsInt("DaemonPort"))));
1939 runge 139
  }
140
 
1945 runge 141
  subscribers.push_back(control::Manager::Instance());
1939 runge 142
 
1945 runge 143
  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"))));
145
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Storage(vm::Manager::Instance()->GetIoService())));
146
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Timer(vm::Manager::Instance()->GetIoService())));
147
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Module(vm::Manager::Instance()->GetIoService())));
148
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Node(vm::Manager::Instance()->GetIoService())));
149
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Socket(vm::Manager::Instance()->GetIoService())));
1939 runge 150
 
151
#ifdef USE_PLUGIN_XORG
1945 runge 152
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Xorg(vm::Manager::Instance()->GetIoService())));
1939 runge 153
#endif // USE_PLUGIN_XORG
1945 runge 154
 
155
#ifdef USE_PLUGIN_MYSQL
156
  vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::MySql(vm::Manager::Instance()->GetIoService())));
157
#endif // USE_PLUGIN_MYSQL
1939 runge 158
 
1945 runge 159
  vm::Manager::Instance()->Start(config::Manager::Instance()->GetAsString("ScriptPath"), config::Manager::Instance()->GetAsString("UserScriptPath"));
1939 runge 160
 
1945 runge 161
  if (config::Manager::Instance()->Exist("CanNet"))
1939 runge 162
  {
1945 runge 163
    common::StringList cannetworks = config::Manager::Instance()->GetAsStringVector("CanNet");
1939 runge 164
 
165
    for (uint n = 0; n < cannetworks.size(); n++)
1599 runge 166
    {
1945 runge 167
      subscribers.push_back(can::Network::Pointer(new can::Network(cannetworks[n])));
1599 runge 168
    }
1939 runge 169
  }
170
 
1945 runge 171
  boost::mutex::scoped_lock guard(guard_mutex);
1939 runge 172
 
1945 runge 173
  on_message_condition.wait(guard);
1939 runge 174
 
1961 runge 175
  log::Info(log_module_, "Cleaning up...");
1939 runge 176
 
177
  CleanUp();
178
 
1961 runge 179
  log::Info(log_module_, "Thank you for using Atom. Goodbye!");
1939 runge 180
 
181
  return EXIT_SUCCESS;
182
}
183
 
184
void CleanUp()
185
{
1961 runge 186
  if (net::Manager::Instance().use_count() > 0)
187
  {
188
    net::Manager::Instance()->Stop();
189
  }
1939 runge 190
 
191
  subscribers.clear();
192
  config::Manager::Delete();
193
  timer::Manager::Delete();
194
  control::Manager::Delete();
195
  can::Protocol::Delete();
196
  broker::Manager::Delete();
197
  vm::Manager::Delete();
198
  net::Manager::Delete();
199
  storage::Manager::Delete();
1964 runge 200
 
201
  log::CloseFile();
1939 runge 202
}
203
 
1945 runge 204
void Handler(int status)
1939 runge 205
{
206
  std::string signal_name = "Unknown";
207
 
208
  switch (status)
209
  {
210
    case SIGTERM:
1599 runge 211
    {
1939 runge 212
      signal_name = "Terminate";
213
      break;
1599 runge 214
    }
1939 runge 215
 
216
    case SIGINT:
1599 runge 217
    {
1940 runge 218
      signal_name = "Interrupt";
1939 runge 219
      break;
1599 runge 220
    }
221
 
1939 runge 222
    case SIGQUIT:
1594 runge 223
    {
1939 runge 224
      signal_name = "Quit";
225
      break;
1594 runge 226
    }
1914 linlun 227
 
1939 runge 228
    case SIGABRT:
1914 linlun 229
    {
1939 runge 230
      signal_name = "Abort";
231
      break;
1914 linlun 232
    }
1939 runge 233
 
234
    case SIGIO:
1594 runge 235
    {
1939 runge 236
      signal_name = "I/O";
237
      break;
1594 runge 238
    }
1598 runge 239
 
1939 runge 240
    case SIGPIPE:
1599 runge 241
    {
1939 runge 242
      signal_name = "Pipe";
243
      break;
1599 runge 244
    }
1939 runge 245
  }
246
 
1961 runge 247
  log::Info(log_module_, "Received signal %s (%d).", signal_name.c_str(), status);
1939 runge 248
 
249
  if (status != SIGPIPE)
250
  {
251
    on_message_condition.notify_all();
252
  }
1675 arune 253
}