From: KIM JungYong Date: Mon, 25 Jul 2016 10:04:58 +0000 (+0900) Subject: Removed NSMessage_consumer structure. X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3b4b3385264216f3c189fabe1ea2132dcec7fbf0;p=contrib%2Fiotivity.git Removed NSMessage_consumer structure. NSMessage_consumer sturcture is for direct request to provider, It is designed for excepted storage module. But, consumer service should set a storage, so, NSMessage_consumer information no need more. Finally, NSMessage_consumer is deleted and internal logic is modified. NSMessage_consumer is deleted. NSStoreMessage is added for storage. bug fix for copy NSMessage structure. Null pointer checking logic is added. Change-Id: I2eb9f9ed3e9921e978e85a299f342eac240635f0 Signed-off-by: KIM JungYong Reviewed-on: https://gerrit.iotivity.org/gerrit/9683 Tested-by: jenkins-iotivity Reviewed-by: Hun-je Yeon Reviewed-by: Uze Choi --- diff --git a/service/notification/src/consumer/NSConsumerCommon.c b/service/notification/src/consumer/NSConsumerCommon.c index 2004680..173f085 100644 --- a/service/notification/src/consumer/NSConsumerCommon.c +++ b/service/notification/src/consumer/NSConsumerCommon.c @@ -214,7 +214,7 @@ void NSMessagePost(NSMessage * msg) { NS_VERIFY_NOT_NULL_V(msg); - NSMessage * retMsg = (NSMessage *)NSCopyMessage((NSMessage_consumer *)msg); + NSMessage * retMsg = NSCopyMessage(msg); NS_VERIFY_NOT_NULL_V(retMsg); NSConsumerThread * thread = NSThreadInit(NSMessagePostFunc, (void *) retMsg); @@ -233,11 +233,11 @@ NSTask * NSMakeTask(NSTaskType type, void * data) return retTask; } -NSMessage_consumer * NSCopyMessage(NSMessage_consumer * msg) +NSMessage * NSCopyMessage(NSMessage * msg) { NS_VERIFY_NOT_NULL(msg, NULL); - NSMessage_consumer * newMsg = (NSMessage_consumer *)OICMalloc(sizeof(NSMessage_consumer)); + NSMessage * newMsg = (NSMessage *)OICMalloc(sizeof(NSMessage)); NS_VERIFY_NOT_NULL(newMsg, NULL); OICStrcpy(newMsg->providerId, NS_DEVICE_ID_LENGTH, msg->providerId); @@ -246,11 +246,15 @@ NSMessage_consumer * NSCopyMessage(NSMessage_consumer * msg) newMsg->title = OICStrdup(msg->title); newMsg->contentText = OICStrdup(msg->contentText); newMsg->sourceName = OICStrdup(msg->sourceName); + newMsg->dateTime = OICStrdup(msg->dateTime); newMsg->type = msg->type; + // TODO change to copy function. + newMsg->mediaContents = msg->mediaContents; + return newMsg; } -void NSRemoveMessage(NSMessage_consumer * msg) +void NSRemoveMessage(NSMessage * msg) { NS_VERIFY_NOT_NULL_V(msg); diff --git a/service/notification/src/consumer/NSConsumerCommon.h b/service/notification/src/consumer/NSConsumerCommon.h index d1e9272..09f1148 100644 --- a/service/notification/src/consumer/NSConsumerCommon.h +++ b/service/notification/src/consumer/NSConsumerCommon.h @@ -170,20 +170,10 @@ typedef struct typedef struct { - // Mandatory - uint64_t messageId; - char providerId[NS_DEVICE_ID_LENGTH]; - - //optional - NSMessageType type; - char * dateTime; - uint64_t ttl; - char * title; - char * contentText; - char * sourceName; - NSMediaContents * mediaContents; + NSSyncType status; + NSMessage * msg; -} NSMessage_consumer; +} NSStoreMessage; bool NSIsStartedConsumer(); void NSSetIsStartedConsumer(bool setValue); @@ -209,8 +199,8 @@ NSTask * NSMakeTask(NSTaskType, void *); NSResult NSConsumerPushEvent(NSTask *); -NSMessage_consumer * NSCopyMessage(NSMessage_consumer *); -void NSRemoveMessage(NSMessage_consumer *); +NSMessage * NSCopyMessage(NSMessage *); +void NSRemoveMessage(NSMessage *); NSProviderConnectionInfo * NSCreateProviderConnections(OCDevAddr * inAddr); NSProviderConnectionInfo * NSCopyProviderConnections(NSProviderConnectionInfo * conn); diff --git a/service/notification/src/consumer/NSConsumerCommunication.c b/service/notification/src/consumer/NSConsumerCommunication.c index 249828a..5ea2921 100644 --- a/service/notification/src/consumer/NSConsumerCommunication.c +++ b/service/notification/src/consumer/NSConsumerCommunication.c @@ -29,10 +29,10 @@ unsigned long NS_MESSAGE_ACCEPTANCE = 1; -NSMessage_consumer * NSCreateMessage_internal(uint64_t msgId, const char * providerId); +NSMessage * NSCreateMessage_internal(uint64_t msgId, const char * providerId); NSSyncInfo * NSCreateSyncInfo_consumer(uint64_t msgId, const char * providerId, NSSyncType state); -NSMessage_consumer * NSGetMessage(OCClientResponse * clientResponse); +NSMessage * NSGetMessage(OCClientResponse * clientResponse); NSSyncInfo * NSGetSyncInfoc(OCClientResponse * clientResponse); char * NSGetCloudUri(const char * providerId, char * uri); @@ -158,7 +158,7 @@ OCStackApplicationResult NSConsumerMessageListener( NS_VERIFY_STACK_SUCCESS(NSOCResultToSuccess(clientResponse->result), OC_STACK_KEEP_TRANSACTION); NS_LOG(DEBUG, "build NSMessage"); - NSMessage_consumer * newNoti = NSGetMessage(clientResponse); + NSMessage * newNoti = NSGetMessage(clientResponse); NS_VERIFY_NOT_NULL(newNoti, OC_STACK_KEEP_TRANSACTION); NSTaskType type = TASK_CONSUMER_RECV_MESSAGE; @@ -188,7 +188,7 @@ void NSGetMessagePostClean(char * pId, OCDevAddr * addr) NSOICFree(addr); } -NSMessage_consumer * NSGetMessage(OCClientResponse * clientResponse) +NSMessage * NSGetMessage(OCClientResponse * clientResponse) { NS_VERIFY_NOT_NULL(clientResponse->payload, NULL); OCRepPayload * payload = (OCRepPayload *)clientResponse->payload; @@ -205,7 +205,7 @@ NSMessage_consumer * NSGetMessage(OCClientResponse * clientResponse) NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL); NS_LOG(DEBUG, "create NSMessage"); - NSMessage_consumer * retMsg = NSCreateMessage_internal(id, pId); + NSMessage * retMsg = NSCreateMessage_internal(id, pId); NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(retMsg, NULL, NSOICFree(pId)); NSOICFree(pId); @@ -261,9 +261,9 @@ NSSyncInfo * NSGetSyncInfoc(OCClientResponse * clientResponse) return retSync; } -NSMessage_consumer * NSCreateMessage_internal(uint64_t id, const char * providerId) +NSMessage * NSCreateMessage_internal(uint64_t id, const char * providerId) { - NSMessage_consumer * retMsg = (NSMessage_consumer *)OICMalloc(sizeof(NSMessage_consumer)); + NSMessage * retMsg = (NSMessage *)OICMalloc(sizeof(NSMessage)); NS_VERIFY_NOT_NULL(retMsg, NULL); retMsg->messageId = id; @@ -281,6 +281,8 @@ NSMessage_consumer * NSCreateMessage_internal(uint64_t id, const char * provider NSSyncInfo * NSCreateSyncInfo_consumer(uint64_t msgId, const char * providerId, NSSyncType state) { + NS_VERIFY_NOT_NULL(providerId, NULL); + NSSyncInfo * retSync = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo)); NS_VERIFY_NOT_NULL(retSync, NULL); @@ -293,6 +295,9 @@ NSSyncInfo * NSCreateSyncInfo_consumer(uint64_t msgId, const char * providerId, OCStackResult NSSendSyncInfo(NSSyncInfo * syncInfo, OCDevAddr * addr) { + NS_VERIFY_NOT_NULL(syncInfo, OC_STACK_ERROR); + NS_VERIFY_NOT_NULL(addr, OC_STACK_ERROR); + OCRepPayload * payload = OCRepPayloadCreate(); NS_VERIFY_NOT_NULL(payload, OC_STACK_ERROR); @@ -301,11 +306,14 @@ OCStackResult NSSendSyncInfo(NSSyncInfo * syncInfo, OCDevAddr * addr) OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, syncInfo->providerId); char * uri = (char*)OICStrdup(NS_SYNC_URI); + NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(uri, OC_STACK_ERROR, OCRepPayloadDestroy(payload)); + OCConnectivityType type = CT_DEFAULT; if(addr->adapter == OC_ADAPTER_TCP) { type = CT_ADAPTER_TCP; uri = NSGetCloudUri(syncInfo->providerId, uri); + NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(uri, OC_STACK_ERROR, OCRepPayloadDestroy(payload)); } OCStackResult ret = NSInvokeRequest(NULL, OC_REST_POST, addr, @@ -320,9 +328,11 @@ char * NSGetCloudUri(const char * providerId, char * uri) { size_t uriLen = NS_DEVICE_ID_LENGTH + 1 + strlen(uri) + 1; char * retUri = (char *)OICMalloc(uriLen); + NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(retUri, NULL, NSOICFree(uri)); + snprintf(retUri, uriLen, "/%s%s", providerId, uri); NSOICFree(uri); - NS_LOG_V(DEBUG, "TCP uri : %s", retUri); + NS_LOG_V(DEBUG, "Cloud uri : %s", retUri); return retUri; } diff --git a/service/notification/src/consumer/NSConsumerDiscovery.c b/service/notification/src/consumer/NSConsumerDiscovery.c index 89b894a..fe84ff0 100644 --- a/service/notification/src/consumer/NSConsumerDiscovery.c +++ b/service/notification/src/consumer/NSConsumerDiscovery.c @@ -29,7 +29,6 @@ #include "oic_string.h" #define NS_DISCOVER_QUERY "/oic/res?rt=oic.r.notification" -#define NS_PRESENCE_SUBSCRIBE_QUERY "coap://224.0.1.187:5683/oic/ad?rt=oic.r.notification" #define NS_PRESENCE_SUBSCRIBE_QUERY_TCP "/oic/ad?rt=oic.r.notification" #define NS_GET_INFORMATION_QUERY "/notification?if=oic.if.notification" diff --git a/service/notification/src/consumer/NSConsumerInternalTaskController.c b/service/notification/src/consumer/NSConsumerInternalTaskController.c index c73178e..af433b3 100644 --- a/service/notification/src/consumer/NSConsumerInternalTaskController.c +++ b/service/notification/src/consumer/NSConsumerInternalTaskController.c @@ -70,7 +70,7 @@ void NSDestroyProviderCacheList() NSSetProviderCacheList(NULL); } -NSMessage_consumer * NSMessageCacheFind(const char * messageId) +NSMessage * NSMessageCacheFind(const char * messageId) { NS_VERIFY_NOT_NULL(messageId, NULL); @@ -88,7 +88,7 @@ NSMessage_consumer * NSMessageCacheFind(const char * messageId) NSCacheElement * cacheElement = NSStorageRead(MessageCache, messageId); NS_VERIFY_NOT_NULL(cacheElement, NULL); - return NSCopyMessage((NSMessage_consumer *) cacheElement->data); + return NSCopyMessage(((NSStoreMessage *) cacheElement->data)->msg); } NSProvider_internal * NSProviderCacheFind(const char * providerId) @@ -112,9 +112,17 @@ NSProvider_internal * NSProviderCacheFind(const char * providerId) return NSCopyProvider((NSProvider_internal *) cacheElement->data); } +void NSRemoveCacheElementMessage(NSCacheElement * obj) +{ + NSRemoveMessage(((NSStoreMessage *)obj->data)->msg); + NSOICFree(obj->data); + NSOICFree(obj); +} -NSResult NSMessageCacheUpdate(NSMessage_consumer * msg, NSSyncType type) +NSResult NSMessageCacheUpdate(NSMessage * msg, NSSyncType type) { + NS_VERIFY_NOT_NULL(msg, NS_ERROR); + NSCacheList * MessageCache = *(NSGetMessageCacheList()); if (!MessageCache) { @@ -126,21 +134,26 @@ NSResult NSMessageCacheUpdate(NSMessage_consumer * msg, NSSyncType type) NSSetMessageCacheList(MessageCache); } - NS_VERIFY_NOT_NULL(msg, NS_ERROR); - - msg->type = type; + NSStoreMessage * sMsg = (NSStoreMessage *)OICMalloc(sizeof(NSStoreMessage)); + NS_VERIFY_NOT_NULL(sMsg, NS_ERROR); NSCacheElement * obj = (NSCacheElement *)OICMalloc(sizeof(NSCacheElement)); - NS_VERIFY_NOT_NULL(obj, NS_ERROR); + NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(obj, NS_ERROR, NSOICFree(sMsg)); + + sMsg->status = type; + sMsg->msg = NSCopyMessage(msg); - obj->data = (NSCacheData *) msg; + obj->data = (NSCacheData *) sMsg; obj->next = NULL; NS_LOG(DEBUG, "try to write to storage"); NSResult ret = NSStorageWrite(MessageCache, obj); NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(ret == NS_OK ? (void *) 1 : NULL, - NS_ERROR, NSOICFree(obj)); + NS_ERROR, NSRemoveCacheElementMessage(obj)); + + NSRemoveCacheElementMessage(obj); + NS_LOG(DEBUG, "Update message done"); return NS_OK; } @@ -270,7 +283,7 @@ void NSConsumerHandleProviderDeleted(NSProvider_internal * provider) NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *)1 : NULL); } -void NSConsumerHandleRecvSubscriptionConfirmed(NSMessage_consumer * msg) +void NSConsumerHandleRecvSubscriptionConfirmed(NSMessage * msg) { NS_VERIFY_NOT_NULL_V(msg); @@ -284,7 +297,7 @@ void NSConsumerHandleRecvSubscriptionConfirmed(NSMessage_consumer * msg) } } -void NSConsumerHandleRecvMessage(NSMessage_consumer * msg) +void NSConsumerHandleRecvMessage(NSMessage * msg) { NS_VERIFY_NOT_NULL_V(msg); @@ -301,7 +314,7 @@ void NSConsumerHandleRecvSyncInfo(NSSyncInfo * sync) char msgId[NS_DEVICE_ID_LENGTH] = { 0, }; snprintf(msgId, NS_DEVICE_ID_LENGTH, "%lld", (long long int)sync->messageId); - NSMessage_consumer * msg = NSMessageCacheFind(msgId); + NSMessage * msg = NSMessageCacheFind(msgId); NS_VERIFY_NOT_NULL_V(msg); NSResult ret = NSMessageCacheUpdate(msg, sync->state); @@ -345,15 +358,15 @@ void NSConsumerInternalTaskProcessing(NSTask * task) case TASK_CONSUMER_RECV_SUBSCRIBE_CONFIRMED: { NS_LOG(DEBUG, "Receive Subscribe confirm from provider."); - NSConsumerHandleRecvSubscriptionConfirmed((NSMessage_consumer *)task->taskData); - NSRemoveMessage((NSMessage_consumer *)task->taskData); + NSConsumerHandleRecvSubscriptionConfirmed((NSMessage *)task->taskData); + NSRemoveMessage((NSMessage *)task->taskData); break; } case TASK_CONSUMER_RECV_MESSAGE: { NS_LOG(DEBUG, "Receive New Notification"); - NSConsumerHandleRecvMessage((NSMessage_consumer *)task->taskData); - NSRemoveMessage((NSMessage_consumer *)task->taskData); + NSConsumerHandleRecvMessage((NSMessage *)task->taskData); + NSRemoveMessage((NSMessage *)task->taskData); break; } case TASK_CONSUMER_PROVIDER_DISCOVERED: diff --git a/service/notification/src/consumer/NSConsumerInternalTaskController.h b/service/notification/src/consumer/NSConsumerInternalTaskController.h index 36b56bc..de3394d 100644 --- a/service/notification/src/consumer/NSConsumerInternalTaskController.h +++ b/service/notification/src/consumer/NSConsumerInternalTaskController.h @@ -44,7 +44,7 @@ void NSDestroyMessageCacheList(); void NSDestroyProviderCacheList(); -NSMessage_consumer * NSMessageCacheFind(const char *); +NSMessage * NSMessageCacheFind(const char *); NSProvider_internal * NSProviderCacheFind(const char *); diff --git a/service/notification/src/consumer/NSConsumerQueue.c b/service/notification/src/consumer/NSConsumerQueue.c index 7db9a32..1294079 100644 --- a/service/notification/src/consumer/NSConsumerQueue.c +++ b/service/notification/src/consumer/NSConsumerQueue.c @@ -43,9 +43,11 @@ void NSDestroyQueue(NSConsumerQueue * queue) NSConsumerQueueObject * node = NSPopQueue(queue); while(node) { - node = (NSConsumerQueueObject *)node->next; + NSConsumerQueueObject * next = (NSConsumerQueueObject *)node->next; NSOICFree(node->data); NSOICFree(node); + + node = next; } NSOICFree(queue); diff --git a/service/notification/src/consumer/NSConsumerScheduler.c b/service/notification/src/consumer/NSConsumerScheduler.c index 1d911fa..2ab4c9b 100644 --- a/service/notification/src/consumer/NSConsumerScheduler.c +++ b/service/notification/src/consumer/NSConsumerScheduler.c @@ -246,7 +246,7 @@ void NSConsumerTaskProcessing(NSTask * task) } } -NSMessage_consumer * NSConsumerFindNSMessage(const char* messageId) +NSMessage * NSConsumerFindNSMessage(const char* messageId) { NS_VERIFY_NOT_NULL(messageId, NULL); diff --git a/service/notification/src/consumer/NSConsumerScheduler.h b/service/notification/src/consumer/NSConsumerScheduler.h index af20bd2..2217e5b 100644 --- a/service/notification/src/consumer/NSConsumerScheduler.h +++ b/service/notification/src/consumer/NSConsumerScheduler.h @@ -39,7 +39,7 @@ void NSConsumerMessageHandlerExit(); extern NSResult NSConsumerPushEvent(NSTask *); -NSMessage_consumer * NSConsumerFindNSMessage(const char *); +NSMessage * NSConsumerFindNSMessage(const char *); NSProvider_internal * NSConsumerFindNSProvider(const char *); diff --git a/service/notification/src/consumer/cache/linux/NSConsumerMemoryCache.c b/service/notification/src/consumer/cache/linux/NSConsumerMemoryCache.c index fb4a8e8..e217b07 100644 --- a/service/notification/src/consumer/cache/linux/NSConsumerMemoryCache.c +++ b/service/notification/src/consumer/cache/linux/NSConsumerMemoryCache.c @@ -36,21 +36,18 @@ void NSSetCacheMutex(pthread_mutex_t mutex) NSCacheList * NSStorageCreate() { pthread_mutex_t * mutex = (pthread_mutex_t *) OICMalloc(sizeof(pthread_mutex_t)); + NS_VERIFY_NOT_NULL(mutex, NULL); + pthread_mutex_init(mutex, NULL); NSSetCacheMutex(*mutex); - mutex = NSGetCacheMutex(); pthread_mutex_lock(mutex); NSCacheList * newList = (NSCacheList *) OICMalloc(sizeof(NSCacheList)); - if (!newList) - { - pthread_mutex_unlock(mutex); - NS_LOG (ERROR, "Failed to Create Cache"); - return NULL; - } + NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(newList, NULL, pthread_mutex_unlock(mutex)); - newList->head = newList->tail = NULL; + newList->head = NULL; + newList->tail = NULL; pthread_mutex_unlock(mutex); @@ -59,88 +56,66 @@ NSCacheList * NSStorageCreate() NSCacheElement * NSStorageRead(NSCacheList * list, const char * findId) { - pthread_mutex_t * mutex = NSGetCacheMutex(); + NS_VERIFY_NOT_NULL(list, NULL); + NS_VERIFY_NOT_NULL(findId, NULL); + pthread_mutex_t * mutex = NSGetCacheMutex(); pthread_mutex_lock(mutex); NSCacheElement * iter = list->head; - NSCacheElement * next = NULL; NSCacheType type = list->cacheType; while (iter) { - next = iter->next; - - pthread_mutex_unlock(mutex); - if (NSConsumerCompareIdCacheData(type, iter->data, findId)) { pthread_mutex_unlock(mutex); - return iter; } - iter = next; + iter = iter->next; } - pthread_mutex_unlock(mutex); NS_LOG (DEBUG, "No Cache Element"); + pthread_mutex_unlock(mutex); return NULL; } NSResult NSStorageWrite(NSCacheList * list, NSCacheElement * newObj) { - pthread_mutex_t * mutex = NSGetCacheMutex(); - - pthread_mutex_lock(mutex); + NS_VERIFY_NOT_NULL(list, NS_ERROR); + NS_VERIFY_NOT_NULL(newObj, NS_ERROR); NSCacheType type = list->cacheType; - - if (!newObj) - { - pthread_mutex_unlock(mutex); - NS_LOG (ERROR, "Failed to Write Cache"); - return NS_ERROR; - } - NS_LOG_V(DEBUG, "cache type : %d", type); + if (type == NS_CONSUMER_CACHE_MESSAGE) { - pthread_mutex_unlock(mutex); - 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); return NS_ERROR; } NSResult NSStorageDelete(NSCacheList * list, const char * delId) { - pthread_mutex_t * mutex = NSGetCacheMutex(); - - pthread_mutex_lock(mutex); + NS_VERIFY_NOT_NULL(list, NS_ERROR); + NS_VERIFY_NOT_NULL(delId, NS_ERROR); NSCacheType type = list->cacheType; - if (!delId) - { - pthread_mutex_unlock(mutex); - NS_LOG (ERROR, "Failed to Delete Cache"); - return NS_ERROR; - } + pthread_mutex_t * mutex = NSGetCacheMutex(); + pthread_mutex_lock(mutex); NSCacheElement * prev = list->head; NSCacheElement * del = list->head; - NS_VERIFY_NOT_NULL(del, NS_ERROR); + NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(del, NS_ERROR, pthread_mutex_unlock(mutex)); if (NSConsumerCompareIdCacheData(type, del->data, delId)) { @@ -152,7 +127,7 @@ NSResult NSStorageDelete(NSCacheList * list, const char * delId) if (type == NS_CONSUMER_CACHE_MESSAGE) { - NSRemoveMessage((NSMessage_consumer *) del->data); + NSRemoveMessage((NSMessage *) del->data); } else if (type == NS_CONSUMER_CACHE_PROVIDER) { @@ -176,7 +151,7 @@ NSResult NSStorageDelete(NSCacheList * list, const char * delId) prev->next = del->next; if (type == NS_CONSUMER_CACHE_MESSAGE) { - NSRemoveMessage((NSMessage_consumer *) del->data); + NSRemoveMessage((NSMessage *) del->data); } else if (type == NS_CONSUMER_CACHE_PROVIDER) { @@ -195,77 +170,63 @@ NSResult NSStorageDelete(NSCacheList * list, const char * delId) return NS_OK; } -NSResult NSConsumerCacheWriteMessage(NSCacheList * list, NSCacheElement * newObj) +void NSConsumerRemoveMessageStore(NSCacheElement * ele, NSStoreMessage * msg) { - pthread_mutex_t * mutex = NSGetCacheMutex(); + NSOICFree(ele); + NSOICFree(msg); +} - pthread_mutex_lock(mutex); +NSResult NSConsumerCacheWriteMessage(NSCacheList * list, NSCacheElement * newObj) +{ + NS_VERIFY_NOT_NULL(list, NS_ERROR); + NS_VERIFY_NOT_NULL(newObj, NS_ERROR); - if (!newObj) - { - pthread_mutex_unlock(mutex); - NS_LOG (ERROR, "Failed to Write Message Cache"); - return NS_ERROR; - } + pthread_mutex_t * mutex = NSGetCacheMutex(); - NSMessage_consumer * newMsgObj = (NSMessage_consumer *) newObj->data; + NSMessage * newMsgObj = ((NSStoreMessage *)newObj->data)->msg; - pthread_mutex_unlock(mutex); char msgId[NS_DEVICE_ID_LENGTH] = {0, }; snprintf(msgId, NS_DEVICE_ID_LENGTH, "%lld", (long long int)newMsgObj->messageId); NSCacheElement * it = NSStorageRead(list, msgId); - pthread_mutex_lock(mutex); if (it) { - NSMessage_consumer * msgObj = (NSMessage_consumer *) it->data; - if(msgObj->type == newMsgObj->type) + NS_LOG(DEBUG, "Update message status."); + pthread_mutex_lock(mutex); + NSStoreMessage * sMsgObj = (NSStoreMessage *) it->data; + if(sMsgObj->status == ((NSStoreMessage *)newObj->data)->status) { NS_LOG (DEBUG, "Already receive message"); pthread_mutex_unlock(mutex); return NS_ERROR; } - - it->data = (void *) NSCopyMessage(newMsgObj); - if (!it->data) - { - NS_LOG (ERROR, "Failed to CopyMessage"); - it->data = (void *) msgObj; - pthread_mutex_unlock(mutex); - - return NS_ERROR; - } - NSRemoveMessage(msgObj); + sMsgObj->status = ((NSStoreMessage *)newObj->data)->status; pthread_mutex_unlock(mutex); - return NS_OK; } + NS_LOG(DEBUG, "Add message at storage."); + NSStoreMessage * sMsgObj = (NSStoreMessage *) OICMalloc(sizeof(NSStoreMessage)); + NS_VERIFY_NOT_NULL(sMsgObj, NS_ERROR); + NSCacheElement * obj = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement)); - if (!obj) - { - NS_LOG(ERROR, "Fail to Create New Object"); - pthread_mutex_unlock(mutex); + NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(obj, NS_ERROR, NSOICFree(sMsgObj)); - return NS_ERROR; - } - obj->data = (void *) NSCopyMessage(newMsgObj); - if (!obj->data) - { - NS_LOG (ERROR, "Failed to CopyMessage"); - pthread_mutex_unlock(mutex); + sMsgObj->status = NS_SYNC_UNREAD; + sMsgObj->msg = (void *) NSCopyMessage(newMsgObj); + NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(sMsgObj->msg, NS_ERROR, + NSConsumerRemoveMessageStore(obj, sMsgObj)); - return NS_ERROR; - } obj->next = NULL; + obj->data = (NSCacheData *) sMsgObj; + pthread_mutex_lock(mutex); if (!list->head) { list->head = obj; list->tail = obj; pthread_mutex_unlock(mutex); - return NS_OK; } @@ -373,32 +334,33 @@ NSResult NSConsumerCacheWriteProvider(NSCacheList * list, NSCacheElement * newOb NSCacheElement * NSPopProviderCacheList(NSCacheList * list) { - pthread_mutex_t * mutex = NSGetCacheMutex(); + NS_VERIFY_NOT_NULL(list, NULL); + pthread_mutex_t * mutex = NSGetCacheMutex(); pthread_mutex_lock(mutex); NSCacheElement * head = list->head; if (head) { - NSCacheElement * next = list->head->next; - - if (list->tail == head) - list->tail = NULL; + if (list->tail == head) + { + list->tail = NULL; + } - list->head = next; - head->next = NULL; + list->head = head->next; + head->next = NULL; } pthread_mutex_unlock(mutex); - return head; } NSResult NSStorageDestroy(NSCacheList * list) { - pthread_mutex_t * mutex = NSGetCacheMutex(); + NS_VERIFY_NOT_NULL(list, NS_ERROR); + pthread_mutex_t * mutex = NSGetCacheMutex(); pthread_mutex_lock(mutex); NSCacheElement * iter = list->head; @@ -412,7 +374,8 @@ NSResult NSStorageDestroy(NSCacheList * list) { next = (NSCacheElement *) iter->next; - NSRemoveMessage((NSMessage_consumer *) iter->data); + NSRemoveMessage(((NSStoreMessage *) iter->data)->msg); + NSOICFree(iter->data); NSOICFree(iter); iter = next; @@ -442,14 +405,12 @@ NSResult NSStorageDestroy(NSCacheList * list) bool NSConsumerCompareIdCacheData(NSCacheType type, void * data, const char * id) { - if (data == NULL) - { - return false; - } + NS_VERIFY_NOT_NULL(data, false); + NS_VERIFY_NOT_NULL(id, false); if (type == NS_CONSUMER_CACHE_MESSAGE) { - NSMessage_consumer * msg = (NSMessage_consumer *) data; + NSMessage * msg = ((NSStoreMessage *) data)->msg; char msgId[NS_DEVICE_ID_LENGTH] = {0, }; snprintf(msgId, NS_DEVICE_ID_LENGTH, "%lld", (long long int)msg->messageId);