~ci-train-bot/location-service/location-service-ubuntu-yakkety-1895

« back to all changes in this revision

Viewing changes to examples/service/service.cpp

  • Committer: Thomas Voß
  • Date: 2013-05-28 14:20:45 UTC
  • Revision ID: thomas.voss@canonical.com-20130528142045-kq5umqdmm4o53vwk
Initial push.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "program_options.h"
 
2
 
 
3
#include "com/ubuntu/location/provider_factory.h"
 
4
 
 
5
#include "com/ubuntu/location/service/default_configuration.h"
 
6
#include "com/ubuntu/location/service/implementation.h"
 
7
 
 
8
#include <org/freedesktop/dbus/announcer.h>
 
9
#include <org/freedesktop/dbus/asio/executor.h>
 
10
 
 
11
#include <thread>
 
12
 
 
13
namespace cul = com::ubuntu::location;
 
14
namespace culs = com::ubuntu::location::service;
 
15
namespace dbus = org::freedesktop::dbus;
 
16
 
 
17
int main(int argc, char** argv)
 
18
{
 
19
    cul::ProgramOptions options;
 
20
 
 
21
    options.add("help", "Produces this help message");
 
22
    options.add(
 
23
        "bus", 
 
24
        "The well-known bus to announce the service upon", 
 
25
        std::string{"session"});
 
26
    options.add_composed<std::vector<std::string>>(
 
27
        "provider", 
 
28
        "The providers that should be added to the engine");
 
29
 
 
30
    if (!options.parse_from_command_line_args(argc, argv))
 
31
        return EXIT_FAILURE;
 
32
 
 
33
    if (options.value_count_for_key("help") > 0)
 
34
    {
 
35
        options.print_help(std::cout);
 
36
        return EXIT_SUCCESS;
 
37
    }
 
38
    
 
39
    if (options.value_count_for_key("provider") == 0)
 
40
    {
 
41
        std::cout << "A set of providers need to be specified. The following providers are known:" << std::endl;
 
42
        cul::ProviderFactory::instance().enumerate(
 
43
            [](const std::string& name, const cul::ProviderFactory::Factory&)
 
44
            {
 
45
                std::cout << "\t" << name << std::endl;
 
46
            });
 
47
        return EXIT_FAILURE;
 
48
    }
 
49
 
 
50
    auto selected_providers = options.value_for_key<std::vector<std::string>>("provider");
 
51
 
 
52
    std::map<std::string, cul::ProviderFactory::Configuration> config_lut;
 
53
    std::set<cul::Provider::Ptr> instantiated_providers;
 
54
 
 
55
    for (const std::string& provider : selected_providers)
 
56
    {
 
57
        std::cout << "Instantiating and configuring: " << provider << std::endl;
 
58
        options.enumerate_unrecognized_options(
 
59
            [&config_lut, provider](const std::string& s)
 
60
            {
 
61
                std::stringstream in(s);
 
62
                std::string key, value;
 
63
                
 
64
                std::getline(in, key, '=');
 
65
                std::getline(in, value, '=');
 
66
                
 
67
                std::size_t pos = key.find(provider);
 
68
                if (pos == std::string::npos)
 
69
                    return;
 
70
                static const std::string option_marker{"--"};
 
71
                static const std::string scope_separator{"::"};
 
72
                key = key.erase(key.find_first_of(option_marker), option_marker.size()); 
 
73
                key = key.erase(key.find_first_of(provider), provider.size());
 
74
                key = key.erase(key.find_first_of(scope_separator), scope_separator.size()); 
 
75
                
 
76
                std::cout << "\t" << key << " -> " << value << std::endl;
 
77
 
 
78
                config_lut[provider][key] = value;
 
79
            });
 
80
 
 
81
        try
 
82
        {
 
83
            auto p = cul::ProviderFactory::instance().create_provider_for_name_with_config(
 
84
                provider, 
 
85
                config_lut[provider]);
 
86
 
 
87
            if (p)
 
88
                instantiated_providers.insert(p);
 
89
            else
 
90
                throw std::runtime_error("Problem instantiating provider");
 
91
            
 
92
        } catch(const std::runtime_error& e)
 
93
        {
 
94
            std::cerr << "Exception instantiating provider: " << e.what() << " ... Aborting now." << std::endl;
 
95
            return EXIT_FAILURE;
 
96
        }
 
97
    }
 
98
    
 
99
    static const std::map<std::string, dbus::WellKnownBus> lut = 
 
100
    {
 
101
        {"session", dbus::WellKnownBus::session},
 
102
        {"system", dbus::WellKnownBus::system},
 
103
    };
 
104
 
 
105
    dbus::Bus::Ptr bus
 
106
    {
 
107
        new dbus::Bus{lut.at(options.value_for_key<std::string>("bus"))}
 
108
    };
 
109
 
 
110
    bus->install_executor(
 
111
        dbus::Executor::Ptr(
 
112
            new dbus::asio::Executor{bus}));
 
113
 
 
114
    culs::DefaultConfiguration config;
 
115
    
 
116
    auto location_service =
 
117
            dbus::announce_service_on_bus<
 
118
                culs::Interface, 
 
119
                culs::Implementation
 
120
            >(
 
121
                bus,
 
122
                config.the_engine(
 
123
                    instantiated_providers,
 
124
                    config.the_provider_selection_policy()),
 
125
                config.the_permission_manager());
 
126
    
 
127
    std::thread t{[bus](){bus->run();}};
 
128
    
 
129
    if (t.joinable())
 
130
        t.join();
 
131
 
 
132
    return EXIT_SUCCESS;
 
133
}