summaryrefslogtreecommitdiff
path: root/lib/abstractroutingengine.h
blob: 489a9abf61734a260b9985a939250e9c9838ab59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
    Copyright (C) 2012  Intel Corporation

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


#ifndef ABSTRACTROUTINGENGINE_H
#define ABSTRACTROUTINGENGINE_H

#include <sys/types.h>
#include <stdlib.h>
#include <boost/any.hpp>
#include <functional>
#include <string>
#include <time.h>

#include "vehicleproperty.h"
#include "abstractpropertytype.h"
#include "propertyinfo.hpp"

class AbstractSink;
class AbstractSource;
class AsyncPropertyReply;
class AsyncRangePropertyReply;
class AsyncSetPropertyRequest;


typedef std::function<void (AsyncPropertyReply*)> GetPropertyCompletedSignal;
typedef std::function<void (AsyncRangePropertyReply*)> GetRangedPropertyCompletedSignal;

/*!
 * \brief The AsyncPropertyRequest class is used by sinks to request property values.
 * \see AbstractRoutingEngine::getPropertyAsync
 * \see AsyncPropertyReply
 */
class AsyncPropertyRequest
{
public:
	AsyncPropertyRequest()
		:property(VehicleProperty::NoValue),zoneFilter(Zone::None), timeout(10000)
	{

	}

	AsyncPropertyRequest(const AsyncPropertyRequest &request)
	{
		this->property = request.property;
		this->completed = request.completed;
		this->sourceUuidFilter = request.sourceUuidFilter;
		this->zoneFilter = request.zoneFilter;
		this->timeout = request.timeout;
	}

	AsyncPropertyRequest & operator = (const AsyncPropertyRequest & other)
	{
		this->property = other.property;
		this->completed = other.completed;
		this->sourceUuidFilter = other.sourceUuidFilter;
		this->zoneFilter = other.zoneFilter;
		this->timeout = other.timeout;

		return *this;
	}

	virtual ~AsyncPropertyRequest() { }

	/*!
	 * \brief property property to request.
	 */
	VehicleProperty::Property property;

	/*!
	 * \brief sourceUuidFilter the requesting sink should use this to filter on a specific source or leave blank to use any source
	 */
	std::string sourceUuidFilter;

	/*!
	 * \brief zoneFilter the requesting sink should use this if he wants to filter on a specific zone
	 */
	Zone::Type zoneFilter;

	/*!
	 * \brief completed the callback when the request has been completed.
	 */
	GetPropertyCompletedSignal completed;

	/*!
	 * \brief use to specify a timeout in ms for the request.  When a timeout occurs, the 'completed' callback
	 * will be called with an error.  @see AsyncPropertyReply
	 * default value is: 10000 ms
	 */
	uint timeout;
};

/*!
 * \brief The AsyncPropertyReply class is used by sources to reply to Get and Set operations.  The source should
 * set success to true if the call is successful or 'false' if the request was not successful and set 'error'
 * to the appropriate error.
 * \see AbstractRoutingEngine::getPropertyAsync
 * \see AsyncPropertyReply
 * \see AbstractSource::Operations
 * \see AbstractSource::getPropertyAsync
 */
class AsyncPropertyReply: public AsyncPropertyRequest
{
public:
	AsyncPropertyReply(const AsyncPropertyRequest &request);

	AsyncPropertyReply(const AsyncSetPropertyRequest &request);

	virtual ~AsyncPropertyReply()
	{
		if(timeoutSource)
		{
			g_source_destroy(timeoutSource);
			g_source_unref(timeoutSource);
		}
	}

	/*!
	 * \brief The Error enum
	 */
	enum Error {
		NoError = 0,
		Timeout,
		InvalidOperation,
		PermissionDenied,
		ZoneNotSupported
	};

	/*!
	 * \brief value of the reply.  This may be null if success = false.  This is owned by the source.
	 */
	AbstractPropertyType* value;

	/*!
	 * \brief success indicates if the request was successfull or not.  True means success.  False means fail and the 'error'
	 * member should be set.
	 */
	bool success;

	/*!
	 * \brief error contains the error if the request was not successful.\
	 * \see Error
	 */
	Error error;

private:
	void setTimeout();
	GSource* timeoutSource;
};

/*!
 * \brief The AsyncSetPropertyRequest class is used by sinks to set a property to the 'value'.  The source will reply
 * with a AsyncPropertyReply containing the new value or an error.
 * \see AbstractRoutingEngine::setProperty
 * \see AsyncPropertyReply
 */
class AsyncSetPropertyRequest: public AsyncPropertyRequest
{
public:
	AsyncSetPropertyRequest()
		:value(NULL)
	{

	}

	AsyncSetPropertyRequest(const AsyncPropertyRequest &request)
		:AsyncPropertyRequest(request), value(NULL)
	{

	}

	virtual ~AsyncSetPropertyRequest()
	{

	}

	/*!
	 * \brief value the new value to set the property to.
	 */
	AbstractPropertyType* value;
};

/*!
 * \brief The AsyncRangePropertyRequest class is used by sinks to request values within a time or sequence range
 * \see AbstractRoutingEngine::getRangePropertyAsync
 */
class AsyncRangePropertyRequest
{
public:
	AsyncRangePropertyRequest()
		:timeBegin(0), timeEnd(0), sequenceBegin(-1), sequenceEnd(-1)
	{

	}

	AsyncRangePropertyRequest(const AsyncRangePropertyRequest &request)
	{
		this->properties = request.properties;
		this->completed = request.completed;
		this->timeBegin = request.timeBegin;
		this->timeEnd = request.timeEnd;
		this->sequenceBegin = request.sequenceBegin;
		this->sequenceEnd = request.sequenceEnd;
		this->sourceUuid = request.sourceUuid;
	}

	virtual ~AsyncRangePropertyRequest() {}

	/*!
	 * \brief properties list of properties to request
	 */
	PropertyList properties;

	/*!
	 * \brief sourceUuid if the sink wishes to request a specific source, this should be set to the uuid of the source.
	 */
	std::string sourceUuid;

	/*!
	 * \brief completed callback that is called when the ranged request is complete.
	 */
	GetRangedPropertyCompletedSignal completed;

	/*!
	 * \brief timeBegin set this to request values for the specified property beggining at this time.  Time is seconds\
	 * since the unix epoc.  Set this to '0' if you do not want values within a time range.
	 */
	double timeBegin;

	/*!
	 * \brief timeEnd set this to request values for the specified property beggining at this time.  Time is seconds\
	 * since the unix epoc.  Set this to '0' if you do not want values within a time range.
	 */
	double timeEnd;

	/*!
	 * \brief sequenceBegin set this to request values with a sequence >= to the sequenceBegin value.  Set to -1 if
	 * you don't want values within a sequence ranges.
	 */
	int32_t sequenceBegin;

	/*!
	 * \brief sequenceEnd set this to request values with a sequence <= to the sequenceEnd value.  Set to -1 if
	 * you don't want values within a sequence ranges.
	 */
	int32_t sequenceEnd;
};

/*!
 * \brief The AsyncRangePropertyReply class is used by a source to reply to an AsyncRangePropertyRequest.
 * the source should set success to 'true' and populate the 'values' member if the request was successful.
 * If the request is not successful, 'success' should be set to 'false' and the 'error' member should be set.
 */
class AsyncRangePropertyReply: public AsyncRangePropertyRequest
{
public:
	AsyncRangePropertyReply(AsyncRangePropertyRequest request)
		:AsyncRangePropertyRequest(request), success(false)
	{

	}

	~AsyncRangePropertyReply()
	{
		for(auto itr = values.begin(); itr != values.end(); itr++)
		{
			delete (*itr);
		}

		values.clear();
	}

	/*!
	 * \brief error this is set if there was an error in the request.  "success" will also be set to false.
	 */
	AsyncPropertyReply::Error error;

	/*!
	 * \brief values if the request was successful, this will contain a list of values meeting the criteria of the request.
	 */
	std::list<AbstractPropertyType*> values;

	/*!
	 * \brief success this will be true if the request was successful.  If not, this is false and error is set.
	 */
	bool success;
};

class AbstractRoutingEngine
{
public:
	virtual ~AbstractRoutingEngine();

	virtual void setSupported(PropertyList supported, AbstractSource* source) = 0;
	virtual void updateSupported(PropertyList added, PropertyList removed) = 0;

	/// Deprecated:
	void updateProperty(VehicleProperty::Property property, AbstractPropertyType* value, std::string uuid)
	{
		DebugOut(DebugOut::Warning)<<"updateProperty(VehicleProperty::Property,AbstractPropertyType*,std::string) is deprecated.  use new updateProperty(AbstractPropertyType*, const std::string &)"<<endl;
		updateProperty(value,uuid);
	}

	virtual void updateProperty(AbstractPropertyType* value, const std::string &uuid) = 0;
	virtual PropertyList supported() = 0;

	/// sinks:
	virtual void registerSink(AbstractSink* self) = 0;
	virtual void  unregisterSink(AbstractSink* self) = 0;

	/**
	 * /brief sourcesForProperty
	 * /param property
	 * /return list of source uuid's that support the "property"
	 */
	virtual std::list<std::string> sourcesForProperty(VehicleProperty::Property property) = 0;

	/**
	 * /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.
	 * /see AsyncPropertyRequest
	 * /see AsyncPropertyReply.
	 * /param request requested property.
	 * /return AsyncPropertyReply. The returned AsyncPropertyReply is owned by the caller of getPropertyAsync.
	 * /example AsyncPropertyRequest request;
	 * request.property = VehicleProperty::VehicleSpeed
	 * request.completed = [](AsyncPropertyReply* reply)
	 * {
	 *   //you own the reply
	 *   delete reply;
	 * };
	 * routingEngine->getPropertyAsync(request);
	 */
	virtual AsyncPropertyReply * getPropertyAsync(AsyncPropertyRequest request) = 0;

	/*!
	 * \brief getRangePropertyAsync is used for getting a range of properties that are within the specified time or sequence parameters.
	 * \param request the request containing the property and other information required by the query
	 * \return a pointer to the reply.
	 * \example AsyncRangePropertyRequest vehicleSpeedFromLastWeek;
	 *
	 *	vehicleSpeedFromLastWeek.timeBegin = amb::currentTime() - 10;
	 *	vehicleSpeedFromLastWeek.timeEnd = amb::currentTime();
	 *
	 *	PropertyList requestList;
	 *	requestList.push_back(VehicleProperty::VehicleSpeed);
	 *	requestList.push_back(VehicleProperty::EngineSpeed);
	 *
	 *	vehicleSpeedFromLastWeek.properties = requestList;
	 *	vehicleSpeedFromLastWeek.completed = [](AsyncRangePropertyReply* reply)
	 *	{
	 *		std::list<AbstractPropertyType*> values = reply->values;
	 *		for(auto itr = values.begin(); itr != values.end(); itr++)
	 *		{
	 *			auto val = *itr;
	 *			DebugOut(1)<<"Value from past: ("<<val->name<<"): "<<val->toString()<<" time: "<<val->timestamp<<endl;
	 *		}
	 *
	 *		delete reply;
	 *	};
	 *
	 *	routingEngine->getRangePropertyAsync(vehicleSpeedFromLastWeek);
	 *
	 */
	virtual AsyncRangePropertyReply * getRangePropertyAsync(AsyncRangePropertyRequest request) = 0;

	/*!
	 * \brief setProperty sets a property to a value.
	 * \see AsyncSetPropertyRequest
	 * \see AsyncPropertyReply
	 * \param request the request containing the property and the value to set
	 * \return a pointer to the reply which is owned by the caller of this method
	 * \example 
	 */
	virtual AsyncPropertyReply * setProperty(AsyncSetPropertyRequest request) = 0;

	/*!
	 * \brief subscribeToProperty subscribe to changes made to a property value.
	 * \param propertyName name of the property to request a subscription for.
	 * \param self pointer to the sink who is subscribing.
	 * \example
	 * //somewhere in the sink:
	 * routingEngine->subscribeToProperty(VehicleProperty::EngineSpeed, this);
	 *
	 * //... elsewhere in the sink, this will be called when a property changes:
	 * void MySink::propertyChanged(const AbstractPropertyType* property)
	 * {
	 *   if(property->name == VehicleProperty::EngineSpeed)
	 *   {
	 *      ...
	 *   }
	 * }
	 */
	virtual void subscribeToProperty(VehicleProperty::Property propertyName, AbstractSink* self) = 0;

	/*!
	 * \brief subscribeToProperty subscribe to changes made to a property value.
	 * \param propertyName name of the property to request a subscription for.
	 * \param sourceUuidFilter source UUID to filter.  Only property updates from this source will be sent to the sink.
	 * \param self pointer to the sink who is subscribing.
	 */
	virtual void subscribeToProperty(VehicleProperty::Property propertyName, std::string sourceUuidFilter, AbstractSink *self) = 0;

	/*!
	 * \brief subscribeToProperty subscribe to changes made to a property value.
	 * \param propertyName name of the property to request a subscription for.
	 * \param sourceUuidFilter source UUID to filter.  Only property updates from this source will be sent to the sink.
	 * \param zoneFilter zone to filter.  Only updates from this zone will be passed to the sink.
	 * \param self pointer to the sink who is subscribing.
	 */
	virtual void subscribeToProperty(VehicleProperty::Property propertyName, std::string sourceUuidFilter, Zone::Type zoneFilter, AbstractSink *self) = 0;

	virtual void unsubscribeToProperty(VehicleProperty::Property, AbstractSink* self) = 0;

	virtual PropertyInfo getPropertyInfo(VehicleProperty::Property, std::string sourceUuid) = 0;
	virtual std::list<std::string> getSourcesForProperty(VehicleProperty::Property) = 0;
};

#endif // ABSTRACTROUTINGENGINE_H