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