Subversion Repositories HomeAutomation

Rev

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

Rev 1596 Rev 1599
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 "Manager.h"
21
#include "Manager.h"
22
 
22
 
23
#include <iostream>
23
#include <iostream>
24
#include <fstream>
24
#include <fstream>
25
 
25
 
26
#include <boost/filesystem.hpp>
26
#include <boost/filesystem.hpp>
27
 
27
 
28
namespace atom {
28
namespace atom {
29
namespace config {
29
namespace config {
30
 
30
 
31
Manager::Pointer Manager::instance_ = Manager::Pointer(new Manager());
31
Manager::Pointer Manager::instance_;
32
 
32
 
33
Manager::Manager() : command_line_("Command line options"), configuration_file_("Configuration file options")
33
Manager::Manager() : command_line_("Command line options"), configuration_file_("Configuration file options")
34
{
34
{
35
    std::string default_config_file = "/etc/atom.conf";
35
    std::string default_config_file = "/etc/atom.conf";
36
   
36
   
37
    if (!boost::filesystem::exists(default_config_file))
37
    if (!boost::filesystem::exists(default_config_file))
38
    {
38
    {
39
        default_config_file = "atom.conf";
39
        default_config_file = "atom.conf";
40
    }
40
    }
41
 
41
 
42
    this->configuration_file_.add_options()
42
    this->configuration_file_.add_options()
43
    ("MonitorPort", boost::program_options::value<int>()->default_value(1201), "TCP port to open for monitor output")
43
    ("MonitorPort", boost::program_options::value<int>()->default_value(1201), "TCP port to open for monitor output")
44
    ("CommandPort", boost::program_options::value<int>()->default_value(1202), "TCP port to open for command input")
44
    ("CommandPort", boost::program_options::value<int>()->default_value(1202), "TCP port to open for command input")
45
    ("LogFile",     boost::program_options::value<std::string>(),              "File to log output to")
45
    ("LogFile",     boost::program_options::value<std::string>(),              "File to log output to")
46
    ("LogLevel",    boost::program_options::value<std::string>(),              "Level of logging")
46
    ("LogLevel",    boost::program_options::value<int>()->default_value(4),    "Level of logging")
47
    ("ScriptsPath", boost::program_options::value<std::string>(),              "Path to where the scripts")
47
    ("ScriptsPath", boost::program_options::value<std::string>(),              "Path to where the scripts")
48
    ("ProtocolFile",boost::program_options::value<std::string>(),              "File to read the protocol form")
48
    ("ProtocolFile",boost::program_options::value<std::string>(),              "File to read the protocol form")
49
    ("CanNet",      boost::program_options::value<type::StringList>(),         "Information on where to locate the CAN networks");
49
    ("CanNet",      boost::program_options::value<type::StringList>(),         "Information on where to locate the CAN networks");
50
 
50
 
51
    this->command_line_.add_options()
51
    this->command_line_.add_options()
52
    ("version,v", "print version string")
52
    ("version,v", "print version string")
53
    ("help,h",    "produce help message")
53
    ("help,h",    "produce help message")
54
    ("daemon,d",  "start in daemon mode")
54
    ("daemon,d",  "start in daemon mode")
55
    ("file,f",    boost::program_options::value<std::string>()->default_value(default_config_file), "configuration file");
55
    ("file,f",    boost::program_options::value<std::string>()->default_value(default_config_file), "configuration file");
56
   
56
   
57
    this->command_line_.add(this->configuration_file_);
57
    this->command_line_.add(this->configuration_file_);
58
}
58
}
59
 
59
 
60
Manager::~Manager()
60
Manager::~Manager()
61
{
61
{
62
}
62
}
63
 
63
 
64
Manager::Pointer Manager::Instance()
64
Manager::Pointer Manager::Instance()
65
{
65
{
66
    return Manager::instance_;
66
    return Manager::instance_;
-
 
67
}
-
 
68
 
-
 
69
void Manager::Create()
-
 
70
{
-
 
71
    Manager::instance_ = Manager::Pointer(new Manager());
67
}
72
}
68
 
73
 
69
void Manager::Delete()
74
void Manager::Delete()
70
{
75
{
71
    Manager::instance_.reset();
76
    Manager::instance_.reset();
72
}
77
}
73
 
78
 
74
bool Manager::Set(int argument_count, char** argument_vector)
79
bool Manager::Set(int argument_count, char** argument_vector)
75
{
80
{
76
    try
81
    try
77
    {
82
    {
78
        boost::program_options::store(boost::program_options::command_line_parser(argument_count, argument_vector).options(this->command_line_).run(), this->variable_map_);
83
        boost::program_options::store(boost::program_options::command_line_parser(argument_count, argument_vector).options(this->command_line_).run(), this->variable_map_);
79
    }
84
    }
80
    catch (boost::program_options::unknown_option e)
85
    catch (boost::program_options::unknown_option e)
81
    {
86
    {
82
        std::cerr << e.what() << std::endl;
87
        std::cerr << e.what() << std::endl;
83
        std::cout << this->command_line_ << std::endl;
88
        std::cout << this->command_line_ << std::endl;
84
        return false;
89
        return false;
85
    }
90
    }
86
    catch (boost::program_options::invalid_syntax e)
91
    catch (boost::program_options::invalid_syntax e)
87
    {
92
    {
88
        std::cerr << e.what() << std::endl;
93
        std::cerr << e.what() << std::endl;
89
        std::cout << this->command_line_ << std::endl;
94
        std::cout << this->command_line_ << std::endl;
90
        return false;
95
        return false;
91
    }
96
    }
92
   
97
   
93
    std::ifstream ifs(this->GetAsString("file").c_str());
98
    std::ifstream ifs(this->GetAsString("file").c_str());
94
       
99
       
95
    if (!ifs.is_open())
100
    if (!ifs.is_open())
96
    {
101
    {
97
        std::cerr << "Error: Could not find " << this->GetAsString("file") << std::endl;
102
        std::cerr << "Error: Could not find " << this->GetAsString("file") << std::endl;
98
        return false;
103
        return false;
99
    }
104
    }
100
   
105
   
101
    try
106
    try
102
    {
107
    {
103
        boost::program_options::store(boost::program_options::parse_config_file(ifs, this->configuration_file_), this->variable_map_);
108
        boost::program_options::store(boost::program_options::parse_config_file(ifs, this->configuration_file_), this->variable_map_);
104
    }
109
    }
105
    catch (boost::program_options::unknown_option e)
110
    catch (boost::program_options::unknown_option e)
106
    {
111
    {
107
        ifs.close();
112
        ifs.close();
108
        std::cerr << e.what() << " found in " << this->GetAsString("file") << std::endl;
113
        std::cerr << e.what() << " found in " << this->GetAsString("file") << std::endl;
109
        std::cout << this->configuration_file_ << std::endl;
114
        std::cout << this->configuration_file_ << std::endl;
110
        return false;
115
        return false;
111
    }
116
    }
112
    catch (boost::program_options::invalid_syntax e)
117
    catch (boost::program_options::invalid_syntax e)
113
    {
118
    {
114
        std::cerr << e.what() << " found in " << this->GetAsString("file") << std::endl;
119
        std::cerr << e.what() << " found in " << this->GetAsString("file") << std::endl;
115
        std::cout << this->command_line_ << std::endl;
120
        std::cout << this->command_line_ << std::endl;
116
        return false;
121
        return false;
117
    }
122
    }
118
       
123
       
119
    ifs.close();
124
    ifs.close();
120
   
125
   
121
    if (this->Exist("help"))
126
    if (this->Exist("help"))
122
    {
127
    {
123
        std::cout << this->command_line_ << std::endl;
128
        std::cout << this->command_line_ << std::endl;
124
        return false;
129
        return false;
125
    }
130
    }
126
   
131
   
127
    return true;
132
    return true;
128
}
133
}
129
 
134
 
130
bool Manager::Exist(std::string name)
135
bool Manager::Exist(std::string name)
131
{
136
{
132
    return this->variable_map_.count(name) != 0;
137
    return this->variable_map_.count(name) != 0;
133
}
138
}
134
 
139
 
135
int Manager::GetAsInt(std::string name)
140
int Manager::GetAsInt(std::string name)
136
{
141
{
137
    return this->variable_map_[name].as<int>();
142
    return this->variable_map_[name].as<int>();
138
}
143
}
139
 
144
 
140
std::string Manager::GetAsString(std::string name)
145
std::string Manager::GetAsString(std::string name)
141
{
146
{
142
    return this->variable_map_[name].as<std::string>();
147
    return this->variable_map_[name].as<std::string>();
143
}
148
}
144
 
149
 
145
type::StringList Manager::GetAsStringVector(std::string name)
150
type::StringList Manager::GetAsStringVector(std::string name)
146
{
151
{
147
    return this->variable_map_[name].as<type::StringList>();
152
    return this->variable_map_[name].as<type::StringList>();
148
}
153
}
149
 
154
 
150
 
155
 
151
}; // namespace config
156
}; // namespace config
152
}; // namespace atom
157
}; // namespace atom