NSResult NSProviderCacheUpdate(NSProvider_internal * provider)
{
    NSCacheList * ProviderCache = *(NSGetProviderCacheList());
    if (!ProviderCache)
    {
        NS_LOG(DEBUG, "Provider Cache Init");
        ProviderCache = NSStorageCreate();
        NS_VERIFY_NOT_NULL(ProviderCache, NS_ERROR);

        ProviderCache->cacheType = NS_CONSUMER_CACHE_PROVIDER;
        NSSetProviderCacheList(ProviderCache);
    }

    NS_VERIFY_NOT_NULL(provider, NS_ERROR);

    NSCacheElement * obj = (NSCacheElement *)OICMalloc(sizeof(NSCacheElement));
    NS_VERIFY_NOT_NULL(obj, NS_ERROR);

    obj->data = (NSCacheData *) provider;
    obj->next = NULL;

    NS_LOG(DEBUG, "try to write to storage");
    NSResult ret = NSStorageWrite(ProviderCache, obj);
    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(ret == NS_OK ? (void *) 1 : NULL,
            NS_ERROR, NSOICFree(obj));

    return NS_OK;
}
예제 #2
0
NSCacheElement * NSConsumerStorageRead(NSCacheList * list, const char * findId)
{
    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;
    NSCacheType type = list->cacheType;

    while (iter)
    {
        if (NSConsumerCompareIdCacheData(type, iter->data, findId))
        {
            pthread_mutex_unlock(mutex);
            return iter;
        }

        iter = iter->next;
    }

    NS_LOG (DEBUG, "No Cache Element");
    pthread_mutex_unlock(mutex);
    return NULL;
}
예제 #3
0
NSResult NSConsumerStorageDestroy(NSCacheList * list)
{
    NS_VERIFY_NOT_NULL(list, NS_ERROR);

    pthread_mutex_t * mutex = NSGetCacheMutex();
    pthread_mutex_lock(mutex);

    NSCacheElement * iter = list->head;
    NSCacheElement * next = NULL;

    NSCacheType type = list->cacheType;

    if (type == NS_CONSUMER_CACHE_PROVIDER)
    {
        while (iter)
        {
            next = (NSCacheElement *) iter->next;

            NSRemoveProvider_internal((void *) iter->data);
            NSOICFree(iter);

            iter = next;
        }

        NSOICFree(list);
    }

    pthread_mutex_unlock(mutex);

    return NS_OK;
}
예제 #4
0
NSResult NSConsumerStorageWrite(NSCacheList * list, NSCacheElement * newObj)
{
    NS_VERIFY_NOT_NULL(list, NS_ERROR);
    NS_VERIFY_NOT_NULL(newObj, NS_ERROR);

    NSCacheType type = list->cacheType;
    NS_LOG_V(DEBUG, "cache type : %d", type);

    if (type == NS_CONSUMER_CACHE_PROVIDER)
    {
        return NSConsumerCacheWriteProvider(list, newObj);
    }

    NS_LOG (ERROR, "Not Supported Type");

    return NS_ERROR;
}
예제 #5
0
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_PROVIDER)
    {
        NSProvider_internal * prov = (NSProvider_internal *) data;
        if (!strcmp(prov->providerId, id))
        {
            return true;
        }

        return false;
    }

    return false;
}
예제 #6
0
NSConsumerThread * NSThreadInit(NSThreadFunc func, void * data, const char *task_name, int stack_size)
{
    NS_VERIFY_NOT_NULL(func, NULL);

    pthread_mutex_init(&g_create_mutex, NULL);

    NSConsumerThread * handle = (NSConsumerThread *)OICMalloc(sizeof(NSConsumerThread));
    NS_VERIFY_NOT_NULL(handle, NULL);

    memset(handle, 0, sizeof(NSConsumerThread));

    pthread_mutexattr_init(&(handle->mutex_attr));

    int pthreadResult = pthread_mutexattr_settype(&(handle->mutex_attr), PTHREAD_MUTEX_RECURSIVE);
    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL,
            NULL, NSDestroyThreadHandle(handle));

    pthreadResult = pthread_mutex_init(&(handle->mutex), &(handle->mutex_attr));
    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL,
            NULL, NSDestroyThreadHandle(handle));

    pthread_mutex_lock(&g_create_mutex);

    handle->isStarted = true;

	pthread_attr_t attr;
	pthread_attr_init(&attr);

	(void)pthread_attr_setschedparam(&attr, PTHREAD_DEFAULT_PRIORITY);
	(void)pthread_attr_setstacksize(&attr, stack_size);

    pthreadResult = pthread_create(&(handle->thread_id), &attr, func,
                           (data == NULL) ? (void *) handle : (void *)data);
    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL,
            NULL, NSDestroyThreadHandle(handle));

	pthread_setname_np(handle->thread_id, task_name);

	pthread_detach(handle->thread_id);

    pthread_mutex_unlock(&g_create_mutex);

    return handle;
}
예제 #7
0
bool NSPushQueue(NSConsumerQueue * queue, NSConsumerQueueObject * object)
{
    NS_VERIFY_NOT_NULL(queue, false);
    NS_VERIFY_NOT_NULL(object, false);

    if (!(queue->head))
    {
        queue->head = object;
    }
    else
    {
        (queue->tail)->next = object;
    }

    queue->tail = object;
    queue->size++;

    return true;
}
NSProvider_internal * NSProviderCacheFind(const char * providerId)
{
    NS_VERIFY_NOT_NULL(providerId, NULL);

    NSCacheList * ProviderCache = *(NSGetProviderCacheList());
    if (!ProviderCache)
    {
        NS_LOG(DEBUG, "Provider Cache Init");
        ProviderCache = NSStorageCreate();
        NS_VERIFY_NOT_NULL(ProviderCache, NULL);

        ProviderCache->cacheType = NS_CONSUMER_CACHE_PROVIDER;
        NSSetProviderCacheList(ProviderCache);
    }

    NSCacheElement * cacheElement = NSStorageRead(ProviderCache, providerId);
    NS_VERIFY_NOT_NULL(cacheElement, NULL);

    return NSCopyProvider((NSProvider_internal *) cacheElement->data);
}
예제 #9
0
NSConsumerQueue * NSCreateQueue()
{
    NSConsumerQueue * newQueue = (NSConsumerQueue *)OICMalloc(sizeof(NSConsumerQueue));
    NS_VERIFY_NOT_NULL(newQueue, NULL);

    newQueue->size = 0;
    newQueue->head = NULL;
    newQueue->tail = NULL;

    return newQueue;
}
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);
}
예제 #11
0
pthread_mutex_t * NSGetCacheMutex()
{
    static pthread_mutex_t * g_NSCacheMutex = NULL;
    if (g_NSCacheMutex == NULL)
    {
        g_NSCacheMutex = (pthread_mutex_t *) OICMalloc(sizeof(pthread_mutex_t));
        NS_VERIFY_NOT_NULL(g_NSCacheMutex, NULL);

        pthread_mutex_init(g_NSCacheMutex, NULL);
    }
    return g_NSCacheMutex;
}
예제 #12
0
NSConsumerThread * NSThreadInit(NSThreadFunc func, void * data)
{
    NS_VERIFY_NOT_NULL(func, NULL);

    pthread_mutex_init(&g_create_mutex, NULL);

    NSConsumerThread * handle = (NSConsumerThread *)OICMalloc(sizeof(NSConsumerThread));
    NS_VERIFY_NOT_NULL(handle, NULL);

    memset(handle, 0, sizeof(NSConsumerThread));

    pthread_mutexattr_init(&(handle->mutex_attr));

    int pthreadResult = pthread_mutexattr_settype(&(handle->mutex_attr), PTHREAD_MUTEX_RECURSIVE);
    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL,
            NULL, NSDestroyThreadHandle(handle));

    pthreadResult = pthread_mutex_init(&(handle->mutex), &(handle->mutex_attr));
    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL,
            NULL, NSDestroyThreadHandle(handle));

    pthread_mutex_lock(&g_create_mutex);

    handle->isStarted = true;

    pthread_attr_t attrDetached = {};
    pthread_attr_init(& attrDetached);
    pthread_attr_setdetachstate(& attrDetached, PTHREAD_CREATE_DETACHED);

    pthreadResult = pthread_create(&(handle->thread_id), & attrDetached, func,
                           (data == NULL) ? (void *) handle : (void *)data);
    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL,
            NULL, NSDestroyThreadHandle(handle));

    pthread_attr_destroy(& attrDetached);

    pthread_mutex_unlock(&g_create_mutex);

    return handle;
}
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;
}
예제 #14
0
NSConsumerQueueObject * NSPopQueue(NSConsumerQueue * queue)
{
    NSConsumerQueueObject * retObject = NULL;

    NS_VERIFY_NOT_NULL(queue, NULL);
    NS_VERIFY_NOT_NULL(queue->head, NULL);

    if (queue->size <= 0)
    {
        return NULL;
    }

    retObject = queue->head;

    queue->head = (NSConsumerQueueObject *)(retObject->next);
    if (!(queue->head))
    {
        queue->tail = NULL;
    }
    retObject->next = NULL;
    queue->size--;

    return retObject;
}
예제 #15
0
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;
}
예제 #16
0
NSCacheElement * NSPopProviderCacheList(NSCacheList * list)
{
    NS_VERIFY_NOT_NULL(list, NULL);

    pthread_mutex_t * mutex = NSGetCacheMutex();
    pthread_mutex_lock(mutex);

    NSCacheElement * head = list->head;
    if (head)
    {
        if (list->tail == head)
        {
            list->tail = NULL;
        }

        list->head = head->next;
        head->next = NULL;
    }

    pthread_mutex_unlock(mutex);
    return head;
}
예제 #17
0
NSResult NSConsumerCacheWriteProvider(NSCacheList * list, NSCacheElement * newObj)
{
    NS_VERIFY_NOT_NULL(list, NS_ERROR);
    NS_VERIFY_NOT_NULL(newObj, NS_ERROR);

    pthread_mutex_t * mutex = NSGetCacheMutex();

    NSProvider_internal * newProvObj = (NSProvider_internal *) newObj->data;

    NSCacheElement * it = NSConsumerStorageRead(list, newProvObj->providerId);

    pthread_mutex_lock(mutex);

    if (it)
    {
        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);
        }

        if (newProvObj->topicLL)
        {
            NSProvider_internal * provObj = (NSProvider_internal *) it->data;
            NSRemoveTopicLL(provObj->topicLL);
            provObj->topicLL = NSCopyTopicLL(newProvObj->topicLL);
        }

        pthread_mutex_unlock(mutex);

        return NS_OK;
    }

    NSCacheElement * obj = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));
    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(obj, NS_ERROR, pthread_mutex_unlock(mutex));

    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;
    }
    obj->next = NULL;

    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;
}
예제 #18
0
NSResult NSConsumerStorageDelete(NSCacheList * list, const char * delId)
{
    NS_VERIFY_NOT_NULL(list, NS_ERROR);
    NS_VERIFY_NOT_NULL(delId, NS_ERROR);

    NSCacheType type = list->cacheType;

    pthread_mutex_t * mutex = NSGetCacheMutex();
    pthread_mutex_lock(mutex);

    NSCacheElement * prev = list->head;
    NSCacheElement * del = list->head;
    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(del, NS_ERROR, pthread_mutex_unlock(mutex));

    if (NSConsumerCompareIdCacheData(type, del->data, delId))
    {
        if (del == list->head)
        {
            if (del == list->tail)
            {
                list->tail = del->next;
            }

            list->head = del->next;

            if (type == NS_CONSUMER_CACHE_PROVIDER)
            {
                NSRemoveProvider_internal((void *) del->data);
            }
            NSOICFree(del);
            pthread_mutex_unlock(mutex);

            return NS_OK;
        }
    }

    del = del->next;
    while (del)
    {
        if (NSConsumerCompareIdCacheData(type, del->data, delId))
        {
            if (del == list->tail)
            {
                list->tail = prev;
            }

            prev->next = del->next;

            if (type == NS_CONSUMER_CACHE_PROVIDER)
            {
                NSRemoveProvider_internal((NSProvider_internal *) del->data);
            }
            NSOICFree(del);
            pthread_mutex_unlock(mutex);

            return NS_OK;
        }

        prev = del;
        del = del->next;
    }
    pthread_mutex_unlock(mutex);
    return NS_OK;
}