2 Copyright (C) 2015 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
19 #ifndef AMB_JSON_PROTOCOL_H_
20 #define AMB_JSON_PROTOCOL_H_
25 #include <unordered_map>
28 #include <abstractroutingengine.h>
29 #include <mappropertytype.hpp>
31 #include <uuidhelper.h>
32 #include <vehicleproperty.h>
34 #include "abstractio.hpp"
40 class Object : public std::unordered_map<std::string, std::shared_ptr<AbstractPropertyType>>
43 Object(): std::unordered_map<std::string, std::shared_ptr<AbstractPropertyType>>() { }
44 Object(const std::string & ifaceName): std::unordered_map<std::string, std::shared_ptr<AbstractPropertyType>>(),
45 interfaceName(ifaceName)
50 static Object fromJson(const picojson::object & obj);
52 static picojson::value toJson(const Object & obj);
54 std::string interfaceName;
63 BaseMessage(std::string n, std::string t)
66 messageId = amb::createUuid();
69 BaseMessage(const BaseMessage & other)
70 : name(other.name), type(other.type), data(other.data)
78 std::string messageId;
80 virtual picojson::value toJson();
81 virtual bool fromJson(const picojson::value & json);
89 static bool validate(const picojson::value & json)
91 return json.is<picojson::object>() && json.contains("type") && json.contains("name") && json.contains("messageId");
95 static bool is(const picojson::value & json)
102 picojson::value data;
107 class MethodCall : public BaseMessage
110 MethodCall(std::string name)
111 :BaseMessage(name, "method"), zone(Zone::None), success(false)
116 MethodCall(const BaseMessage & other)
117 :BaseMessage(other), zone(Zone::None), success(false)
122 MethodCall(const MethodCall & other)
123 :MethodCall(other.name)
125 sourceUuid = other.sourceUuid;
127 success = other.success;
130 static bool is(const BaseMessage * msg)
132 return (msg->type == "method");
136 * \brief is checks if json message is of this message type
137 * Assumes that json is already a valid \ref BaseMessage
141 static bool is(const picojson::value & json)
143 return json.contains("source") && json.get("source").is<std::string>()
144 && json.contains("zone") && json.get("zone").is<double>();
147 virtual picojson::value toJson();
148 virtual bool fromJson(const picojson::value &json);
150 std::string sourceUuid;
155 class ListMethodCall : public MethodCall
158 ListMethodCall(): MethodCall("list") {}
159 ListMethodCall(const MethodCall & other)
163 throw std::runtime_error("type not list");
166 picojson::value toJson();
167 bool fromJson(const picojson::value &json);
169 std::vector<Object> objectNames;
171 static bool is(const BaseMessage * msg)
173 return msg->name == "list";
176 static bool is(const picojson::value & json)
178 return json.get("name").to_str() == "list";
182 class GetMethodCall : public MethodCall
191 picojson::value toJson();
192 bool fromJson(const picojson::value &json);
194 static bool is(const BaseMessage * msg)
196 return msg->name == "get";
199 static bool is(const picojson::value & json)
201 return json.get("name").to_str() == "get";
207 class SetMethodCall : public MethodCall
216 picojson::value toJson();
217 bool fromJson(const picojson::value &json);
219 static bool is(const BaseMessage * msg)
221 return msg->name == "set";
224 static bool is(const picojson::value & json)
226 return json.get("name").to_str() != "set";
232 class BaseJsonMessageReader
235 BaseJsonMessageReader(AbstractIo* io);
241 virtual void hasJsonMessage(const picojson::value & message) = 0;
246 std::string buff = msg.toJson().serialize()+"\n";
247 DebugOut() << "writing: " << buff << endl;
251 std::shared_ptr<AbstractIo> mIo;
257 std::string incompleteMessage;
261 class AmbRemoteClient: public BaseJsonMessageReader
264 typedef std::function<void (std::vector<Object>)> ListCallback;
265 typedef std::function<void (Object&)> ObjectCallback;
266 typedef std::function<void (bool)> SetCallback;
268 AmbRemoteClient(AbstractIo* io);
270 void list(ListCallback cb);
272 void get(const std::string & objectName, ObjectCallback cb);
274 void get(const std::string & objectName, const std::string & sourceUuid, ObjectCallback cb);
276 void get(const std::string & objectName, Zone::Type zone, ObjectCallback cb);
278 void get(const std::string & objectName, const std::string & sourceUuid, Zone::Type zone, ObjectCallback cb);
280 void set(const std::string & objectName, const Object & value, SetCallback cb);
282 void set(const std::string & objectName, const Object & value, const std::string & sourceUuid, Zone::Type zone, SetCallback cb);
284 void listen(const std::string & objectName, const std::string & sourceUuid, Zone::Type zone, ObjectCallback cb);
286 void listen(const std::string & objectName, ObjectCallback cb);
292 void hasJsonMessage(const picojson::value & message);
294 std::string createSubscriptionId(const std::string & objectName, const std::string & sourceUuid, Zone::Type zone);
295 std::unordered_map<std::string, ListCallback> mListCalls;
296 std::unordered_map<std::string, ObjectCallback> mGetMethodCalls;
297 std::unordered_map<std::string, SetCallback> mSetMethodCalls;
298 std::unordered_map<std::string, std::vector<ObjectCallback>> mSubsriptions;
301 class AmbRemoteServer : public BaseJsonMessageReader
304 AmbRemoteServer(AbstractIo* io, AbstractRoutingEngine* routingEngine);
309 * \brief list called when a ListMessageCall was received
311 virtual void list(ListMethodCall & call);
314 * \brief get called when a GetMessageCall was received
316 virtual void get(GetMethodCall &get);
319 * \brief set called when SetMessageCall was received
323 * \brief listen called when ListenMessageCall was received
325 virtual void listen();
327 void hasJsonMessage(const picojson::value & json);
330 AbstractRoutingEngine* routingEngine;