summaryrefslogtreecommitdiff
path: root/plugins/common/jsonprotocol.h
blob: c641a36f13db5168e630743212fd45778a29904b (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
/*
Copyright (C) 2015 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 AMB_JSON_PROTOCOL_H_
#define AMB_JSON_PROTOCOL_H_

#include <functional>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>

#include <abstractroutingengine.h>
#include <mappropertytype.hpp>
#include <picojson.h>
#include <uuidhelper.h>
#include <vehicleproperty.h>

#include "abstractio.hpp"


namespace amb
{

class Object : public std::unordered_map<std::string, std::shared_ptr<AbstractPropertyType>>
{
public:
	Object(): std::unordered_map<std::string, std::shared_ptr<AbstractPropertyType>>() { }
	Object(const std::string & ifaceName): std::unordered_map<std::string, std::shared_ptr<AbstractPropertyType>>(),
		interfaceName(ifaceName)
	{

	}

	static Object fromJson(const picojson::object & obj);

	static picojson::value toJson(const Object & obj);

	std::string interfaceName;

};

class BaseMessage
{
public:
	BaseMessage() { }

	BaseMessage(std::string n, std::string t)
		: name(n), type(t)
	{
		messageId = amb::createUuid();
	}

	BaseMessage(const BaseMessage & other)
		: name(other.name), type(other.type), data(other.data)
	{

	}

	std::string name;
	std::string type;

	std::string messageId;

	virtual picojson::value toJson();
	virtual bool fromJson(const picojson::value & json);

	template <typename T>
	bool is()
	{
		return T::is(this);
	}

	static bool validate(const picojson::value & json)
	{
		return json.is<picojson::object>() && json.contains("type") && json.contains("name") && json.contains("messageId");
	}

	template <typename T>
	static bool is(const picojson::value & json)
	{
		return T::is(json);
	}

protected:

	picojson::value data;

private:
};

class MethodCall : public BaseMessage
{
public:
	MethodCall(std::string name)
		:BaseMessage(name, "method"), zone(Zone::None), success(false)
	{

	}

	MethodCall(const BaseMessage & other)
		:BaseMessage(other), zone(Zone::None), success(false)
	{
		name = other.name;
	}

	MethodCall(const MethodCall & other)
		:MethodCall(other.name)
	{
		sourceUuid = other.sourceUuid;
		zone = other.zone;
		success = other.success;
	}

	static bool is(const BaseMessage * msg)
	{
		return (msg->type == "method");
	}

	/*!
	 * \brief is checks if json message is of this message type
	 * Assumes that json is already a valid \ref BaseMessage
	 * \param json
	 * \return
	 */
	static bool is(const picojson::value & json)
	{
		return json.contains("source") && json.get("source").is<std::string>()
				&& json.contains("zone") && json.get("zone").is<double>();
	}

	virtual picojson::value toJson();
	virtual bool fromJson(const picojson::value &json);

	std::string sourceUuid;
	Zone::Type zone;
	bool success;
};

class ListMethodCall : public MethodCall
{
public:
	ListMethodCall(): MethodCall("list") {}
	ListMethodCall(const MethodCall & other)
		:MethodCall(other)
	{
		if(!is(&other))
			throw std::runtime_error("type not list");
	}

	picojson::value toJson();
	bool fromJson(const picojson::value &json);

	std::vector<Object> objectNames;

	static bool is(const BaseMessage * msg)
	{
		return msg->name == "list";
	}

	static bool is(const picojson::value & json)
	{
		return json.get("name").to_str() == "list";
	}
};

class GetMethodCall : public MethodCall
{
public:
	GetMethodCall()
		: MethodCall("get")
	{

	}

	picojson::value toJson();
	bool fromJson(const picojson::value &json);

	static bool is(const BaseMessage * msg)
	{
		return msg->name == "get";
	}

	static bool is(const picojson::value & json)
	{
		return json.get("name").to_str() == "get";
	}

	Object value;
};

class SetMethodCall : public MethodCall
{
public:
	SetMethodCall()
		: MethodCall("set")
	{

	}

	picojson::value toJson();
	bool fromJson(const picojson::value &json);

	static bool is(const BaseMessage * msg)
	{
		return msg->name == "set";
	}

	static bool is(const picojson::value & json)
	{
		return json.get("name").to_str() != "set";
	}

	Object value;
};

class BaseJsonMessageReader
{
public:
	BaseJsonMessageReader(AbstractIo* io);

	void canHasData();

protected:

	virtual void hasJsonMessage(const picojson::value & message) = 0;

	template <class T>
	void send(T & msg)
	{
		mIo->write(msg.toJson().serialize());
	}

	std::shared_ptr<AbstractIo> mIo;

private:

	std::string incompleteMessage;

};

class AmbRemoteClient: public BaseJsonMessageReader
{
public:
	typedef std::function<void (std::vector<Object>)> ListCallback;
	typedef std::function<void (Object)> ObjectCallback;
	typedef std::function<void (bool)> SetCallback;

	AmbRemoteClient(AbstractIo* io);

	void list(ListCallback cb);

	void get(const std::string & objectName, ObjectCallback cb);

	void get(const std::string & objectName, const std::string & sourceUuid, ObjectCallback cb);

	void get(const std::string & objectName, Zone::Type zone, ObjectCallback cb);

	void get(const std::string & objectName, const std::string & sourceUuid, Zone::Type zone, ObjectCallback cb);

	void set(const std::string & objectName, const Object & value, SetCallback cb);

	void set(const std::string & objectName, const Object & value, const std::string & sourceUuid, Zone::Type zone, SetCallback cb);

	void listen(const std::string & objectName, const std::string & sourceUuid, Zone::Type zone, ObjectCallback cb);

	void listen(const std::string & objectName, ObjectCallback cb);

protected:

private:

	void hasJsonMessage(const picojson::value & message);

	std::string createSubscriptionId(const std::string & objectName,  const std::string & sourceUuid, Zone::Type zone);
	std::unordered_map<std::string, ListCallback> mListCalls;
	std::unordered_map<std::string, ObjectCallback> mGetMethodCalls;
	std::unordered_map<std::string, SetCallback> mSetMethodCalls;
	std::unordered_map<std::string, std::vector<ObjectCallback>> mSubsriptions;
};

class AmbRemoteServer : public BaseJsonMessageReader
{
public:
	AmbRemoteServer(AbstractIo* io, AbstractRoutingEngine* routingEngine);

protected:

	/*!
	 * \brief list called when a ListMessageCall was received
	 */
	virtual void list(ListMethodCall & call);

	/*!
	 * \brief get called when a GetMessageCall was received
	 */
	virtual void get(GetMethodCall &get);

	/*!
	 * \brief set called when SetMessageCall was received
	 */
	virtual void set();
	/*!
	 * \brief listen called when ListenMessageCall was received
	 */
	virtual void listen();

	void hasJsonMessage(const picojson::value & json);

protected:
	AbstractRoutingEngine* routingEngine;


};

} //namespace amb

#endif