+
+// 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 ** NSGetMessageStateListAddr()
+{
+ 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;
+}
+
+NSMessageStateList * NSGetMessageStateList()
+{
+ return * NSGetMessageStateListAddr();
+}
+
+NSMessageStateLL * NSFindMessageState(uint64_t msgId)
+{
+ NS_LOG_V(DEBUG, "%s", __func__);
+ if (msgId <= NS_RESERVED_MESSAGEID)
+ {
+ return NULL;
+ }
+ NSMessageStateLL * iter = NULL;
+
+ NSLockMessageListMutex();
+ if (NSGetMessageStateList()->head == NULL)
+ {
+ NSUnlockMessageListMutex();
+ return NULL;
+ }
+
+ 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 false;
+ }
+ 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 false;
+ }
+
+ 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);
+ }
+
+ NSGetMessageStateList()->head = NULL;
+ NSGetMessageStateList()->tail = NULL;
+
+ NSUnlockMessageListMutex();
+
+ pthread_mutex_t * mu = *NSGetMessageListMutex();
+ pthread_mutex_destroy(mu);
+ NSOICFree(mu);
+ *NSGetMessageListMutex() = NULL;
+
+ NSMessageStateList * list = NSGetMessageStateList();
+ NSOICFree(list);
+ *NSGetMessageStateListAddr() = NULL;
+}