aboutsummaryrefslogtreecommitdiff
path: root/libcontextprovider/src/propertyprivate.cpp
blob: a59566d6cc43c73e415e2ee9a291817044606d8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * Copyright (C) 2008, 2009 Nokia Corporation.
 *
 * Contact: Marius Vollmer <marius.vollmer@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include "propertyprivate.h"
#include "servicebackend.h"
#include "logging.h"
#include "sconnect.h"
#include "loggingfeatures.h"
#include <time.h>

namespace ContextProvider {

/*!
    \class PropertyPrivate ContextProvider ContextProvider

    \brief The private implementation of Property.

    For each (ServiceBackend*, key) pair there exists only one
    PropertyPrivate; multiple Property objects may share it.
*/


QHash<QPair<ServiceBackend*,QString>, PropertyPrivate*> PropertyPrivate::propertyPrivateMap;

/// Constructor. Register the PropertyPrivate to its ServiceBackend;
/// this will make the Property object appear on D-Bus.
PropertyPrivate::PropertyPrivate(ServiceBackend* serviceBackend, const QString &key, QObject *parent)
    : QObject(parent), refCount(0), serviceBackend(serviceBackend),
      key(key), value(QVariant()),  timestamp(currentTimestamp()), subscribed(false),
      emittedValue(value), emittedTimestamp(timestamp), overheard(false)
{
    // Associate the property to the service backend
    serviceBackend->addProperty(key, this);
}

/// Set value for the PropertyPrivate. Results in a valueChanged
/// signal emission, if 1) the value was different than the current
/// value of the PropertyPrivate, or 2) The provider has overheard
/// another provider setting a different value having a more recent
/// time stamp than our last emission.
void PropertyPrivate::setValue(const QVariant& v)
{
    contextDebug() << F_PROPERTY << "Setting key:" << key << "to type:" << v.typeName();

    // Always store the intention of the provider
    timestamp = currentTimestamp();
    value = v;

    // The provider is setting a different value than it has previously.
    if (value != emittedValue ||
        value.isNull() != emittedValue.isNull() ||
        value.type() != emittedValue.type()) {
        emitValue();
        return;
    }

    // The provider is setting the same value again. But it has
    // overheard a different value after the emission of
    // emittedValue. Thus, emit again.
    if (overheard) {
        emitValue();
        return;
    }
}

/// Emit the valueChanged signal and update the emittedValue and
/// emittedTimestamp. (If subscribed is false, no value is emitted.)
void PropertyPrivate::emitValue()
{
    // No difference between intention and emitted value, nothing
    // happens
    if (emittedTimestamp == timestamp &&
        emittedValue == value &&
        emittedValue.isNull() == value.isNull() &&
        emittedValue.type() == value.type())
        return;

    emittedValue = value;
    emittedTimestamp = timestamp;
    overheard = false;

    // If nobody is subscribed to us, no D-Bus signal needs to be
    // emitted. Note: The bookkeeping above must be done, though.
    if (!subscribed) {
        return;
    }

    QVariantList values;
    if (value.isNull() == false) {
        values << value;
    }
    Q_EMIT valueChanged(values, timestamp);
}

/// Set the PropertyPrivate to subscribed state. If it was in the
/// unsubscribed state, the firstSubscriberAppeared signal is
/// emitted. (Property transmits the signal forward.)
void PropertyPrivate::setSubscribed()
{
    if (subscribed == false) {
        subscribed = true;
        Q_EMIT firstSubscriberAppeared(key);
    }
}

/// Set the PropertyPrivate to unsubscribed state. If it was in the
/// subscribed state, the lastSubscriberDisappeared signal is
/// emitted. (Property transmits the signal forward.)
void PropertyPrivate::setUnsubscribed()
{
    if (subscribed == true) {
      subscribed = false;
      Q_EMIT lastSubscriberDisappeared(key);
    }
}

/// Called by PropertyAdaptor when it has overheard another provider
/// sending a value on D-Bus. Check if the value is different and more
/// recent than the value we've emitted last. If so, emit our value
/// again. This way we ensure that the client gets the correct time
/// stamp for our value.
void PropertyPrivate::updateOverheardValue(const QVariantList& v, const quint64& t)
{
    contextDebug() << "Updating overheard value" << v << t;

    QVariant overheardValue;
    // Update the "overheard" value
    if (v.size() == 0) {
        overheardValue = QVariant();
    }
    else {
        overheardValue = v[0];
    }
    if (t > emittedTimestamp && overheardValue != emittedValue){
        // record that a different and more recent value than the one
        // emitted has been overheard
        overheard = true;
        // emit the value because provider might have a more recent
        // timestamp than t
        emitValue();
    }
}

#define NSECS_IN_SEC 1000000000ULL

/// Compute a unique time stamp for our value. The time stamp is based
/// on monotonic clock and its value is: seconds * 10e9 + nanoseconds.
quint64 PropertyPrivate::currentTimestamp()
{
    struct timespec time;
    clock_gettime(CLOCK_MONOTONIC, &time);
    quint64 toReturn = time.tv_nsec;
    toReturn += (NSECS_IN_SEC * time.tv_sec);
    return toReturn;
}

} // end namespace