added overload for subscribe that includes a zone filter. it is unimplemented in...
[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         VehicleProperty::Property property;
73         std::string sourceUuidFilter;
74         Zone::Type zoneFilter;
75         GetPropertyCompletedSignal completed;
76         uint timeout;
77 };
78
79 class AsyncPropertyReply: public AsyncPropertyRequest
80 {
81 public:
82         AsyncPropertyReply(const AsyncPropertyRequest &request)
83                 :AsyncPropertyRequest(request), value(NULL), success(false), timeoutSource(nullptr)
84         {
85                 auto timeoutfunc = [](gpointer userData) {
86                         AsyncPropertyReply* thisReply = static_cast<AsyncPropertyReply*>(userData);
87                         if(thisReply->success == false)
88                         {
89                                 thisReply->error = Timeout;
90                                 thisReply->completed(thisReply);
91                         }
92                         return 0;
93                 };
94
95                 if(timeout)
96                 {
97                         timeoutSource = g_timeout_source_new(timeout);
98                         g_source_set_callback(timeoutSource,(GSourceFunc) timeoutfunc, this, nullptr);
99                         g_source_attach(timeoutSource, nullptr);
100                 }
101         }
102
103         ~AsyncPropertyReply()
104         {
105                 if(timeoutSource)
106                 {
107                         g_source_destroy(timeoutSource);
108                         g_source_unref(timeoutSource);
109                 }
110         }
111
112         enum Error {
113                 NoError = 0,
114                 Timeout,
115                 InvalidOperation,
116                 PermissionDenied,
117                 ZoneNotSupported
118         };
119
120         /**
121          * @brief value of the reply.  This may be null if success = false.  This is owned by the source.
122          */
123         AbstractPropertyType* value;
124         bool success;
125         Error error;
126
127 private:
128         GSource* timeoutSource;
129 };
130
131 class AsyncSetPropertyRequest: public AsyncPropertyRequest
132 {
133 public:
134         AsyncSetPropertyRequest()
135                 :value(NULL)
136         {
137
138         }
139
140         AsyncSetPropertyRequest(const AsyncPropertyRequest &request)
141                 :AsyncPropertyRequest(request), value(NULL)
142         {
143
144         }
145
146         AbstractPropertyType* value;
147 };
148
149 class AsyncRangePropertyRequest
150 {
151 public:
152         AsyncRangePropertyRequest()
153                 :timeBegin(0), timeEnd(0), sequenceBegin(-1), sequenceEnd(-1)
154         {
155
156         }
157
158         AsyncRangePropertyRequest(const AsyncRangePropertyRequest &request)
159
160         {
161                 this->property = request.property;
162                 this->completed = request.completed;
163                 this->timeBegin = request.timeBegin;
164                 this->timeEnd = request.timeEnd;
165                 this->sequenceBegin = request.sequenceBegin;
166                 this->sequenceEnd = request.sequenceEnd;
167                 this->sourceUuid = request.sourceUuid;
168         }
169
170         VehicleProperty::Property property;
171         std::string sourceUuid;
172         GetRangedPropertyCompletedSignal completed;
173         double timeBegin;
174         double timeEnd;
175         int32_t sequenceBegin;
176         int32_t sequenceEnd;
177 };
178
179 class AsyncRangePropertyReply: public AsyncRangePropertyRequest
180 {
181 public:
182         AsyncRangePropertyReply(AsyncRangePropertyRequest request)
183                 :AsyncRangePropertyRequest(request), success(false)
184         {
185
186         }
187
188         ~AsyncRangePropertyReply()
189         {
190                 for(auto itr = values.begin(); itr != values.end(); itr++)
191                 {
192                         delete (*itr);
193                 }
194
195                 values.clear();
196         }
197
198         std::list<AbstractPropertyType*> values;
199         bool success;
200 };
201
202 class AbstractRoutingEngine
203 {
204 public:
205         virtual ~AbstractRoutingEngine();
206
207         virtual void setSupported(PropertyList supported, AbstractSource* source) = 0;
208         virtual void updateSupported(PropertyList added, PropertyList removed) = 0;
209         virtual void updateProperty(VehicleProperty::Property property, AbstractPropertyType* value, std::string uuid) = 0;
210         virtual PropertyList supported() = 0;
211
212         /// sinks:
213         virtual void registerSink(AbstractSink* self) = 0;
214         virtual void  unregisterSink(AbstractSink* self) = 0;
215
216         /**
217          * @brief sourcesForProperty
218          * @param property
219          * @return list of source uuid's that support the "property"
220          */
221         virtual std::list<std::string> sourcesForProperty(VehicleProperty::Property property) = 0;
222
223         /**
224          * @brief getPropertyAsync requests a property value from a source.  This call has a timeout and will always return.
225          * @see AsyncPropertyRequest
226          * @see AsyncPropertyReply.
227          * @param request requested property.
228          * @return AsyncPropertyReply. The returned AsyncPropertyReply is owned by the caller of getPropertyAsync.
229          * @example AsyncPropertyRequest request;
230          * request.property = VehicleProperty::VehicleSpeed
231          * request.completed = [](AsyncPropertyReply* reply) { delete reply; };
232          * routingEngine->getPropertyAsync(request);
233          */
234         virtual AsyncPropertyReply * getPropertyAsync(AsyncPropertyRequest request) = 0;
235         virtual AsyncRangePropertyReply * getRangePropertyAsync(AsyncRangePropertyRequest request) = 0;
236         virtual AsyncPropertyReply * setProperty(AsyncSetPropertyRequest request) = 0;
237         virtual void subscribeToProperty(VehicleProperty::Property, AbstractSink* self) = 0;
238         virtual void subscribeToProperty(VehicleProperty::Property, std::string sourceUuidFilter, AbstractSink *self) = 0;
239         virtual void subscribeToProperty(VehicleProperty::Property, std::string sourceUuidFilter, Zone::Type zoneFilter, AbstractSink *self) = 0;
240         virtual void unsubscribeToProperty(VehicleProperty::Property, AbstractSink* self) = 0;
241
242         virtual PropertyInfo getPropertyInfo(VehicleProperty::Property, std::string sourceUuid) = 0;
243         virtual std::list<std::string> getSourcesForProperty(VehicleProperty::Property) = 0;
244 };
245
246 #endif // ABSTRACTROUTINGENGINE_H