Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / linux / configuration / con-server.cpp
index c83c7a0..f609adb 100755 (executable)
@@ -23,7 +23,7 @@
 ///
 
 #include <functional>
-#include <thread>
+#include <pthread.h>
 
 #include "OCPlatform.h"
 #include "OCApi.h"
@@ -38,19 +38,15 @@ using namespace OIC;
 const int SUCCESS_RESPONSE = 0;
 int g_Steps = 0;
 int isWaiting = 0;
+pthread_mutex_t mutex_lock = PTHREAD_MUTEX_INITIALIZER;
 
 // Default system configuration value's variables
 // The variable's names should be same as the names of "extern" variables defined in
-// "ConfigurationCollection.h"
-std::string defaultRegionValue;
-std::string defaultTimeValue;
-std::string defaultCurrentTimeValue;
-std::string defaultNetworkValue;
-std::string defaultIPAddressValue;
-std::string defaultSecurityValue;
-std::string defaultModeValue;
-std::string defaultConfigurationValue;
-std::string defaultFactorySetValue;
+// "ConfigurationResource.h"
+std::string defaultLocation;
+std::string defaultRegion;
+std::string defaultSystemTime;
+std::string defaultCurrency;
 
 static ThingsManager* g_thingsmanager;
 
@@ -59,9 +55,9 @@ bool prepareResponseForResource(std::shared_ptr< OCResourceRequest > request);
 OCStackResult sendResponseForResource(std::shared_ptr< OCResourceRequest > pRequest);
 OCEntityHandlerResult entityHandlerForResource(std::shared_ptr< OCResourceRequest > request);
 
-ConfigurationCollection *myConfigurationCollection;
-DiagnosticsCollection *myDiagnosticsCollection;
-FactorySetCollection *myFactorySetCollection;
+ConfigurationResource *myConfigurationResource;
+DiagnosticsResource *myDiagnosticsResource;
+FactorySetResource *myFactorySetResource;
 
 typedef std::function< void(OCRepresentation&) > putFunc;
 typedef std::function< OCRepresentation(void) > getFunc;
@@ -70,33 +66,15 @@ getFunc getGetFunction(std::string uri)
 {
     getFunc res = NULL;
 
-    if (uri == myConfigurationCollection->getTimeUri())
+    if (uri == myConfigurationResource->getUri())
     {
-        res = std::bind(&ConfigurationCollection::getTimeRepresentation, myConfigurationCollection);
+        res = std::bind(&ConfigurationResource::getConfigurationRepresentation,
+                myConfigurationResource);
     }
-    else if (uri == myConfigurationCollection->getConfigurationUri())
+    else if (uri == myDiagnosticsResource->getUri())
     {
-        res = std::bind(&ConfigurationCollection::getConfigurationRepresentation,
-                myConfigurationCollection);
-    }
-    else if (uri == myConfigurationCollection->myTimeCollection->getCurrentTimeUri())
-    {
-        res = std::bind(&TimeCollection::getCurrentTimeRepresentation,
-                myConfigurationCollection->myTimeCollection);
-    }
-    else if (uri == myConfigurationCollection->getRegionUri())
-    {
-        res = std::bind(&ConfigurationCollection::getRegionRepresentation,
-                myConfigurationCollection);
-    }
-    else if (uri == myDiagnosticsCollection->getFactoryResetUri())
-    {
-        res = std::bind(&DiagnosticsCollection::getFactoryResetRepresentation,
-                myDiagnosticsCollection);
-    }
-    else if (uri == myDiagnosticsCollection->getRebootUri())
-    {
-        res = std::bind(&DiagnosticsCollection::getRebootRepresentation, myDiagnosticsCollection);
+        res = std::bind(&DiagnosticsResource::getDiagnosticsRepresentation,
+                myDiagnosticsResource);
     }
 
     return res;
@@ -106,25 +84,15 @@ putFunc getPutFunction(std::string uri)
 {
     putFunc res = NULL;
 
-    if (uri == myConfigurationCollection->getRegionUri())
+    if (uri == myConfigurationResource->getUri())
     {
-        res = std::bind(&ConfigurationCollection::setRegionRepresentation,
-                myConfigurationCollection, std::placeholders::_1);
+        res = std::bind(&ConfigurationResource::setConfigurationRepresentation,
+                myConfigurationResource, std::placeholders::_1);
     }
-    else if (uri == myConfigurationCollection->myTimeCollection->getCurrentTimeUri())
+    else if (uri == myDiagnosticsResource->getUri())
     {
-        res = std::bind(&TimeCollection::setCurrentTimeRepresentation,
-                myConfigurationCollection->myTimeCollection, std::placeholders::_1);
-    }
-    else if (uri == myDiagnosticsCollection->getFactoryResetUri())
-    {
-        res = std::bind(&DiagnosticsCollection::setFactoryResetRepresentation,
-                myDiagnosticsCollection, std::placeholders::_1);
-    }
-    else if (uri == myDiagnosticsCollection->getRebootUri())
-    {
-        res = std::bind(&DiagnosticsCollection::setRebootRepresentation, myDiagnosticsCollection,
-                std::placeholders::_1);
+        res = std::bind(&DiagnosticsResource::setDiagnosticsRepresentation,
+                myDiagnosticsResource, std::placeholders::_1);
     }
 
     return res;
@@ -141,13 +109,7 @@ bool prepareResponseForResource(std::shared_ptr< OCResourceRequest > request)
         std::string requestType = request->getRequestType();
         int requestFlag = request->getRequestHandlerFlag();
 
-        if (requestFlag == RequestHandlerFlag::InitFlag)
-        {
-            std::cout << "\t\trequestFlag : Init\n";
-
-            // entity handler to perform resource initialization operations
-        }
-        else if (requestFlag == RequestHandlerFlag::RequestFlag)
+        if (requestFlag == RequestHandlerFlag::RequestFlag)
         {
             std::cout << "\t\trequestFlag : Request\n";
 
@@ -231,6 +193,8 @@ OCEntityHandlerResult entityHandlerForResource(std::shared_ptr< OCResourceReques
     std::cout << "\tIn Server CPP (entityHandlerForResource) entity handler:\n";
     OCEntityHandlerResult ehResult = OC_EH_ERROR;
 
+    QueryParamsMap test = request->getQueryParameters();
+
     if (prepareResponseForResource(request))
     {
         if (OC_STACK_OK == sendResponseForResource(request))
@@ -252,38 +216,29 @@ OCEntityHandlerResult entityHandlerForResource(std::shared_ptr< OCResourceReques
 // callback handler on GET request
 void onBootstrap(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode)
 {
-    if (eCode == SUCCESS_RESPONSE)
-    {
-        std::cout << "\n\nGET request was successful" << std::endl;
-        std::cout << "\tResource URI: " << rep.getUri() << std::endl;
-
-        defaultRegionValue = rep.getValue< std::string >("regionValue");
-        defaultTimeValue = rep.getValue< std::string >("timeValue");
-        defaultCurrentTimeValue = rep.getValue< std::string >("currentTimeValue");
-        defaultNetworkValue = rep.getValue< std::string >("networkValue");
-        defaultIPAddressValue = rep.getValue< std::string >("IPAddressValue");
-        defaultSecurityValue = rep.getValue< std::string >("securityValue");
-        defaultModeValue = rep.getValue< std::string >("modeValue");
-        defaultConfigurationValue = rep.getValue< std::string >("configurationValue");
-        defaultFactorySetValue = rep.getValue< std::string >("factorySetValue");
-
-        std::cout << "\tregionValue : " << defaultRegionValue << std::endl;
-        std::cout << "\ttimeValue : " << defaultTimeValue << std::endl;
-        std::cout << "\tcurrentTimeValue : " << defaultCurrentTimeValue << std::endl;
-        std::cout << "\tnetworkValue : " << defaultNetworkValue << std::endl;
-        std::cout << "\tIPAddressValue : " << defaultIPAddressValue << std::endl;
-        std::cout << "\tsecurityValue : " << defaultSecurityValue << std::endl;
-        std::cout << "\tmodeValue : " << defaultModeValue << std::endl;
-        std::cout << "\tconfigurationValue : " << defaultConfigurationValue << std::endl;
-        std::cout << "\tfactorySetValue : " << defaultFactorySetValue << std::endl;
+    pthread_mutex_lock(&mutex_lock);
+    isWaiting = 0;
+    pthread_mutex_unlock(&mutex_lock);
 
-    }
-    else
+    if (eCode != SUCCESS_RESPONSE)
     {
         std::cout << "onGET Response error: " << eCode << std::endl;
-        std::exit(-1);
+        return ;
     }
-    isWaiting = 0;
+
+    std::cout << "\n\nGET request was successful" << std::endl;
+    std::cout << "\tResource URI: " << rep.getUri() << std::endl;
+
+    defaultRegion = rep.getValue< std::string >("r");
+    defaultSystemTime = rep.getValue< std::string >("st");
+    defaultCurrency = rep.getValue< std::string >("c");
+    defaultLocation = rep.getValue< std::string >("loc");
+
+    std::cout << "\tLocation : " << defaultLocation << std::endl;
+    std::cout << "\tSystemTime : " << defaultSystemTime << std::endl;
+    std::cout << "\tCurrency : " << defaultCurrency << std::endl;
+    std::cout << "\tRegion : " << defaultRegion << std::endl;
+
 }
 
 int main()
@@ -309,41 +264,57 @@ int main()
         // Perform app tasks
         while (true)
         {
-
+            pthread_mutex_lock(&mutex_lock);
             if (isWaiting > 0)
+            {
+                pthread_mutex_unlock(&mutex_lock);
                 continue;
+            }
 
             isWaiting = 0;
+            pthread_mutex_unlock(&mutex_lock);
 
             std::cout << endl << endl << "(0) Quit" << std::endl;
             std::cout << "(1) Bootstrap" << std::endl;
             std::cout << "(2) Create Configuration Resources" << std::endl;
 
             cin >> g_Steps;
-            //
+
             if (g_Steps == 0)
+            {
                 break;
+            }
             else if (g_Steps == 1)
             {
                 if( g_thingsmanager->doBootstrap(&onBootstrap) == OC_STACK_OK)
+                {
+                    pthread_mutex_lock(&mutex_lock);
                     isWaiting = 1;
+                    pthread_mutex_unlock(&mutex_lock);
+                }
                 else
+                {
                     std::cout << "A callback pointer of the function is NULL." << std::endl;
+                }
             }
             else if (g_Steps == 2)
             {
-                myConfigurationCollection = new ConfigurationCollection();
-                myConfigurationCollection->createResources(&entityHandlerForResource);
+                myConfigurationResource = new ConfigurationResource();
+                myConfigurationResource->createResources(&entityHandlerForResource);
+
+                myDiagnosticsResource = new DiagnosticsResource();
+                myDiagnosticsResource->createResources(&entityHandlerForResource);
+
 
-                myDiagnosticsCollection = new DiagnosticsCollection();
-                myDiagnosticsCollection->createResources(&entityHandlerForResource);
+                myFactorySetResource = new FactorySetResource();
+                myFactorySetResource->createResources(&entityHandlerForResource);
+                myDiagnosticsResource->factoryReset = std::function < void()
+                        > (std::bind(&ConfigurationResource::factoryReset,
+                                myConfigurationResource));
 
-                myFactorySetCollection = new FactorySetCollection();
-                myFactorySetCollection->createResources(&entityHandlerForResource);
-                myDiagnosticsCollection->factoryReset = std::function < void()
-                        > (std::bind(&ConfigurationCollection::factoryReset,
-                                myConfigurationCollection));
+                pthread_mutex_lock(&mutex_lock);
                 isWaiting = 1;
+                pthread_mutex_unlock(&mutex_lock);
             }
         }
     }
@@ -354,5 +325,6 @@ int main()
 
     // No explicit call to stop the platform.
     // When OCPlatform destructor is invoked, internally we do platform cleanup
+    return 0;
 }