diff options
author | Kevron Rees <tripzero.kev@gmail.com> | 2015-02-02 10:04:40 -0800 |
---|---|---|
committer | Kevron Rees <tripzero.kev@gmail.com> | 2015-02-02 10:04:40 -0800 |
commit | cfe14084f4c40308ab28c419d2965ec750f530c8 (patch) | |
tree | ccfe86978df159471e13aa7e8b417866091d8248 | |
parent | 108d479b3eead2b6719ce26292baafd5cf143556 (diff) | |
download | automotive-message-broker-cfe14084f4c40308ab28c419d2965ec750f530c8.tar.gz |
[AMBClient] - some work on JSonReader
-rw-r--r-- | plugins/common/jsonprotocol.cpp | 27 | ||||
-rw-r--r-- | plugins/common/jsonprotocol.h | 4 | ||||
-rw-r--r-- | tests/testProtocol.cpp | 20 | ||||
-rw-r--r-- | tests/testProtocolClient.cpp | 7 |
4 files changed, 47 insertions, 11 deletions
diff --git a/plugins/common/jsonprotocol.cpp b/plugins/common/jsonprotocol.cpp index 52735423..2374931d 100644 --- a/plugins/common/jsonprotocol.cpp +++ b/plugins/common/jsonprotocol.cpp @@ -248,16 +248,22 @@ amb::BaseJsonMessageReader::BaseJsonMessageReader(AbstractIo *io) void amb::BaseJsonMessageReader::canHasData() { std::string d = mIo->read(); + incompleteMessage += d; + + while(hasJson(incompleteMessage)); +} + +bool amb::BaseJsonMessageReader::hasJson(string & d) +{ std::string::size_type start = d.find("{"); - if(start == std::string::npos) + if(start == std::string::npos && incompleteMessage.empty()) { - incompleteMessage += d; - return; + return false; } - if(incompleteMessage.empty() && start > 0) + if(start > 0) { DebugOut(7) << "We have an incomplete message at the beginning. Toss it away." << endl; d = d.substr(start-1); @@ -268,11 +274,10 @@ void amb::BaseJsonMessageReader::canHasData() if(end == std::string::npos) { - incompleteMessage += d; - return; + return false; } - std::string tryMessage = incompleteMessage + d.substr(0, end+1); + std::string tryMessage = d.substr(0, end+1); DebugOut(6) << "Trying to parse message: " << tryMessage << endl; @@ -286,13 +291,13 @@ void amb::BaseJsonMessageReader::canHasData() { DebugOut(7) << "Invalid or incomplete message" << endl; DebugOut(7) << parseError << endl; - incompleteMessage += d; - return; + return false; } incompleteMessage = end == d.length()-1 ? "" : d.substr(end); hasJsonMessage(doc); + return true; } @@ -468,7 +473,9 @@ amb::Object amb::Object::fromJson(const picojson::object &obj) for(auto i : obj) { if(i.second.is<picojson::object>()) + { ambObj[i.first] = std::shared_ptr<AbstractPropertyType>(amb::jsonToProperty(i.second)); + } } return ambObj; @@ -480,7 +487,7 @@ picojson::value amb::Object::toJson(const amb::Object &obj) jsonObj["interfaceName"] = picojson::value(obj.interfaceName); for(auto i : obj) { - jsonObj[i.second->alias()] = i.second->toJson(); + jsonObj[i.first] = i.second->toJson(); } return picojson::value(jsonObj); diff --git a/plugins/common/jsonprotocol.h b/plugins/common/jsonprotocol.h index c641a36f..ccc37522 100644 --- a/plugins/common/jsonprotocol.h +++ b/plugins/common/jsonprotocol.h @@ -250,6 +250,8 @@ protected: private: + bool hasJson(string &d); + std::string incompleteMessage; }; @@ -258,7 +260,7 @@ class AmbRemoteClient: public BaseJsonMessageReader { public: typedef std::function<void (std::vector<Object>)> ListCallback; - typedef std::function<void (Object)> ObjectCallback; + typedef std::function<void (Object&)> ObjectCallback; typedef std::function<void (bool)> SetCallback; AmbRemoteClient(AbstractIo* io); diff --git a/tests/testProtocol.cpp b/tests/testProtocol.cpp index c29c566e..92edc6e5 100644 --- a/tests/testProtocol.cpp +++ b/tests/testProtocol.cpp @@ -26,6 +26,9 @@ protected: amb::Object interface2("interface2"); interface1.emplace("vehicleSpeed", std::shared_ptr<AbstractPropertyType>(new VehicleProperty::VehicleSpeedType(100))); + interface1.emplace("engineSpeed", std::shared_ptr<AbstractPropertyType>(new VehicleProperty::EngineSpeedType(1999))); + + interface2.emplace("engineSpeed", std::shared_ptr<AbstractPropertyType>(new VehicleProperty::EngineSpeedType(3099))); call.objectNames.push_back(interface1); call.objectNames.push_back(interface2); @@ -36,6 +39,23 @@ protected: void get(amb::GetMethodCall &get) { DebugOut(0) << "get called" << endl; + + if(get.value.interfaceName == "interface1") + { + amb::Object interface1("interface1"); + + interface1.emplace("vehicleSpeed", std::shared_ptr<AbstractPropertyType>(new VehicleProperty::VehicleSpeedType(100))); + interface1.emplace("engineSpeed", std::shared_ptr<AbstractPropertyType>(new VehicleProperty::EngineSpeedType(1999))); + get.value = interface1; + } + else if(get.value.interfaceName == "interface2") + { + amb::Object interface2("interface2"); + interface2.emplace("engineSpeed", std::shared_ptr<AbstractPropertyType>(new VehicleProperty::EngineSpeedType(3099))); + get.value = interface2; + } + + send(get); } }; diff --git a/tests/testProtocolClient.cpp b/tests/testProtocolClient.cpp index 07a728d4..24ea5b8a 100644 --- a/tests/testProtocolClient.cpp +++ b/tests/testProtocolClient.cpp @@ -18,6 +18,13 @@ void runTest(amb::AmbRemoteClient *c) DebugOut(0) << "list call reply" << endl; g_assert(supported.size() == 2); }); + + DebugOut(0) << "calling client->get()" << endl; + c->get("interface1", [](amb::Object &obj) + { + DebugOut(0) << "get call reply" << endl; + g_assert(obj.size() == 3); + }); } int main(int argc, char** argv) |