Remove NSMessage Storage of consumer service.
authorKIM JungYong <jyong2.kim@samsung.com>
Wed, 7 Sep 2016 05:33:14 +0000 (14:33 +0900)
committerUze Choi <uzchoi@samsung.com>
Thu, 8 Sep 2016 10:01:06 +0000 (10:01 +0000)
NSMessage storage is removed reason by unnecessariness functionality.
But, for the prevent of duplicated message, messageId and NSSyncType need to store.
So, linked list of information for message sync state is added.

Change-Id: Iea80b20a4ca44d9b17b2d3f0746d1f33212d9a3e
Signed-off-by: KIM JungYong <jyong2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/11479
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Chihyun Cho <ch79.cho@samsung.com>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/notification/src/consumer/NSConsumerCommon.h
service/notification/src/consumer/NSConsumerInternalTaskController.c
service/notification/src/consumer/NSConsumerMemoryCache.c
service/notification/src/consumer/NSConsumerMemoryCache.h
service/notification/src/consumer/NSConsumerScheduler.c

index ea1dea8..417e6a0 100644 (file)
@@ -97,13 +97,6 @@ typedef struct
 
 } NSSyncInfo_internal;
 
-typedef struct
-{
-    NSSyncType status;
-    NSMessage * msg;
-
-} NSStoreMessage;
-
 bool NSIsStartedConsumer();
 void NSSetIsStartedConsumer(bool setValue);
 
index f1e5220..0c156d0 100644 (file)
 #include "oic_malloc.h"
 #include "oic_string.h"
 
-NSCacheList ** NSGetMessageCacheList()
+#define NS_RESERVED_MESSAGEID 10
+
+// MessageState storage structure
+typedef struct _NSMessageStateLL
 {
-    static NSCacheList * messageCache = NULL;
-    return & messageCache;
-}
+    uint64_t messageId;
+    NSSyncType state;
+    struct _NSMessageStateLL * next;
+
+} NSMessageStateLL;
 
-void NSSetMessageCacheList(NSCacheList * cache)
+typedef struct
 {
-    *(NSGetMessageCacheList()) = cache;
-}
+    NSMessageStateLL * head;
+    NSMessageStateLL * tail;
+
+} NSMessageStateList;
+
+// Mutex of MessageState storage
+pthread_mutex_t * NSGetMessageListMutex();
+void NSLockMessageListMutex();
+void NSUnlockMessageListMutex();
+
+// Function for MessageState
+NSMessageStateList * NSGetMessageStateList();
+NSMessageStateLL * NSFindMessageState(uint64_t msgId);
+bool NSUpdateMessageState(uint64_t msgId, NSSyncType state);
+bool NSDeleteMessageState(uint64_t msgId);
+bool NSInsertMessageState(uint64_t msgId, NSSyncType state);
+void NSDestroyMessageStateList();
 
 NSCacheList ** NSGetProviderCacheList()
 {
@@ -48,17 +68,6 @@ void NSSetProviderCacheList(NSCacheList * cache)
     *(NSGetProviderCacheList()) = cache;
 }
 
-void NSDestroyMessageCacheList()
-{
-    NSCacheList * cache = *(NSGetMessageCacheList());
-    if (cache)
-    {
-        NSStorageDestroy(cache);
-    }
-
-    NSSetMessageCacheList(NULL);
-}
-
 void NSDestroyProviderCacheList()
 {
     NSCacheList * cache = *(NSGetProviderCacheList());
@@ -70,27 +79,6 @@ void NSDestroyProviderCacheList()
     NSSetProviderCacheList(NULL);
 }
 
-NSMessage * NSMessageCacheFind(const char * messageId)
-{
-    NS_VERIFY_NOT_NULL(messageId, NULL);
-
-    NSCacheList * MessageCache = *(NSGetMessageCacheList());
-    if (!MessageCache)
-    {
-        NS_LOG(DEBUG, "Message Cache Init");
-        MessageCache = NSStorageCreate();
-        NS_VERIFY_NOT_NULL(MessageCache, NULL);
-
-        MessageCache->cacheType = NS_CONSUMER_CACHE_MESSAGE;
-        NSSetMessageCacheList(MessageCache);
-    }
-
-    NSCacheElement * cacheElement = NSStorageRead(MessageCache, messageId);
-    NS_VERIFY_NOT_NULL(cacheElement, NULL);
-
-    return NSCopyMessage(((NSStoreMessage *) cacheElement->data)->msg);
-}
-
 NSProvider_internal * NSProviderCacheFind(const char * providerId)
 {
     NS_VERIFY_NOT_NULL(providerId, NULL);
@@ -131,51 +119,6 @@ NSProvider_internal * NSFindProviderFromAddr(OCDevAddr * addr)
     return NSCopyProvider_internal((NSProvider_internal *) cacheElement->data);
 }
 
-void NSRemoveCacheElementMessage(NSCacheElement * obj)
-{
-    NSRemoveMessage(((NSStoreMessage *)obj->data)->msg);
-    NSOICFree(obj->data);
-    NSOICFree(obj);
-}
-
-NSResult NSMessageCacheUpdate(NSMessage * msg, NSSyncType type)
-{
-    NS_VERIFY_NOT_NULL(msg, NS_ERROR);
-
-    NSCacheList * MessageCache = *(NSGetMessageCacheList());
-    if (!MessageCache)
-    {
-        NS_LOG(DEBUG, "Message Cache Init");
-        MessageCache = NSStorageCreate();
-        NS_VERIFY_NOT_NULL(MessageCache, NS_ERROR);
-
-        MessageCache->cacheType = NS_CONSUMER_CACHE_MESSAGE;
-        NSSetMessageCacheList(MessageCache);
-    }
-
-    NSStoreMessage * sMsg = (NSStoreMessage *)OICMalloc(sizeof(NSStoreMessage));
-    NS_VERIFY_NOT_NULL(sMsg, NS_ERROR);
-
-    NSCacheElement * obj = (NSCacheElement *)OICMalloc(sizeof(NSCacheElement));
-    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(obj, NS_ERROR, NSOICFree(sMsg));
-
-    sMsg->status = type;
-    sMsg->msg = NSCopyMessage(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, NSRemoveCacheElementMessage(obj));
-
-    NSRemoveCacheElementMessage(obj);
-
-    NS_LOG(DEBUG, "Update message done");
-    return NS_OK;
-}
-
 NSResult NSProviderCacheUpdate(NSProvider_internal * provider)
 {
     NSCacheList * ProviderCache = *(NSGetProviderCacheList());
@@ -400,27 +343,25 @@ void NSConsumerHandleRecvMessage(NSMessage * msg)
 {
     NS_VERIFY_NOT_NULL_V(msg);
 
-    NSResult ret = NSMessageCacheUpdate(msg, NS_SYNC_UNREAD);
-    NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
-
-    NSMessagePost((NSMessage *) msg);
+    if (NSInsertMessageState(msg->messageId, NS_SYNC_UNREAD))
+    {
+        NSMessagePost(msg);
+    }
 }
 
 void NSConsumerHandleRecvSyncInfo(NSSyncInfo * sync)
 {
     NS_VERIFY_NOT_NULL_V(sync);
 
-    char msgId[NS_DEVICE_ID_LENGTH] = { 0, };
-    snprintf(msgId, NS_DEVICE_ID_LENGTH, "%lld", (long long int)sync->messageId);
-
-    NSMessage * msg = NSMessageCacheFind(msgId);
-    NS_VERIFY_NOT_NULL_V(msg);
-
-    NSResult ret = NSMessageCacheUpdate(msg, sync->state);
-    NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
-    NSRemoveMessage(msg);
+    if (NSUpdateMessageState(sync->messageId, sync->state))
+    {
+        NSNotificationSync(sync);
+    }
 
-    NSNotificationSync(sync);
+    if (sync->state == NS_SYNC_DELETED)
+    {
+        NSDeleteMessageState(sync->messageId);
+    }
 }
 
 void NSConsumerHandleMakeSyncInfo(NSSyncInfo * sync)
@@ -431,6 +372,7 @@ void NSConsumerHandleMakeSyncInfo(NSSyncInfo * sync)
     NS_VERIFY_NOT_NULL_V (provider);
 
     NSProviderConnectionInfo * connections = NSCopyProviderConnections(provider->connection);
+    NSRemoveProvider_internal(provider);
     NS_VERIFY_NOT_NULL_V (connections);
 
     NSSyncInfo_internal * syncInfo = (NSSyncInfo_internal *)OICMalloc(sizeof(NSSyncInfo_internal));
@@ -553,3 +495,169 @@ void NSConsumerInternalTaskProcessing(NSTask * task)
     }
     NSOICFree(task);
 }
+
+// implements of MessageState function
+pthread_mutex_t * NSGetMessageListMutex()
+{
+    static pthread_mutex_t * g_mutex = NULL;
+    if (g_mutex == NULL)
+    {
+        g_mutex = (pthread_mutex_t *) OICMalloc(sizeof(pthread_mutex_t));
+        NS_VERIFY_NOT_NULL(g_mutex, NULL);
+
+        pthread_mutex_init(g_mutex, NULL);
+    }
+    return g_mutex;
+}
+
+void NSLockMessageListMutex()
+{
+    NS_LOG_V(DEBUG, "%s", __func__);
+    pthread_mutex_lock(NSGetMessageListMutex());
+}
+
+void NSUnlockMessageListMutex()
+{
+    NS_LOG_V(DEBUG, "%s", __func__);
+    pthread_mutex_unlock(NSGetMessageListMutex());
+}
+
+NSMessageStateList * NSGetMessageStateList()
+{
+    static NSMessageStateList * g_messageStateList = NULL;
+    if (g_messageStateList == NULL)
+    {
+        g_messageStateList = (NSMessageStateList *)OICMalloc(sizeof(NSMessageStateList));
+        NS_VERIFY_NOT_NULL(g_messageStateList, NULL);
+
+        g_messageStateList->head = NULL;
+        g_messageStateList->tail = NULL;
+    }
+
+    return g_messageStateList;
+}
+
+NSMessageStateLL * NSFindMessageState(uint64_t msgId)
+{
+    NS_LOG_V(DEBUG, "%s", __func__);
+    if (msgId <= NS_RESERVED_MESSAGEID) return NULL;
+    NSMessageStateLL * iter = NULL;
+
+    NSLockMessageListMutex();
+    for (iter = NSGetMessageStateList()->head; iter; iter = iter->next)
+    {
+        if (iter->messageId == msgId)
+        {
+            NSUnlockMessageListMutex();
+            return iter;
+        }
+    }
+
+    NSUnlockMessageListMutex();
+    return NULL;
+}
+
+bool NSUpdateMessageState(uint64_t msgId, NSSyncType state)
+{
+    NS_LOG_V(DEBUG, "%s", __func__);
+    if (msgId <= NS_RESERVED_MESSAGEID) return NULL;
+    NSMessageStateLL * iter = NULL;
+
+    NSLockMessageListMutex();
+    for (iter = NSGetMessageStateList()->head; iter; iter = iter->next)
+    {
+        if (iter->messageId == msgId && state != iter->state)
+        {
+            iter->state = state;
+            NSUnlockMessageListMutex();
+            return true;
+        }
+    }
+
+    NSUnlockMessageListMutex();
+    return false;
+}
+
+bool NSDeleteMessageState(uint64_t msgId)
+{
+    NS_LOG_V(DEBUG, "%s", __func__);
+    if (msgId <= NS_RESERVED_MESSAGEID) return NULL;
+    NSMessageStateLL * iter = NULL;
+    NSMessageStateLL * prev = NULL;
+
+    NSLockMessageListMutex();
+    for (iter = NSGetMessageStateList()->head; iter; iter = iter->next)
+    {
+        if (iter->messageId == msgId)
+        {
+            if (iter == NSGetMessageStateList()->head)
+            {
+                NSGetMessageStateList()->head = NULL;
+                NSGetMessageStateList()->tail = NULL;
+            }
+            else if (iter == NSGetMessageStateList()->tail)
+            {
+                prev->next = NULL;
+                NSGetMessageStateList()->tail = prev;
+            }
+            else
+            {
+                prev->next = iter->next;
+            }
+            NSUnlockMessageListMutex();
+
+            NSOICFree(iter);
+            return true;
+        }
+        prev = iter;
+    }
+
+    NSUnlockMessageListMutex();
+    return false;
+}
+
+bool NSInsertMessageState(uint64_t msgId, NSSyncType state)
+{
+    NS_LOG_V(DEBUG, "%s", __func__);
+    if (NSFindMessageState(msgId))
+    {
+        return false;
+    }
+
+    NSMessageStateLL * insertMsg = (NSMessageStateLL * )OICMalloc(sizeof(NSMessageStateLL));
+    NS_VERIFY_NOT_NULL(insertMsg, false);
+
+    insertMsg->messageId = msgId;
+    insertMsg->state = state;
+    insertMsg->next = NULL;
+
+    NSLockMessageListMutex();
+    if (NSGetMessageStateList()->head == NULL)
+    {
+        NSGetMessageStateList()->head = insertMsg;
+    }
+    else
+    {
+        NSGetMessageStateList()->tail->next = insertMsg;
+    }
+    NSGetMessageStateList()->tail = insertMsg;
+    NSUnlockMessageListMutex();
+
+    return true;
+}
+
+void NSDestroyMessageStateList()
+{
+    NS_LOG_V(DEBUG, "%s", __func__);
+    NSLockMessageListMutex();
+
+    NSMessageStateLL * iter = NSGetMessageStateList()->head;
+    while (iter)
+    {
+        NSMessageStateLL * del = iter;
+        iter = iter->next;
+        NSOICFree(del);
+    }
+
+    NSUnlockMessageListMutex();
+}
index 7501958..f3dff6a 100644 (file)
@@ -123,11 +123,7 @@ NSResult NSStorageWrite(NSCacheList * list, NSCacheElement * newObj)
     NSCacheType type = list->cacheType;
     NS_LOG_V(DEBUG, "cache type : %d", type);
 
-    if (type == NS_CONSUMER_CACHE_MESSAGE)
-    {
-        return NSConsumerCacheWriteMessage(list, newObj);
-    }
-    else if (type == NS_CONSUMER_CACHE_PROVIDER)
+    if (type == NS_CONSUMER_CACHE_PROVIDER)
     {
         return NSConsumerCacheWriteProvider(list, newObj);
     }
@@ -162,11 +158,7 @@ NSResult NSStorageDelete(NSCacheList * list, const char * delId)
 
             list->head = del->next;
 
-            if (type == NS_CONSUMER_CACHE_MESSAGE)
-            {
-                NSRemoveMessage((NSMessage *) del->data);
-            }
-            else if (type == NS_CONSUMER_CACHE_PROVIDER)
+            if (type == NS_CONSUMER_CACHE_PROVIDER)
             {
                 NSRemoveProvider_internal((NSProvider_internal *) del->data);
             }
@@ -188,11 +180,8 @@ NSResult NSStorageDelete(NSCacheList * list, const char * delId)
             }
 
             prev->next = del->next;
-            if (type == NS_CONSUMER_CACHE_MESSAGE)
-            {
-                NSRemoveMessage((NSMessage *) del->data);
-            }
-            else if (type == NS_CONSUMER_CACHE_PROVIDER)
+
+            if (type == NS_CONSUMER_CACHE_PROVIDER)
             {
                 NSRemoveProvider_internal((NSProvider_internal *) del->data);
             }
@@ -209,73 +198,6 @@ NSResult NSStorageDelete(NSCacheList * list, const char * delId)
     return NS_OK;
 }
 
-void NSConsumerRemoveMessageStore(NSCacheElement * ele, NSStoreMessage * msg)
-{
-    NSOICFree(ele);
-    NSOICFree(msg);
-}
-
-NSResult NSConsumerCacheWriteMessage(NSCacheList * list, NSCacheElement * newObj)
-{
-    NS_VERIFY_NOT_NULL(list, NS_ERROR);
-    NS_VERIFY_NOT_NULL(newObj, NS_ERROR);
-
-    pthread_mutex_t * mutex = NSGetCacheMutex();
-
-    NSMessage * newMsgObj = ((NSStoreMessage *)newObj->data)->msg;
-
-    char msgId[NS_DEVICE_ID_LENGTH] = {0, };
-    snprintf(msgId, NS_DEVICE_ID_LENGTH, "%lld", (long long int)newMsgObj->messageId);
-    NSCacheElement * it = NSStorageRead(list, msgId);
-
-    if (it)
-    {
-        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;
-        }
-
-        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));
-    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(obj, NS_ERROR, NSOICFree(sMsgObj));
-
-    sMsgObj->status = NS_SYNC_UNREAD;
-    sMsgObj->msg = (void *) NSCopyMessage(newMsgObj);
-    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(sMsgObj->msg, NS_ERROR,
-          NSConsumerRemoveMessageStore(obj, sMsgObj));
-
-    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;
-    }
-
-    (list->tail)->next = obj;
-    list->tail = obj;
-    pthread_mutex_unlock(mutex);
-
-    return NS_OK;
-}
-
 NSResult NSConsumerCacheWriteProvider(NSCacheList * list, NSCacheElement * newObj)
 {
     NS_VERIFY_NOT_NULL(list, NS_ERROR);
@@ -406,22 +328,7 @@ NSResult NSStorageDestroy(NSCacheList * list)
 
     NSCacheType type = list->cacheType;
 
-    if (type == NS_CONSUMER_CACHE_MESSAGE)
-    {
-        while (iter)
-        {
-            next = (NSCacheElement *) iter->next;
-
-            NSRemoveMessage(((NSStoreMessage *) iter->data)->msg);
-            NSOICFree(iter->data);
-            NSOICFree(iter);
-
-            iter = next;
-        }
-
-        NSOICFree(list);
-    }
-    else if (type == NS_CONSUMER_CACHE_PROVIDER)
+    if (type == NS_CONSUMER_CACHE_PROVIDER)
     {
         while (iter)
         {
@@ -446,20 +353,7 @@ bool NSConsumerCompareIdCacheData(NSCacheType type, void * data, const char * id
     NS_VERIFY_NOT_NULL(data, false);
     NS_VERIFY_NOT_NULL(id, false);
 
-    if (type == NS_CONSUMER_CACHE_MESSAGE)
-    {
-        NSMessage * msg = ((NSStoreMessage *) data)->msg;
-
-        char msgId[NS_DEVICE_ID_LENGTH] = {0, };
-        snprintf(msgId, NS_DEVICE_ID_LENGTH, "%lld", (long long int)msg->messageId);
-        if (!strcmp(msgId, id))
-        {
-            return true;
-        }
-
-        return false;
-    }
-    else if (type == NS_CONSUMER_CACHE_PROVIDER)
+    if (type == NS_CONSUMER_CACHE_PROVIDER)
     {
         NSProvider_internal * prov = (NSProvider_internal *) data;
         if (!strcmp(prov->providerId, id))
index 130e78a..8d6a608 100644 (file)
@@ -35,7 +35,6 @@ pthread_mutex_t * NSGetCacheMutex();
 
 bool NSConsumerCompareIdCacheData(NSCacheType type, void * data, const char * id);
 
-NSResult NSConsumerCacheWriteMessage(NSCacheList * list, NSCacheElement * newObj);
 NSResult NSConsumerCacheWriteProvider(NSCacheList * list, NSCacheElement * newObj);
 NSCacheElement * NSPopProviderCacheList(NSCacheList * list);
 
index 47224cd..170769f 100644 (file)
@@ -124,7 +124,6 @@ void NSConsumerMessageHandlerExit()
     NSDestroyQueue(*(NSGetMsgHandleQueue()));
     NSSetMsgHandleQueue(NULL);
 
-    NSDestroyMessageCacheList();
     NSDestroyProviderCacheList();
 }
 
@@ -341,13 +340,6 @@ void NSConsumerTaskProcessing(NSTask * task)
     }
 }
 
-NSMessage * NSConsumerFindNSMessage(const char* messageId)
-{
-    NS_VERIFY_NOT_NULL(messageId, NULL);
-
-    return NSMessageCacheFind(messageId);
-}
-
 NSProvider_internal * NSConsumerFindNSProvider(const char * providerId)
 {
     NS_VERIFY_NOT_NULL(providerId, NULL);