replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / notification / src / consumer / NSConsumerMemoryCache.c
index a9e0c27..366ef8c 100644 (file)
@@ -35,7 +35,7 @@ pthread_mutex_t * NSGetCacheMutex()
     return g_NSCacheMutex;
 }
 
-NSCacheList * NSStorageCreate()
+NSCacheList * NSConsumerStorageCreate()
 {
     pthread_mutex_t * mutex = NSGetCacheMutex();
     pthread_mutex_lock(mutex);
@@ -51,7 +51,7 @@ NSCacheList * NSStorageCreate()
     return newList;
 }
 
-NSCacheElement * NSStorageRead(NSCacheList * list, const char * findId)
+NSCacheElement * NSConsumerStorageRead(NSCacheList * list, const char * findId)
 {
     NS_VERIFY_NOT_NULL(list, NULL);
     NS_VERIFY_NOT_NULL(findId, NULL);
@@ -78,7 +78,44 @@ NSCacheElement * NSStorageRead(NSCacheList * list, const char * findId)
     return NULL;
 }
 
-NSResult NSStorageWrite(NSCacheList * list, NSCacheElement * newObj)
+NSCacheElement * NSGetProviderFromAddr(NSCacheList * list, const char * addr, uint16_t port)
+{
+    NS_VERIFY_NOT_NULL(list, NULL);
+    NS_VERIFY_NOT_NULL(addr, NULL);
+    NS_VERIFY_NOT_NULL(
+            (list->cacheType != NS_CONSUMER_CACHE_PROVIDER) ? NULL : (void *) 1, NULL);
+
+    pthread_mutex_t * mutex = NSGetCacheMutex();
+    pthread_mutex_lock(mutex);
+
+    NSCacheElement * iter = list->head;
+
+    while (iter)
+    {
+        NSProviderConnectionInfo * connection =
+                ((NSProvider_internal *) iter->data)->connection;
+        while (connection)
+        {
+            char * conAddr = connection->addr->addr;
+            uint16_t conPort = connection->addr->port;
+
+            if (!strcmp(conAddr, addr) && conPort == port)
+            {
+                pthread_mutex_unlock(mutex);
+                return iter;
+            }
+            connection = connection->next;
+        }
+
+        iter = iter->next;
+    }
+
+    NS_LOG (DEBUG, "No Cache Element");
+    pthread_mutex_unlock(mutex);
+    return NULL;
+}
+
+NSResult NSConsumerStorageWrite(NSCacheList * list, NSCacheElement * newObj)
 {
     NS_VERIFY_NOT_NULL(list, NS_ERROR);
     NS_VERIFY_NOT_NULL(newObj, NS_ERROR);
@@ -86,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);
     }
@@ -100,7 +133,7 @@ NSResult NSStorageWrite(NSCacheList * list, NSCacheElement * newObj)
     return NS_ERROR;
 }
 
-NSResult NSStorageDelete(NSCacheList * list, const char * delId)
+NSResult NSConsumerStorageDelete(NSCacheList * list, const char * delId)
 {
     NS_VERIFY_NOT_NULL(list, NS_ERROR);
     NS_VERIFY_NOT_NULL(delId, NS_ERROR);
@@ -125,13 +158,9 @@ NSResult NSStorageDelete(NSCacheList * list, const char * delId)
 
             list->head = del->next;
 
-            if (type == NS_CONSUMER_CACHE_MESSAGE)
+            if (type == NS_CONSUMER_CACHE_PROVIDER)
             {
-                NSRemoveMessage((NSMessage *) del->data);
-            }
-            else if (type == NS_CONSUMER_CACHE_PROVIDER)
-            {
-                NSRemoveProvider((NSProvider_internal *) del->data);
+                NSRemoveProvider_internal((void *) del->data);
             }
             NSOICFree(del);
             pthread_mutex_unlock(mutex);
@@ -151,13 +180,10 @@ 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((NSProvider_internal *) del->data);
+                NSRemoveProvider_internal((NSProvider_internal *) del->data);
             }
             NSOICFree(del);
             pthread_mutex_unlock(mutex);
@@ -172,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);
@@ -246,28 +205,34 @@ NSResult NSConsumerCacheWriteProvider(NSCacheList * list, NSCacheElement * newOb
 
     pthread_mutex_t * mutex = NSGetCacheMutex();
 
-    NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1");
-    NSProvider_internal * prov = (NSProvider_internal *)newObj->data;
-    NS_LOG_V (DEBUG, "%s", prov->providerId);
-    NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1");
-
     NSProvider_internal * newProvObj = (NSProvider_internal *) newObj->data;
 
-    NSCacheElement * it = NSStorageRead(list, newProvObj->providerId);
+    NSCacheElement * it = NSConsumerStorageRead(list, newProvObj->providerId);
+
     pthread_mutex_lock(mutex);
 
     if (it)
     {
-        NSProvider_internal * provObj = (NSProvider_internal *) it->data;
+        if (newProvObj->connection)
+        {
+            NSProvider_internal * provObj = (NSProvider_internal *) it->data;
+
+            NSProviderConnectionInfo * infos = provObj->connection;
+            NSProviderConnectionInfo * lastConn = infos->next;
+            while(lastConn)
+            {
+                infos = lastConn;
+                lastConn = lastConn->next;
+            }
+            infos->next = NSCopyProviderConnections(newProvObj->connection);
+        }
 
-        NSProviderConnectionInfo * infos = provObj->connection;
-        NSProviderConnectionInfo * lastConn = infos->next;
-        while(lastConn)
+        if (newProvObj->topicLL)
         {
-            infos = lastConn;
-            lastConn = lastConn->next;
+            NSProvider_internal * provObj = (NSProvider_internal *) it->data;
+            NSRemoveTopicLL(provObj->topicLL);
+            provObj->topicLL = NSCopyTopicLL(newProvObj->topicLL);
         }
-        infos->next = NSCopyProviderConnections(newProvObj->connection);
 
         pthread_mutex_unlock(mutex);
 
@@ -277,17 +242,13 @@ NSResult NSConsumerCacheWriteProvider(NSCacheList * list, NSCacheElement * newOb
     NSCacheElement * obj = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));
     NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(obj, NS_ERROR, pthread_mutex_unlock(mutex));
 
-    NS_LOG_V(DEBUG, "New Object address : %s:%d", newProvObj->connection->addr->addr, newProvObj->connection->addr->port);
-    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");
+    NS_LOG_V(INFO_PRIVATE, "New Object address : %s:%d", newProvObj->connection->addr->addr, newProvObj->connection->addr->port);
+    obj->data = (void *) NSCopyProvider_internal(newProvObj);
 
     if (!obj->data)
     {
         NS_LOG (ERROR, "Failed to CopyProvider");
+        NSOICFree(obj);
         pthread_mutex_unlock(mutex);
 
         return NS_ERROR;
@@ -299,11 +260,6 @@ NSResult NSConsumerCacheWriteProvider(NSCacheList * list, NSCacheElement * newOb
         list->head = obj;
         list->tail = obj;
 
-        NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!3");
-        prov = (NSProvider_internal *)list->tail->data;
-        NS_LOG_V (DEBUG, "%s", prov->providerId);
-        NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!3");
-
         pthread_mutex_unlock(mutex);
 
         return NS_OK;
@@ -312,11 +268,6 @@ NSResult NSConsumerCacheWriteProvider(NSCacheList * list, NSCacheElement * newOb
     (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;
@@ -346,7 +297,7 @@ NSCacheElement * NSPopProviderCacheList(NSCacheList * list)
 }
 
 
-NSResult NSStorageDestroy(NSCacheList * list)
+NSResult NSConsumerStorageDestroy(NSCacheList * list)
 {
     NS_VERIFY_NOT_NULL(list, NS_ERROR);
 
@@ -358,28 +309,13 @@ 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)
         {
             next = (NSCacheElement *) iter->next;
 
-            NSRemoveProvider((NSProvider_internal *) iter->data);
+            NSRemoveProvider_internal((void *) iter->data);
             NSOICFree(iter);
 
             iter = next;
@@ -398,20 +334,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))