RD Server storage functionality
authorHabib Virji <habib.virji@samsung.com>
Tue, 18 Aug 2015 13:28:50 +0000 (14:28 +0100)
committerUze Choi <uzchoi@samsung.com>
Wed, 19 Aug 2015 04:19:13 +0000 (04:19 +0000)
Implements server storage functionality where published resources
are held at the RD.

Change-Id: Ifd432cfdc94cef3d4b435ecb21a0b04dc24d01d8
Signed-off-by: Habib Virji <habib.virji@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2189
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/resource-directory/SConscript
service/resource-directory/include/rd_payload.h
service/resource-directory/src/internal/rd_storage.c [new file with mode: 0644]
service/resource-directory/src/internal/rd_storage.h [new file with mode: 0644]
service/resource-directory/src/rd_payload.c
service/resource-directory/src/rd_server.c

index 2671d3f..fb69246 100755 (executable)
 Import('env')
 
 if env.get('RELEASE'):
-       env.AppendUnique(CCFLAGS = ['-Os'])
-       env.AppendUnique(CPPDEFINES = ['NDEBUG'])
+    env.AppendUnique(CCFLAGS = ['-Os'])
+    env.AppendUnique(CPPDEFINES = ['NDEBUG'])
 else:
-       env.AppendUnique(CCFLAGS = ['-g'])
+    env.AppendUnique(CCFLAGS = ['-g'])
 
 if env.get('LOGGING'):
-       env.AppendUnique(CPPDEFINES = ['-DTB_LOG'])
+    env.AppendUnique(CPPDEFINES = ['-DTB_LOG'])
 
 lib_env = env.Clone()
 SConscript(env.get('SRC_DIR') + '/service/third_party_libs.scons', 'lib_env')
@@ -47,27 +47,28 @@ rd_env.AppendUnique(CPPPATH = ['../../resource/csdk/logger/include'])
 rd_env.PrependUnique(LIBS = ['oc', 'octbstack', 'oc_logger', 'connectivity_abstraction', 'libcoap'])
 
 if target_os not in ['windows', 'winrt']:
-       rd_env.AppendUnique(CXXFLAGS = ['-O2', '-g', '-Wall', '-Wextra'])
+    rd_env.AppendUnique(CXXFLAGS = ['-O2', '-g', '-Wall', '-Wextra'])
 
 if target_os == 'linux':
-       rd_env.AppendUnique(LIBS = ['pthread'])
+    rd_env.AppendUnique(LIBS = ['pthread'])
 
 if target_os == 'android':
-       rd_env.AppendUnique(CXXFLAGS = ['-frtti', '-fexceptions'])
-       rd_env.AppendUnique(LIBS = ['gnustl_static'])
+    rd_env.AppendUnique(CXXFLAGS = ['-frtti', '-fexceptions'])
+    rd_env.AppendUnique(LIBS = ['gnustl_static'])
 
-       if not env.get('RELEASE'):
-               rd_env.AppendUnique(LIBS = ['log'])
+    if not env.get('RELEASE'):
+        rd_env.AppendUnique(LIBS = ['log'])
 
 ######################################################################
 # Source files and Targets
 ######################################################################
 RD_SRC_DIR = 'src/'
 rd_src = [
-               RD_SRC_DIR + 'rd_server.c',
-               RD_SRC_DIR + 'rd_payload.c',
-               RD_SRC_DIR + 'rd_client.c',
-        ]
+        RD_SRC_DIR + '/internal/rd_storage.c',
+        RD_SRC_DIR + 'rd_server.c',
+        RD_SRC_DIR + 'rd_payload.c',
+        RD_SRC_DIR + 'rd_client.c',
+         ]
 
 if target_os in ['tizen'] :
     rdsdk = rd_env.SharedLibrary('resource_directory', rd_src)
index df2b823..9146975 100644 (file)
@@ -117,6 +117,16 @@ void OCRDPayloadDestroy(OCRDPayload *payload);
  */
 void OCRDPayloadLog(LogLevel level, const char *tag, const OCRDPayload *payload);
 
+/**
+ * Logs the subset of the OCRDPayload, prints separately OCRDPublish.
+ *
+ * @param level Log level DEBUG or INFO or ERROR.
+ * @param tag File specific tag to use.
+ * @param payload Pointer to already allocated memory for OCRDPublish.
+ */
+void OCRDPublishPayloadLog(LogLevel level, const char *tag,
+        const OCRDPublishPayload *rdPublish);
+
 #ifdef __cplusplus
 }
 #endif // __cplusplus
diff --git a/service/resource-directory/src/internal/rd_storage.c b/service/resource-directory/src/internal/rd_storage.c
new file mode 100644 (file)
index 0000000..e07dd3f
--- /dev/null
@@ -0,0 +1,85 @@
+//******************************************************************
+//
+// Copyright 2015 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+#include "rd_storage.h"
+
+#include <pthread.h>
+
+#include "oic_malloc.h"
+#include "logger.h"
+
+#include "rd_payload.h"
+
+#define TAG  PCF("RDStorage")
+
+pthread_mutex_t storageMutex = PTHREAD_MUTEX_INITIALIZER;
+// This variable holds the published resources on the RD.
+static OCRDStorePublishResources *g_rdStorage = NULL;
+
+static void printStoragedResources(OCRDStorePublishResources *payload)
+{
+    OC_LOG_V(DEBUG, TAG, "Print Storage Resources ... ");
+    for (OCRDStorePublishResources *temp = payload; temp; temp = temp->next)
+    {
+        if (temp->publishResource)
+        {
+            OCRDPublishPayloadLog(DEBUG, TAG, temp->publishResource);
+        }
+    }
+}
+
+OCStackResult OCRDStorePublishedResources(OCRDPublishPayload *payload)
+{
+    OCRDStorePublishResources *storeResource = OICCalloc(1, sizeof(OCRDStorePublishResources));
+    if (!storeResource)
+    {
+        OC_LOG_V(ERROR, TAG, "Failed allocating memory for OCRDStorePublishResources.");
+        return OC_STACK_NO_MEMORY;
+    }
+
+    OC_LOG_V(DEBUG, TAG, "Storing Resources ... ");
+
+    storeResource->publishResource = OCRDPublishPayloadCreate(payload->ttl, payload->links);
+    if (!storeResource->publishResource)
+    {
+        OC_LOG_V(ERROR, TAG, "Failed allocating memory for OCRDPublishResources.");
+        OICFree(storeResource);
+        return OC_STACK_NO_MEMORY;
+    }
+    storeResource->next = NULL;
+
+    pthread_mutex_lock(&storageMutex);
+    if (g_rdStorage)
+    {
+        OCRDStorePublishResources *temp = g_rdStorage;
+        while(temp->next)
+        {
+            temp = temp->next;
+        }
+        temp->next = storeResource;
+    }
+    else
+    {
+        g_rdStorage = storeResource;
+    }
+    pthread_mutex_unlock(&storageMutex);
+
+    printStoragedResources(g_rdStorage);
+    return OC_STACK_OK;
+}
diff --git a/service/resource-directory/src/internal/rd_storage.h b/service/resource-directory/src/internal/rd_storage.h
new file mode 100644 (file)
index 0000000..cc55425
--- /dev/null
@@ -0,0 +1,47 @@
+//******************************************************************
+//
+// Copyright 2015 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+#ifndef _RESOURCE_DIRECTORY_SERVER_STORAGE_H_
+#define _RESOURCE_DIRECTORY_SERVER_STORAGE_H_
+
+#include "rd_types.h"
+
+/** Stucture holding Published Resources on the Resource Directory. */
+typedef struct OCRDStorePublishResources
+{
+    /** Publish resource. */
+    OCRDPublishPayload *publishResource;
+    /** Linked list pointing to next published resource. */
+    struct OCRDStorePublishResources *next;
+} OCRDStorePublishResources;
+
+/**
+ * Stores the publish resources.
+ *
+ * @param payload RDPublish payload sent from the remote device.
+ *
+ * @return ::OC_STACK_OK upon success, ::OC_STACK_ERROR in case of error.
+ */
+OCStackResult OCRDStorePublishedResources(OCRDPublishPayload *payload);
+
+#ifdef __cplusplus
+}
+#endif // __cplusplus
+
+#endif //_RESOURCE_DIRECTORY_SERVER_STORAGE_H_
index 23a5f81..9ec372a 100644 (file)
@@ -43,7 +43,6 @@ static void linksPayloadDestroy(OCRDLinksPayload *linkPayload)
         links = links->next;
         OICFree(tmp);
     }
-
 }
 
 OCStackResult OCRDPayloadToCbor(const OCRDPayload *rdPayload, uint8_t **outPayload, size_t *size)
@@ -128,6 +127,7 @@ OCStackResult OCRDPayloadToCbor(const OCRDPayload *rdPayload, uint8_t **outPaylo
             OC_LOG_V(DEBUG, TAG, "RD Payload bias factor: %d", rdPayload->rdDiscovery->sel);
         }
     }
+
     cborEncoderResult = cbor_encoder_close_container(&rootArray, &map);
     if (CborNoError != cborEncoderResult)
     {
@@ -378,7 +378,6 @@ void OCRDLinksPayloadCreate(const char *uri, const char *rt, const char *itf,
         OCRDLinksPayload **linksPayload)
 {
     OCRDLinksPayload *payload = OICCalloc(1, sizeof(OCRDLinksPayload));
-
     if (!payload)
     {
         goto no_memory;
@@ -442,7 +441,6 @@ OCRDPublishPayload* OCRDPublishPayloadCreate(int ttl,
         OCRDLinksPayload *linksPayload)
 {
     OCRDPublishPayload *rdPublish = OICCalloc(1, sizeof(OCRDPublishPayload));
-
     if (!rdPublish)
     {
         return NULL;
@@ -504,8 +502,12 @@ void OCRDPayloadLog(LogLevel level, const char *tag, const OCRDPayload *payload)
     {
         OC_LOG_V(level, tag, "RD Payload Discovery BIAS : %d", payload->rdDiscovery->sel);
     }
+    OCRDPublishPayloadLog(level, tag, payload->rdPublish);
+}
 
-    if (payload->rdPublish)
+void OCRDPublishPayloadLog(LogLevel level, const char *tag, const OCRDPublishPayload *rdPublish)
+{
+    if (rdPublish)
     {
         if (payload->rdPublish->deviceName.deviceName)
         {
@@ -517,9 +519,9 @@ void OCRDPayloadLog(LogLevel level, const char *tag, const OCRDPayload *payload)
             OC_LOG_V(level, tag, "RD Payload Publish ID : %s",  payload->rdPublish->deviceId.id);
         }
 
-        OC_LOG_V(level, tag, "RD Payload Publish TTL : %d", payload->rdPublish->ttl);
+        OC_LOG_V(level, tag, "RD Payload Publish TTL : %d", rdPublish->ttl);
 
-        if (payload->rdPublish->links)
+        if (rdPublish->links)
         {
             for (OCRDLinksPayload *temp = payload->rdPublish->links; temp; temp = temp->next)
             {
index 72446f3..228f59c 100644 (file)
 //
 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 #include "rd_server.h"
+
 #include "rd_types.h"
 #include "rd_payload.h"
+#include "rd_storage.h"
 
 #include "logger.h"
 
@@ -97,10 +99,10 @@ static OCEntityHandlerResult handlePublishRequest(const OCEntityHandlerRequest *
         return OC_EH_ERROR;
     }
 
-    OCRDPayload *payload = (OCRDPayload*)ehRequest->payload;
+    OCRDPayload *payload = (OCRDPayload *)ehRequest->payload;
     if (payload->payloadType == RD_PAYLOAD_TYPE_PUBLISH)
     {
-        // TODO STORE RESOURCE...
+        OCRDStorePublishedResources(payload->rdPublish);
     }
 
     OC_LOG_V(DEBUG, TAG, "Sending success response");
@@ -115,11 +117,12 @@ static OCEntityHandlerResult handlePublishRequest(const OCEntityHandlerRequest *
 
     return ehResult;
 }
+
 /*
  * This internal method is the entity handler for RD resources and
  * will handle REST request (GET/PUT/POST/DEL) for them.
  */
-static OCEntityHandlerResult RDEntityHandler(OCEntityHandlerFlag flag,
+static OCEntityHandlerResult rdEntityHandler(OCEntityHandlerFlag flag,
         OCEntityHandlerRequest *ehRequest, void *callbackParameter)
 {
     OCEntityHandlerResult ehRet = OC_EH_ERROR;
@@ -169,7 +172,7 @@ OCStackResult OCRDStart()
                                   OC_RSRVD_RESOURCE_TYPE_RD,
                                   OC_RSRVD_INTERFACE_DEFAULT,
                                   OC_RSRVD_RD_URI,
-                                  RDEntityHandler,
+                                  rdEntityHandler,
                                   NULL,
                                   (OC_ACTIVE | OC_DISCOVERABLE | OC_OBSERVABLE));