From 952511c08da99fd809415a14f192eb9d52134f85 Mon Sep 17 00:00:00 2001 From: "jaesick.shin" Date: Mon, 10 Oct 2016 13:10:43 +0900 Subject: [PATCH] change asynccall Function to synccall function. this patch include, 1. change sync call about NSProviderSetConsumerTopic Function. 2. change sync call about NSProviderUnsetConsumerTopic Function. second patch include, modify invalid return value. Change-Id: Ia73d6544489dd36f4612c97108ca488f76e038e3 Signed-off-by: jaesick.shin Reviewed-on: https://gerrit.iotivity.org/gerrit/12991 Tested-by: jenkins-iotivity Reviewed-by: Uze Choi Tested-by: Uze Choi --- .../src/provider/NSProviderInterface.c | 28 +++++++++---- .../notification/src/provider/NSProviderTopic.c | 48 +++++++++++++++------- .../notification/src/provider/NSProviderTopic.h | 2 +- 3 files changed, 56 insertions(+), 22 deletions(-) diff --git a/service/notification/src/provider/NSProviderInterface.c b/service/notification/src/provider/NSProviderInterface.c index ea574a9..84c3809 100644 --- a/service/notification/src/provider/NSProviderInterface.c +++ b/service/notification/src/provider/NSProviderInterface.c @@ -346,7 +346,7 @@ NSResult NSProviderRegisterTopic(const char * topicName) } NSTopicSyncResult topicSyncResult; - topicSyncResult.topicName = OICStrdup(topicName); + topicSyncResult.topicData = (void *) OICStrdup(topicName); topicSyncResult.condition = &nstopicCond; topicSyncResult.result = NS_OK; topicSyncResult.mutex = &nsInitMutex; @@ -377,7 +377,7 @@ NSResult NSProviderUnregisterTopic(const char * topicName) } NSTopicSyncResult topicSyncResult; - topicSyncResult.topicName = OICStrdup(topicName); + topicSyncResult.topicData = (void *) OICStrdup(topicName); topicSyncResult.condition = &nstopicCond; topicSyncResult.result = NS_OK; topicSyncResult.mutex = &nsInitMutex; @@ -389,7 +389,7 @@ NSResult NSProviderUnregisterTopic(const char * topicName) pthread_mutex_unlock(&nsInitMutex); return NS_FAIL; } - OICFree(topicSyncResult.topicName); + OICFree(topicSyncResult.topicData); pthread_mutex_unlock(&nsInitMutex); NS_LOG(DEBUG, "NSProviderDeleteTopics - OUT"); @@ -417,11 +417,18 @@ NSResult NSProviderSetConsumerTopic(const char * consumerId, const char * topicN OICStrcpy(topicSubData->id, NS_UUID_STRING_SIZE, consumerId); topicSubData->topicName = OICStrdup(topicName); - NSPushQueue(TOPIC_SCHEDULER, TASK_SUBSCRIBE_TOPIC, (void *)topicSubData); + NSTopicSyncResult topicSyncResult; + topicSyncResult.topicData = (void *) topicSubData; + topicSyncResult.condition = &nstopicCond; + topicSyncResult.result = NS_FAIL; + topicSyncResult.mutex = &nsInitMutex; + + NSPushQueue(TOPIC_SCHEDULER, TASK_SUBSCRIBE_TOPIC, (void *)&topicSyncResult); + pthread_cond_wait(topicSyncResult.condition, &nsInitMutex); pthread_mutex_unlock(&nsInitMutex); NS_LOG(DEBUG, "NSProviderSelectTopics - OUT"); - return NS_OK; + return topicSyncResult.result; } NSResult NSProviderUnsetConsumerTopic(const char * consumerId, const char * topicName) @@ -445,9 +452,16 @@ NSResult NSProviderUnsetConsumerTopic(const char * consumerId, const char * topi OICStrcpy(topicSubData->id, NS_UUID_STRING_SIZE, consumerId); topicSubData->topicName = OICStrdup(topicName); - NSPushQueue(TOPIC_SCHEDULER, TASK_UNSUBSCRIBE_TOPIC, (void *)topicSubData); + NSTopicSyncResult topicSyncResult; + topicSyncResult.topicData = (void *) topicSubData; + topicSyncResult.condition = &nstopicCond; + topicSyncResult.result = NS_FAIL; + topicSyncResult.mutex = &nsInitMutex; + + NSPushQueue(TOPIC_SCHEDULER, TASK_UNSUBSCRIBE_TOPIC, (void *)&topicSyncResult); + pthread_cond_wait(topicSyncResult.condition, &nsInitMutex); pthread_mutex_unlock(&nsInitMutex); NS_LOG(DEBUG, "NSProviderUnselectTopics - OUT"); - return NS_OK; + return topicSyncResult.result; } diff --git a/service/notification/src/provider/NSProviderTopic.c b/service/notification/src/provider/NSProviderTopic.c index e398e65..3037467 100644 --- a/service/notification/src/provider/NSProviderTopic.c +++ b/service/notification/src/provider/NSProviderTopic.c @@ -444,31 +444,51 @@ void * NSTopicSchedule(void * ptr) case TASK_SUBSCRIBE_TOPIC: { NS_LOG(DEBUG, "CASE TASK_SUBSCRIBE_TOPIC : "); + NSTopicSyncResult * topicSyncResult = (NSTopicSyncResult *) node->taskData; + pthread_mutex_lock(topicSyncResult->mutex); NSCacheElement * newObj = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement)); - if (newObj) + NSCacheTopicSubData * subData = + (NSCacheTopicSubData *) topicSyncResult->topicData; + if (!newObj) + { + OICFree(subData->topicName); + OICFree(subData); + pthread_cond_signal(topicSyncResult->condition); + pthread_mutex_unlock(topicSyncResult->mutex); + } + else { - newObj->data = node->taskData; - newObj->next = NULL; - if (NSProviderStorageWrite(consumerTopicList, newObj) == NS_OK) + if (NSProviderStorageRead(registeredTopicList, subData->topicName)) { - NSCacheTopicSubData * topicSubData = - (NSCacheTopicSubData *) node->taskData; - NSSendTopicUpdationToConsumer(topicSubData->id); + newObj->data = topicSyncResult->topicData; + newObj->next = NULL; + NSProviderStorageWrite(consumerTopicList, newObj); + NSSendTopicUpdationToConsumer(subData->id); + topicSyncResult->result = NS_OK; } - } + } + pthread_cond_signal(topicSyncResult->condition); + pthread_mutex_unlock(topicSyncResult->mutex); + } break; case TASK_UNSUBSCRIBE_TOPIC: { - NS_LOG(DEBUG, "CASE TASK_SUBSCRIBE_TOPIC : "); - NSCacheTopicSubData * topicSubData = (NSCacheTopicSubData *) node->taskData; - if (NSProviderDeleteConsumerTopic(consumerTopicList, - (NSCacheTopicSubData *) node->taskData) == NS_OK) + NS_LOG(DEBUG, "CASE TASK_UNSUBSCRIBE_TOPIC : "); + NSTopicSyncResult * topicSyncResult = (NSTopicSyncResult *) node->taskData; + pthread_mutex_lock(topicSyncResult->mutex); + NSCacheTopicSubData * topicSubData = + (NSCacheTopicSubData *) topicSyncResult->topicData; + if (NSProviderDeleteConsumerTopic(consumerTopicList, topicSubData) == NS_OK) { NSSendTopicUpdationToConsumer(topicSubData->id); + topicSyncResult->result = NS_OK; } OICFree(topicSubData->topicName); OICFree(topicSubData); + pthread_cond_signal(topicSyncResult->condition); + pthread_mutex_unlock(topicSyncResult->mutex); + } break; case TASK_REGISTER_TOPIC: @@ -478,7 +498,7 @@ void * NSTopicSchedule(void * ptr) pthread_mutex_lock(topicSyncResult->mutex); topicSyncResult->result = NSRegisterTopic( - (const char *) topicSyncResult->topicName); + (const char *) topicSyncResult->topicData); pthread_cond_signal(topicSyncResult->condition); pthread_mutex_unlock(topicSyncResult->mutex); } @@ -489,7 +509,7 @@ void * NSTopicSchedule(void * ptr) NSTopicSyncResult * topicSyncResult = (NSTopicSyncResult *) node->taskData; pthread_mutex_lock(topicSyncResult->mutex); topicSyncResult->result = NSUnregisterTopic( - (const char *) topicSyncResult->topicName); + (const char *) topicSyncResult->topicData); pthread_cond_signal(topicSyncResult->condition); pthread_mutex_unlock(topicSyncResult->mutex); } diff --git a/service/notification/src/provider/NSProviderTopic.h b/service/notification/src/provider/NSProviderTopic.h index c087a96..f75a1f5 100644 --- a/service/notification/src/provider/NSProviderTopic.h +++ b/service/notification/src/provider/NSProviderTopic.h @@ -38,7 +38,7 @@ typedef struct { typedef struct { pthread_cond_t * condition; pthread_mutex_t * mutex; - char * topicName; + void * topicData; NSResult result; } NSTopicSyncResult; -- 2.7.4