1. arrays in OCRepresentation
authorSashi Penta <sashi.kumar.penta@intel.com>
Tue, 23 Sep 2014 07:06:07 +0000 (00:06 -0700)
committerSashi Penta <sashi.kumar.penta@intel.com>
Tue, 23 Sep 2014 20:22:43 +0000 (13:22 -0700)
2. Fixed review comments from Jesse.
3. Refactored the code to get JSON from vector.

Change-Id: I1f31f8f0c242f53f56cf50f9d9e781d8ed770f8c

examples/garageclient.cpp
examples/garageserver.cpp
include/OCApi.h

index 9f7ad5b..60d6fa2 100644 (file)
@@ -38,8 +38,8 @@ public:
 
     bool m_state;
     std::string m_name;
-    bool m_lightState;
-    int m_lightPower;
+    std::vector<bool> m_lightStates;
+    std::vector<int> m_lightPowers;
     OCRepresentation m_lightRep;
 
     Garage() : m_state(false), m_name("")
@@ -49,24 +49,59 @@ public:
 
 Garage myGarage;
 
-// callback handler on PUT request
-void onPut(const OCRepresentation& rep, const int eCode)
+void printRepresentation(const OCRepresentation& rep)
 {
-    if(eCode == SUCCESS_RESPONSE)
-    {
-        std::cout << "PUT request was successful" << std::endl;
-
         rep.getValue("state", myGarage.m_state);
         rep.getValue("name", myGarage.m_name);
         rep.getValue("light", myGarage.m_lightRep);
 
-        myGarage.m_lightRep.getValue("state", myGarage.m_lightState);
-        myGarage.m_lightRep.getValue("power", myGarage.m_lightPower);
+        myGarage.m_lightRep.getValue("states", myGarage.m_lightStates);
+        myGarage.m_lightRep.getValue("powers", myGarage.m_lightPowers);
 
         std::cout << "\tstate: " << myGarage.m_state << std::endl;
         std::cout << "\tname: " << myGarage.m_name << std::endl;
-        std::cout << "\tlightRep: state: " << myGarage.m_lightState << std::endl;
-        std::cout << "\tlightRep: power: " << myGarage.m_lightPower << std::endl;
+        std::cout << "\tlightRep: states: ";
+
+        int first = 1;
+        for(auto state: myGarage.m_lightStates)
+        {
+            if(first)
+            {
+                std::cout << state;
+                first = 0;
+            }
+            else
+            {
+                std::cout << "," << state;
+            }
+        }
+
+        std::cout << std::endl;
+        std::cout << "\tlightRep: powers: ";
+        first = 1;
+        for(auto power: myGarage.m_lightPowers)
+        {
+            if(first)
+            {
+                std::cout << power;
+                first = 0;
+            }
+            else
+            {
+                std::cout << "," << power;
+            }
+        }
+        std::cout << std::endl;
+
+}
+// callback handler on PUT request
+void onPut(const OCRepresentation& rep, const int eCode)
+{
+    if(eCode == SUCCESS_RESPONSE)
+    {
+        std::cout << "PUT request was successful" << std::endl;
+
+        printRepresentation(rep);
     }
     else
     {
@@ -104,17 +139,7 @@ void onGet(const OCRepresentation& rep, const int eCode)
         std::cout << "GET request was successful" << std::endl;
         std::cout << "Resource URI: " << rep.getUri() << std::endl;
 
-        rep.getValue("state", myGarage.m_state);
-        rep.getValue("name", myGarage.m_name);
-        rep.getValue("light", myGarage.m_lightRep);
-
-        myGarage.m_lightRep.getValue("state", myGarage.m_lightState);
-        myGarage.m_lightRep.getValue("power", myGarage.m_lightPower);
-
-        std::cout << "\tstate: " << myGarage.m_state << std::endl;
-        std::cout << "\tname: " << myGarage.m_name << std::endl;
-        std::cout << "\tlightRep: state: " << myGarage.m_lightState << std::endl;
-        std::cout << "\tlightRep: power: " << myGarage.m_lightPower << std::endl;
+        printRepresentation(rep);
 
         putLightRepresentation(curResource);
     }
index ea2576c..637d054 100644 (file)
@@ -49,10 +49,10 @@ public:
     OCRepresentation m_garageRep;
     ObservationIds m_interestedObservers;
 
-    // Light representation with in GarageResource
+    // array of lights representation with in GarageResource
     OCRepresentation m_lightRep;
-    bool m_lightState;
-    int m_lightPower;
+    std::vector<bool> m_lightStates;
+    std::vector<int> m_lightPowers;
 
 public:
     /// Constructor
@@ -63,10 +63,14 @@ public:
         m_garageRep.setValue("state", m_state);
         m_garageRep.setValue("name", m_name);
 
-        m_lightState = true;
-        m_lightPower = 10;
-        m_lightRep.setValue("state", m_lightState);
-        m_lightRep.setValue("power", m_lightPower);
+        for(int i = 0; i <= 9; i++)
+        {
+            m_lightStates.push_back(i % 2 == 0);
+            m_lightPowers.push_back(i);
+        }
+
+        m_lightRep.setValue("states", m_lightStates);
+        m_lightRep.setValue("powers", m_lightPowers);
 
         // Storing another representation within a representation
         m_garageRep.setValue("light", m_lightRep);
index 54fa462..0d66c8e 100644 (file)
@@ -26,6 +26,7 @@
 #include <vector>
 #include <map>
 #include <memory>
+#include <iterator>
 
 #include <boost/property_tree/ptree.hpp>
 #include <boost/property_tree/json_parser.hpp>
@@ -212,36 +213,42 @@ namespace OC
         }
     };
 
-    struct AttributeNull {};
-
-    // Forward declaration
-    struct AttributeDataValue;
-
-    typedef std::map<std::string, AttributeDataValue> AttributeMapValue;
-    typedef std::vector<AttributeDataValue> AttributeValueVector;
-
     typedef boost::variant<
-                AttributeNull,
+                // TODO NULL value to be implmented.
                 int,
+                double,
                 bool,
                 std::string,
-                AttributeMapValue,
-                OCRepresentation,
-                AttributeValueVector> AttributeValue;
+                std::vector<int>,
+                std::vector<double>,
+                std::vector<bool>,
+                std::vector<std::string>,
+                OCRepresentation> AttributeValue;
 
     struct AttributeDataValue
     {
         AttributeValue data;
     };
 
+    template <typename T>
+    inline std::string getJSONFromVector(std::vector<T> v)
+    {
+        std::ostringstream json;
+
+        json << "\"[";
+        if(v.size() != 0)
+        {
+            std::copy(v.begin(), v.end() - 1, std::ostream_iterator<T>(json, ","));
+            json << v.back();
+        }
+        json << "]\"";
+
+        return json.str();
+    }
+
     class ComposeVisitor : public boost::static_visitor<std::string>
     {
         public:
-            std::string operator() (const AttributeNull& nl) const
-            {
-                // TODO Not Implemented
-                return std::string();
-            }
 
             // TODO different int sizes
             std::string operator() (const int i) const
@@ -249,6 +256,11 @@ namespace OC
                 return std::to_string(i);
             }
 
+            std::string operator() (const double d) const
+            {
+                return std::to_string(d);
+            }
+
             std::string operator() (const std::string& str) const
             {
                 std::ostringstream json;
@@ -271,18 +283,42 @@ namespace OC
                 }
             }
 
-            std::string operator() (const AttributeMapValue& objValue) const
+            std::string operator() (const std::vector<int> numbers) const
             {
-                // TODO Not Implemented
-                return std::string();
-                std::ostringstream json;
+                return getJSONFromVector(numbers);
+            }
+
+            std::string operator() (const std::vector<double> numbers) const
+            {
+                return getJSONFromVector(numbers);
             }
 
-            std::string operator() (const AttributeValueVector& values) const
+            std::string operator() (const std::vector<bool> bools) const
             {
-                // TODO Not Implemented
-                return std::string();
                 std::ostringstream json;
+                int first = 1;
+
+                json << "\"[";
+                for(auto b: bools)
+                {
+                    if(first)
+                    {
+                        b ? json << "true" : json << "false";
+                        first = 0;
+                    }
+                    else
+                    {
+                        b ? json << ",true" : json << ",false";
+                    }
+                }
+                json << "]\"";
+
+                return json.str();
+            }
+
+            std::string operator() (const std::vector<std::string> strings) const
+            {
+                return getJSONFromVector(strings);
             }
 
             std::string operator() (const OCRepresentation& rep) const
@@ -300,6 +336,17 @@ namespace OC
 
     };
 
+    inline void split(std::string input, char delimiter, std::vector<std::string>& tokens)
+    {
+        std::stringstream ss(input);
+        std::string item;
+
+        while(std::getline(ss, item, delimiter))
+        {
+            tokens.push_back(item);
+        }
+    }
+
     class ParseVisitor : public boost::static_visitor<void>
     {
         public:
@@ -308,14 +355,14 @@ namespace OC
             {
             }
 
-            void operator() (AttributeNull& nl) const
+            void operator() (int& i) const
             {
-                // TODO Not Implemented
+                i = std::stoi(m_str);
             }
 
-            void operator() (int& i) const
+            void operator() (double& d) const
             {
-                i = std::stoi(m_str);
+                d = std::stod(m_str);
             }
 
             void operator() (std::string& str) const
@@ -328,14 +375,97 @@ namespace OC
                 b = m_str.compare("true") == 0;
             }
 
-            void operator() (AttributeMapValue& objValue) const
+            void operator() (std::vector<int>& numbers) const
             {
-                // TODO Not Implemented
+                numbers.clear();
+
+                if(m_str.length() >= 2)
+                {
+                    std::string str = m_str.substr(1, m_str.length()-2);
+
+                    std::vector<std::string> tokens;
+                    split(str, ',', tokens);
+
+                    for(auto s: tokens)
+                    {
+                        numbers.push_back(std::stoi(s));
+                    }
+                }
+                else
+                {
+                    // TODO Logging
+                    std::cout << "Array type should have atleast []\n";
+                }
+
+            }
+
+            void operator() (std::vector<double>& numbers) const
+            {
+                numbers.clear();
+
+                if(m_str.length() >= 2)
+                {
+                    std::string str = m_str.substr(1, m_str.length()-2);
+                    std::vector<std::string> tokens;
+                    split(str, ',', tokens);
+
+                    for(auto s: tokens)
+                    {
+                        numbers.push_back(std::stod(s));
+                    }
+                }
+                else
+                {
+                    // TODO Logging
+                    std::cout << "Array type should have atleast []\n";
+                }
+            }
+
+            void operator() (std::vector<bool>& bools) const
+            {
+                bools.clear();
+
+                if(m_str.length() >= 2)
+                {
+                    std::string str = m_str.substr(1, m_str.length()-2);
+
+                    std::vector<std::string> tokens;
+                    split(str, ',', tokens);
+
+                    for(auto s: tokens)
+                    {
+                        bools.push_back(s.compare("true") == 0);
+                    }
+                }
+                else
+                {
+                    // TODO Logging
+                    std::cout << "Array type should have atleast []\n";
+                }
+
             }
 
-            void operator() (AttributeValueVector& values) const
+            void operator() (std::vector<std::string>& strings) const
             {
-                // TODO Not Implemented
+                strings.clear();
+
+                if(m_str.length() >= 2)
+                {
+                    std::string str = m_str.substr(1, m_str.length()-2);
+
+                    std::vector<std::string> tokens;
+                    split(str, ',', tokens);
+
+                    for(auto s: tokens)
+                    {
+                        strings.push_back(s);
+                    }
+                }
+                else
+                {
+                    // TODO Logging
+                    std::cout << "Array type should have atleast []\n";
+                }
             }
 
             void operator() (OCRepresentation& rep) const