Subversion Repositories HomeAutomation

Rev

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

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