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; }
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; }
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; }
NSCacheList * NSConsumerStorageCreate() { pthread_mutex_t * mutex = NSGetCacheMutex(); pthread_mutex_lock(mutex); NSCacheList * newList = (NSCacheList *) OICMalloc(sizeof(NSCacheList)); NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(newList, NULL, pthread_mutex_unlock(mutex)); newList->head = NULL; newList->tail = NULL; pthread_mutex_unlock(mutex); return newList; }
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; }
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; }