\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
\r
// put notification resource\r
OCResourceHandle notificationResourceHandle;\r
- if(NSPutNotificationResource(NSGetSubscriptionAccepter(), ¬ificationResourceHandle) != NS_OK)\r
+ if (NSPutNotificationResource(NSGetSubscriptionAccepter(), ¬ificationResourceHandle)\r
+ != NS_OK)\r
{\r
OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("Failed to put notification resource"));\r
return NS_ERROR;\r
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
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
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
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
// 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
// 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
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
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
\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
\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