Subversion Repositories HomeAutomation

Rev

Rev 1652 | Rev 1768 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1594 runge 1
/*
2
 *
3
 *  Copyright (C) 2010  Mattias Runge
4
 *
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
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
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.,
17
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
19
 */
20
 
21
#include "Manager.h"
22
 
23
#include <iostream>
24
#include <fstream>
25
 
26
#include <boost/filesystem.hpp>
27
 
28
namespace atom {
29
namespace config {
30
 
1599 runge 31
Manager::Pointer Manager::instance_;
1594 runge 32
 
33
Manager::Manager() : command_line_("Command line options"), configuration_file_("Configuration file options")
34
{
1656 runge 35
    std::string default_config_file = "/etc/atom/atom.conf";
1594 runge 36
 
37
    if (!boost::filesystem::exists(default_config_file))
38
    {
39
        default_config_file = "atom.conf";
40
    }
41
 
42
    this->configuration_file_.add_options()
1656 runge 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")
45
    ("LogFile",       boost::program_options::value<std::string>(),              "File to log output to")
46
    ("LogLevel",      boost::program_options::value<int>()->default_value(4),    "Level of logging")
47
    ("ScriptPath",    boost::program_options::value<std::string>(),              "Path to where the scripts are")
48
    ("UserScriptPath",boost::program_options::value<std::string>(),              "Path to where the user scripts are")
49
    ("StoragePath",   boost::program_options::value<std::string>(),              "Path to where the storage files are")
50
    ("ProtocolFile",  boost::program_options::value<std::string>(),              "File to read the protocol form")
51
    ("CanNet",        boost::program_options::value<common::StringList>(),       "Information on where to locate the CAN networks")
52
    ("Legacy",                                                                   "Start legacy VM environment");
1594 runge 53
 
54
    this->command_line_.add_options()
55
    ("help,h",    "produce help message")
56
    ("daemon,d",  "start in daemon mode")
57
    ("file,f",    boost::program_options::value<std::string>()->default_value(default_config_file), "configuration file");
58
 
59
    this->command_line_.add(this->configuration_file_);
60
}
61
 
62
Manager::~Manager()
63
{
64
}
65
 
66
Manager::Pointer Manager::Instance()
67
{
68
    return Manager::instance_;
69
}
70
 
1599 runge 71
void Manager::Create()
72
{
73
    Manager::instance_ = Manager::Pointer(new Manager());
74
}
75
 
1594 runge 76
void Manager::Delete()
77
{
78
    Manager::instance_.reset();
79
}
80
 
81
bool Manager::Set(int argument_count, char** argument_vector)
82
{
83
    try
84
    {
85
        boost::program_options::store(boost::program_options::command_line_parser(argument_count, argument_vector).options(this->command_line_).run(), this->variable_map_);
86
    }
87
    catch (boost::program_options::unknown_option e)
88
    {
89
        std::cerr << e.what() << std::endl;
90
        std::cout << this->command_line_ << std::endl;
91
        return false;
92
    }
93
    catch (boost::program_options::invalid_syntax e)
94
    {
95
        std::cerr << e.what() << std::endl;
96
        std::cout << this->command_line_ << std::endl;
97
        return false;
98
    }
99
 
1652 runge 100
    std::ifstream ifs(this->GetAsString("file").data());
1594 runge 101
 
102
    if (!ifs.is_open())
103
    {
104
        std::cerr << "Error: Could not find " << this->GetAsString("file") << std::endl;
105
        return false;
106
    }
107
 
108
    try
109
    {
110
        boost::program_options::store(boost::program_options::parse_config_file(ifs, this->configuration_file_), this->variable_map_);
111
    }
112
    catch (boost::program_options::unknown_option e)
113
    {
114
        ifs.close();
115
        std::cerr << e.what() << " found in " << this->GetAsString("file") << std::endl;
116
        std::cout << this->configuration_file_ << std::endl;
117
        return false;
118
    }
119
    catch (boost::program_options::invalid_syntax e)
120
    {
121
        std::cerr << e.what() << " found in " << this->GetAsString("file") << std::endl;
122
        std::cout << this->command_line_ << std::endl;
123
        return false;
124
    }
125
 
126
    ifs.close();
127
 
128
    if (this->Exist("help"))
129
    {
130
        std::cout << this->command_line_ << std::endl;
131
        return false;
132
    }
133
 
134
    return true;
135
}
136
 
137
bool Manager::Exist(std::string name)
138
{
139
    return this->variable_map_.count(name) != 0;
140
}
141
 
142
int Manager::GetAsInt(std::string name)
143
{
144
    return this->variable_map_[name].as<int>();
145
}
146
 
147
std::string Manager::GetAsString(std::string name)
148
{
149
    return this->variable_map_[name].as<std::string>();
150
}
151
 
1642 runge 152
common::StringList Manager::GetAsStringVector(std::string name)
1594 runge 153
{
1642 runge 154
    return this->variable_map_[name].as<common::StringList>();
1594 runge 155
}
156
 
157
 
158
}; // namespace config
159
}; // namespace atom