Subversion Repositories HomeAutomation

Rev

Rev 1959 | Rev 1964 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1959 Rev 1961
Line 26... Line 26...
26
#include <boost/thread/condition.hpp>
26
#include <boost/thread/condition.hpp>
27
#include <boost/thread/locks.hpp>
27
#include <boost/thread/locks.hpp>
28
 
28
 
29
#include "config.h"
29
#include "config.h"
30
 
30
 
-
 
31
#include "logging/Logger.h" // Deprecated
31
#include "logging/Logger.h"
32
#include "common/log.h"
32
#include "net/Manager.h"
33
#include "net/Manager.h"
33
#include "timer/Manager.h"
34
#include "timer/Manager.h"
34
#include "config/Manager.h"
35
#include "config/Manager.h"
35
#include "can/Protocol.h"
36
#include "can/Protocol.h"
36
#include "broker/Manager.h"
37
#include "broker/Manager.h"
Line 57... Line 58...
57
#include "vm/plugin/MySql.h"
58
#include "vm/plugin/MySql.h"
58
#endif // USE_PLUGIN_MYSQL
59
#endif // USE_PLUGIN_MYSQL
59
 
60
 
60
using namespace atom;
61
using namespace atom;
61
 
62
 
62
logging::Logger LOG("Main");
63
logging::Logger LOG("Main"); // Deprecated
-
 
64
static const std::string log_module_ = "main";
63
std::vector<broker::Subscriber::Pointer> subscribers;
65
std::vector<broker::Subscriber::Pointer> subscribers;
64
boost::condition on_message_condition;
66
boost::condition on_message_condition;
65
boost::mutex guard_mutex;
67
boost::mutex guard_mutex;
66
 
68
 
67
void Handler(int status);
69
void Handler(int status);
Line 70... Line 72...
70
int main(int argc, char **argv)
72
int main(int argc, char **argv)
71
{
73
{
72
  rlimit core_limit = { RLIM_INFINITY, RLIM_INFINITY };
74
  rlimit core_limit = { RLIM_INFINITY, RLIM_INFINITY };
73
  setrlimit( RLIMIT_CORE, &core_limit ); // enable core dumps
75
  setrlimit( RLIMIT_CORE, &core_limit ); // enable core dumps
74
     
76
     
75
 
-
 
76
  LOG.Info("\033[29;1mAtom Daemon, version " + std::string(VERSION) + " starting...\033[0m");
77
  log::Info(log_module_, "Atom daemon, version %s starting...", VERSION);
77
  LOG.Info("\033[29;1mReleased under " + std::string(LICENSE) + ".\033[0m");
78
  log::Info(log_module_, "Released under %s.", LICENSE);
78
  LOG.Info("Written by Mattias Runge 2010-2012.");
79
  log::Info(log_module_, "Written by Mattias Runge 2010-2012.");
79
 
80
 
80
  signal(SIGTERM, Handler);
81
  signal(SIGTERM, Handler);
81
  signal(SIGINT, Handler);
82
  signal(SIGINT,  Handler);
82
  signal(SIGQUIT, Handler);
83
  signal(SIGQUIT, Handler);
83
  signal(SIGABRT, Handler);
84
  signal(SIGABRT, Handler);
84
  signal(SIGPIPE, Handler);
85
  signal(SIGPIPE, Handler);
85
 
86
 
86
  config::Manager::Create();
87
  config::Manager::Create();
87
 
88
 
88
  if (!config::Manager::Instance()->Set(argc, argv))
89
  if (!config::Manager::Instance()->Set(argc, argv))
89
  {
90
  {
90
    CleanUp();
91
    CleanUp();
91
    return EXIT_SUCCESS;
92
    return EXIT_SUCCESS;
92
  }
93
  }
93
 
94
 
94
  if (config::Manager::Instance()->Exist("LogFile"))
95
  if (config::Manager::Instance()->Exist("LogFile"))
95
  {
96
  {
96
    logging::Logger::OpenFile(config::Manager::Instance()->GetAsString("LogFile"));
97
    logging::Logger::OpenFile(config::Manager::Instance()->GetAsString("LogFile"));
97
  }
98
  }
98
 
99
 
99
  if (config::Manager::Instance()->Exist("LogLevel"))
100
  if (config::Manager::Instance()->Exist("LogLevelMask"))
-
 
101
  {
-
 
102
    log::SetLogLevelByString(config::Manager::Instance()->GetAsString("LogLevelMask"));
-
 
103
  }
-
 
104
 
-
 
105
  if (config::Manager::Instance()->Exist("LogLevel")) // Deprecated
100
  {
106
  {
101
    logging::Logger::SetLevel((logging::Logger::Level) config::Manager::Instance()->GetAsInt("LogLevel"));
107
    logging::Logger::SetLevel((logging::Logger::Level) config::Manager::Instance()->GetAsInt("LogLevel"));
102
  }
108
  }
103
 
109
 
104
  if (config::Manager::Instance()->Exist("daemon"))
110
  if (config::Manager::Instance()->Exist("daemon"))
105
  {
111
  {
106
    LOG.Info("Entering daemon mode...");
112
    LOG.Info("Entering daemon mode...");
107
 
113
 
108
    if (daemon(0, 0) == -1)
114
    if (daemon(0, 0) == -1)
109
    {
115
    {
110
      LOG.Error("Could not enter daemon mode. Exiting...");
116
      log::Error(log_module_, "Could not enter daemon mode. Exiting...");
111
      CleanUp();
117
      CleanUp();
112
      return EXIT_FAILURE;
118
      return EXIT_FAILURE;
113
    }
119
    }
114
 
120
 
115
    LOG.Info("Deamon mode entered successfully!");
121
    log::Info(log_module_, "Deamon mode entered successfully!");
116
  }
122
  }
117
 
123
 
118
  storage::Manager::Create();
124
  storage::Manager::Create();
119
  timer::Manager::Create();
125
  timer::Manager::Create();
120
  broker::Manager::Create();
126
  broker::Manager::Create();
Line 162... Line 168...
162
    common::StringList cannetworks = config::Manager::Instance()->GetAsStringVector("CanNet");
168
    common::StringList cannetworks = config::Manager::Instance()->GetAsStringVector("CanNet");
163
 
169
 
164
    for (uint n = 0; n < cannetworks.size(); n++)
170
    for (uint n = 0; n < cannetworks.size(); n++)
165
    {
171
    {
166
      subscribers.push_back(can::Network::Pointer(new can::Network(cannetworks[n])));
172
      subscribers.push_back(can::Network::Pointer(new can::Network(cannetworks[n])));
167
    }
173
    }
168
  }
174
  }
169
 
175
 
170
  boost::mutex::scoped_lock guard(guard_mutex);
176
  boost::mutex::scoped_lock guard(guard_mutex);
171
 
-
 
172
  on_message_condition.wait(guard);
-
 
173
 
177
 
174
  LOG.Info("Cleaning up...");
178
  on_message_condition.wait(guard);
175
 
-
 
176
  CleanUp();
-
 
177
 
179
 
-
 
180
  log::Info(log_module_, "Cleaning up...");
-
 
181
 
-
 
182
  CleanUp();
-
 
183
 
178
  LOG.Info("Thank you for using Atom. Goodbye!");
184
  log::Info(log_module_, "Thank you for using Atom. Goodbye!");
179
 
185
 
180
  return EXIT_SUCCESS;
186
  return EXIT_SUCCESS;
181
}
187
}
182
 
188
 
183
void CleanUp()
189
void CleanUp()
-
 
190
{
-
 
191
  if (net::Manager::Instance().use_count() > 0)
184
{
192
  {
185
  net::Manager::Instance()->Stop();
193
    net::Manager::Instance()->Stop();
-
 
194
  }
186
 
195
 
187
  subscribers.clear();
196
  subscribers.clear();
188
  config::Manager::Delete();
197
  config::Manager::Delete();
189
  timer::Manager::Delete();
198
  timer::Manager::Delete();
190
  control::Manager::Delete();
199
  control::Manager::Delete();
Line 236... Line 245...
236
      signal_name = "Pipe";
245
      signal_name = "Pipe";
237
      break;
246
      break;
238
    }
247
    }
239
  }
248
  }
240
 
249
 
241
  LOG.Debug("Received signal " + signal_name + "(" + boost::lexical_cast<std::string>(status) + ").");
250
  log::Info(log_module_, "Received signal %s (%d).", signal_name.c_str(), status);
242
 
251
 
243
  if (status != SIGPIPE)
252
  if (status != SIGPIPE)
244
  {
253
  {
245
    on_message_condition.notify_all();
254
    on_message_condition.notify_all();
246
  }
255
  }