[AMBClient] - some work on JSonReader
authorKevron Rees <tripzero.kev@gmail.com>
Mon, 2 Feb 2015 18:04:40 +0000 (10:04 -0800)
committerKevron Rees <tripzero.kev@gmail.com>
Mon, 2 Feb 2015 18:04:40 +0000 (10:04 -0800)
plugins/common/jsonprotocol.cpp
plugins/common/jsonprotocol.h
tests/testProtocol.cpp
tests/testProtocolClient.cpp

index 5273542..2374931 100644 (file)
@@ -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);
index c641a36..ccc3752 100644 (file)
@@ -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);
index c29c566..92edc6e 100644 (file)
@@ -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);
        }
 };
 
index 07a728d..24ea5b8 100644 (file)
@@ -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)