X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=service%2Fnotification%2Fsrc%2Fprovider%2FNSProviderSubscription.c;h=3116cbb4f48da4aacaa5ad772c185f9e8741b798;hb=7f00f942c39b7bc27c7eeecf213a239c3fe4173c;hp=361a2e81b5dbbb4769862e9a0ed3e7d17bbd6c67;hpb=fef266b1c75e567c02bc705c93aa92d477a9a62e;p=platform%2Fupstream%2Fiotivity.git diff --git a/service/notification/src/provider/NSProviderSubscription.c b/service/notification/src/provider/NSProviderSubscription.c index 361a2e8..3116cbb 100644 --- a/service/notification/src/provider/NSProviderSubscription.c +++ b/service/notification/src/provider/NSProviderSubscription.c @@ -19,49 +19,46 @@ //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #include "NSProviderSubscription.h" +#include "NSProviderListener.h" NSResult NSInitSubscriptionList() { NS_LOG(DEBUG, "NSInitSubscriptionList - IN"); - consumerSubList = NSStorageCreate(); + consumerSubList = NSProviderStorageCreate(); + NS_VERIFY_NOT_NULL(consumerSubList, NS_FAIL); consumerSubList->cacheType = NS_PROVIDER_CACHE_SUBSCRIBER; NS_LOG(DEBUG, "NSInitSubscriptionList - OUT"); return NS_OK; } -NSResult NSSetSubscriptionAccessPolicy(NSAccessPolicy access) +NSResult NSSetSubscriptionAccessPolicy(bool policy) { NS_LOG(DEBUG, "NSSetSubscriptionAcceptPolicy - IN"); - if (access == NS_ACCESS_ALLOW) + if (policy == NS_POLICY_PROVIDER) { NS_LOG(DEBUG, "Place Provider as a subscription accepter"); } - else if (access == NS_ACCESS_DENY) + else if (policy == NS_POLICY_CONSUMER) { NS_LOG(DEBUG, "Place Consumer as a subscription accepter"); } - NSSubscriptionAccess = access; + NSSetPolicy(policy); NS_LOG(DEBUG, "NSSetSubscriptionAcceptPolicy - OUT"); return NS_OK; } -int NSGetSubscriptionAccepter() -{ - return NSSubscriptionAccess; -} - NSResult NSSendAccessPolicyResponse(OCEntityHandlerRequest *entityHandlerRequest) { NS_LOG(DEBUG, "NSSendAccessPolicyResponse - IN"); // put notification resource - OCResourceHandle notificationResourceHandle; - if (NSPutNotificationResource(NSGetSubscriptionAccepter(), ¬ificationResourceHandle) + OCResourceHandle notificationResourceHandle = NULL; + if (NSPutNotificationResource(NSGetPolicy(), ¬ificationResourceHandle) != NS_OK) { NS_LOG(ERROR, "Fail to put notification resource"); @@ -82,13 +79,26 @@ NSResult NSSendAccessPolicyResponse(OCEntityHandlerRequest *entityHandlerRequest return NS_ERROR; } - NS_LOG_V(DEBUG, "NS Provider ID: %s", NSGetProviderInfo()->providerId); + NS_LOG_V(INFO_PRIVATE, "NS Provider ID: %s", NSGetProviderInfo()->providerId); + + char * copyReq = OICStrdup(entityHandlerRequest->query); + char * reqInterface = NSGetValueFromQuery(copyReq, NS_QUERY_INTERFACE); + + if (reqInterface && strcmp(reqInterface, NS_INTERFACE_BASELINE) == 0) + { + OCResourcePayloadAddStringLL(&payload->interfaces, NS_INTERFACE_BASELINE); + OCResourcePayloadAddStringLL(&payload->interfaces, NS_INTERFACE_READ); + OCResourcePayloadAddStringLL(&payload->types, NS_ROOT_TYPE); + } + NSOICFree(copyReq); OCRepPayloadSetUri(payload, NS_ROOT_URI); OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, NSGetProviderInfo()->providerId); - OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_POLICY, NSGetSubscriptionAccepter()); + OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_VERSION, VERSION); + OCRepPayloadSetPropBool(payload, NS_ATTRIBUTE_POLICY, NSGetPolicy()); OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_MESSAGE, NS_COLLECTION_MESSAGE_URI); OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_SYNC, NS_COLLECTION_SYNC_URI); + OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_TOPIC, NS_COLLECTION_TOPIC_URI); response.requestHandle = entityHandlerRequest->requestHandle; response.resourceHandle = entityHandlerRequest->resource; @@ -100,6 +110,7 @@ NSResult NSSendAccessPolicyResponse(OCEntityHandlerRequest *entityHandlerRequest if (OCDoResponse(&response) != OC_STACK_OK) { NS_LOG(ERROR, "Fail to AccessPolicy send response"); + OCRepPayloadDestroy(payload); return NS_ERROR; } OCRepPayloadDestroy(payload); @@ -113,76 +124,106 @@ void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, NSResour { NS_LOG(DEBUG, "NSHandleSubscription - IN"); - char * id = NSGetValueFromQuery(OICStrdup(entityHandlerRequest->query), NS_QUERY_CONSUMER_ID); + char * copyReq = OICStrdup(entityHandlerRequest->query); + char * id = NSGetValueFromQuery(copyReq, NS_QUERY_CONSUMER_ID); - if(!id) + if (!id) { + NSOICFree(copyReq); NSFreeOCEntityHandlerRequest(entityHandlerRequest); NS_LOG(ERROR, "Invalid ConsumerID"); return; } - NS_LOG_V(DEBUG, "consumerId = %s", id); + NS_LOG_V(INFO_PRIVATE, "consumerId = %s", id); if (resourceType == NS_RESOURCE_MESSAGE) { NS_LOG(DEBUG, "resourceType == NS_RESOURCE_MESSAGE"); NSCacheElement * element = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement)); + NS_VERIFY_NOT_NULL_V(element); NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData)); + NS_VERIFY_NOT_NULL_V(subData); OICStrcpy(subData->id, UUID_STRING_SIZE, id); - NS_LOG_V(DEBUG, "SubList ID = [%s]", subData->id); + NS_LOG_V(INFO_PRIVATE, "SubList ID = [%s]", subData->id); + + NS_LOG_V(INFO_PRIVATE, "Consumer Address: %s", entityHandlerRequest->devAddr.addr); + + subData->remote_messageObId = subData->messageObId = 0; + + bool iSRemoteServer = false; - NS_LOG_V(DEBUG, "Consumer Address: %s", entityHandlerRequest->devAddr.addr); - if(NSIsRemoteServerAddress(entityHandlerRequest->devAddr.addr)) +#if(defined WITH_CLOUD) + iSRemoteServer = NSIsRemoteServerAddress(entityHandlerRequest->devAddr.addr); + if (iSRemoteServer) { NS_LOG(DEBUG, "Requested by remote server"); subData->remote_messageObId = entityHandlerRequest->obsInfo.obsId; NS_LOG_V(DEBUG, "SubList message observation ID = [%d]", subData->remote_messageObId); } - else +#endif + + if (!iSRemoteServer) { NS_LOG(DEBUG, "Requested by local consumer"); subData->messageObId = entityHandlerRequest->obsInfo.obsId; NS_LOG_V(DEBUG, "SubList message observation ID = [%d]", subData->messageObId); } + subData->isWhite = false; + subData->remote_syncObId = 0; + subData->syncObId = 0; element->data = (void*) subData; element->next = NULL; - if (NSStorageWrite(consumerSubList, element) != NS_OK) + if (NSProviderStorageWrite(consumerSubList, element) != NS_OK) { NS_LOG(DEBUG, "fail to write cache"); } - if (NSGetSubscriptionAccepter() == NS_ACCESS_ALLOW) + bool currPolicy = NSGetPolicy(); + NSAskAcceptanceToUser(NSCopyOCEntityHandlerRequest(entityHandlerRequest)); + + if (currPolicy == NS_POLICY_PROVIDER) { NS_LOG(DEBUG, "NSGetSubscriptionAccepter == NS_ACCEPTER_PROVIDER"); - NSAskAcceptanceToUser(entityHandlerRequest); } - else if (NSGetSubscriptionAccepter() == NS_ACCESS_DENY) + else if (currPolicy == NS_POLICY_CONSUMER) { NS_LOG(DEBUG, "NSGetSubscriptionAccepter == NS_ACCEPTER_CONSUMER"); - NSSendSubscriptionResponse(entityHandlerRequest, true); + NSSendConsumerSubResponse(NSCopyOCEntityHandlerRequest(entityHandlerRequest)); } + + NSFreeOCEntityHandlerRequest(entityHandlerRequest); } else if (resourceType == NS_RESOURCE_SYNC) { NS_LOG(DEBUG, "resourceType == NS_RESOURCE_SYNC"); NSCacheElement * element = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement)); + NS_VERIFY_NOT_NULL_V(element); NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData)); + NS_VERIFY_NOT_NULL_V(subData); OICStrcpy(subData->id, UUID_STRING_SIZE, id); - NS_LOG_V(DEBUG, "SubList ID = [%s]", subData->id); + NS_LOG_V(INFO_PRIVATE, "SubList ID = [%s]", subData->id); + + NS_LOG_V(INFO_PRIVATE, "Consumer Address: %s", entityHandlerRequest->devAddr.addr); + + subData->remote_syncObId = subData->syncObId = 0; + bool isRemoteServer = false; - NS_LOG_V(DEBUG, "Consumer Address: %s", entityHandlerRequest->devAddr.addr); - if(NSIsRemoteServerAddress(entityHandlerRequest->devAddr.addr)) +#if (defined WITH_CLOUD) + isRemoteServer = NSIsRemoteServerAddress(entityHandlerRequest->devAddr.addr); + if (isRemoteServer) { NS_LOG(DEBUG, "Requested by remote server"); subData->remote_syncObId = entityHandlerRequest->obsInfo.obsId; NS_LOG_V(DEBUG, "SubList sync observation ID = [%d]", subData->remote_syncObId); } - else +#endif + + if (!isRemoteServer) { NS_LOG(DEBUG, "Requested by local consumer"); subData->syncObId = entityHandlerRequest->obsInfo.obsId; @@ -190,17 +231,20 @@ void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, NSResour } subData->isWhite = false; + subData->messageObId = 0; + subData->remote_messageObId = 0; element->data = (void*) subData; element->next = NULL; - if (NSStorageWrite(consumerSubList, element) != NS_OK) + if (NS_OK != NSProviderStorageWrite(consumerSubList, element)) { NS_LOG(ERROR, "Fail to write cache"); } NSFreeOCEntityHandlerRequest(entityHandlerRequest); } + NSOICFree(copyReq); NS_LOG(DEBUG, "NSHandleSubscription - OUT"); } @@ -209,43 +253,21 @@ void NSHandleUnsubscription(OCEntityHandlerRequest *entityHandlerRequest) { NS_LOG(DEBUG, "NSHandleUnsubscription - IN"); - char * id = NSGetValueFromQuery(OICStrdup(entityHandlerRequest->query), NS_QUERY_CONSUMER_ID); - - if(!id) - { - NSFreeOCEntityHandlerRequest(entityHandlerRequest); - NS_LOG(ERROR, "Invalid ConsumerID"); - return; - } - - NSCacheElement * element = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement)); - NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData)); - - OICStrcpy(subData->id, UUID_STRING_SIZE, id); - subData->isWhite = false; - subData->messageObId = entityHandlerRequest->obsInfo.obsId; - - element->data = (void*) subData; - element->next = NULL; + consumerSubList->cacheType = NS_PROVIDER_CACHE_SUBSCRIBER_OBSERVE_ID; - NS_LOG_V(DEBUG, "SubList IP[ID] = [%s]", subData->id); - NS_LOG_V(DEBUG, "SubList observation ID = [%d]", subData->syncObId); - - if (NSStorageWrite(consumerSubList, element) != NS_OK) - { - NS_LOG(ERROR, "fail to write consumer white list"); - } - - NS_LOG(DEBUG, "NSHandleUnsubscription - IN"); + while (NSProviderStorageDelete(consumerSubList, (char *) + &(entityHandlerRequest->obsInfo.obsId)) != NS_FAIL); + consumerSubList->cacheType = NS_PROVIDER_CACHE_SUBSCRIBER; NSFreeOCEntityHandlerRequest(entityHandlerRequest); + NS_LOG(DEBUG, "NSHandleUnsubscription - OUT"); } void NSAskAcceptanceToUser(OCEntityHandlerRequest *entityHandlerRequest) { NS_LOG(DEBUG, "NSAskAcceptanceToUser - IN"); - NSPushQueue(INTERFACE_SCHEDULER, TASK_CB_SUBSCRIPTION, entityHandlerRequest); + NSPushQueue(CALLBACK_RESPONSE_SCHEDULER, TASK_CB_SUBSCRIPTION, entityHandlerRequest); NS_LOG(DEBUG, "NSAskAcceptanceToUser - OUT"); } @@ -261,7 +283,7 @@ NSResult NSSendResponse(const char * id, bool accepted) return NS_ERROR; } - OCResourceHandle rHandle; + OCResourceHandle rHandle = NULL; if (NSPutMessageResource(NULL, &rHandle) != NS_OK) { NS_LOG(ERROR, "Fail to put notification resource"); @@ -269,79 +291,94 @@ NSResult NSSendResponse(const char * id, bool accepted) } OCRepPayloadSetUri(payload, NS_COLLECTION_MESSAGE_URI); - OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_MESSAGE_ID, 1); - OCRepPayloadSetPropBool(payload, NS_ATTRIBUTE_ACCPETANCE, accepted); + (accepted) ? OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_MESSAGE_ID, NS_ALLOW) + : OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_MESSAGE_ID, NS_DENY); OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, NSGetProviderInfo()->providerId); - NSCacheElement * element = NSStorageRead(consumerSubList, id); + NSCacheElement * element = NSProviderStorageRead(consumerSubList, id); - if(element == NULL) + if (element == NULL) { NS_LOG(ERROR, "element is NULL"); return NS_ERROR; } + NSCacheSubData * subData = (NSCacheSubData*) element->data; - if (OCNotifyListOfObservers(rHandle, (OCObservationId*)&subData->messageObId, 1, payload, OC_HIGH_QOS) - != OC_STACK_OK) + if (OCNotifyListOfObservers(rHandle, (OCObservationId*)&subData->messageObId, 1, + payload, OC_LOW_QOS) != OC_STACK_OK) { NS_LOG(ERROR, "fail to send Acceptance"); OCRepPayloadDestroy(payload); return NS_ERROR; } - OCRepPayloadDestroy(payload); + OCRepPayloadDestroy(payload); NS_LOG(DEBUG, "NSSendResponse - OUT"); return NS_OK; } -NSResult NSSendSubscriptionResponse(OCEntityHandlerRequest *entityHandlerRequest, bool accepted) +NSResult NSSendConsumerSubResponse(OCEntityHandlerRequest * entityHandlerRequest) { NS_LOG(DEBUG, "NSSendSubscriptionResponse - IN"); if (!entityHandlerRequest) { NS_LOG(ERROR, "Invalid request pointer"); - return OC_EH_ERROR; + return NS_ERROR; } - char * id = NSGetValueFromQuery(OICStrdup(entityHandlerRequest->query), NS_QUERY_CONSUMER_ID); + char * copyReq = OICStrdup(entityHandlerRequest->query); + char * id = NSGetValueFromQuery(copyReq, NS_QUERY_CONSUMER_ID); - if(!id) + if (!id) { + NSOICFree(copyReq); NSFreeOCEntityHandlerRequest(entityHandlerRequest); NS_LOG(ERROR, "Invalid ConsumerID"); return NS_ERROR; } - if (accepted) - { - NS_LOG(DEBUG, "accepted is true"); - NSCacheElement * element = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement)); - NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData)); + NSCacheUpdateSubScriptionState(consumerSubList, id, true); + NSSendResponse(id, true); + NSOICFree(copyReq); + NSFreeOCEntityHandlerRequest(entityHandlerRequest); + NS_LOG(DEBUG, "NSSendSubscriptionResponse - OUT"); + return NS_OK; +} - OICStrcpy(subData->id, UUID_STRING_SIZE, id); +#ifdef WITH_MQ +void NSProviderMQSubscription(NSMQTopicAddress * topicAddr) +{ + char * serverUri = topicAddr->serverAddr; + char * topicName = topicAddr->topicName; - subData->isWhite = true; - subData->messageObId = entityHandlerRequest->obsInfo.obsId; + NS_LOG_V(DEBUG, "input Topic Name2 : %s", topicAddr->topicName); - element->data = (void*) subData; - element->next = NULL; + OCDevAddr * addr = NSChangeAddress(serverUri); + OCCallbackData cbdata = { NULL, NULL, NULL }; + cbdata.cb = NSProviderGetMQResponseCB; + cbdata.context = OICStrdup(topicName); + cbdata.cd = NSOICFree; - if (NSStorageWrite(consumerSubList, element) != NS_OK) - { - NS_LOG(ERROR, "fail to write consumer white list"); - } - } + char requestUri[100] = "coap+tcp://"; - NSSendResponse(id, accepted); + NS_LOG_V(DEBUG, "requestUri1 = %s", requestUri); + OICStrcat(requestUri, strlen(requestUri)+strlen(serverUri)+1, serverUri); + NS_LOG_V(DEBUG, "requestUri2 = %s", requestUri); + OICStrcat(requestUri, strlen(requestUri)+ strlen("/oic/ps") + 1, "/oic/ps"); + NS_LOG_V(DEBUG, "requestUri3 = %s", requestUri); + OCStackResult ret = OCDoResource(NULL, OC_REST_GET, requestUri, addr, + NULL, CT_DEFAULT, OC_HIGH_QOS, &cbdata, NULL, 0); - NSFreeOCEntityHandlerRequest(entityHandlerRequest); + NSOCResultToSuccess(ret); - NS_LOG(DEBUG, "NSSendSubscriptionResponse - OUT"); - return NS_OK; + NSOICFree(topicAddr->serverAddr); + NSOICFree(topicAddr->topicName); + NSOICFree(topicAddr); } +#endif void * NSSubScriptionSchedule(void *ptr) { @@ -381,21 +418,21 @@ void * NSSubScriptionSchedule(void *ptr) case TASK_SEND_ALLOW: { NS_LOG(DEBUG, "CASE TASK_SEND_ALLOW : "); - NSConsumer * consumer = (NSConsumer *) node->taskData; + char * consumerId = (char *) node->taskData; - NSCacheUpdateSubScriptionState(consumerSubList, consumer->consumerId, true); - NSSendResponse(consumer->consumerId, true); - NSFreeConsumer(consumer); + NSCacheUpdateSubScriptionState(consumerSubList, consumerId, true); + NSSendResponse(consumerId, true); + NSOICFree(consumerId); break; } case TASK_SEND_DENY: { NS_LOG(DEBUG, "CASE TASK_SEND_DENY : "); - NSConsumer * consumer = (NSConsumer *) node->taskData; + char * consumerId = (char *) node->taskData; - NSCacheUpdateSubScriptionState(consumerSubList, consumer->consumerId, false); - NSSendResponse(consumer->consumerId, false); - NSFreeConsumer(consumer); + NSCacheUpdateSubScriptionState(consumerSubList, consumerId, false); + NSSendResponse(consumerId, false); + NSOICFree(consumerId); break; } @@ -404,11 +441,17 @@ void * NSSubScriptionSchedule(void *ptr) NSHandleSubscription((OCEntityHandlerRequest*) node->taskData, NS_RESOURCE_SYNC); break; +#ifdef WITH_MQ + case TASK_MQ_REQ_SUBSCRIBE: + NS_LOG(DEBUG, "CASE TASK_MQ_REQ_SUBSCRIBE : "); + NSProviderMQSubscription((NSMQTopicAddress*) node->taskData); + break; +#endif default: break; } - OICFree(node); + NSOICFree(node); } pthread_mutex_unlock(&NSMutex[SUBSCRIPTION_SCHEDULER]);