{\r
NS_LOG(DEBUG, "NSInitSubscriptionList - IN");\r
\r
- consumerSubList = NSCacheCreate();\r
+ consumerSubList = NSStorageCreate();\r
consumerSubList->cacheType = NS_PROVIDER_CACHE_SUBSCRIBER;\r
\r
NS_LOG(DEBUG, "NSInitSubscriptionList - OUT");\r
return NS_OK;\r
}\r
\r
-NSResult NSSetSubscriptionAcceptPolicy(NSAccessPolicy policy)\r
+NSResult NSSetSubscriptionAccessPolicy(bool policy)\r
{\r
NS_LOG(DEBUG, "NSSetSubscriptionAcceptPolicy - IN");\r
\r
- if (policy == NS_ACCEPTER_PROVIDER)\r
+ if (policy == NS_POLICY_PROVIDER)\r
{\r
- OIC_LOG(INFO, SUBSCRIPTION_TAG, "Place Provider as a subscription accepter");\r
NS_LOG(DEBUG, "Place Provider as a subscription accepter");\r
}\r
- else if (policy == NS_ACCEPTER_CONSUMER)\r
+ else if (policy == NS_POLICY_CONSUMER)\r
{\r
- OIC_LOG(INFO, SUBSCRIPTION_TAG, "Place Consumer as a subscription accepter");\r
NS_LOG(DEBUG, "Place Consumer as a subscription accepter");\r
}\r
\r
- NSSubscriptionAccepter = policy;\r
+ NSSetPolicy(policy);\r
\r
NS_LOG(DEBUG, "NSSetSubscriptionAcceptPolicy - OUT");\r
return NS_OK;\r
}\r
\r
-int NSGetSubscriptionAccepter()\r
-{\r
- return NSSubscriptionAccepter;\r
-}\r
-\r
NSResult NSSendAccessPolicyResponse(OCEntityHandlerRequest *entityHandlerRequest)\r
{\r
- OIC_LOG(DEBUG, NOTIFICATION_TAG, "Send Notification Policy to consumer");\r
NS_LOG(DEBUG, "NSSendAccessPolicyResponse - IN");\r
\r
// put notification resource\r
- OCResourceHandle notificationResourceHandle;\r
- if (NSPutNotificationResource(NSGetSubscriptionAccepter(), ¬ificationResourceHandle)\r
+ OCResourceHandle notificationResourceHandle = NULL;\r
+ if (NSPutNotificationResource(NSGetPolicy(), ¬ificationResourceHandle)\r
!= NS_OK)\r
{\r
- OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("Failed to put notification resource"));\r
NS_LOG(ERROR, "Fail to put notification resource");\r
return NS_ERROR;\r
}\r
OCRepPayload* payload = OCRepPayloadCreate();\r
if (!payload)\r
{\r
- OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("Failed to allocate Payload"));\r
NS_LOG(ERROR, "payload is NULL");\r
return NS_ERROR;\r
}\r
\r
- OCRepPayloadSetUri(payload, NSGetNotificationUri());\r
- OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_POLICY, NSGetSubscriptionAccepter());\r
- OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_MESSAGE, NSGetNotificationMessageUri());\r
- OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_SYNC, NSGetNotificationSyncUri());\r
+ NS_LOG_V(DEBUG, "NS Provider ID: %s", NSGetProviderInfo()->providerId);\r
+\r
+ OCRepPayloadSetUri(payload, NS_ROOT_URI);\r
+ OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, NSGetProviderInfo()->providerId);\r
+ OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_POLICY, NSGetPolicy());\r
+ OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_MESSAGE, NS_COLLECTION_MESSAGE_URI);\r
+ OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_SYNC, NS_COLLECTION_SYNC_URI);\r
+ OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_TOPIC, NS_COLLECTION_TOPIC_URI);\r
\r
response.requestHandle = entityHandlerRequest->requestHandle;\r
response.resourceHandle = entityHandlerRequest->resource;\r
// Send Response\r
if (OCDoResponse(&response) != OC_STACK_OK)\r
{\r
- OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("Fail to send response"));\r
NS_LOG(ERROR, "Fail to AccessPolicy send response");\r
return NS_ERROR;\r
}\r
\r
void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, NSResourceType resourceType)\r
{\r
-\r
- OIC_LOG(INFO, SUBSCRIPTION_TAG, "Start to subscription process");\r
NS_LOG(DEBUG, "NSHandleSubscription - IN");\r
\r
+ char * id = NSGetValueFromQuery(OICStrdup(entityHandlerRequest->query), NS_QUERY_CONSUMER_ID);\r
+\r
+ if(!id)\r
+ {\r
+ NSFreeOCEntityHandlerRequest(entityHandlerRequest);\r
+ NS_LOG(ERROR, "Invalid ConsumerID");\r
+ return;\r
+ }\r
+\r
+ NS_LOG_V(DEBUG, "consumerId = %s", id);\r
if (resourceType == NS_RESOURCE_MESSAGE)\r
{\r
NS_LOG(DEBUG, "resourceType == NS_RESOURCE_MESSAGE");\r
NSCacheElement * element = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));\r
NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData));\r
\r
- subData->id = OICStrdup(entityHandlerRequest->devAddr.addr);\r
+ OICStrcpy(subData->id, UUID_STRING_SIZE, id);\r
+ NS_LOG_V(DEBUG, "SubList ID = [%s]", subData->id);\r
+\r
+ NS_LOG_V(DEBUG, "Consumer Address: %s", entityHandlerRequest->devAddr.addr);\r
+\r
+ subData->remote_messageObId = subData->messageObId = 0;\r
+\r
+ bool iSRemoteServer = false;\r
+\r
+#ifdef RD_CLIEND\r
+ iSRemoteServer = NSIsRemoteServerAddress(entityHandlerRequest->devAddr.addr);\r
+ if(iSRemoteServer)\r
+ {\r
+ NS_LOG(DEBUG, "Requested by remote server");\r
+ subData->remote_messageObId = entityHandlerRequest->obsInfo.obsId;\r
+ NS_LOG_V(DEBUG, "SubList message observation ID = [%d]", subData->remote_messageObId);\r
+ }\r
+#endif\r
+\r
+ if(!iSRemoteServer)\r
+ {\r
+ NS_LOG(DEBUG, "Requested by local consumer");\r
+ subData->messageObId = entityHandlerRequest->obsInfo.obsId;\r
+ NS_LOG_V(DEBUG, "SubList message observation ID = [%d]", subData->messageObId);\r
+ }\r
+\r
subData->isWhite = false;\r
- subData->messageObId = entityHandlerRequest->obsInfo.obsId;\r
+ subData->remote_syncObId = 0;\r
subData->syncObId = 0;\r
\r
element->data = (void*) subData;\r
element->next = NULL;\r
\r
- NS_LOG_V(DEBUG, "SubList IP[ID] = [%s]", subData->id);\r
- NS_LOG_V(DEBUG, "SubList message observation ID = [%d]", subData->messageObId);\r
-\r
- if (NSCacheWrite(consumerSubList, element) != NS_OK)\r
+ if (NSStorageWrite(consumerSubList, element) != NS_OK)\r
{\r
NS_LOG(DEBUG, "fail to write cache");\r
}\r
\r
- if (NSGetSubscriptionAccepter() == NS_ACCEPTER_PROVIDER)\r
+ bool currPolicy = NSGetPolicy();\r
+\r
+ if (currPolicy == NS_POLICY_PROVIDER)\r
{\r
NS_LOG(DEBUG, "NSGetSubscriptionAccepter == NS_ACCEPTER_PROVIDER");\r
NSAskAcceptanceToUser(entityHandlerRequest);\r
}\r
- else if (NSGetSubscriptionAccepter() == NS_ACCEPTER_CONSUMER)\r
+ else if (currPolicy == NS_POLICY_CONSUMER)\r
{\r
NS_LOG(DEBUG, "NSGetSubscriptionAccepter == NS_ACCEPTER_CONSUMER");\r
- NSSendSubscriptionResponse(entityHandlerRequest, true);\r
+ NSSendConsumerSubResponse(entityHandlerRequest);\r
}\r
}\r
else if (resourceType == NS_RESOURCE_SYNC)\r
{\r
NS_LOG(DEBUG, "resourceType == NS_RESOURCE_SYNC");\r
NSCacheElement * element = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));\r
-\r
NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData));\r
\r
- subData->id = OICStrdup(entityHandlerRequest->devAddr.addr);\r
+ OICStrcpy(subData->id, UUID_STRING_SIZE, id);\r
+ NS_LOG_V(DEBUG, "SubList ID = [%s]", subData->id);\r
+\r
+ NS_LOG_V(DEBUG, "Consumer Address: %s", entityHandlerRequest->devAddr.addr);\r
+\r
+ subData->remote_syncObId = subData->syncObId = 0;\r
+ bool isRemoteServer = false;\r
+\r
+#ifdef RD_CLIENT\r
+ isRemoteServer = NSIsRemoteServerAddress(entityHandlerRequest->devAddr.addr);\r
+ if(isRemoteServer)\r
+ {\r
+ NS_LOG(DEBUG, "Requested by remote server");\r
+ subData->remote_syncObId = entityHandlerRequest->obsInfo.obsId;\r
+ NS_LOG_V(DEBUG, "SubList sync observation ID = [%d]", subData->remote_syncObId);\r
+ }\r
+#endif\r
+\r
+ if(!isRemoteServer)\r
+ {\r
+ NS_LOG(DEBUG, "Requested by local consumer");\r
+ subData->syncObId = entityHandlerRequest->obsInfo.obsId;\r
+ NS_LOG_V(DEBUG, "SubList sync observation ID = [%d]", subData->syncObId);\r
+ }\r
+\r
subData->isWhite = false;\r
- subData->syncObId = entityHandlerRequest->obsInfo.obsId;\r
subData->messageObId = 0;\r
+ subData->remote_messageObId = 0;\r
\r
element->data = (void*) subData;\r
element->next = NULL;\r
\r
- NS_LOG_V(DEBUG, "SubList IP[ID] = [%s]", subData->id);\r
- NS_LOG_V(DEBUG, "SubList sync observation ID = [%d]", subData->syncObId);\r
-\r
- if (NSCacheWrite(consumerSubList, element) != NS_OK)\r
+ if (NSStorageWrite(consumerSubList, element) != NS_OK)\r
{\r
NS_LOG(ERROR, "Fail to write cache");\r
}\r
+\r
+ NSFreeOCEntityHandlerRequest(entityHandlerRequest);\r
}\r
\r
NS_LOG(DEBUG, "NSHandleSubscription - OUT");\r
\r
void NSHandleUnsubscription(OCEntityHandlerRequest *entityHandlerRequest)\r
{\r
- OIC_LOG(INFO, SUBSCRIPTION_TAG, "Start to unsubscription process");\r
NS_LOG(DEBUG, "NSHandleUnsubscription - IN");\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
- NS_LOG_V(DEBUG, "SubList IP[ID] = [%s]", subData->id);\r
- NS_LOG_V(DEBUG, "SubList observation ID = [%d]", subData->syncObId);\r
+ consumerSubList->cacheType = NS_PROVIDER_CACHE_SUBSCRIBER_OBSERVE_ID;\r
\r
- if (NSCacheWrite(consumerSubList, element) != NS_OK)\r
- {\r
- OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to write consumer white list");\r
- NS_LOG(ERROR, "fail to write consumer white list");\r
- }\r
+ while(NSStorageDelete(consumerSubList, (unsigned char *)\r
+ &(entityHandlerRequest->obsInfo.obsId)) != NS_FAIL);\r
+ consumerSubList->cacheType = NS_PROVIDER_CACHE_SUBSCRIBER;\r
\r
- NS_LOG(DEBUG, "NSHandleUnsubscription - IN");\r
NSFreeOCEntityHandlerRequest(entityHandlerRequest);\r
+\r
+ NS_LOG(DEBUG, "NSHandleUnsubscription - OUT");\r
}\r
\r
void NSAskAcceptanceToUser(OCEntityHandlerRequest *entityHandlerRequest)\r
{\r
- OIC_LOG(DEBUG, SUBSCRIPTION_TAG, "Ask for user to Allow or Deny");\r
NS_LOG(DEBUG, "NSAskAcceptanceToUser - IN");\r
\r
- NSPushQueue(RESPONSE_SCHEDULER, TASK_CB_SUBSCRIPTION, entityHandlerRequest);\r
+ NSPushQueue(CALLBACK_RESPONSE_SCHEDULER, TASK_CB_SUBSCRIPTION, entityHandlerRequest);\r
\r
NS_LOG(DEBUG, "NSAskAcceptanceToUser - OUT");\r
}\r
OCRepPayload* payload = OCRepPayloadCreate();\r
if (!payload)\r
{\r
- OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("Failed to allocate Payload"));\r
NS_LOG(ERROR, "fail to create playload");\r
return NS_ERROR;\r
}\r
\r
- OCResourceHandle rHandle;\r
+ OCResourceHandle rHandle = NULL;\r
if (NSPutMessageResource(NULL, &rHandle) != NS_OK)\r
{\r
- OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Fail to put notification resource"));\r
NS_LOG(ERROR, "Fail to put notification resource");\r
return NS_ERROR;\r
}\r
\r
- OCRepPayloadSetUri(payload, NSGetNotificationMessageUri());\r
- OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_ID, "0000-0000-0000-0000");\r
- OCRepPayloadSetPropBool(payload, NS_ATTRIBUTE_ACCPETANCE, accepted);\r
+ OCRepPayloadSetUri(payload, NS_COLLECTION_MESSAGE_URI);\r
+ (accepted) ? OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_MESSAGE_ID, NS_ALLOW)\r
+ : OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_MESSAGE_ID, NS_DENY);\r
+ OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, NSGetProviderInfo()->providerId);\r
\r
- NSCacheElement * element = NSCacheRead(consumerSubList, id);\r
+ NSCacheElement * element = NSStorageRead(consumerSubList, id);\r
\r
if(element == NULL)\r
{\r
}\r
NSCacheSubData * subData = (NSCacheSubData*) element->data;\r
\r
- if (OCNotifyListOfObservers(rHandle, (OCObservationId*)&subData->messageObId, 1, payload, OC_HIGH_QOS)\r
+ if (OCNotifyListOfObservers(rHandle, (OCObservationId*)&subData->messageObId, 1, payload, OC_LOW_QOS)\r
!= OC_STACK_OK)\r
{\r
- OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to send Acceptance");\r
NS_LOG(ERROR, "fail to send Acceptance");\r
OCRepPayloadDestroy(payload);\r
return NS_ERROR;\r
return NS_OK;\r
}\r
\r
-NSResult NSSendSubscriptionResponse(OCEntityHandlerRequest *entityHandlerRequest, bool accepted)\r
+NSResult NSSendConsumerSubResponse(OCEntityHandlerRequest * entityHandlerRequest)\r
{\r
- OIC_LOG(INFO, SUBSCRIPTION_TAG, "Send ACCEPT message to consumer");\r
NS_LOG(DEBUG, "NSSendSubscriptionResponse - IN");\r
\r
if (!entityHandlerRequest)\r
{\r
- OIC_LOG (ERROR, LISTENER_TAG, "Invalid request pointer");\r
+ NS_LOG(ERROR, "Invalid request pointer");\r
return OC_EH_ERROR;\r
}\r
\r
- if (accepted)\r
- {\r
- NS_LOG(DEBUG, "accepted is true");\r
- NSCacheElement * element = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));\r
+ char * id = NSGetValueFromQuery(OICStrdup(entityHandlerRequest->query), NS_QUERY_CONSUMER_ID);\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
- NS_LOG(ERROR, "fail to write consumer white list");\r
- }\r
+ if(!id)\r
+ {\r
+ NSFreeOCEntityHandlerRequest(entityHandlerRequest);\r
+ NS_LOG(ERROR, "Invalid ConsumerID");\r
+ return NS_ERROR;\r
}\r
\r
- NSSendResponse(entityHandlerRequest->devAddr.addr, accepted);\r
+ NSCacheUpdateSubScriptionState(consumerSubList, id, true);\r
+ NSSendResponse(id, true);\r
NSFreeOCEntityHandlerRequest(entityHandlerRequest);\r
-\r
NS_LOG(DEBUG, "NSSendSubscriptionResponse - OUT");\r
return NS_OK;\r
}\r
\r
void * NSSubScriptionSchedule(void *ptr)\r
{\r
-\r
if (ptr == NULL)\r
{\r
- OIC_LOG(INFO, SUBSCRIPTION_TAG, "Create NSSubscriptionSchedule");\r
NS_LOG(DEBUG, "Create NSSubScriptionSchedule");\r
}\r
\r
NS_LOG(DEBUG, "CASE TASK_SEND_ALLOW : ");\r
NSConsumer * consumer = (NSConsumer *) node->taskData;\r
\r
- NSCacheSubData * subData = (NSCacheSubData *) 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
- NSSendResponse(consumer->mId, true);\r
-\r
+ NSCacheUpdateSubScriptionState(consumerSubList, consumer->consumerId, true);\r
+ NSSendResponse(consumer->consumerId, true);\r
+ NSFreeConsumer(consumer);\r
break;\r
}\r
case TASK_SEND_DENY:\r
{\r
NS_LOG(DEBUG, "CASE TASK_SEND_DENY : ");\r
NSConsumer * consumer = (NSConsumer *) node->taskData;\r
- int * pObId = (int *) consumer->mUserData;\r
\r
- NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData));\r
- subData->id = OICStrdup(consumer->mId);\r
- subData->isWhite = false;\r
- subData->messageObId = 0;\r
- subData->syncObId = 0;\r
+ NSCacheUpdateSubScriptionState(consumerSubList, consumer->consumerId, false);\r
+ NSSendResponse(consumer->consumerId, false);\r
+ NSFreeConsumer(consumer);\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
pthread_mutex_unlock(&NSMutex[SUBSCRIPTION_SCHEDULER]);\r
\r
}\r
+ NS_LOG(INFO, "Destroy NSSubScriptionSchedule");\r
return NULL;\r
}\r