Subversion Repositories HomeAutomation

Rev

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

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