#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()
{
*(NSGetProviderCacheList()) = cache;
}
-void NSDestroyMessageCacheList()
-{
- NSCacheList * cache = *(NSGetMessageCacheList());
- if (cache)
- {
- NSStorageDestroy(cache);
- }
-
- NSSetMessageCacheList(NULL);
-}
-
void NSDestroyProviderCacheList()
{
NSCacheList * cache = *(NSGetProviderCacheList());
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);
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());
{
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)
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));
}
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();
+}
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);
}
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);
}
}
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);
}
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);
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)
{
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))