Subversion Repositories HomeAutomation

Rev

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