Subversion Repositories HomeAutomation

Rev

Rev 1599 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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