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;
38 class AsyncSetPropertyRequest;
41 typedef std::function<void (AsyncPropertyReply*)> GetPropertyCompletedSignal;
42 typedef std::function<void (AsyncRangePropertyReply*)> GetRangedPropertyCompletedSignal;
45 * \brief The AsyncPropertyRequest class is used by sinks to request property values.
46 * \see AbstractRoutingEngine::getPropertyAsync
47 * \see AsyncPropertyReply
49 class AsyncPropertyRequest
52 AsyncPropertyRequest()
53 :property(VehicleProperty::NoValue),zoneFilter(Zone::None), timeout(10000)
58 AsyncPropertyRequest(const AsyncPropertyRequest &request)
60 this->property = request.property;
61 this->completed = request.completed;
62 this->sourceUuidFilter = request.sourceUuidFilter;
63 this->zoneFilter = request.zoneFilter;
64 this->timeout = request.timeout;
67 AsyncPropertyRequest & operator = (const AsyncPropertyRequest & other)
69 this->property = other.property;
70 this->completed = other.completed;
71 this->sourceUuidFilter = other.sourceUuidFilter;
72 this->zoneFilter = other.zoneFilter;
73 this->timeout = other.timeout;
78 virtual ~AsyncPropertyRequest() { }
81 * \brief property property to request.
83 VehicleProperty::Property property;
86 * \brief sourceUuidFilter the requesting sink should use this to filter on a specific source or leave blank to use any source
88 std::string sourceUuidFilter;
91 * \brief zoneFilter the requesting sink should use this if he wants to filter on a specific zone
93 Zone::Type zoneFilter;
96 * \brief completed the callback when the request has been completed.
98 GetPropertyCompletedSignal completed;
101 * \brief use to specify a timeout in ms for the request. When a timeout occurs, the 'completed' callback
102 * will be called with an error. @see AsyncPropertyReply
103 * default value is: 10000 ms
109 * \brief The AsyncPropertyReply class is used by sources to reply to Get and Set operations. The source should
110 * set success to true if the call is successful or 'false' if the request was not successful and set 'error'
111 * to the appropriate error.
112 * \see AbstractRoutingEngine::getPropertyAsync
113 * \see AsyncPropertyReply
114 * \see AbstractSource::Operations
115 * \see AbstractSource::getPropertyAsync
117 class AsyncPropertyReply: public AsyncPropertyRequest
120 AsyncPropertyReply(const AsyncPropertyRequest &request);
122 AsyncPropertyReply(const AsyncSetPropertyRequest &request);
124 virtual ~AsyncPropertyReply()
128 g_source_destroy(timeoutSource);
129 g_source_unref(timeoutSource);
134 * \brief The Error enum
145 * \brief value of the reply. This may be null if success = false. This is owned by the source.
147 AbstractPropertyType* value;
150 * \brief success indicates if the request was successfull or not. True means success. False means fail and the 'error'
151 * member should be set.
156 * \brief error contains the error if the request was not successful.\
163 GSource* timeoutSource;
167 * \brief The AsyncSetPropertyRequest class is used by sinks to set a property to the 'value'. The source will reply
168 * with a AsyncPropertyReply containing the new value or an error.
169 * \see AbstractRoutingEngine::setProperty
170 * \see AsyncPropertyReply
172 class AsyncSetPropertyRequest: public AsyncPropertyRequest
175 AsyncSetPropertyRequest()
181 AsyncSetPropertyRequest(const AsyncPropertyRequest &request)
182 :AsyncPropertyRequest(request), value(NULL)
187 virtual ~AsyncSetPropertyRequest()
193 * \brief value the new value to set the property to.
195 AbstractPropertyType* value;
199 * \brief The AsyncRangePropertyRequest class is used by sinks to request values within a time or sequence range
200 * \see AbstractRoutingEngine::getRangePropertyAsync
202 class AsyncRangePropertyRequest
205 AsyncRangePropertyRequest()
206 :timeBegin(0), timeEnd(0), sequenceBegin(-1), sequenceEnd(-1)
211 AsyncRangePropertyRequest(const AsyncRangePropertyRequest &request)
213 this->properties = request.properties;
214 this->completed = request.completed;
215 this->timeBegin = request.timeBegin;
216 this->timeEnd = request.timeEnd;
217 this->sequenceBegin = request.sequenceBegin;
218 this->sequenceEnd = request.sequenceEnd;
219 this->sourceUuid = request.sourceUuid;
222 virtual ~AsyncRangePropertyRequest() {}
225 * \brief properties list of properties to request
227 PropertyList properties;
230 * \brief sourceUuid if the sink wishes to request a specific source, this should be set to the uuid of the source.
232 std::string sourceUuid;
235 * \brief completed callback that is called when the ranged request is complete. The reply from this request is passed
236 * into the completed call. The completed callback must free the reply before it returns or there will be a leak.
238 GetRangedPropertyCompletedSignal completed;
241 * \brief timeBegin set this to request values for the specified property beggining at this time. Time is seconds\
242 * since the unix epoc. Set this to '0' if you do not want values within a time range.
247 * \brief timeEnd set this to request values for the specified property beggining at this time. Time is seconds\
248 * since the unix epoc. Set this to '0' if you do not want values within a time range.
253 * \brief sequenceBegin set this to request values with a sequence >= to the sequenceBegin value. Set to -1 if
254 * you don't want values within a sequence ranges.
256 int32_t sequenceBegin;
259 * \brief sequenceEnd set this to request values with a sequence <= to the sequenceEnd value. Set to -1 if
260 * you don't want values within a sequence ranges.
266 * \brief The AsyncRangePropertyReply class is used by a source to reply to an AsyncRangePropertyRequest.
267 * the source should set success to 'true' and populate the 'values' member if the request was successful.
268 * If the request is not successful, 'success' should be set to 'false' and the 'error' member should be set.
270 class AsyncRangePropertyReply: public AsyncRangePropertyRequest
273 AsyncRangePropertyReply(AsyncRangePropertyRequest request)
274 :AsyncRangePropertyRequest(request), success(false)
279 ~AsyncRangePropertyReply()
281 for(auto itr = values.begin(); itr != values.end(); itr++)
290 * \brief error this is set if there was an error in the request. "success" will also be set to false.
292 AsyncPropertyReply::Error error;
295 * \brief values if the request was successful, this will contain a list of values meeting the criteria of the request.
297 std::list<AbstractPropertyType*> values;
300 * \brief success this will be true if the request was successful. If not, this is false and error is set.
305 class AbstractRoutingEngine
308 virtual ~AbstractRoutingEngine();
310 virtual void setSupported(PropertyList supported, AbstractSource* source) = 0;
311 virtual void updateSupported(PropertyList added, PropertyList removed) = 0;
314 void updateProperty(VehicleProperty::Property property, AbstractPropertyType* value, std::string uuid)
316 DebugOut(DebugOut::Warning)<<"updateProperty(VehicleProperty::Property,AbstractPropertyType*,std::string) is deprecated. use new updateProperty(AbstractPropertyType*, const std::string &)"<<endl;
317 updateProperty(value,uuid);
320 virtual void updateProperty(AbstractPropertyType* value, const std::string &uuid) = 0;
321 virtual PropertyList supported() = 0;
324 virtual void registerSink(AbstractSink* self) = 0;
325 virtual void unregisterSink(AbstractSink* self) = 0;
328 * /brief sourcesForProperty
330 * /return list of source uuid's that support the "property"
332 virtual std::list<std::string> sourcesForProperty(VehicleProperty::Property property) = 0;
335 * /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.
336 * /see AsyncPropertyRequest
337 * /see AsyncPropertyReply.
338 * /param request requested property.
339 * /return AsyncPropertyReply. The returned AsyncPropertyReply is owned by the caller of getPropertyAsync.
340 * /example AsyncPropertyRequest request;
341 * request.property = VehicleProperty::VehicleSpeed
342 * request.completed = [](AsyncPropertyReply* reply)
344 * //you own the reply
347 * routingEngine->getPropertyAsync(request);
349 virtual AsyncPropertyReply * getPropertyAsync(AsyncPropertyRequest request) = 0;
352 * \brief getRangePropertyAsync is used for getting a range of properties that are within the specified time or sequence parameters.
353 * \param request the request containing the property and other information required by the query
354 * \return a pointer to the reply.
355 * \example AsyncRangePropertyRequest vehicleSpeedFromLastWeek;
357 * vehicleSpeedFromLastWeek.timeBegin = amb::currentTime() - 10;
358 * vehicleSpeedFromLastWeek.timeEnd = amb::currentTime();
360 * PropertyList requestList;
361 * requestList.push_back(VehicleProperty::VehicleSpeed);
362 * requestList.push_back(VehicleProperty::EngineSpeed);
364 * vehicleSpeedFromLastWeek.properties = requestList;
365 * vehicleSpeedFromLastWeek.completed = [](AsyncRangePropertyReply* reply)
367 * std::list<AbstractPropertyType*> values = reply->values;
368 * for(auto itr = values.begin(); itr != values.end(); itr++)
371 * DebugOut(1)<<"Value from past: ("<<val->name<<"): "<<val->toString()<<" time: "<<val->timestamp<<endl;
377 * routingEngine->getRangePropertyAsync(vehicleSpeedFromLastWeek);
380 virtual AsyncRangePropertyReply * getRangePropertyAsync(AsyncRangePropertyRequest request) = 0;
383 * \brief setProperty sets a property to a value.
384 * \see AsyncSetPropertyRequest
385 * \see AsyncPropertyReply
386 * \param request the request containing the property and the value to set
387 * \return a pointer to the reply which is owned by the caller of this method
390 virtual AsyncPropertyReply * setProperty(AsyncSetPropertyRequest request) = 0;
393 * \brief subscribeToProperty subscribe to changes made to a property value.
394 * \param propertyName name of the property to request a subscription for.
395 * \param self pointer to the sink who is subscribing.
397 * //somewhere in the sink:
398 * routingEngine->subscribeToProperty(VehicleProperty::EngineSpeed, this);
400 * //... elsewhere in the sink, this will be called when a property changes:
401 * void MySink::propertyChanged(const AbstractPropertyType* property)
403 * if(property->name == VehicleProperty::EngineSpeed)
409 virtual void subscribeToProperty(VehicleProperty::Property propertyName, AbstractSink* self) = 0;
412 * \brief subscribeToProperty subscribe to changes made to a property value.
413 * \param propertyName name of the property to request a subscription for.
414 * \param sourceUuidFilter source UUID to filter. Only property updates from this source will be sent to the sink.
415 * \param self pointer to the sink who is subscribing.
417 virtual void subscribeToProperty(VehicleProperty::Property propertyName, std::string sourceUuidFilter, AbstractSink *self) = 0;
420 * \brief subscribeToProperty subscribe to changes made to a property value.
421 * \param propertyName name of the property to request a subscription for.
422 * \param sourceUuidFilter source UUID to filter. Only property updates from this source will be sent to the sink.
423 * \param zoneFilter zone to filter. Only updates from this zone will be passed to the sink.
424 * \param self pointer to the sink who is subscribing.
426 virtual void subscribeToProperty(VehicleProperty::Property propertyName, std::string sourceUuidFilter, Zone::Type zoneFilter, AbstractSink *self) = 0;
428 virtual void unsubscribeToProperty(VehicleProperty::Property, AbstractSink* self) = 0;
430 virtual PropertyInfo getPropertyInfo(VehicleProperty::Property, std::string sourceUuid) = 0;
433 #endif // ABSTRACTROUTINGENGINE_H