Subversion Repositories HomeAutomation

Rev

Rev 1647 | Rev 1652 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1647 Rev 1651
1
/*
1
/*
2
 *
2
 *
3
 *  Copyright (C) 2010  Mattias Runge
3
 *  Copyright (C) 2010  Mattias Runge
4
 *
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
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
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
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
8
 *  (at your option) any later version.
9
 *
9
 *
10
 *  This program is distributed in the hope that it will be useful,
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
13
 *  GNU General Public License for more details.
14
 *
14
 *
15
 *  You should have received a copy of the GNU General Public License along
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.,
16
 *  with this program; if not, write to the Free Software Foundation, Inc.,
17
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
18
 *
19
 */
19
 */
20
 
20
 
21
#include <signal.h>
21
#include <signal.h>
22
 
22
 
23
#include <boost/program_options.hpp>
23
#include <boost/program_options.hpp>
24
#include <boost/thread/mutex.hpp>
24
#include <boost/thread/mutex.hpp>
25
#include <boost/thread/condition.hpp>
25
#include <boost/thread/condition.hpp>
26
#include <boost/thread/locks.hpp>
26
#include <boost/thread/locks.hpp>
27
 
27
 
28
#include "logging/Logger.h"
28
#include "logging/Logger.h"
29
#include "net/Manager.h"
29
#include "net/Manager.h"
30
#include "timer/Manager.h"
30
#include "timer/Manager.h"
31
#include "config/Manager.h"
31
#include "config/Manager.h"
32
#include "can/Protocol.h"
32
#include "can/Protocol.h"
33
#include "broker/Manager.h"
33
#include "broker/Manager.h"
34
#include "control/Manager.h"
34
#include "control/Manager.h"
35
#include "can/Network.h"
35
#include "can/Network.h"
36
#include "can/Monitor.h"
36
#include "can/Monitor.h"
37
#include "vm/Manager.h"
37
#include "vm/Manager.h"
38
#include "storage/Manager.h"
38
#include "storage/Manager.h"
39
 
39
 
40
#include "vm/plugin/System.h"
40
#include "vm/plugin/System.h"
41
#include "vm/plugin/Timer.h"
41
#include "vm/plugin/Timer.h"
42
#include "vm/plugin/Module.h"
42
#include "vm/plugin/Module.h"
43
#include "vm/plugin/Console.h"
43
#include "vm/plugin/Console.h"
44
#include "vm/plugin/Storage.h"
44
#include "vm/plugin/Storage.h"
45
#include "vm/plugin/Xorg.h"
45
#include "vm/plugin/Xorg.h"
-
 
46
#include "vm/plugin/Socket.h"
46
 
47
 
47
using namespace atom;
48
using namespace atom;
48
 
49
 
49
logging::Logger LOG("Main");
50
logging::Logger LOG("Main");
50
std::vector<broker::Subscriber::Pointer> subscribers;
51
std::vector<broker::Subscriber::Pointer> subscribers;
51
boost::condition on_message_condition;
52
boost::condition on_message_condition;
52
boost::mutex guard_mutex;
53
boost::mutex guard_mutex;
53
 
54
 
54
void Handler(int status);
55
void Handler(int status);
55
void CleanUp();
56
void CleanUp();
56
 
57
 
57
int main(int argc, char **argv)
58
int main(int argc, char **argv)
58
{
59
{
59
    LOG.Info("Atom Daemon, version 1.5.0 starting...");
60
    LOG.Info("Atom Daemon, version 1.5.0 starting...");
60
    LOG.Info("Written by Mattias Runge 2010.");
61
    LOG.Info("Written by Mattias Runge 2010.");
61
    LOG.Info("Released under GPL version 2.");
62
    LOG.Info("Released under GPL version 2.");
62
   
63
   
63
    signal(SIGTERM, Handler);
64
    signal(SIGTERM, Handler);
64
    signal(SIGINT, Handler);
65
    signal(SIGINT, Handler);
65
    signal(SIGQUIT, Handler);
66
    signal(SIGQUIT, Handler);
66
    signal(SIGABRT, Handler);
67
    signal(SIGABRT, Handler);
67
    signal(SIGPIPE, Handler);
68
    signal(SIGPIPE, Handler);
68
   
69
   
69
    config::Manager::Create();
70
    config::Manager::Create();
70
   
71
   
71
    if (!config::Manager::Instance()->Set(argc, argv))
72
    if (!config::Manager::Instance()->Set(argc, argv))
72
    {
73
    {
73
        CleanUp();
74
        CleanUp();
74
        return EXIT_SUCCESS;
75
        return EXIT_SUCCESS;
75
    }
76
    }
76
   
77
   
77
    if (config::Manager::Instance()->Exist("LogFile"))
78
    if (config::Manager::Instance()->Exist("LogFile"))
78
    {
79
    {
79
        logging::Logger::OpenFile(config::Manager::Instance()->GetAsString("LogFile"));
80
        logging::Logger::OpenFile(config::Manager::Instance()->GetAsString("LogFile"));
80
    }
81
    }
81
   
82
   
82
    if (config::Manager::Instance()->Exist("LogLevel"))
83
    if (config::Manager::Instance()->Exist("LogLevel"))
83
    {
84
    {
84
        logging::Logger::SetLevel((logging::Logger::Level)config::Manager::Instance()->GetAsInt("LogLevel"));
85
        logging::Logger::SetLevel((logging::Logger::Level)config::Manager::Instance()->GetAsInt("LogLevel"));
85
    }
86
    }
86
   
87
   
87
    if (config::Manager::Instance()->Exist("daemon"))
88
    if (config::Manager::Instance()->Exist("daemon"))
88
    {
89
    {
89
        LOG.Info("Entering daemon mode...");
90
        LOG.Info("Entering daemon mode...");
90
       
91
       
91
        if (daemon(0, 0) == -1)
92
        if (daemon(0, 0) == -1)
92
        {
93
        {
93
            LOG.Error("Could not enter daemon mode. Exiting...");
94
            LOG.Error("Could not enter daemon mode. Exiting...");
94
            CleanUp();
95
            CleanUp();
95
            return EXIT_FAILURE;
96
            return EXIT_FAILURE;
96
        }
97
        }
97
       
98
       
98
        LOG.Info("Deamon mode entered successfully!");
99
        LOG.Info("Deamon mode entered successfully!");
99
    }
100
    }
100
 
101
 
101
    storage::Manager::Create();
102
    storage::Manager::Create();
102
    timer::Manager::Create();
103
    timer::Manager::Create();
103
    broker::Manager::Create();
104
    broker::Manager::Create();
104
    net::Manager::Create();
105
    net::Manager::Create();
105
    can::Protocol::Create();
106
    can::Protocol::Create();
106
    control::Manager::Create();
107
    control::Manager::Create();
107
    vm::Manager::Create();
108
    vm::Manager::Create();
108
   
109
   
109
    storage::Manager::Instance()->SetRootPath(config::Manager::Instance()->GetAsString("StoragePath"));
110
    storage::Manager::Instance()->SetRootPath(config::Manager::Instance()->GetAsString("StoragePath"));
110
   
111
   
111
    can::Protocol::Instance()->Load(config::Manager::Instance()->GetAsString("ProtocolFile"));
112
    can::Protocol::Instance()->Load(config::Manager::Instance()->GetAsString("ProtocolFile"));
112
   
113
   
113
    if (config::Manager::Instance()->Exist("MonitorPort"))
114
    if (config::Manager::Instance()->Exist("MonitorPort"))
114
    {
115
    {
115
        subscribers.push_back(can::Monitor::Pointer(new can::Monitor(config::Manager::Instance()->GetAsInt("MonitorPort"))));  
116
        subscribers.push_back(can::Monitor::Pointer(new can::Monitor(config::Manager::Instance()->GetAsInt("MonitorPort"))));  
116
    }
117
    }
117
   
118
   
118
    subscribers.push_back(control::Manager::Instance());  
119
    subscribers.push_back(control::Manager::Instance());  
119
   
120
   
120
    vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::System(vm::Manager::Instance()->GetIoService())));
121
    vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::System(vm::Manager::Instance()->GetIoService())));
121
    vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Console(vm::Manager::Instance()->GetIoService(), config::Manager::Instance()->GetAsInt("CommandPort"))));
122
    vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Console(vm::Manager::Instance()->GetIoService(), config::Manager::Instance()->GetAsInt("CommandPort"))));
122
    vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Storage(vm::Manager::Instance()->GetIoService())));
123
    vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Storage(vm::Manager::Instance()->GetIoService())));
123
    vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Timer(vm::Manager::Instance()->GetIoService())));
124
    vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Timer(vm::Manager::Instance()->GetIoService())));
124
    vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Module(vm::Manager::Instance()->GetIoService())));
125
    vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Module(vm::Manager::Instance()->GetIoService())));
125
    vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Xorg(vm::Manager::Instance()->GetIoService())));
126
    vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Xorg(vm::Manager::Instance()->GetIoService())));
126
   
-
 
-
 
127
    vm::Manager::Instance()->AddPlugin(vm::Plugin::Pointer(new vm::plugin::Socket(vm::Manager::Instance()->GetIoService())));
127
   
128
   
128
   
129
   
129
    vm::Manager::Instance()->Start(config::Manager::Instance()->GetAsString("ScriptPath"));
130
    vm::Manager::Instance()->Start(config::Manager::Instance()->GetAsString("ScriptPath"));
130
   
131
   
131
    if (config::Manager::Instance()->Exist("CanNet"))
132
    if (config::Manager::Instance()->Exist("CanNet"))
132
    {
133
    {
133
        common::StringList cannetworks = config::Manager::Instance()->GetAsStringVector("CanNet");
134
        common::StringList cannetworks = config::Manager::Instance()->GetAsStringVector("CanNet");
134
       
135
       
135
        for (int n = 0; n < cannetworks.size(); n++)
136
        for (int n = 0; n < cannetworks.size(); n++)
136
        {
137
        {
137
            subscribers.push_back(can::Network::Pointer(new can::Network(cannetworks[n])));            
138
            subscribers.push_back(can::Network::Pointer(new can::Network(cannetworks[n])));            
138
        }
139
        }
139
    }
140
    }
140
   
141
   
141
    boost::mutex::scoped_lock guard(guard_mutex);
142
    boost::mutex::scoped_lock guard(guard_mutex);
142
    on_message_condition.wait(guard);
143
    on_message_condition.wait(guard);
143
   
144
   
144
    LOG.Info("Cleaning up...");
145
    LOG.Info("Cleaning up...");
145
   
146
   
146
    CleanUp();
147
    CleanUp();
147
   
148
   
148
    LOG.Info("Thank you for using Atom. Goodbye!");
149
    LOG.Info("Thank you for using Atom. Goodbye!");
149
   
150
   
150
    return EXIT_SUCCESS;
151
    return EXIT_SUCCESS;
151
}
152
}
152
 
153
 
153
void CleanUp()
154
void CleanUp()
154
{
155
{
155
    subscribers.clear();
156
    subscribers.clear();
156
    config::Manager::Delete();
157
    config::Manager::Delete();
157
    timer::Manager::Delete();
158
    timer::Manager::Delete();
158
    control::Manager::Delete();
159
    control::Manager::Delete();
159
    vm::Manager::Delete();
160
    vm::Manager::Delete();
160
    can::Protocol::Delete();
161
    can::Protocol::Delete();
161
    broker::Manager::Delete();
162
    broker::Manager::Delete();
162
    net::Manager::Delete();
163
    net::Manager::Delete();
163
    storage::Manager::Delete();
164
    storage::Manager::Delete();
164
}
165
}
165
 
166
 
166
void Handler(int status)
167
void Handler(int status)
167
{
168
{
168
    std::string signal_name = "Unknown";
169
    std::string signal_name = "Unknown";
169
   
170
   
170
    switch (status)
171
    switch (status)
171
    {
172
    {
172
        case SIGTERM:
173
        case SIGTERM:
173
        {
174
        {
174
            signal_name = "Terminate";
175
            signal_name = "Terminate";
175
            break;
176
            break;
176
        }  
177
        }  
177
        case SIGINT:
178
        case SIGINT:
178
        {
179
        {
179
            signal_name = "Interupt";
180
            signal_name = "Interupt";
180
            break;
181
            break;
181
        }  
182
        }  
182
        case SIGQUIT:
183
        case SIGQUIT:
183
        {
184
        {
184
            signal_name = "Quit";
185
            signal_name = "Quit";
185
            break;
186
            break;
186
        }  
187
        }  
187
        case SIGABRT:
188
        case SIGABRT:
188
        {
189
        {
189
            signal_name = "Abort";
190
            signal_name = "Abort";
190
            break;
191
            break;
191
        }  
192
        }  
192
        case SIGIO:
193
        case SIGIO:
193
        {
194
        {
194
            signal_name = "I/O";
195
            signal_name = "I/O";
195
            break;
196
            break;
196
        }  
197
        }  
197
        case SIGPIPE:
198
        case SIGPIPE:
198
        {
199
        {
199
            signal_name = "Pipe";
200
            signal_name = "Pipe";
200
            break;
201
            break;
201
        }
202
        }
202
    }
203
    }
203
   
204
   
204
    LOG.Debug("Received signal " + signal_name + "(" + boost::lexical_cast<std::string>(status) + ").");
205
    LOG.Debug("Received signal " + signal_name + "(" + boost::lexical_cast<std::string>(status) + ").");
205
   
206
   
206
    if (status != SIGPIPE)
207
    if (status != SIGPIPE)
207
    {
208
    {
208
        on_message_condition.notify_all();
209
        on_message_condition.notify_all();
209
    }
210
    }
210
}
211
}