\r
#include "NSProviderSubscription.h"\r
\r
-NSResult NSSetSubscriptionAcceptPolicy(NSAccessPolicy policy)\r
+NSResult NSInitSubscriptionList()\r
{\r
- if (policy == NS_ACCEPTER_PROVIDER)\r
+ NS_LOG(DEBUG, "NSInitSubscriptionList - IN");\r
+\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 NSSetSubscriptionAccessPolicy(bool policy)\r
+{\r
+ NS_LOG(DEBUG, "NSSetSubscriptionAcceptPolicy - IN");\r
+\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) != NS_OK)\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
\r
memset(response.resourceUri, 0, sizeof response.resourceUri);\r
\r
OCRepPayload* payload = OCRepPayloadCreate();\r
- //response.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
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
{\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
OCRepPayloadDestroy(payload);\r
+ NSFreeOCEntityHandlerRequest(entityHandlerRequest);\r
\r
+ NS_LOG(DEBUG, "NSSendAccessPolicyResponse - OUT");\r
return NS_OK;\r
}\r
\r
-void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, int state)\r
+void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, NSResourceType resourceType)\r
{\r
+ NS_LOG(DEBUG, "NSHandleSubscription - IN");\r
\r
- OIC_LOG(INFO, SUBSCRIPTION_TAG, "Start to subscription process");\r
+ char * id = NSGetValueFromQuery(OICStrdup(entityHandlerRequest->query), NS_QUERY_CONSUMER_ID);\r
\r
- printf("state = 0 \n");\r
+ if(!id)\r
+ {\r
+ NSFreeOCEntityHandlerRequest(entityHandlerRequest);\r
+ NS_LOG(ERROR, "Invalid ConsumerID");\r
+ return;\r
+ }\r
\r
- // write consumer info to cache\r
- if(state == 0)\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
+ 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
- printf("state = 0 - 1\n");\r
+ bool iSRemoteServer = false;\r
\r
- if (NSProviderWriteToCache(NS_CONSUMER_BLACKLIST, entityHandlerRequest->obsInfo.obsId, state) != NS_OK)\r
+#ifdef RD_CLIEND\r
+ iSRemoteServer = NSIsRemoteServerAddress(entityHandlerRequest->devAddr.addr);\r
+ if(iSRemoteServer)\r
{\r
- OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("fail to write consumer black list"));\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
- printf("state = 0 - 2\n");\r
- // check access policy\r
- if (NSGetSubscriptionAccepter() == NS_ACCEPTER_PROVIDER)\r
+#endif\r
+\r
+ if(!iSRemoteServer)\r
{\r
- // OCDevAddr --> NSConsumer\r
- printf("before NSGetSubscriptionAccepter() == NS_ACCEPTER_PROVIDER)\n");\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->remote_syncObId = 0;\r
+ subData->syncObId = 0;\r
+\r
+ element->data = (void*) subData;\r
+ element->next = NULL;\r
+\r
+ if (NSStorageWrite(consumerSubList, element) != NS_OK)\r
+ {\r
+ NS_LOG(DEBUG, "fail to write cache");\r
+ }\r
+\r
+ bool currPolicy = NSGetPolicy();\r
+\r
+ if (currPolicy == NS_POLICY_PROVIDER)\r
+ {\r
+ NS_LOG(DEBUG, "NSGetSubscriptionAccepter == NS_ACCEPTER_PROVIDER");\r
NSAskAcceptanceToUser(entityHandlerRequest);\r
- printf("After NSGetSubscriptionAccepter() == NS_ACCEPTER_PROVIDER)\n");\r
}\r
- else if (NSGetSubscriptionAccepter() == NS_ACCEPTER_CONSUMER)\r
+ else if (currPolicy == NS_POLICY_CONSUMER)\r
{\r
- printf("state = 0 - 3\n");\r
- NSSendSubscriptionResponse(entityHandlerRequest, NS_ACCEPTER_CONSUMER, true);\r
+ NS_LOG(DEBUG, "NSGetSubscriptionAccepter == NS_ACCEPTER_CONSUMER");\r
+ NSSendConsumerSubResponse(entityHandlerRequest);\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
+ NS_LOG(DEBUG, "resourceType == NS_RESOURCE_SYNC");\r
+ NSCacheElement * element = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));\r
+ NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData));\r
+\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
- OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("fail to write consumer white list"));\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
- printf("state = 1 - 1\n");\r
+\r
+ subData->isWhite = false;\r
+ subData->messageObId = 0;\r
+ subData->remote_messageObId = 0;\r
+\r
+ element->data = (void*) subData;\r
+ element->next = NULL;\r
+\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
\r
void NSHandleUnsubscription(OCEntityHandlerRequest *entityHandlerRequest)\r
{\r
- OIC_LOG(INFO, SUBSCRIPTION_TAG, "Start to unsubscription process");\r
+ NS_LOG(DEBUG, "NSHandleUnsubscription - IN");\r
\r
- // ProcessObserveUnregister (entityHandlerRequest);\r
+ consumerSubList->cacheType = NS_PROVIDER_CACHE_SUBSCRIBER_OBSERVE_ID;\r
\r
- // write consumer info to cache\r
- if (NSProviderWriteToCache(NS_CONSUMER_BLACKLIST, entityHandlerRequest->devAddr) != NS_OK)\r
- {\r
- OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to write consumer black list");\r
- }\r
+ while(NSStorageDelete(consumerSubList, (unsigned char *)\r
+ &(entityHandlerRequest->obsInfo.obsId)) != NS_FAIL);\r
+ consumerSubList->cacheType = NS_PROVIDER_CACHE_SUBSCRIBER;\r
+\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
- // one queue is required for response callback to ask subscription allowance\r
+ NSPushQueue(CALLBACK_RESPONSE_SCHEDULER, TASK_CB_SUBSCRIPTION, entityHandlerRequest);\r
+\r
+ NS_LOG(DEBUG, "NSAskAcceptanceToUser - OUT");\r
}\r
\r
-NSResult NSSendResponse(int observeId, bool accepted)\r
+NSResult NSSendResponse(const char * id, bool accepted)\r
{\r
+ NS_LOG(DEBUG, "NSSendResponse - IN");\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 = NSStorageRead(consumerSubList, id);\r
+\r
+ if(element == NULL)\r
+ {\r
+ NS_LOG(ERROR, "element is NULL");\r
+ return NS_ERROR;\r
+ }\r
+ NSCacheSubData * subData = (NSCacheSubData*) element->data;\r
\r
- if (OCNotifyListOfObservers(rHandle, &observeId, 1, payload, OC_HIGH_QOS) != OC_STACK_OK)\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
\r
}\r
OCRepPayloadDestroy(payload);\r
\r
+ NS_LOG(DEBUG, "NSSendResponse - OUT");\r
return NS_OK;\r
}\r
\r
-NSResult NSSendSubscriptionResponse(OCEntityHandlerRequest *entityHandlerRequest,\r
- NSAccessPolicy accepter, 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
- // write consumer info to cache\r
+ char * id = NSGetValueFromQuery(OICStrdup(entityHandlerRequest->query), NS_QUERY_CONSUMER_ID);\r
\r
- printf("accepted 1 \n");\r
- if (accepted)\r
+ if(!id)\r
{\r
- if (NSProviderWriteToCache(NS_CONSUMER_WHITELIST, entityHandlerRequest->obsInfo.obsId) != NS_OK)\r
- {\r
- OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to write consumer white list");\r
- }\r
+ NSFreeOCEntityHandlerRequest(entityHandlerRequest);\r
+ NS_LOG(ERROR, "Invalid ConsumerID");\r
+ return NS_ERROR;\r
}\r
\r
- NSSendResponse(&entityHandlerRequest->obsInfo.obsId, accepted);\r
-\r
+ NSCacheUpdateSubScriptionState(consumerSubList, id, true);\r
+ NSSendResponse(id, true);\r
+ NSFreeOCEntityHandlerRequest(entityHandlerRequest);\r
+ NS_LOG(DEBUG, "NSSendSubscriptionResponse - OUT");\r
return NS_OK;\r
-\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
while (NSIsRunning[SUBSCRIPTION_SCHEDULER])\r
\r
switch (node->taskType)\r
{\r
-\r
case TASK_SEND_POLICY:\r
-\r
+ NS_LOG(DEBUG, "CASE TASK_SEND_POLICY : ");\r
NSSendAccessPolicyResponse((OCEntityHandlerRequest*) node->taskData);\r
break;\r
\r
case TASK_RECV_SUBSCRIPTION:\r
- printf("before TASK_RECV_SUBSCRIPTION:\n");\r
- NSHandleSubscription((OCEntityHandlerRequest*) node->taskData, 0);\r
- printf("after TASK_RECV_SUBSCRIPTION:\n");\r
+ NS_LOG(DEBUG, "CASE TASK_RECV_SUBSCRIPTION : ");\r
+ NSHandleSubscription((OCEntityHandlerRequest*) node->taskData,\r
+ NS_RESOURCE_MESSAGE);\r
break;\r
\r
case TASK_RECV_UNSUBSCRIPTION:\r
-\r
+ NS_LOG(DEBUG, "CASE TASK_RECV_UNSUBSCRIPTION : ");\r
NSHandleUnsubscription((OCEntityHandlerRequest*) node->taskData);\r
break;\r
\r
case TASK_SEND_ALLOW:\r
{\r
- NSConsumer * consumer = (NSConsumer *)node->taskData;\r
- int * pObId = (int *)consumer->mUserData;\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
+ NS_LOG(DEBUG, "CASE TASK_SEND_ALLOW : ");\r
+ NSConsumer * consumer = (NSConsumer *) node->taskData;\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
\r
-// NSSendSubscriptionResponse((OCEntityHandlerRequest*) node->taskData,\r
-// NS_ACCEPTER_PROVIDER, false);\r
- break;\r
+ NSCacheUpdateSubScriptionState(consumerSubList, consumer->consumerId, false);\r
+ NSSendResponse(consumer->consumerId, false);\r
+ NSFreeConsumer(consumer);\r
\r
+ break;\r
+ }\r
case TASK_SYNC_SUBSCRIPTION:\r
- NSHandleSubscription((OCEntityHandlerRequest*) node->taskData, 1);\r
+ NS_LOG(DEBUG, "CASE TASK_SYNC_SUBSCRIPTION : ");\r
+ NSHandleSubscription((OCEntityHandlerRequest*) node->taskData,\r
+ NS_RESOURCE_SYNC);\r
+ break;\r
+ default:\r
break;\r
\r
}\r
pthread_mutex_unlock(&NSMutex[SUBSCRIPTION_SCHEDULER]);\r
\r
}\r
+ NS_LOG(INFO, "Destroy NSSubScriptionSchedule");\r
return NULL;\r
}\r