2
* Copyright © 2012-2013 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License version 3,
6
* as published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored by: Thomas Voß <thomas.voss@canonical.com>
18
#include "program_options.h"
20
#include "com/ubuntu/location/provider_factory.h"
22
#include "com/ubuntu/location/service/default_configuration.h"
23
#include "com/ubuntu/location/service/implementation.h"
25
#include <core/dbus/announcer.h>
26
#include <core/dbus/asio/executor.h>
30
namespace cul = com::ubuntu::location;
31
namespace culs = com::ubuntu::location::service;
32
namespace dbus = core::dbus;
34
int main(int argc, char** argv)
36
cul::ProgramOptions options;
38
options.add("help", "Produces this help message");
41
"The well-known bus to announce the service upon",
42
std::string{"session"});
43
options.add_composed<std::vector<std::string>>(
45
"The providers that should be added to the engine");
47
if (!options.parse_from_command_line_args(argc, argv))
50
if (options.value_count_for_key("help") > 0)
52
options.print_help(std::cout);
56
if (options.value_count_for_key("provider") == 0)
58
std::cout << "A set of providers need to be specified. The following providers are known:" << std::endl;
59
cul::ProviderFactory::instance().enumerate(
60
[](const std::string& name, const cul::ProviderFactory::Factory&)
62
std::cout << "\t" << name << std::endl;
67
auto selected_providers = options.value_for_key<std::vector<std::string>>("provider");
69
std::map<std::string, cul::ProviderFactory::Configuration> config_lut;
70
std::set<cul::Provider::Ptr> instantiated_providers;
72
for (const std::string& provider : selected_providers)
74
std::cout << "Instantiating and configuring: " << provider << std::endl;
75
options.enumerate_unrecognized_options(
76
[&config_lut, provider](const std::string& s)
78
std::stringstream in(s);
79
std::string key, value;
81
std::getline(in, key, '=');
82
std::getline(in, value, '=');
84
std::size_t pos = key.find(provider);
85
if (pos == std::string::npos)
87
static const std::string option_marker{"--"};
88
static const std::string scope_separator{"::"};
89
key = key.erase(key.find_first_of(option_marker), option_marker.size());
90
key = key.erase(key.find_first_of(provider), provider.size());
91
key = key.erase(key.find_first_of(scope_separator), scope_separator.size());
93
std::cout << "\t" << key << " -> " << value << std::endl;
95
config_lut[provider].put(key, value);
100
auto p = cul::ProviderFactory::instance().create_provider_for_name_with_config(
102
config_lut[provider]);
105
instantiated_providers.insert(p);
107
throw std::runtime_error("Problem instantiating provider");
109
} catch(const std::runtime_error& e)
111
std::cerr << "Exception instantiating provider: " << e.what() << " ... Aborting now." << std::endl;
116
static const std::map<std::string, dbus::WellKnownBus> lut =
118
{"session", dbus::WellKnownBus::session},
119
{"system", dbus::WellKnownBus::system},
124
new dbus::Bus{lut.at(options.value_for_key<std::string>("bus"))}
127
bus->install_executor(dbus::asio::make_executor(bus));
129
culs::DefaultConfiguration config;
131
auto location_service =
132
dbus::announce_service_on_bus<
138
instantiated_providers,
139
config.the_provider_selection_policy()),
140
config.the_permission_manager());
142
std::thread t{[bus](){bus->run();}};