909918124bfae9ae8ccce432ffc08b62e565e1f1
[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
33 class AbstractSink;
34 class AbstractSource;
35 class AsyncPropertyReply;
36 class AsyncRangePropertyReply;
37
38
39 typedef std::function<void (AsyncPropertyReply*)> GetPropertyCompletedSignal;
40 typedef std::function<void (AsyncRangePropertyReply*)> GetRangedPropertyCompletedSignal;
41
42 class AsyncPropertyRequest
43 {
44 public:
45         AsyncPropertyRequest()
46                 :property(VehicleProperty::NoValue),timeout(10000)
47         {
48
49         }
50
51         AsyncPropertyRequest(const AsyncPropertyRequest &request)
52         {
53                 this->property = request.property;
54                 this->completed = request.completed;
55                 this->sourceUuid = request.sourceUuid;
56                 this->timeout = request.timeout;
57         }
58
59         AsyncPropertyRequest & operator = (const AsyncPropertyRequest & other)
60         {
61                 this->property = other.property;
62                 this->completed = other.completed;
63                 this->sourceUuid = other.sourceUuid;
64                 this->timeout = other.timeout;
65
66                 return *this;
67         }
68
69         VehicleProperty::Property property;
70         std::string sourceUuid;
71         GetPropertyCompletedSignal completed;
72         uint timeout;
73 };
74
75 class AsyncPropertyReply: public AsyncPropertyRequest
76 {
77 public:
78         AsyncPropertyReply(const AsyncPropertyRequest &request)
79                 :AsyncPropertyRequest(request), value(NULL), success(false), timeoutSource(nullptr)
80         {
81                 auto timeoutfunc = [](gpointer userData) {
82                         AsyncPropertyReply* thisReply = static_cast<AsyncPropertyReply*>(userData);
83                         if(thisReply->success == false)
84                         {
85                                 thisReply->error = Timeout;
86                                 thisReply->completed(thisReply);
87                         }
88                         return 0;
89                 };
90
91                 if(timeout)
92                 {
93                         timeoutSource = g_timeout_source_new(timeout);
94                         g_source_set_callback(timeoutSource,(GSourceFunc) timeoutfunc, this, nullptr);
95                         g_source_attach(timeoutSource, nullptr);
96                 }
97         }
98
99         ~AsyncPropertyReply()
100         {
101                 if(timeoutSource)
102                 {
103                         g_source_destroy(timeoutSource);
104                         g_source_unref(timeoutSource);
105                 }
106         }
107
108         enum Error {
109                 NoError,
110                 Timeout,
111                 InvalidOperation,
112                 PermissionDenied
113         };
114
115         /**
116          * @brief value of the reply.  This may be null if success = false.  This is owned by the source.
117          */
118         AbstractPropertyType* value;
119         bool success;
120         Error error;
121
122 private:
123         GSource* timeoutSource;
124 };
125
126 class AsyncSetPropertyRequest: public AsyncPropertyRequest
127 {
128 public:
129         AsyncSetPropertyRequest()
130                 :value(NULL)
131         {
132
133         }
134
135         AsyncSetPropertyRequest(const AsyncPropertyRequest &request)
136                 :AsyncPropertyRequest(request), value(NULL)
137         {
138
139         }
140
141         AbstractPropertyType* value;
142 };
143
144 class AsyncRangePropertyRequest
145 {
146 public:
147         AsyncRangePropertyRequest()
148                 :timeBegin(0), timeEnd(0), sequenceBegin(-1), sequenceEnd(-1)
149         {
150
151         }
152
153         AsyncRangePropertyRequest(const AsyncRangePropertyRequest &request)
154
155         {
156                 this->property = request.property;
157                 this->completed = request.completed;
158                 this->timeBegin = request.timeBegin;
159                 this->timeEnd = request.timeEnd;
160                 this->sequenceBegin = request.sequenceBegin;
161                 this->sequenceEnd = request.sequenceEnd;
162                 this->sourceUuid = request.sourceUuid;
163         }
164
165         VehicleProperty::Property property;
166         std::string sourceUuid;
167         GetRangedPropertyCompletedSignal completed;
168         double timeBegin;
169         double timeEnd;
170         int32_t sequenceBegin;
171         int32_t sequenceEnd;
172 };
173
174 class AsyncRangePropertyReply: public AsyncRangePropertyRequest
175 {
176 public:
177         AsyncRangePropertyReply(AsyncRangePropertyRequest request)
178                 :AsyncRangePropertyRequest(request), success(false)
179         {
180
181         }
182
183         ~AsyncRangePropertyReply()
184         {
185                 for(auto itr = values.begin(); itr != values.end(); itr++)
186                 {
187                         delete (*itr);
188                 }
189
190                 values.clear();
191         }
192
193         std::list<AbstractPropertyType*> values;
194         bool success;
195 };
196
197 class AbstractRoutingEngine
198 {
199 public:
200         virtual ~AbstractRoutingEngine();
201
202         virtual void setSupported(PropertyList supported, AbstractSource* source) = 0;
203         virtual void updateSupported(PropertyList added, PropertyList removed) = 0;
204         virtual void updateProperty(VehicleProperty::Property property, AbstractPropertyType* value, std::string uuid) = 0;
205
206         /// sinks:
207         virtual void registerSink(AbstractSink* self) = 0;
208         virtual void  unregisterSink(AbstractSink* self) = 0;
209         virtual AsyncPropertyReply * getPropertyAsync(AsyncPropertyRequest request) = 0;
210         virtual AsyncRangePropertyReply * getRangePropertyAsync(AsyncRangePropertyRequest request) = 0;
211         virtual AsyncPropertyReply * setProperty(AsyncSetPropertyRequest request) = 0;
212         virtual void subscribeToProperty(VehicleProperty::Property, AbstractSink* self) = 0;
213         virtual void unsubscribeToProperty(VehicleProperty::Property, AbstractSink* self) = 0;
214         virtual PropertyList supported() = 0;
215 };
216
217 #endif // ABSTRACTROUTINGENGINE_H