Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / resource / examples / simpleclientserver.cpp
index d3ee18b..f4282a3 100644 (file)
 #include "OCApi.h"
 using namespace OC;
 
+
 class ClientWorker
 {
 private:
-    bool m_isFoo;
-    int m_barCount;
     void putResourceInfo(const HeaderOptions& headerOptions,
             const OCRepresentation rep, const OCRepresentation rep2, const int eCode)
     {
@@ -142,13 +141,20 @@ private:
     }
 
 public:
+    ClientWorker(OCConnectivityType ct):m_connectivityType{ct}
+    {}
+
     void start()
     {
+        std::ostringstream requestURI;
+        requestURI << OC_MULTICAST_DISCOVERY_URI << "?rt=core.foo";
+
         std::cout<<"Starting Client find:"<<std::endl;
         FindCallback f (std::bind(&ClientWorker::foundResource, this, std::placeholders::_1));
         std::cout<<"result:" <<
-            OCPlatform::findResource("", "coap://224.0.1.187/oc/core?rt=core.foo", f)
-            << std::endl;
+        OCPlatform::findResource("", requestURI.str(), OC_ALL, f)
+        << std::endl;
+
         std::cout<<"Finding Resource..."<<std::endl;
 
         {
@@ -161,6 +167,9 @@ private:
     std::mutex m_resourceLock;
     std::condition_variable m_cv;
     std::shared_ptr<OCResource> m_resource;
+    OCConnectivityType m_connectivityType;
+    bool m_isFoo;
+    int m_barCount;
 };
 
 struct FooResource
@@ -269,11 +278,7 @@ struct FooResource
             {
                 std::cout <<"\t\trequestFlag : UNSUPPORTED: ";
 
-                if(request->getRequestHandlerFlag()==RequestHandlerFlag::InitFlag)
-                {
-                    std::cout<<"InitFlag"<<std::endl;
-                }
-                else if(request->getRequestHandlerFlag()== RequestHandlerFlag::ObserverFlag)
+                if(request->getRequestHandlerFlag()== RequestHandlerFlag::ObserverFlag)
                 {
                     std::cout<<"ObserverFlag"<<std::endl;
                 }
@@ -289,8 +294,54 @@ struct FooResource
 
 };
 
-int main()
+int main(int argc, char* argv[])
 {
+    OCConnectivityType connectivityType = OC_IPV4;
+
+    if(argc == 2)
+    {
+        try
+        {
+            std::size_t inputValLen;
+            int optionSelected = std::stoi(argv[1], &inputValLen);
+
+            if(inputValLen == strlen(argv[1]))
+            {
+                if(optionSelected == 0)
+                {
+                    connectivityType = OC_IPV4;
+                }
+                else if(optionSelected == 1)
+                {
+                    // TODO: re-enable IPv4/IPv6 command line selection when IPv6 is supported
+                    // connectivityType = OC_IPV6;
+                    connectivityType = OC_IPV4;
+                    std::cout << "IPv6 not currently supported. Using default IPv4" << std::endl;
+                }
+                else
+                {
+                    std::cout << "Invalid connectivity type selected. Using default IPv4"
+                    << std::endl;
+                }
+            }
+            else
+            {
+                std::cout << "Invalid connectivity type selected. Using default IPv4" << std::endl;
+            }
+        }
+        catch(std::exception& )
+        {
+            std::cout << "Invalid input argument. Using IPv4 as connectivity type" << std::endl;
+        }
+    }
+    else
+    {
+        std::cout<< "Usage simpleclientserver <ConnectivityType(0|1)>" << std::endl;
+        std::cout<< "    ConnectivityType: Default IPv4" << std::endl;
+        std::cout << "   ConnectivityType : 0 - IPv4" << std::endl;
+        std::cout << "   ConnectivityType : 1 - IPv6 (not currently supported)" << std::endl;
+    }
+
     PlatformConfig cfg {
         OC::ServiceType::InProc,
         OC::ModeType::Both,
@@ -310,7 +361,7 @@ int main()
             return -1;
         }
 
-        ClientWorker cw;
+        ClientWorker cw(connectivityType);
         cw.start();
     }
     catch(OCException& e)
@@ -320,3 +371,4 @@ int main()
 
     return 0;
 }
+