From: YounghyunJoo Date: Wed, 22 Jun 2016 10:25:13 +0000 (+0900) Subject: Update Logic to manage Provider list. X-Git-Tag: 1.2.0+RC1~52^2~139 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=798da8b7010fff84d3b9e65addcbbb18cb98a672;p=platform%2Fupstream%2Fiotivity.git Update Logic to manage Provider list. - Add storage to store Provider List & Logic to manage Provider List - Modify type of Message format in consumer to NSSyncType(delete existing type(NSConsumerMessageTypes)) - Modify way to sending syncInfo to use providerId - Modify to set providerId when send acceptance response in Provider. Change-Id: Id667403c70c88e6bce16bc92a3ec6605e36e9dc5 Signed-off-by: YounghyunJoo Reviewed-on: https://gerrit.iotivity.org/gerrit/8799 Tested-by: jenkins-iotivity Reviewed-by: Uze Choi --- diff --git a/service/notification/src/common/NSConstants.h b/service/notification/src/common/NSConstants.h index 4aef533b8..d313c0eb9 100644 --- a/service/notification/src/common/NSConstants.h +++ b/service/notification/src/common/NSConstants.h @@ -119,7 +119,8 @@ typedef enum eTaskType TASK_RECV_SYNCINFO = 5000, TASK_RECV_READ = 5001, TASK_RECV_DISMISS = 5003, - TASK_SEND_SYNCINFO = 5100, + TASK_SEND_SYNCINFO = 5099, + TASK_MAKE_SYNCINFO = 5100, TASK_SEND_READ = 5101, TASK_SEND_DISMISS = 5102, @@ -153,6 +154,7 @@ typedef enum eCacheType NS_PROVIDER_CACHE_SUBSCRIBER = 1000, NS_PROVIDER_CACHE_MESSAGE = 1001, + NS_CONSUMER_CACHE_PROVIDER = 2000, NS_CONSUMER_CACHE_MESSAGE = 2001, } NSCacheType; diff --git a/service/notification/src/consumer/NSConsumerCommon.c b/service/notification/src/consumer/NSConsumerCommon.c index 3dddedd4e..a6c3dd108 100644 --- a/service/notification/src/consumer/NSConsumerCommon.c +++ b/service/notification/src/consumer/NSConsumerCommon.c @@ -211,9 +211,10 @@ NSMessage_consumer * NSCopyMessage(NSMessage_consumer * msg) NSMessage_consumer * newMsg = (NSMessage_consumer *)OICMalloc(sizeof(NSMessage_consumer)); NS_VERIFY_NOT_NULL(newMsg, NULL); - newMsg->addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr)); + OICStrcpy(newMsg->providerId, NS_DEVICE_ID_LENGTH, msg->providerId); + newMsg->_addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr)); NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(newMsg, NULL, OICFree(newMsg)); - memcpy(newMsg->addr, msg->addr, sizeof(OCDevAddr)); + memcpy(newMsg->_addr, msg->_addr, sizeof(OCDevAddr)); newMsg->messageId = msg->messageId; newMsg->title = OICStrdup(msg->title); @@ -228,11 +229,42 @@ void NSRemoveMessage(NSMessage_consumer * msg) NSOICFree(msg->title); NSOICFree(msg->contentText); NSOICFree(msg->sourceName); - NSOICFree(msg->addr); + NSOICFree(msg->_addr); NSOICFree(msg); } +NSProvider_internal * NSCopyProvider(NSProvider_internal * prov) +{ + NS_VERIFY_NOT_NULL(prov, NULL); + + NSProvider_internal * newProv = (NSProvider_internal *)OICMalloc(sizeof(NSProvider_internal)); + NS_VERIFY_NOT_NULL(newProv, NULL); + + OICStrcpy(newProv->providerId, NS_DEVICE_ID_LENGTH, prov->providerId); + newProv->_addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr)); + NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(newProv, NULL, OICFree(newProv)); + memcpy(newProv->_addr, prov->_addr, sizeof(OCDevAddr)); + + newProv->messageUri = OICStrdup(prov->messageUri); + newProv->syncUri = OICStrdup(prov->syncUri); + newProv->messageHandle = prov->messageHandle; + newProv->syncHandle = prov->syncHandle; + newProv->accessPolicy = prov->accessPolicy; + + return newProv; +} +void NSRemoveProvider(NSProvider_internal * prov) +{ + NSOICFree(prov->messageUri); + NSOICFree(prov->syncUri); + NSOICFree(prov->messageHandle); + NSOICFree(prov->syncHandle); + NSOICFree(prov->_addr); + + NSOICFree(prov); +} + OCStackResult NSInvokeRequest(OCDoHandle * handle, OCMethod method, const OCDevAddr * addr, const char * queryUrl, OCPayload * payload, diff --git a/service/notification/src/consumer/NSConsumerCommon.h b/service/notification/src/consumer/NSConsumerCommon.h index 304ab336d..ba593becd 100644 --- a/service/notification/src/consumer/NSConsumerCommon.h +++ b/service/notification/src/consumer/NSConsumerCommon.h @@ -32,7 +32,6 @@ extern "C" { #include "NSStructs.h" #include "ocstack.h" - #define NS_QOS OC_LOW_QOS #define NS_RESOURCE_TYPE "oic.r.notification" #define NS_RESOURCE_URI "/notification" @@ -61,6 +60,17 @@ extern "C" { } \ } +#define NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(obj, func) \ + { \ + if ((obj) == NULL) \ + { \ + NS_LOG_V(ERROR, "%s : %s is null", __func__, #obj); \ + NS_LOG(ERROR, "execute deletion"); \ + (func); \ + return; \ + } \ + } + #define NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(obj, retVal, func) \ { \ if ((obj) == NULL) \ @@ -113,32 +123,34 @@ extern "C" { } \ } -typedef enum -{ - Read, - Dismiss, - Notification, -} NSConsumerMessageTypes; - typedef struct { - char providerId[37]; + char providerId[NS_DEVICE_ID_LENGTH]; char * messageUri; char * syncUri; OCDoHandle messageHandle; OCDoHandle syncHandle; - OCDevAddr * addr; + OCDevAddr * _addr; NSAccessPolicy accessPolicy; } NSProvider_internal; +typedef struct +{ + uint64_t messageId; + char providerId[NS_DEVICE_ID_LENGTH]; + NSSyncType state; + + OCDevAddr * _addr; +} NSSyncInfo_internal; + typedef struct { // Mandatory uint64_t messageId; - char providerId[37]; + char providerId[NS_DEVICE_ID_LENGTH]; //optional NSMessageType type; @@ -149,8 +161,9 @@ typedef struct char * sourceName; NSMediaContents mediaContents; - OCDevAddr * addr; - NSConsumerMessageTypes messageTypes; + OCDevAddr * _addr; + //NSConsumerMessageTypes messageTypes; + NSSyncType _messageTypes; } NSMessage_consumer; bool NSIsStartedConsumer(); @@ -180,6 +193,9 @@ NSResult NSConsumerPushEvent(NSTask *); NSMessage_consumer * NSCopyMessage(NSMessage_consumer *); void NSRemoveMessage(NSMessage_consumer *); +NSProvider_internal * NSCopyProvider(NSProvider_internal *); +void NSRemoveProvider(NSProvider_internal *); + OCStackResult NSInvokeRequest(OCDoHandle * handle, OCMethod method, const OCDevAddr * addr, const char * queryUrl, OCPayload * payload, diff --git a/service/notification/src/consumer/NSConsumerCommunication.c b/service/notification/src/consumer/NSConsumerCommunication.c index e275c91f4..20ddd67e3 100644 --- a/service/notification/src/consumer/NSConsumerCommunication.c +++ b/service/notification/src/consumer/NSConsumerCommunication.c @@ -53,7 +53,7 @@ NSResult NSConsumerSubscribeProvider(NSProvider * provider) NS_LOG(DEBUG, "subscribe message"); NS_LOG_V(DEBUG, "subscribe query : %s", query); OCStackResult ret = NSInvokeRequest(&(provider_internal->messageHandle), - OC_REST_OBSERVE, provider_internal->addr, + OC_REST_OBSERVE, provider_internal->_addr, query, NULL, NSConsumerMessageListener, NULL); NS_VERIFY_STACK_OK(ret, NS_ERROR); NSOICFree(query); @@ -65,7 +65,7 @@ NSResult NSConsumerSubscribeProvider(NSProvider * provider) NS_LOG(DEBUG, "subscribe sync"); NS_LOG_V(DEBUG, "subscribe query : %s", query); ret = NSInvokeRequest(&(provider_internal->syncHandle), - OC_REST_OBSERVE, provider_internal->addr, + OC_REST_OBSERVE, provider_internal->_addr, query, NULL, NSConsumerSyncInfoListener, NULL); NS_VERIFY_STACK_OK(ret, NS_ERROR); NSOICFree(query); @@ -183,6 +183,7 @@ NSMessage_consumer * NSGetMessage(OCClientResponse * clientResponse) NS_LOG(DEBUG, "get provider id"); char * pId = NULL; getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, &pId); + NS_LOG_V (DEBUG, "provider id: %s", pId); NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL); NS_LOG(DEBUG, "get provider address"); @@ -191,32 +192,32 @@ NSMessage_consumer * NSGetMessage(OCClientResponse * clientResponse) memcpy(addr, clientResponse->addr, sizeof(OCDevAddr)); NS_LOG(DEBUG, "create NSMessage"); - NSMessage_consumer * retNoti = NSCreateMessage_internal(id, pId); - NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(retNoti, NULL, NSGetMessagePostClean(pId, addr)); + NSMessage_consumer * retMsg = NSCreateMessage_internal(id, pId); + NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(retMsg, NULL, NSGetMessagePostClean(pId, addr)); NSOICFree(pId); - retNoti->addr = addr; - retNoti->messageTypes = Notification; + retMsg->_addr = addr; + retMsg->_messageTypes = NS_SYNC_UNREAD; NS_LOG(DEBUG, "get msg optional field"); - OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_TITLE, &retNoti->title); - OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_TEXT, &retNoti->contentText); - OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_SOURCE, &retNoti->sourceName); - - OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_TYPE, (int64_t *)&retNoti->type); - OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_DATETIME, &retNoti->dateTime); - OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_TTL, (int64_t *)&retNoti->ttl); - - NS_LOG_V(DEBUG, "Msg Address : %s", retNoti->addr->addr); - NS_LOG_V(DEBUG, "Msg ID : %ld", retNoti->messageId); - NS_LOG_V(DEBUG, "Msg Title : %s", retNoti->title); - NS_LOG_V(DEBUG, "Msg Content : %s", retNoti->contentText); - NS_LOG_V(DEBUG, "Msg Source : %s", retNoti->sourceName); - NS_LOG_V(DEBUG, "Msg Type : %d", retNoti->type); - NS_LOG_V(DEBUG, "Msg Date : %s", retNoti->dateTime); - NS_LOG_V(DEBUG, "Msg ttl : %ld", retNoti->ttl); - - return retNoti; + OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_TITLE, &retMsg->title); + OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_TEXT, &retMsg->contentText); + OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_SOURCE, &retMsg->sourceName); + + OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_TYPE, (int64_t *)&retMsg->type); + OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_DATETIME, &retMsg->dateTime); + OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_TTL, (int64_t *)&retMsg->ttl); + + NS_LOG_V(DEBUG, "Msg Address : %s", retMsg->_addr->addr); + NS_LOG_V(DEBUG, "Msg ID : %ld", retMsg->messageId); + NS_LOG_V(DEBUG, "Msg Title : %s", retMsg->title); + NS_LOG_V(DEBUG, "Msg Content : %s", retMsg->contentText); + NS_LOG_V(DEBUG, "Msg Source : %s", retMsg->sourceName); + NS_LOG_V(DEBUG, "Msg Type : %d", retMsg->type); + NS_LOG_V(DEBUG, "Msg Date : %s", retMsg->dateTime); + NS_LOG_V(DEBUG, "Msg ttl : %ld", retMsg->ttl); + + return retMsg; } NSSyncInfo * NSGetSyncInfoc(OCClientResponse * clientResponse) @@ -253,20 +254,20 @@ NSSyncInfo * NSGetSyncInfoc(OCClientResponse * clientResponse) NSMessage_consumer * NSCreateMessage_internal(uint64_t id, const char * providerId) { - NSMessage_consumer * retNoti = (NSMessage_consumer *)OICMalloc(sizeof(NSMessage_consumer)); - NS_VERIFY_NOT_NULL(retNoti, NULL); - - retNoti->messageId = id; - OICStrcpy(retNoti->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, providerId); - retNoti->title = NULL; - retNoti->contentText = NULL; - retNoti->sourceName = NULL; - retNoti->type = NS_MESSAGE_INFO; - retNoti->dateTime = NULL; - retNoti->ttl = 0; - retNoti->addr = NULL; - - return retNoti; + NSMessage_consumer * retMsg = (NSMessage_consumer *)OICMalloc(sizeof(NSMessage_consumer)); + NS_VERIFY_NOT_NULL(retMsg, NULL); + + retMsg->messageId = id; + OICStrcpy(retMsg->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, providerId); + retMsg->title = NULL; + retMsg->contentText = NULL; + retMsg->sourceName = NULL; + retMsg->type = NS_MESSAGE_INFO; + retMsg->dateTime = NULL; + retMsg->ttl = 0; + retMsg->_addr = NULL; + + return retMsg; } NSSyncInfo * NSCreateSyncInfo_consumer(uint64_t msgId, const char * providerId, NSSyncType state) @@ -281,6 +282,20 @@ NSSyncInfo * NSCreateSyncInfo_consumer(uint64_t msgId, const char * providerId, return retSync; } +OCStackResult NSSendSyncInfo(NSSyncInfo * syncInfo, OCDevAddr * addr) +{ + OCRepPayload * payload = OCRepPayloadCreate(); + NS_VERIFY_NOT_NULL(payload, OC_STACK_ERROR); + + OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t)syncInfo->messageId); + OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_STATE, syncInfo->state); + OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, syncInfo->providerId); + + return NSInvokeRequest(NULL, OC_REST_POST, addr, + NS_SYNC_URI, (OCPayload*)payload, + NSConsumerCheckPostResult, NULL); +} + void NSConsumerCommunicationTaskProcessing(NSTask * task) { NS_VERIFY_NOT_NULL_V(task); @@ -295,8 +310,13 @@ void NSConsumerCommunicationTaskProcessing(NSTask * task) else if (task->taskType == TASK_SEND_SYNCINFO) { // TODO find target OCDevAddr using provider Id. - //OCStackResult ret = NSSendSyncInfo((NSSyncInfo *)task->taskData, ); - //NS_VERTIFY_STACK_OK_V(ret); + NSSyncInfo_internal * syncInfo = (NSSyncInfo_internal *)task->taskData; + OCDevAddr * addr = syncInfo->_addr; + OCStackResult ret = NSSendSyncInfo((NSSyncInfo *)(task->taskData), addr); + NS_VERIFY_STACK_OK_V(ret); + + NSOICFree(syncInfo->_addr); + NSOICFree(syncInfo); } else if (task->taskType == TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL) { @@ -310,16 +330,3 @@ void NSConsumerCommunicationTaskProcessing(NSTask * task) NS_LOG(ERROR, "Unknown type message"); } } - -OCStackResult NSSendSyncInfo(NSSyncInfo * syncInfo, OCDevAddr * addr) -{ - OCRepPayload * payload = OCRepPayloadCreate(); - NS_VERIFY_NOT_NULL(payload, OC_STACK_ERROR); - - OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t)syncInfo->messageId); - OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_STATE, syncInfo->state); - - return NSInvokeRequest(NULL, OC_REST_POST, addr, - NS_SYNC_URI, (OCPayload*)payload, - NSConsumerCheckPostResult, NULL); -} diff --git a/service/notification/src/consumer/NSConsumerDiscovery.c b/service/notification/src/consumer/NSConsumerDiscovery.c index b4f8f6f7a..fefd63f95 100644 --- a/service/notification/src/consumer/NSConsumerDiscovery.c +++ b/service/notification/src/consumer/NSConsumerDiscovery.c @@ -117,7 +117,7 @@ void NSRemoveProviderObj(NSProvider_internal * provider) provider->messageHandle = NULL; provider->syncHandle = NULL; - NSOICFree(provider->addr); + NSOICFree(provider->_addr); NSOICFree(provider); } @@ -207,6 +207,7 @@ NSProvider_internal * NSGetProvider(OCClientResponse * clientResponse) addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr)); NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(addr, NULL, NSGetProviderPostClean(providerId, messageUri, syncUri, addr)); + memcpy(addr, clientResponse->addr, sizeof(OCDevAddr)); NSProvider_internal * newProvider @@ -218,7 +219,7 @@ NSProvider_internal * NSGetProvider(OCClientResponse * clientResponse) newProvider->messageUri = messageUri; newProvider->syncUri = syncUri; newProvider->accessPolicy = (NSAccessPolicy)accepter; - newProvider->addr = addr; + newProvider->_addr = addr; newProvider->messageHandle = NULL; newProvider->syncHandle = NULL; diff --git a/service/notification/src/consumer/NSConsumerInterface.c b/service/notification/src/consumer/NSConsumerInterface.c index e246eb5e1..b73adfb1a 100644 --- a/service/notification/src/consumer/NSConsumerInterface.c +++ b/service/notification/src/consumer/NSConsumerInterface.c @@ -92,7 +92,7 @@ NSResult NSConsumerSendSyncInfo(const char * providerId, uint64_t messageId, NSS syncInfo->messageId = messageId; syncInfo->state = type; - NSTask * syncTask = NSMakeTask(TASK_SEND_SYNCINFO, (void *) syncInfo); + NSTask * syncTask = NSMakeTask(TASK_MAKE_SYNCINFO, (void *) syncInfo); NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(syncTask, NS_ERROR, NSOICFree(syncInfo)); return NSConsumerPushEvent(syncTask); diff --git a/service/notification/src/consumer/NSConsumerInternalTaskController.c b/service/notification/src/consumer/NSConsumerInternalTaskController.c index 39bc9334f..7a9412dbc 100644 --- a/service/notification/src/consumer/NSConsumerInternalTaskController.c +++ b/service/notification/src/consumer/NSConsumerInternalTaskController.c @@ -26,45 +26,85 @@ #include "oic_malloc.h" #include "oic_string.h" -NSCacheList ** NSGetCacheList() +NSCacheList ** NSGetMessageCacheList() { - static NSCacheList * cache = NULL; - return & cache; + static NSCacheList * messageCache = NULL; + return & messageCache; } -void NSSetCacheList(NSCacheList * cache) +void NSSetMessageCacheList(NSCacheList * cache) { - *(NSGetCacheList()) = cache; + *(NSGetMessageCacheList()) = cache; } -void NSDestroyCacheList() +NSCacheList ** NSGetProviderCacheList() { - NSCacheList * cache = *(NSGetCacheList()); + static NSCacheList * providerCache = NULL; + return & providerCache; +} + +void NSSetProviderCacheList(NSCacheList * cache) +{ + *(NSGetProviderCacheList()) = cache; +} + +void NSDestroyMessageCacheList() +{ + NSCacheList * cache = *(NSGetMessageCacheList()); + if (cache) + { + NSStorageDestroy(cache); + } +} + +void NSDestroyProviderCacheList() +{ + NSCacheList * cache = *(NSGetProviderCacheList()); if (cache) { NSStorageDestroy(cache); } } -NSResult NSCacheUpdate(NSCacheList * cache, NSTask * task, NSConsumerMessageTypes type) +NSResult NSMessageCacheUpdate(NSCacheList * cache, NSMessage_consumer * msg, NSSyncType type) { - NSMessage_consumer * noti = (NSMessage_consumer *) task->taskData; - NS_VERIFY_NOT_NULL(noti, NS_ERROR); + NS_VERIFY_NOT_NULL(msg, NS_ERROR); - noti->type = type; + msg->type = type; NSCacheElement * obj = (NSCacheElement *)OICMalloc(sizeof(NSCacheElement)); NS_VERIFY_NOT_NULL(obj, NS_ERROR); - obj->data = (NSCacheData *) noti; + obj->data = (NSCacheData *) msg; obj->next = NULL; NS_LOG(DEBUG, "try to write to storage"); NSResult ret = NSStorageWrite(cache, obj); NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(ret == NS_OK ? (void *) 1 : NULL, - NS_ERROR, NSRemoveMessage(noti)); + NS_ERROR, NSRemoveMessage(msg)); - NSRemoveMessage(noti); + //NSRemoveMessage(msg); + NSOICFree(obj); + + return NS_OK; +} + +NSResult NSProviderCacheUpdate(NSCacheList * cache, NSProvider_internal * provider) +{ + NS_VERIFY_NOT_NULL(provider, NS_ERROR); + + NSCacheElement * obj = (NSCacheElement *)OICMalloc(sizeof(NSCacheElement)); + NS_VERIFY_NOT_NULL(obj, NS_ERROR); + + obj->data = (NSCacheData *) provider; + obj->next = NULL; + + NS_LOG(DEBUG, "try to write to storage"); + NSResult ret = NSStorageWrite(cache, obj); + NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(ret == NS_OK ? (void *) 1 : NULL, + NS_ERROR, NSRemoveProvider(provider)); + + //NSRemoveProvider(provider); NSOICFree(obj); return NS_OK; @@ -72,7 +112,22 @@ NSResult NSCacheUpdate(NSCacheList * cache, NSTask * task, NSConsumerMessageType void NSConsumerHandleProviderDiscovered(NSProvider_internal * provider) { - // TODO need to check for discovered provider is new and store provider. + // TODO need to check for discovered provider is new and store provider.[DONE] + NS_VERIFY_NOT_NULL_V(provider); + + NSCacheList * cache = *(NSGetProviderCacheList()); + NSCacheElement * cacheElement = NSStorageRead(cache, provider->providerId); + if (cacheElement) + { + NS_LOG_V (ERROR, "Provider is already discovered - ProviderID[%s]", provider->providerId); + return ; + } + else + { + NS_LOG (ERROR, "New provider is discovered"); + NSResult ret = NSProviderCacheUpdate(cache, provider); + NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL); + } if (provider->accessPolicy == NS_ACCESS_DENY) { @@ -93,13 +148,12 @@ void NSConsumerHandleRecvSubscriptionConfirmed(NSMessage_consumer * msg) { NS_VERIFY_NOT_NULL_V(msg); - // TODO change to find provider using pId. - NSProvider * provider = NULL; - { - provider = (NSProvider *)OICMalloc(sizeof(NSProvider)); - NS_VERIFY_NOT_NULL_V(provider); - OICStrcpy(provider->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, msg->providerId); - } + // TODO change to find provider using pId. [DONE] + NSCacheList * cache = *(NSGetProviderCacheList()); + NSCacheElement * cacheElement = NSStorageRead(cache, msg->providerId); + NS_VERIFY_NOT_NULL_V (cacheElement); + + NSProvider * provider = (NSProvider *) cacheElement->data; NSSubscriptionAccepted(provider); } @@ -107,9 +161,10 @@ void NSConsumerHandleRecvSubscriptionConfirmed(NSMessage_consumer * msg) void NSConsumerHandleRecvMessage(NSMessage_consumer * msg) { NS_VERIFY_NOT_NULL_V(msg); - // TODO store message to cache -// NSResult ret = NSCacheUpdate(cache, task, Notification); -// NS_VERTIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL); + // TODO store message to cache [DONE] + NSCacheList * cache = *(NSGetMessageCacheList()); + NSResult ret = NSMessageCacheUpdate(cache, msg, NS_SYNC_UNREAD); + NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL); NSMessagePost((NSMessage *) msg); } @@ -118,27 +173,79 @@ void NSConsumerHandleRecvSyncInfo(NSSyncInfo * sync) { NS_VERIFY_NOT_NULL_V(sync); - // TODO need to check for provider is available. - // TODO need to update msg list. -// NSResult ret = NSPushToCache(clientResponse, taskType); -// NS_VERTIFY_NOT_NULL(ret == NS_OK ? (void *)1 : NULL, OC_STACK_KEEP_TRANSACTION); + // TODO need to check for provider is available. [DONE] + NSCacheList * providerCache = *(NSGetProviderCacheList()); + NSCacheElement * providerCacheElement = NSStorageRead(providerCache, sync->providerId); + NS_VERIFY_NOT_NULL_V (providerCacheElement); + + // TODO need to update msg list. [DONE] + char msgId[NS_DEVICE_ID_LENGTH] = { 0, }; + snprintf(msgId, NS_DEVICE_ID_LENGTH, "%ld", sync->messageId); + + NSCacheList * messageCache = *(NSGetMessageCacheList()); + NSCacheElement * messageCacheElement = NSStorageRead(messageCache, msgId); + NS_VERIFY_NOT_NULL_V (messageCacheElement); + + NSMessage_consumer * msg = (NSMessage_consumer *) messageCacheElement->data; + NSResult ret = NSMessageCacheUpdate(messageCache, msg, sync->state); + NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL); NSNotificationSync(sync); } +void NSConsumerHandleMakeSyncInfo(NSSyncInfo * sync) +{ + NS_VERIFY_NOT_NULL_V(sync); + + // TODO need to check for provider is available. [DONE] + NSCacheList * providerCache = *(NSGetProviderCacheList()); + NSCacheElement * providerCacheElement = NSStorageRead(providerCache, sync->providerId); + NS_VERIFY_NOT_NULL_V (providerCacheElement); + NSProvider_internal * provider = (NSProvider_internal *) providerCacheElement->data; + NS_VERIFY_NOT_NULL_V (provider); + + NSSyncInfo_internal * syncInfo = (NSSyncInfo_internal *)OICMalloc(sizeof(NSSyncInfo_internal)); + NS_VERIFY_NOT_NULL_V(syncInfo); + + OICStrcpy(syncInfo->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, sync->providerId); + syncInfo->messageId = sync->messageId; + syncInfo->state = sync->state; + syncInfo->_addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr)); + NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(syncInfo->_addr, NSOICFree(syncInfo)); + memcpy(syncInfo->_addr, provider->_addr, sizeof(OCDevAddr)); + + NSTask * syncTask = NSMakeTask(TASK_SEND_SYNCINFO, (void *) syncInfo); + NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(syncTask, NSOICFree(syncInfo)); + + NSConsumerPushEvent(syncTask); + + NSOICFree(sync); +} + void NSConsumerInternalTaskProcessing(NSTask * task) { NS_VERIFY_NOT_NULL_V(task); - NSCacheList * cache = *(NSGetCacheList()); - if (!cache) + NSCacheList * MessageCache = *(NSGetMessageCacheList()); + if (!MessageCache) { NS_LOG(DEBUG, "Cache Init"); - cache = NSStorageCreate(); - NS_VERIFY_NOT_NULL_V(cache); + MessageCache = NSStorageCreate(); + NS_VERIFY_NOT_NULL_V(MessageCache); - cache->cacheType = NS_CONSUMER_CACHE_MESSAGE; - NSSetCacheList(cache); + MessageCache->cacheType = NS_CONSUMER_CACHE_MESSAGE; + NSSetMessageCacheList(MessageCache); + } + + NSCacheList * ProviderCache = *(NSGetProviderCacheList()); + if (!ProviderCache) + { + NS_LOG(DEBUG, "Cache Init"); + ProviderCache = NSStorageCreate(); + NS_VERIFY_NOT_NULL_V(ProviderCache); + + ProviderCache->cacheType = NS_CONSUMER_CACHE_PROVIDER; + NSSetProviderCacheList(ProviderCache); } NSResult ret = NS_ERROR; @@ -170,22 +277,28 @@ void NSConsumerInternalTaskProcessing(NSTask * task) NSConsumerHandleRecvSyncInfo((NSSyncInfo *)task->taskData); break; } - case TASK_RECV_READ: + case TASK_MAKE_SYNCINFO: { - NS_LOG(DEBUG, "Receive Read Notification"); - - ret = NSCacheUpdate(cache, task, Read); - NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL); + NS_LOG(DEBUG, "Make SyncInfo, get Provider's Addr"); + NSConsumerHandleMakeSyncInfo((NSSyncInfo *)task->taskData); break; } - case TASK_RECV_DISMISS: - { - NS_LOG(DEBUG, "Receive Dismiss Notification"); + case TASK_RECV_READ: + { + NS_LOG(DEBUG, "Receive Read Notification"); - ret = NSCacheUpdate(cache, task, Dismiss); - NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL); - break; - } + ret = NSMessageCacheUpdate(MessageCache, task, NS_SYNC_READ); + NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL); + break; + } + case TASK_RECV_DISMISS: + { + NS_LOG(DEBUG, "Receive Dismiss Notification"); + + ret = NSMessageCacheUpdate(MessageCache, task, NS_SYNC_DELETED); + NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL); + break; + } default : { NS_LOG(ERROR, "Unknown TASK Type"); diff --git a/service/notification/src/consumer/NSConsumerInternalTaskController.h b/service/notification/src/consumer/NSConsumerInternalTaskController.h index 6578b9069..04d3312b9 100644 --- a/service/notification/src/consumer/NSConsumerInternalTaskController.h +++ b/service/notification/src/consumer/NSConsumerInternalTaskController.h @@ -29,7 +29,9 @@ extern "C" { #include "NSStorageAdapter.h" #include "NSConsumerCommunication.h" -void NSDestroyCacheList(); +void NSDestroyMessageCacheList(); + +void NSDestroyProviderCacheList(); void NSConsumerInternalTaskProcessing(NSTask *); diff --git a/service/notification/src/consumer/NSConsumerQueueScheduler.c b/service/notification/src/consumer/NSConsumerQueueScheduler.c index 5d7d0c0a9..160c68885 100644 --- a/service/notification/src/consumer/NSConsumerQueueScheduler.c +++ b/service/notification/src/consumer/NSConsumerQueueScheduler.c @@ -111,7 +111,7 @@ NSResult NSConsumerPushEvent(NSTask * task) void NSConsumerMessageHandlerExit() { - NSDestroyCacheList(); + NSDestroyMessageCacheList(); NSConsumerListenerTermiate(); NSThreadStop(*(NSGetMsgHandleThreadHandle())); NSDestroyQueue(*(NSGetMsgHandleQueue())); @@ -222,6 +222,7 @@ void NSConsumerTaskProcessing(NSTask * task) case TASK_CONSUMER_RECV_MESSAGE: case TASK_CONSUMER_PROVIDER_DISCOVERED: case TASK_CONSUMER_RECV_SUBSCRIBE_CONFIRMED: + case TASK_MAKE_SYNCINFO: { NSConsumerInternalTaskProcessing(task); break; diff --git a/service/notification/src/consumer/NSThread.c b/service/notification/src/consumer/NSThread.c index 063b03262..9dbf64bb8 100644 --- a/service/notification/src/consumer/NSThread.c +++ b/service/notification/src/consumer/NSThread.c @@ -82,10 +82,10 @@ void NSThreadStop(NSConsumerThread * handle) void NSThreadJoin(NSConsumerThread * handle) { - if (handle->thread_id) - { - pthread_join(handle->thread_id, NULL); - } + if (handle->thread_id) + { + pthread_join(handle->thread_id, NULL); + } } void NSDestroyThreadHandle(NSConsumerThread * handle) diff --git a/service/notification/src/consumer/cache/linux/NSConsumerMemoryCache.c b/service/notification/src/consumer/cache/linux/NSConsumerMemoryCache.c index cb6a198dd..e85f7a11c 100644 --- a/service/notification/src/consumer/cache/linux/NSConsumerMemoryCache.c +++ b/service/notification/src/consumer/cache/linux/NSConsumerMemoryCache.c @@ -35,12 +35,11 @@ void NSSetCacheMutex(pthread_mutex_t mutex) NSCacheList * NSStorageCreate() { - pthread_mutex_t * mutex = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t)); + pthread_mutex_t * mutex = (pthread_mutex_t *) OICMalloc(sizeof(pthread_mutex_t)); pthread_mutex_init(mutex, NULL); NSSetCacheMutex(*mutex); mutex = NSGetCacheMutex(); - pthread_mutex_lock(mutex); NSCacheList * newList = (NSCacheList *) OICMalloc(sizeof(NSCacheList)); @@ -110,6 +109,12 @@ NSResult NSStorageWrite(NSCacheList * list, NSCacheElement * newObj) return NSConsumerCacheWriteMessage(list, newObj); } + else if (type == NS_CONSUMER_CACHE_PROVIDER) + { + pthread_mutex_unlock(mutex); + + return NSConsumerCacheWriteProvider(list, newObj); + } NS_LOG (ERROR, "Not Supported Type"); pthread_mutex_unlock(mutex); @@ -123,6 +128,8 @@ NSResult NSStorageDelete(NSCacheList * list, const char * delId) pthread_mutex_lock(mutex); + NSCacheType type = list->cacheType; + if (!delId) { pthread_mutex_unlock(mutex); @@ -133,8 +140,6 @@ NSResult NSStorageDelete(NSCacheList * list, const char * delId) NSCacheElement * prev = list->head; NSCacheElement * del = list->head; - NSCacheType type = list->cacheType; - if (NSConsumerCompareIdCacheData(type, del->data, delId)) { if (del == list->head) @@ -143,8 +148,15 @@ NSResult NSStorageDelete(NSCacheList * list, const char * delId) list->tail = del->next; list->head = del->next; - NSRemoveMessage((NSMessage_consumer *) del->data); - OICFree(del); + if (type == NS_CONSUMER_CACHE_MESSAGE) + { + NSRemoveMessage((NSMessage_consumer *) del->data); + } + else if (type == NS_CONSUMER_CACHE_PROVIDER) + { + NSRemoveProvider((NSProvider_internal *) del->data); + } + NSOICFree(del); pthread_mutex_unlock(mutex); return NS_OK; @@ -160,8 +172,15 @@ NSResult NSStorageDelete(NSCacheList * list, const char * delId) list->tail = prev; prev->next = del->next; - NSRemoveMessage((NSMessage_consumer *) del->data); - OICFree(del); + if (type == NS_CONSUMER_CACHE_MESSAGE) + { + NSRemoveMessage((NSMessage_consumer *) del->data); + } + else if (type == NS_CONSUMER_CACHE_PROVIDER) + { + NSRemoveProvider((NSProvider_internal *) del->data); + } + NSOICFree(del); pthread_mutex_unlock(mutex); return NS_OK; @@ -190,8 +209,8 @@ NSResult NSConsumerCacheWriteMessage(NSCacheList * list, NSCacheElement * newObj NSMessage_consumer * newMsgObj = (NSMessage_consumer *) newObj->data; pthread_mutex_unlock(mutex); - char msgId[] = {0, }; - sprintf(msgId, "%ld", newMsgObj->messageId); + char msgId[NS_DEVICE_ID_LENGTH] = {0, }; + snprintf(msgId, NS_DEVICE_ID_LENGTH, "%ld", newMsgObj->messageId); NSCacheElement * it = NSStorageRead(list, msgId); pthread_mutex_lock(mutex); @@ -247,6 +266,99 @@ NSResult NSConsumerCacheWriteMessage(NSCacheList * list, NSCacheElement * newObj return NS_OK; } +NSResult NSConsumerCacheWriteProvider(NSCacheList * list, NSCacheElement * newObj) +{ + pthread_mutex_t * mutex = NSGetCacheMutex(); + + pthread_mutex_lock(mutex); + + NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1"); + NSProvider_internal * prov = (NSProvider_internal *)newObj->data; + NS_LOG_V (DEBUG, "%s", prov->providerId); + NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1"); + + if (!newObj) + { + pthread_mutex_unlock(mutex); + NS_LOG (ERROR, "Failed to Write Provider Cache"); + return NS_ERROR; + } + + NSProvider_internal * newProvObj = (NSProvider_internal *) newObj->data; + + pthread_mutex_unlock(mutex); + NSCacheElement * it = NSStorageRead(list, newProvObj->providerId); + pthread_mutex_lock(mutex); + + if (it) + { + NSProvider_internal * provObj = (NSProvider_internal *) it->data; + it->data = (void *) NSCopyProvider(newProvObj); + if (!it->data) + { + NS_LOG (ERROR, "Failed to CopyProvider"); + it->data = (void *) provObj; + pthread_mutex_unlock(mutex); + + return NS_ERROR; + } + NSRemoveProvider(provObj); + pthread_mutex_unlock(mutex); + + return NS_OK; + } + + NSCacheElement * obj = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement)); + if (!obj) + { + NS_LOG(ERROR, "Fail to Create New Object"); + pthread_mutex_unlock(mutex); + + return NS_ERROR; + } + obj->data = (void *) NSCopyProvider(newProvObj); + + NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!2"); + prov = (NSProvider_internal *)obj->data; + NS_LOG_V (DEBUG, "%s", prov->providerId); + NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!2"); + + if (!obj->data) + { + NS_LOG (ERROR, "Failed to CopyProvider"); + pthread_mutex_unlock(mutex); + + return NS_ERROR; + } + obj->next = NULL; + + if (!list->head) + { + list->head = obj; + list->tail = obj; + pthread_mutex_unlock(mutex); + + NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!3"); + prov = (NSProvider_internal *)list->tail->data; + NS_LOG_V (DEBUG, "%s", prov->providerId); + NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!3"); + + return NS_OK; + } + + (list->tail)->next = obj; + list->tail = obj; + + NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!4"); + prov = (NSProvider_internal *)list->tail->data; + NS_LOG_V (DEBUG, "%s", prov->providerId); + NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!4"); + + pthread_mutex_unlock(mutex); + + return NS_OK; +} + NSResult NSStorageDestroy(NSCacheList * list) { pthread_mutex_t * mutex = NSGetCacheMutex(); @@ -260,18 +372,31 @@ NSResult NSStorageDestroy(NSCacheList * list) if (type == NS_CONSUMER_CACHE_MESSAGE) { - while (iter) { next = (NSCacheElement *) iter->next; NSRemoveMessage((NSMessage_consumer *) iter->data); - OICFree(iter); + NSOICFree(iter); iter = next; } - OICFree(list); + NSOICFree(list); + } + else if (type == NS_CONSUMER_CACHE_PROVIDER) + { + while (iter) + { + next = (NSCacheElement *) iter->next; + + NSRemoveProvider((NSProvider_internal *) iter->data); + NSOICFree(iter); + + iter = next; + } + + NSOICFree(list); } pthread_mutex_unlock(mutex); @@ -290,8 +415,8 @@ bool NSConsumerCompareIdCacheData(NSCacheType type, void * data, const char * id { NSMessage_consumer * msg = (NSMessage_consumer *) data; - char msgId[] = {0, }; - sprintf(msgId, "%ld", msg->messageId); + char msgId[NS_DEVICE_ID_LENGTH] = {0, }; + snprintf(msgId, NS_DEVICE_ID_LENGTH, "%ld", msg->messageId); if (!strcmp(msgId, id)) { return true; @@ -299,6 +424,17 @@ bool NSConsumerCompareIdCacheData(NSCacheType type, void * data, const char * id return false; } + else if (type == NS_CONSUMER_CACHE_PROVIDER) + { + NSProvider_internal * prov = (NSProvider_internal *) data; + + if (!strcmp(prov->providerId, id)) + { + return true; + } + + return false; + } return false; } diff --git a/service/notification/src/consumer/cache/linux/NSConsumerMemoryCache.h b/service/notification/src/consumer/cache/linux/NSConsumerMemoryCache.h index 19c3f11b6..9cc47f383 100644 --- a/service/notification/src/consumer/cache/linux/NSConsumerMemoryCache.h +++ b/service/notification/src/consumer/cache/linux/NSConsumerMemoryCache.h @@ -34,6 +34,7 @@ extern "C" { bool NSConsumerCompareIdCacheData(NSCacheType type, void * data, const char * id); NSResult NSConsumerCacheWriteMessage(NSCacheList * list, NSCacheElement * newObj); +NSResult NSConsumerCacheWriteProvider(NSCacheList * list, NSCacheElement * newObj); #ifdef __cplusplus } diff --git a/service/notification/src/provider/NSProviderSubscription.c b/service/notification/src/provider/NSProviderSubscription.c index 35175e726..461069882 100644 --- a/service/notification/src/provider/NSProviderSubscription.c +++ b/service/notification/src/provider/NSProviderSubscription.c @@ -249,6 +249,7 @@ 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); + OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, NSGetProviderInfo()->providerId); NSCacheElement * element = NSStorageRead(consumerSubList, id); diff --git a/service/notification/src/provider/NSProviderSubscription.h b/service/notification/src/provider/NSProviderSubscription.h index a26f51a2e..e6d3a25f4 100644 --- a/service/notification/src/provider/NSProviderSubscription.h +++ b/service/notification/src/provider/NSProviderSubscription.h @@ -29,6 +29,7 @@ #include "NSProviderScheduler.h" #include "NSProviderResource.h" #include "NSProviderMemoryCache.h" +#include "NSProviderSystem.h" #include "oic_string.h" #include "oic_malloc.h"