merge from 0.10
[profile/ivi/automotive-message-broker.git] / lib / abstractroutingengine.h
1 /*
2     Copyright (C) 2012  Intel Corporation
3
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Lesser General Public
6     License as published by the Free Software Foundation; either
7     version 2.1 of the License, or (at your option) any later version.
8
9     This library is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12     Lesser General Public License for more details.
13
14     You should have received a copy of the GNU Lesser General Public
15     License along with this library; if not, write to the Free Software
16     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17 */
18
19
20 #ifndef ABSTRACTROUTINGENGINE_H
21 #define ABSTRACTROUTINGENGINE_H
22
23 #include <sys/types.h>
24 #include <stdlib.h>
25 #include <boost/any.hpp>
26 #include <functional>
27 #include <string>
28 #include <time.h>
29
30 #include "vehicleproperty.h"
31 #include "abstractpropertytype.h"
32 #include "propertyinfo.hpp"
33
34 class AbstractSink;
35 class AbstractSource;
36 class AsyncPropertyReply;
37 class AsyncRangePropertyReply;
38
39
40 typedef std::function<void (AsyncPropertyReply*)> GetPropertyCompletedSignal;
41 typedef std::function<void (AsyncRangePropertyReply*)> GetRangedPropertyCompletedSignal;
42
43 class AsyncPropertyRequest
44 {
45 public:
46         AsyncPropertyRequest()
47                 :property(VehicleProperty::NoValue),timeout(10000)
48         {
49
50         }
51
52         AsyncPropertyRequest(const AsyncPropertyRequest &request)
53         {
54                 this->property = request.property;
55                 this->completed = request.completed;
56                 this->sourceUuidFilter = request.sourceUuidFilter;
57                 this->zoneFilter = request.zoneFilter;
58                 this->timeout = request.timeout;
59         }
60
61         AsyncPropertyRequest & operator = (const AsyncPropertyRequest & other)
62         {
63                 this->property = other.property;
64                 this->completed = other.completed;
65                 this->sourceUuidFilter = other.sourceUuidFilter;
66                 this->zoneFilter = other.zoneFilter;
67                 this->timeout = other.timeout;
68
69                 return *this;
70         }
71
72         virtual ~AsyncPropertyRequest() { }
73
74         VehicleProperty::Property property;
75         std::string sourceUuidFilter;
76         Zone::Type zoneFilter;
77         GetPropertyCompletedSignal completed;
78         uint timeout;
79 };
80
81 class AsyncPropertyReply: public AsyncPropertyRequest
82 {
83 public:
84         AsyncPropertyReply(const AsyncPropertyRequest &request)
85                 :AsyncPropertyRequest(request), value(NULL), success(false), timeoutSource(nullptr)
86         {
87                 auto timeoutfunc = [](gpointer userData) {
88                         AsyncPropertyReply* thisReply = static_cast<AsyncPropertyReply*>(userData);
89                         if(thisReply->success == false)
90                         {
91                                 thisReply->error = Timeout;
92                                 thisReply->completed(thisReply);
93                         }
94                         return 0;
95                 };
96
97                 if(timeout)
98                 {
99                         timeoutSource = g_timeout_source_new(timeout);
100                         g_source_set_callback(timeoutSource,(GSourceFunc) timeoutfunc, this, nullptr);
101                         g_source_attach(timeoutSource, nullptr);
102                 }
103         }
104
105         virtual ~AsyncPropertyReply()
106         {
107                 if(timeoutSource)
108                 {
109                         g_source_destroy(timeoutSource);
110                         g_source_unref(timeoutSource);
111                 }
112         }
113
114         enum Error {
115                 NoError = 0,
116                 Timeout,
117                 InvalidOperation,
118                 PermissionDenied,
119                 ZoneNotSupported
120         };
121
122         /**
123          * @brief value of the reply.  This may be null if success = false.  This is owned by the source.
124          */
125         AbstractPropertyType* value;
126         bool success;
127         Error error;
128
129 private:
130         GSource* timeoutSource;
131 };
132
133 class AsyncSetPropertyRequest: public AsyncPropertyRequest
134 {
135 public:
136         AsyncSetPropertyRequest()
137                 :value(NULL)
138         {
139
140         }
141
142         AsyncSetPropertyRequest(const AsyncPropertyRequest &request)
143                 :AsyncPropertyRequest(request), value(NULL)
144         {
145
146         }
147
148         virtual ~AsyncSetPropertyRequest() { }
149
150         AbstractPropertyType* value;
151 };
152
153 class AsyncRangePropertyRequest
154 {
155 public:
156         AsyncRangePropertyRequest()
157                 :timeBegin(0), timeEnd(0), sequenceBegin(-1), sequenceEnd(-1)
158         {
159
160         }
161
162         AsyncRangePropertyRequest(const AsyncRangePropertyRequest &request)
163
164         {
165                 this->property = request.property;
166                 this->completed = request.completed;
167                 this->timeBegin = request.timeBegin;
168                 this->timeEnd = request.timeEnd;
169                 this->sequenceBegin = request.sequenceBegin;
170                 this->sequenceEnd = request.sequenceEnd;
171                 this->sourceUuid = request.sourceUuid;
172         }
173
174         virtual ~AsyncRangePropertyRequest() {}
175
176         VehicleProperty::Property property;
177         std::string sourceUuid;
178         GetRangedPropertyCompletedSignal completed;
179         double timeBegin;
180         double timeEnd;
181         int32_t sequenceBegin;
182         int32_t sequenceEnd;
183 };
184
185 class AsyncRangePropertyReply: public AsyncRangePropertyRequest
186 {
187 public:
188         AsyncRangePropertyReply(AsyncRangePropertyRequest request)
189                 :AsyncRangePropertyRequest(request), success(false)
190         {
191
192         }
193
194         ~AsyncRangePropertyReply()
195         {
196                 for(auto itr = values.begin(); itr != values.end(); itr++)
197                 {
198                         delete (*itr);
199                 }
200
201                 values.clear();
202         }
203
204         std::list<AbstractPropertyType*> values;
205         bool success;
206 };
207
208 class AbstractRoutingEngine
209 {
210 public:
211         virtual ~AbstractRoutingEngine();
212
213         virtual void setSupported(PropertyList supported, AbstractSource* source) = 0;
214         virtual void updateSupported(PropertyList added, PropertyList removed) = 0;
215         virtual void updateProperty(VehicleProperty::Property property, AbstractPropertyType* value, std::string uuid) = 0;
216         virtual PropertyList supported() = 0;
217
218         /// sinks:
219         virtual void registerSink(AbstractSink* self) = 0;
220         virtual void  unregisterSink(AbstractSink* self) = 0;
221
222         /**
223          * @brief sourcesForProperty
224          * @param property
225          * @return list of source uuid's that support the "property"
226          */
227         virtual std::list<std::string> sourcesForProperty(VehicleProperty::Property property) = 0;
228
229         /**
230          * @brief getPropertyAsync requests a property value from a source.  This call has a timeout and will always return.
231          * @see AsyncPropertyRequest
232          * @see AsyncPropertyReply.
233          * @param request requested property.
234          * @return AsyncPropertyReply. The returned AsyncPropertyReply is owned by the caller of getPropertyAsync.
235          * @example AsyncPropertyRequest request;
236          * request.property = VehicleProperty::VehicleSpeed
237          * request.completed = [](AsyncPropertyReply* reply) { delete reply; };
238          * routingEngine->getPropertyAsync(request);
239          */
240         virtual AsyncPropertyReply * getPropertyAsync(AsyncPropertyRequest request) = 0;
241         virtual AsyncRangePropertyReply * getRangePropertyAsync(AsyncRangePropertyRequest request) = 0;
242         virtual AsyncPropertyReply * setProperty(AsyncSetPropertyRequest request) = 0;
243         virtual void subscribeToProperty(VehicleProperty::Property, AbstractSink* self) = 0;
244         virtual void subscribeToProperty(VehicleProperty::Property, std::string sourceUuidFilter, AbstractSink *self) = 0;
245         virtual void subscribeToProperty(VehicleProperty::Property, std::string sourceUuidFilter, Zone::Type zoneFilter, AbstractSink *self) = 0;
246         virtual void unsubscribeToProperty(VehicleProperty::Property, AbstractSink* self) = 0;
247
248         virtual PropertyInfo getPropertyInfo(VehicleProperty::Property, std::string sourceUuid) = 0;
249         virtual std::list<std::string> getSourcesForProperty(VehicleProperty::Property) = 0;
250 };
251
252 #endif // ABSTRACTROUTINGENGINE_H