2 Copyright (C) 2012 Intel Corporation
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.
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.
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
20 #ifndef ABSTRACTROUTINGENGINE_H
21 #define ABSTRACTROUTINGENGINE_H
23 #include <sys/types.h>
25 #include <boost/any.hpp>
30 #include "vehicleproperty.h"
31 #include "abstractpropertytype.h"
32 #include "propertyinfo.hpp"
36 class AsyncPropertyReply;
37 class AsyncRangePropertyReply;
40 typedef std::function<void (AsyncPropertyReply*)> GetPropertyCompletedSignal;
41 typedef std::function<void (AsyncRangePropertyReply*)> GetRangedPropertyCompletedSignal;
44 * \brief The AsyncPropertyRequest class is used by sinks to request property values.
45 * \see AbstractRoutingEngine::getPropertyAsync
46 * \see AsyncPropertyReply
48 class AsyncPropertyRequest
51 AsyncPropertyRequest()
52 :property(VehicleProperty::NoValue),timeout(10000)
57 AsyncPropertyRequest(const AsyncPropertyRequest &request)
59 this->property = request.property;
60 this->completed = request.completed;
61 this->sourceUuidFilter = request.sourceUuidFilter;
62 this->zoneFilter = request.zoneFilter;
63 this->timeout = request.timeout;
66 AsyncPropertyRequest & operator = (const AsyncPropertyRequest & other)
68 this->property = other.property;
69 this->completed = other.completed;
70 this->sourceUuidFilter = other.sourceUuidFilter;
71 this->zoneFilter = other.zoneFilter;
72 this->timeout = other.timeout;
77 virtual ~AsyncPropertyRequest() { }
80 * \brief property property to request.
82 VehicleProperty::Property property;
85 * \brief sourceUuidFilter the requesting sink should use this to filter on a specific source or leave blank to use any source
87 std::string sourceUuidFilter;
90 * \brief zoneFilter the requesting sink should use this if he wants to filter on a specific zone
92 Zone::Type zoneFilter;
95 * \brief completed the callback when the request has been completed.
97 GetPropertyCompletedSignal completed;
100 * \brief use to specify a timeout in ms for the request. When a timeout occurs, the 'completed' callback
101 * will be called with an error. @see AsyncPropertyReply
102 * default value is: 10000 ms
108 * \brief The AsyncPropertyReply class is used by sources to reply to Get and Set operations. The source should
109 * set success to true if the call is successful or 'false' if the request was not successful and set 'error'
110 * to the appropriate error.
111 * \see AbstractRoutingEngine::getPropertyAsync
112 * \see AsyncPropertyReply
113 * \see AbstractSource::Operations
114 * \see AbstractSource::getPropertyAsync
116 class AsyncPropertyReply: public AsyncPropertyRequest
119 AsyncPropertyReply(const AsyncPropertyRequest &request);
121 virtual ~AsyncPropertyReply()
125 g_source_destroy(timeoutSource);
126 g_source_unref(timeoutSource);
131 * \brief The Error enum
142 * \brief value of the reply. This may be null if success = false. This is owned by the source.
144 AbstractPropertyType* value;
147 * \brief success indicates if the request was successfull or not. True means success. False means fail and the 'error'
148 * member should be set.
153 * \brief error contains the error if the request was not successful.\
159 GSource* timeoutSource;
163 * \brief The AsyncSetPropertyRequest class is used by sinks to set a property to the 'value'. The source will reply
164 * with a AsyncPropertyReply containing the new value or an error
165 * \see AbstractRoutingEngine::setProperty
166 * \see AsyncPropertyReply
168 class AsyncSetPropertyRequest: public AsyncPropertyRequest
171 AsyncSetPropertyRequest()
177 AsyncSetPropertyRequest(const AsyncPropertyRequest &request)
178 :AsyncPropertyRequest(request), value(NULL)
183 virtual ~AsyncSetPropertyRequest() { }
186 * \brief value the new value to set the property to.
188 AbstractPropertyType* value;
192 * \brief The AsyncRangePropertyRequest class is used by sinks to request values within a time or sequence range
193 * \see AbstractRoutingEngine::getRangePropertyAsync
195 class AsyncRangePropertyRequest
198 AsyncRangePropertyRequest()
199 :timeBegin(0), timeEnd(0), sequenceBegin(-1), sequenceEnd(-1)
204 AsyncRangePropertyRequest(const AsyncRangePropertyRequest &request)
206 this->properties = request.properties;
207 this->completed = request.completed;
208 this->timeBegin = request.timeBegin;
209 this->timeEnd = request.timeEnd;
210 this->sequenceBegin = request.sequenceBegin;
211 this->sequenceEnd = request.sequenceEnd;
212 this->sourceUuid = request.sourceUuid;
215 virtual ~AsyncRangePropertyRequest() {}
218 * \brief properties list of properties to request
220 PropertyList properties;
223 * \brief sourceUuid if the sink wishes to request a specific source, this should be set to the uuid of the source.
225 std::string sourceUuid;
228 * \brief completed callback that is called when the ranged request is complete.
230 GetRangedPropertyCompletedSignal completed;
233 * \brief timeBegin set this to request values for the specified property beggining at this time. Time is seconds\
234 * since the unix epoc. Set this to '0' if you do not want values within a time range.
239 * \brief timeEnd set this to request values for the specified property beggining at this time. Time is seconds\
240 * since the unix epoc. Set this to '0' if you do not want values within a time range.
245 * \brief sequenceBegin set this to request values with a sequence >= to the sequenceBegin value. Set to -1 if
246 * you don't want values within a sequence ranges.
248 int32_t sequenceBegin;
251 * \brief sequenceEnd set this to request values with a sequence <= to the sequenceEnd value. Set to -1 if
252 * you don't want values within a sequence ranges.
258 * \brief The AsyncRangePropertyReply class is used by a source to reply to an AsyncRangePropertyRequest.
259 * the source should set success to 'true' and populate the 'values' member if the request was successful.
260 * If the request is not successful, 'success' should be set to 'false' and the 'error' member should be set.
262 class AsyncRangePropertyReply: public AsyncRangePropertyRequest
265 AsyncRangePropertyReply(AsyncRangePropertyRequest request)
266 :AsyncRangePropertyRequest(request), success(false)
271 ~AsyncRangePropertyReply()
273 for(auto itr = values.begin(); itr != values.end(); itr++)
282 * \brief error this is set if there was an error in the request. "success" will also be set to false.
284 AsyncPropertyReply::Error error;
287 * \brief values if the request was successful, this will contain a list of values meeting the criteria of the request.
289 std::list<AbstractPropertyType*> values;
292 * \brief success this will be true if the request was successful. If not, this is false and error is set.
297 class AbstractRoutingEngine
300 virtual ~AbstractRoutingEngine();
302 virtual void setSupported(PropertyList supported, AbstractSource* source) = 0;
303 virtual void updateSupported(PropertyList added, PropertyList removed) = 0;
306 void updateProperty(VehicleProperty::Property property, AbstractPropertyType* value, std::string uuid)
308 DebugOut(DebugOut::Warning)<<"updateProperty(VehicleProperty::Property,AbstractPropertyType*,std::string) is deprecated. use new updateProperty(AbstractPropertyType*, const std::string &)"<<endl;
309 updateProperty(value,uuid);
312 virtual void updateProperty(AbstractPropertyType* value, const std::string &uuid) = 0;
313 virtual PropertyList supported() = 0;
316 virtual void registerSink(AbstractSink* self) = 0;
317 virtual void unregisterSink(AbstractSink* self) = 0;
320 * /brief sourcesForProperty
322 * /return list of source uuid's that support the "property"
324 virtual std::list<std::string> sourcesForProperty(VehicleProperty::Property property) = 0;
327 * /brief getPropertyAsync requests a property value from a source. This call has a timeout and the callback specified in the request will always be called.
328 * /see AsyncPropertyRequest
329 * /see AsyncPropertyReply.
330 * /param request requested property.
331 * /return AsyncPropertyReply. The returned AsyncPropertyReply is owned by the caller of getPropertyAsync.
332 * /example AsyncPropertyRequest request;
333 * request.property = VehicleProperty::VehicleSpeed
334 * request.completed = [](AsyncPropertyReply* reply)
336 * //you own the reply
339 * routingEngine->getPropertyAsync(request);
341 virtual AsyncPropertyReply * getPropertyAsync(AsyncPropertyRequest request) = 0;
344 * \brief getRangePropertyAsync is used for getting a range of properties that are within the specified time or sequence parameters.
345 * \param request the request containing the property and other information required by the query
346 * \return a pointer to the reply.
347 * \example AsyncRangePropertyRequest vehicleSpeedFromLastWeek;
349 * vehicleSpeedFromLastWeek.timeBegin = amb::currentTime() - 10;
350 * vehicleSpeedFromLastWeek.timeEnd = amb::currentTime();
352 * PropertyList requestList;
353 * requestList.push_back(VehicleProperty::VehicleSpeed);
354 * requestList.push_back(VehicleProperty::EngineSpeed);
356 * vehicleSpeedFromLastWeek.properties = requestList;
357 * vehicleSpeedFromLastWeek.completed = [](AsyncRangePropertyReply* reply)
359 * std::list<AbstractPropertyType*> values = reply->values;
360 * for(auto itr = values.begin(); itr != values.end(); itr++)
363 * DebugOut(1)<<"Value from past: ("<<val->name<<"): "<<val->toString()<<" time: "<<val->timestamp<<endl;
369 * routingEngine->getRangePropertyAsync(vehicleSpeedFromLastWeek);
372 virtual AsyncRangePropertyReply * getRangePropertyAsync(AsyncRangePropertyRequest request) = 0;
375 * \brief setProperty sets a property to a value.
376 * \see AsyncSetPropertyRequest
377 * \see AsyncPropertyReply
378 * \param request the request containing the property and the value to set
379 * \return a pointer to the reply which is owned by the caller of this method
381 virtual AsyncPropertyReply * setProperty(AsyncSetPropertyRequest request) = 0;
382 virtual void subscribeToProperty(VehicleProperty::Property, AbstractSink* self) = 0;
383 virtual void subscribeToProperty(VehicleProperty::Property, std::string sourceUuidFilter, AbstractSink *self) = 0;
384 virtual void subscribeToProperty(VehicleProperty::Property, std::string sourceUuidFilter, Zone::Type zoneFilter, AbstractSink *self) = 0;
385 virtual void unsubscribeToProperty(VehicleProperty::Property, AbstractSink* self) = 0;
387 virtual PropertyInfo getPropertyInfo(VehicleProperty::Property, std::string sourceUuid) = 0;
388 virtual std::list<std::string> getSourcesForProperty(VehicleProperty::Property) = 0;
391 #endif // ABSTRACTROUTINGENGINE_H