~mandel/location-service/tvoss-time-delta-fixes

« back to all changes in this revision

Viewing changes to src/location_service/com/ubuntu/location/service/harvester.h

  • Committer: thomas-voss
  • Date: 2014-06-01 17:56:58 UTC
  • mto: This revision was merged to the branch mainline in revision 66.
  • Revision ID: thomas.voss@canonical.com-20140601175658-du147zizedzgvbtn
Add a reporter sending off harvested location data to the mozilla location service.
Add test-cases for the reporter.
Adjust debian/copyright to include mongoose.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2012-2013 Canonical Ltd.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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/>.
 
15
 *
 
16
 * Authored by: Thomas Voß <thomas.voss@canonical.com>
 
17
 */
 
18
#ifndef LOCATION_SERVICE_COM_UBUNTU_LOCATION_SERVICE_HARVESTER_H_
 
19
#define LOCATION_SERVICE_COM_UBUNTU_LOCATION_SERVICE_HARVESTER_H_
 
20
 
 
21
#include <com/ubuntu/location/engine.h>
 
22
#include <com/ubuntu/location/connectivity/manager.h>
 
23
 
 
24
#include <com/ubuntu/location/logging.h>
 
25
 
 
26
#include <atomic>
 
27
 
 
28
namespace com
 
29
{
 
30
namespace ubuntu
 
31
{
 
32
namespace location
 
33
{
 
34
namespace service
 
35
{
 
36
/** @brief Models a wifi- and cell-id harvester for crowd-sourcing purposes. */
 
37
class Harvester
 
38
{
 
39
public:
 
40
 
 
41
    /** @brief Models a reporter of position updates, augmented with wifi and cell ids. */
 
42
    struct Reporter
 
43
    {
 
44
        /** @cond */
 
45
        typedef std::shared_ptr<Reporter> Ptr;
 
46
 
 
47
        Reporter() = default;
 
48
        virtual ~Reporter() = default;
 
49
        /** @endcond */
 
50
 
 
51
        /** @brief Tell the reporter that it should start operating. */
 
52
        virtual void start() = 0;
 
53
 
 
54
        /** @brief Tell the reporter to shut down its operation. */
 
55
        virtual void stop() = 0;
 
56
 
 
57
        /**
 
58
         * @brief Triggers the reporter to send off the information.
 
59
         */
 
60
        virtual void report(const Update<Position>& update,
 
61
                            const std::vector<connectivity::WirelessNetwork::Ptr>& wifis,
 
62
                            const std::vector<connectivity::RadioCell>& cells) = 0;
 
63
    };
 
64
 
 
65
    /** @brief Configuration encapsulates all creation time options of class Harvester */
 
66
    struct Configuration
 
67
    {
 
68
        /** The position engine that the harvester should use. */
 
69
        std::shared_ptr<Engine> engine;
 
70
        /** The connectivity manager that the harvester should use. */
 
71
        std::shared_ptr<connectivity::Manager> connectivity_manager;
 
72
        /** The reporter implementation */
 
73
        std::shared_ptr<Reporter> reporter;
 
74
    };
 
75
 
 
76
    /** @brief Creates a new instance and wires up to system components for receiving
 
77
     *  location updates, and wifi and cell id measurements.
 
78
     */
 
79
    Harvester(const Configuration& configuration)
 
80
        : config(configuration),
 
81
          is_running{false}
 
82
    {
 
83
        config.engine->updates.reference_location.changed().connect([this](const Update<Position>& update)
 
84
        {
 
85
            VLOG(10) << "Reference location changed: " << update;
 
86
 
 
87
            if (not is_running.load())
 
88
                return;
 
89
 
 
90
            auto visible_wifis = config.connectivity_manager->visible_wireless_networks().get();
 
91
            auto connected_cells = config.connectivity_manager->connected_radio_cells().get();
 
92
 
 
93
            config.reporter->report(update, visible_wifis, connected_cells);
 
94
        });
 
95
    }
 
96
 
 
97
    virtual ~Harvester()
 
98
    {
 
99
        stop();
 
100
    }
 
101
 
 
102
    virtual void start()
 
103
    {
 
104
        if (is_running.load())
 
105
            return;
 
106
 
 
107
        is_running.exchange(true);
 
108
 
 
109
        config.reporter->start();
 
110
    }
 
111
 
 
112
    virtual void stop()
 
113
    {
 
114
        if (not is_running.load())
 
115
            return;
 
116
 
 
117
        is_running.exchange(false);
 
118
 
 
119
        config.reporter->stop();
 
120
    }
 
121
 
 
122
private:
 
123
    Configuration config;
 
124
    std::atomic<bool> is_running;
 
125
};
 
126
}
 
127
}
 
128
}
 
129
}
 
130
 
 
131
#endif // LOCATION_SERVICE_COM_UBUNTU_LOCATION_SERVICE_HARVESTER_H_