added cacheadapter interface and memorycache of notification.
[platform/upstream/iotivity.git] / service / notification / src / provider / NSProviderSubscription.c
index ac58034..a0aae6d 100644 (file)
 \r
 #include "NSProviderSubscription.h"\r
 \r
+NSResult NSInitSubscriptionList()\r
+{\r
+    consumerSubList = NSCacheCreate();\r
+    consumerSubList->cacheType = NS_PROVIDER_CACHE_SUBSCRIBER;\r
+    return NS_OK;\r
+}\r
+\r
 NSResult NSSetSubscriptionAcceptPolicy(NSAccessPolicy policy)\r
 {\r
     if (policy == NS_ACCEPTER_PROVIDER)\r
@@ -47,7 +54,8 @@ NSResult NSSendAccessPolicyResponse(OCEntityHandlerRequest *entityHandlerRequest
 \r
     // put notification resource\r
     OCResourceHandle notificationResourceHandle;\r
-    if(NSPutNotificationResource(NSGetSubscriptionAccepter(), &notificationResourceHandle) != NS_OK)\r
+    if (NSPutNotificationResource(NSGetSubscriptionAccepter(), &notificationResourceHandle)\r
+            != NS_OK)\r
     {\r
         OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("Failed to put notification resource"));\r
         return NS_ERROR;\r
@@ -77,8 +85,7 @@ NSResult NSSendAccessPolicyResponse(OCEntityHandlerRequest *entityHandlerRequest
     response.resourceHandle = entityHandlerRequest->resource;\r
     response.persistentBufferFlag = 0;\r
     response.ehResult = OC_EH_OK;\r
-    response.payload = (OCPayload *)payload;\r
-\r
+    response.payload = (OCPayload *) payload;\r
 \r
     // Send Response\r
     if (OCDoResponse(&response) != OC_STACK_OK)\r
@@ -91,7 +98,7 @@ NSResult NSSendAccessPolicyResponse(OCEntityHandlerRequest *entityHandlerRequest
     return NS_OK;\r
 }\r
 \r
-void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, int state)\r
+void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, NSResourceType resourceType)\r
 {\r
 \r
     OIC_LOG(INFO, SUBSCRIPTION_TAG, "Start to subscription process");\r
@@ -99,17 +106,29 @@ void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, int stat
     printf("state = 0 \n");\r
 \r
     // write consumer info to cache\r
-    if(state == 0)\r
+\r
+    if (resourceType == NS_RESOURCE_MESSAGE)\r
     {\r
+        int obId = entityHandlerRequest->obsInfo.obsId;\r
+\r
+        NSCacheElement * element = (NSCacheElement *) malloc(sizeof(NSCacheElement));\r
+\r
+        NSCacheSubData * subData = (NSCacheSubData *) malloc(sizeof(NSCacheSubData));\r
+        subData->id = OICStrdup(entityHandlerRequest->devAddr.addr);\r
+        subData->isWhite = false;\r
+        subData->messageObId = entityHandlerRequest->obsInfo.obsId;\r
+        subData->syncObId = 0;\r
 \r
-        printf("state = 0 - 1\n");\r
+        element->data = (void*) subData;\r
+        element->next = NULL;\r
 \r
-        if (NSProviderWriteToCache(NS_CONSUMER_BLACKLIST, entityHandlerRequest->obsInfo.obsId, state) != NS_OK)\r
+        printf("NS_ message ob Id = %d\n", subData->messageObId);\r
+\r
+        if (NSCacheWrite(consumerSubList, element) != NS_OK)\r
         {\r
-            OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("fail to write consumer black list"));\r
+            printf("Cache Write Error\n");\r
         }\r
-        printf("state = 0 - 2\n");\r
-        // check access policy\r
+\r
         if (NSGetSubscriptionAccepter() == NS_ACCEPTER_PROVIDER)\r
         {\r
             // OCDevAddr --> NSConsumer\r
@@ -123,14 +142,28 @@ void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, int stat
             NSSendSubscriptionResponse(entityHandlerRequest, NS_ACCEPTER_CONSUMER, true);\r
         }\r
     }\r
-    else if(state == 1)\r
+    else if (resourceType == NS_RESOURCE_SYNC)\r
     {\r
-        printf("state = 1 - 0\n");\r
-        if (NSProviderWriteToCache(NS_CONSUMER_WHITELIST, entityHandlerRequest->obsInfo.obsId, state) != NS_OK)\r
+        int obId = entityHandlerRequest->obsInfo.obsId;\r
+\r
+        NSCacheElement * element = (NSCacheElement *) malloc(sizeof(NSCacheElement));\r
+\r
+        NSCacheSubData * subData = (NSCacheSubData *) malloc(sizeof(NSCacheSubData));\r
+        printf("NS_ entityHandlerRequest->devAddr.addr = %s\n", entityHandlerRequest->devAddr.addr);\r
+        subData->id = OICStrdup(entityHandlerRequest->devAddr.addr);\r
+        subData->isWhite = false;\r
+        subData->syncObId = entityHandlerRequest->obsInfo.obsId;\r
+        subData->messageObId = 0;\r
+\r
+        element->data = (void*) subData;\r
+        element->next = NULL;\r
+\r
+        printf("NS_ sync ob Id = %d\n", subData->syncObId);\r
+\r
+        if (NSCacheWrite(consumerSubList, element) != NS_OK)\r
         {\r
-            OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("fail to write consumer white list"));\r
+            printf("Cache Write Error\n");\r
         }\r
-        printf("state = 1 - 1\n");\r
     }\r
 }\r
 \r
@@ -141,9 +174,20 @@ void NSHandleUnsubscription(OCEntityHandlerRequest *entityHandlerRequest)
     // ProcessObserveUnregister (entityHandlerRequest);\r
 \r
     // write consumer info to cache\r
-    if (NSProviderWriteToCache(NS_CONSUMER_BLACKLIST, entityHandlerRequest->devAddr) != NS_OK)\r
+    int obId = entityHandlerRequest->obsInfo.obsId;\r
+\r
+    NSCacheElement * element = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));\r
+    NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData));\r
+    subData->id = OICStrdup(entityHandlerRequest->devAddr.addr);\r
+    subData->isWhite = false;\r
+    subData->messageObId = entityHandlerRequest->obsInfo.obsId;\r
+\r
+    element->data = (void*) subData;\r
+    element->next = NULL;\r
+\r
+    if (NSCacheWrite(consumerSubList, element) != NS_OK)\r
     {\r
-        OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to write consumer black list");\r
+        OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to write consumer white list");\r
     }\r
 }\r
 \r
@@ -155,7 +199,7 @@ void NSAskAcceptanceToUser(OCEntityHandlerRequest *entityHandlerRequest)
     // one queue is required for response callback to ask subscription allowance\r
 }\r
 \r
-NSResult NSSendResponse(int observeId, bool accepted)\r
+NSResult NSSendResponse(const char * id, bool accepted)\r
 {\r
 \r
     OCRepPayload* payload = OCRepPayloadCreate();\r
@@ -176,7 +220,11 @@ NSResult NSSendResponse(int observeId, bool accepted)
     OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_ID, "0000-0000-0000-0000");\r
     OCRepPayloadSetPropBool(payload, NS_ATTRIBUTE_ACCPETANCE, accepted);\r
 \r
-    if (OCNotifyListOfObservers(rHandle, &observeId, 1, payload, OC_HIGH_QOS) != OC_STACK_OK)\r
+    NSCacheElement * element = NSCacheRead(consumerSubList, id);\r
+    NSCacheSubData * subData = element->data;\r
+\r
+    if (OCNotifyListOfObservers(rHandle, &subData->messageObId, 1, payload, OC_HIGH_QOS)\r
+            != OC_STACK_OK)\r
     {\r
         OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to send Acceptance");\r
         OCRepPayloadDestroy(payload);\r
@@ -204,7 +252,19 @@ NSResult NSSendSubscriptionResponse(OCEntityHandlerRequest *entityHandlerRequest
     printf("accepted 1 \n");\r
     if (accepted)\r
     {\r
-        if (NSProviderWriteToCache(NS_CONSUMER_WHITELIST, entityHandlerRequest->obsInfo.obsId) != NS_OK)\r
+        int obId = entityHandlerRequest->obsInfo.obsId;\r
+\r
+        NSCacheElement * element = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));\r
+\r
+        NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData));\r
+        subData->id = OICStrdup(entityHandlerRequest->devAddr.addr);\r
+        subData->isWhite = true;\r
+        subData->messageObId = entityHandlerRequest->obsInfo.obsId;\r
+\r
+        element->data = (void*) subData;\r
+        element->next = NULL;\r
+\r
+        if (NSCacheWrite(consumerSubList, element) != NS_OK)\r
         {\r
             OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to write consumer white list");\r
         }\r
@@ -244,7 +304,8 @@ void * NSSubScriptionSchedule(void *ptr)
 \r
                 case TASK_RECV_SUBSCRIPTION:\r
                     printf("before TASK_RECV_SUBSCRIPTION:\n");\r
-                    NSHandleSubscription((OCEntityHandlerRequest*) node->taskData, 0);\r
+                    NSHandleSubscription((OCEntityHandlerRequest*) node->taskData,\r
+                            NS_RESOURCE_MESSAGE);\r
                     printf("after TASK_RECV_SUBSCRIPTION:\n");\r
                     break;\r
 \r
@@ -255,27 +316,42 @@ void * NSSubScriptionSchedule(void *ptr)
 \r
                 case TASK_SEND_ALLOW:\r
                 {\r
-                    NSConsumer * consumer = (NSConsumer *)node->taskData;\r
-                    int * pObId = (int *)consumer->mUserData;\r
+                    NSConsumer * consumer = (NSConsumer *) node->taskData;\r
+                    int * pObId = (int *) consumer->mUserData;\r
+\r
+                    NSCacheSubData * subData = (NSCacheMsgData *) OICMalloc(sizeof(NSCacheSubData));\r
+                    subData->id = OICStrdup(consumer->mId);\r
+                    subData->isWhite = true;\r
+                    subData->messageObId = 0;\r
+                    subData->syncObId = 0;\r
+\r
+                    NSCacheUpdateSubScriptionState(consumerSubList, subData);\r
 \r
                     printf("observer ID = %d\n", *pObId);\r
 \r
-                    if (NSProviderWriteToCache(NS_CONSUMER_WHITELIST, *pObId, 0) != NS_OK)\r
-                    {\r
-                        OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to write consumer white list");\r
-                    }\r
-                    NSSendResponse(*pObId, true);\r
+                    NSSendResponse(consumer->mId, true);\r
 \r
                     break;\r
                 }\r
                 case TASK_SEND_DENY:\r
+                {\r
+                    NSConsumer * consumer = (NSConsumer *) node->taskData;\r
+                    int * pObId = (int *) consumer->mUserData;\r
 \r
-//                    NSSendSubscriptionResponse((OCEntityHandlerRequest*) node->taskData,\r
-//                            NS_ACCEPTER_PROVIDER, false);\r
-                    break;\r
+                    NSCacheSubData * subData = (NSCacheMsgData *) OICMalloc(sizeof(NSCacheSubData));\r
+                    subData->id = OICStrdup(consumer->mId);\r
+                    subData->isWhite = false;\r
+                    subData->messageObId = 0;\r
+                    subData->syncObId = 0;\r
 \r
+                    NSCacheUpdateSubScriptionState(consumerSubList, subData);\r
+                    printf("observer ID = %d\n", *pObId);\r
+                    NSSendResponse(consumer->mId, false);\r
+                    break;\r
+                }\r
                 case TASK_SYNC_SUBSCRIPTION:\r
-                    NSHandleSubscription((OCEntityHandlerRequest*) node->taskData, 1);\r
+                    NSHandleSubscription((OCEntityHandlerRequest*) node->taskData,\r
+                            NS_RESOURCE_SYNC);\r
                     break;\r
 \r
             }\r