Modified Error handle of consumer. 53/8553/3
authorKIM JungYong <jyong2.kim@samsung.com>
Wed, 8 Jun 2016 04:39:52 +0000 (13:39 +0900)
committerUze Choi <uzchoi@samsung.com>
Wed, 8 Jun 2016 11:10:57 +0000 (11:10 +0000)
error handling of consumer was modified to macro functions.
TASK_CONSUMER_RECV_NOTIFICATION is renamed to MESSAGE.

POST callback for sendSyncInfo request is added.
it is bug for fail of sending POST msg since listener module is removed.

Change-Id: Ibad81bc7ee8e51a63feb640f55711a98fa022eda
Signed-off-by: KIM JungYong <jyong2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/8553
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
Tested-by: Uze Choi <uzchoi@samsung.com>
12 files changed:
service/notification/src/common/NSConstants.h
service/notification/src/consumer/NSConsumerCommon.c
service/notification/src/consumer/NSConsumerCommon.h
service/notification/src/consumer/NSConsumerCommunication.c
service/notification/src/consumer/NSConsumerDiscovery.c
service/notification/src/consumer/NSConsumerInterface.c
service/notification/src/consumer/NSConsumerInternalTaskController.c
service/notification/src/consumer/NSConsumerNetworkEventListener.c
service/notification/src/consumer/NSConsumerNetworkEventListener.h
service/notification/src/consumer/NSConsumerQueue.c
service/notification/src/consumer/NSConsumerQueueScheduler.c
service/notification/src/consumer/NSThread.c

index 40c6273..210e958 100644 (file)
@@ -112,7 +112,7 @@ typedef enum eTaskType
     TASK_CONSUMER_REQ_DISCOVER = 8001,
     TASK_CONSUMER_REQ_SUBSCRIBE = 8002,
     TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL = 8003,
-    TASK_CONSUMER_RECV_NOTIFICATION = 8101,
+    TASK_CONSUMER_RECV_MESSAGE = 8101,
 
     TASK_EVENT_CONNECTED = 9000,
     TASK_EVENT_DISCONNECTED = 9001,
index 7ad36f7..f5108f0 100644 (file)
@@ -67,11 +67,8 @@ void * NSDiscoveredProviderFunc(void * provider)
 
 void NSDiscoveredProvider(NSProvider * provider)
 {
-    if (!NSThreadInit(NSDiscoveredProviderFunc, (void *) provider))
-    {
-        NS_LOG(ERROR, "execute discovered provider callback fail");
-        return;
-    }
+    NSConsumerThread * thread = NSThreadInit(NSDiscoveredProviderFunc, (void *) provider);
+    NS_VERTIFY_NOT_NULL_V(thread);
 }
 
 NSSyncCallback * NSGetBoneNotificationSyncCb()
@@ -103,14 +100,12 @@ void * NSNotificationSyncFunc(void * obj)
 void NSNotificationSync(NSProvider * provider, NSSync * sync)
 {
     NSSyncData * obj = (NSSyncData *)OICMalloc(sizeof(NSSyncData));
+    NS_VERTIFY_NOT_NULL_V(obj);
     obj->provider = provider;
     obj->sync = sync;
 
-    if (!NSThreadInit(NSNotificationSyncFunc, (void *) obj))
-    {
-        NS_LOG(ERROR, "execute noti post callback fail");
-        return;
-    }
+    NSConsumerThread * thread = NSThreadInit(NSNotificationSyncFunc, (void *) obj);
+    NS_VERTIFY_NOT_NULL_V(thread);
 }
 
 NSNotificationReceivedCallback  * NSGetBoneNotificationPostedCb()
@@ -148,28 +143,18 @@ void * NSNotificationPostFunc(void * obj)
 void NSNotificationPost(NSProvider * provider, NSMessage * msg)
 {
     NSMessageData * obj = (NSMessageData *)OICMalloc(sizeof(NSMessageData));
-    if (!obj)
-    {
-        NS_LOG(ERROR, "NSMessageData allocation is failed");
-        return;
-    }
+    NS_VERTIFY_NOT_NULL_V(obj);
     obj->provider = provider;
     obj->msg = msg;
 
-    if (!NSThreadInit(NSNotificationPostFunc, (void *) obj))
-    {
-        NS_LOG(ERROR, "execute noti post callback fail");
-        return;
-    }
+    NSConsumerThread * thread = NSThreadInit(NSNotificationPostFunc, (void *) obj);
+    NS_VERTIFY_NOT_NULL_V(thread);
 }
 
 NSTask * NSMakeTask(NSTaskType type, void * data)
 {
     NSTask * retTask = OICMalloc(sizeof(NSTask));
-    if (!retTask)
-    {
-        return NULL;
-    }
+    NS_VERTIFY_NOT_NULL(retTask, NULL);
 
     retTask->taskType = type;
     retTask->taskData = data;
@@ -180,27 +165,19 @@ NSTask * NSMakeTask(NSTaskType type, void * data)
 
 NSMessage_consumer * NSCopyMessage(NSMessage_consumer * msg)
 {
-    if (!msg)
-    {
-        return NULL;
-    }
+    NS_VERTIFY_NOT_NULL(msg, NULL);
 
     NSMessage_consumer * newMsg = (NSMessage_consumer *)OICMalloc(sizeof(NSMessage_consumer));
-    if (!newMsg)
-    {
-        return NULL;
-    }
+    NS_VERTIFY_NOT_NULL(newMsg, NULL);
+
+    newMsg->addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
+    NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(newMsg, NULL, OICFree(newMsg));
+    memcpy(newMsg->addr, msg->addr, sizeof(OCDevAddr));
 
     newMsg->mId = OICStrdup(msg->mId);
     newMsg->mTitle = OICStrdup(msg->mTitle);
     newMsg->mContentText = OICStrdup(msg->mContentText);
     newMsg->mSource = OICStrdup(msg->mSource);
-    newMsg->addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
-    if (!newMsg->addr)
-    {
-        NS_LOG(ERROR, "OCDevAddr allocation is failed");
-    }
-    memcpy(newMsg->addr, msg->addr, sizeof(OCDevAddr));
 
     return newMsg;
 }
index 07ff1db..fd74633 100644 (file)
@@ -41,6 +41,12 @@ extern "C" {
 
 #define NS_DISCOVER_QUERY "/oic/res?rt=oic.r.notification"
 
+#define NS_VERTIFY_NOT_NULL_V(obj) { if ((obj) == NULL) \
+    { \
+        NS_LOG_V(ERROR, "%s : obj is null", __func__); \
+        return; \
+    }}
+
 #define NS_VERTIFY_NOT_NULL(obj, retVal) { if ((obj) == NULL) \
     { \
         NS_LOG_V(ERROR, "%s : obj is null", __func__); \
@@ -56,9 +62,26 @@ extern "C" {
         return (retVal); \
     }}
 
-#define NS_VERTIFY_STACK_OK(obj, retVal) { if ((obj) != OC_STACK_OK) \
+#define NS_VERTIFY_STACK_OK_V(obj) { OCStackResult _ret = (obj); \
+    if ( _ret != OC_STACK_OK) \
+    { \
+        NS_LOG_V(ERROR, "%s : obj is not OC_STACK_OK : %d", __func__, _ret); \
+        return; \
+    }}
+
+#define NS_VERTIFY_STACK_OK(obj, retVal) { OCStackResult _ret = (obj); \
+    if ( _ret != OC_STACK_OK) \
+    { \
+        NS_LOG_V(ERROR, "%s : obj is not OC_STACK_OK : %d", __func__, _ret); \
+        return (retVal); \
+    }}
+
+#define NS_VERTIFY_STACK_OK_WITH_POST_CLEANING(obj, retVal, func) { \
+    OCStackResult _ret = (obj); \
+    if ( _ret != OC_STACK_OK) \
     { \
-        NS_LOG_V(ERROR, "%s : obj is not OC_STACK_OK", __func__); \
+        NS_LOG_V(ERROR, "%s : obj is not OC_STACK_OK : %d", __func__, _ret); \
+        (func); \
         return (retVal); \
     }}
 
index 1c68409..661b329 100644 (file)
@@ -29,35 +29,46 @@ const char NS_MESSAGE_ACCEPTANCE[] = "0000-0000-0000-0000";
 
 NSMessage_consumer * NSGetNSMessage(OCClientResponse * clientResponse);
 NSSync * NSGetNSSyncInfo(OCClientResponse * clientResponse);
-
 NSProvider * NSGetNSProvider(OCClientResponse * clientResponse);
+OCRepPayload * NSGetPayloadofSyncInfo(NSMessage_consumer * message, int type);
+
+
 NSResult NSPushToCache(OCClientResponse * clientResponse, NSTaskType type);
 
+OCStackResult NSSendSyncInfo(NSMessage_consumer * message, int type);
+
 NSResult NSConsumerSubscribeProvider(NSProvider * provider)
 {
-    if (OC_STACK_OK != NSInvokeRequest(&(provider->messageHandle),
-            OC_REST_OBSERVE, (OCDevAddr *) provider->mUserData,
-            provider->messageUri, NULL, NSConsumerMessageListener, NULL))
-    {
-        return NS_ERROR;
-    }
+    OCStackResult ret = NSInvokeRequest(&(provider->messageHandle),
+                          OC_REST_OBSERVE, (OCDevAddr *) provider->mUserData,
+                          provider->messageUri, NULL, NSConsumerMessageListener, NULL);
+    NS_VERTIFY_STACK_OK(ret, NS_ERROR);
 
-    if (OC_STACK_OK != NSInvokeRequest(&(provider->syncHandle),
-            OC_REST_OBSERVE, (OCDevAddr *) provider->mUserData,
-            provider->syncUri, NULL, NSConsumerSyncInfoListener, NULL))
-    {
-        return NS_ERROR;
-    }
+    ret = NSInvokeRequest(&(provider->syncHandle),
+                          OC_REST_OBSERVE, (OCDevAddr *) provider->mUserData,
+                          provider->syncUri, NULL, NSConsumerSyncInfoListener, NULL);
+    NS_VERTIFY_STACK_OK(ret, NS_ERROR);
 
     return NS_OK;
 }
 
+OCStackApplicationResult NSConsumerPostResultCheck(
+        void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
+{
+    (void) ctx;
+    (void) handle;
+
+    NS_VERTIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
+    NS_VERTIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
+
+    return OC_STACK_KEEP_TRANSACTION;
+}
+
 NSResult NSConsumerPostProvider(OCDevAddr * addr, OCPayload * payload, const char * uri)
 {
-    if (OC_STACK_OK != NSInvokeRequest(NULL, OC_REST_POST, addr, uri, payload, NULL, NULL))
-    {
-        return NS_ERROR;
-    }
+    OCStackResult ret = NSInvokeRequest(NULL, OC_REST_POST, addr, uri, payload,
+                                        NSConsumerPostResultCheck, NULL);
+    NS_VERTIFY_STACK_OK(ret, NS_ERROR);
 
     return NS_OK;
 }
@@ -72,18 +83,13 @@ OCStackApplicationResult NSConsumerSyncInfoListener(
     NS_VERTIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
 
     NSSync * newNoti = NULL;
+
+    NS_LOG(ERROR, "get provider");
     NSProvider * provider = NSGetNSProvider(clientResponse);
-    if (!provider)
-    {
-        NS_LOG(ERROR, "getting provider is failed");
-        return OC_STACK_KEEP_TRANSACTION;
-    }
+    NS_VERTIFY_NOT_NULL(provider, OC_STACK_KEEP_TRANSACTION);
 
     newNoti = NSGetNSSyncInfo(clientResponse);
-    if (!newNoti)
-    {
-        return OC_STACK_KEEP_TRANSACTION;
-    }
+    NS_VERTIFY_NOT_NULL(newNoti, OC_STACK_KEEP_TRANSACTION);
 
     NSTaskType taskType = TASK_RECV_READ;
 
@@ -99,10 +105,8 @@ OCStackApplicationResult NSConsumerSyncInfoListener(
 
     NSNotificationSync(provider, newNoti);
 
-    if (NS_OK != NSPushToCache(clientResponse, taskType))
-    {
-        return OC_STACK_KEEP_TRANSACTION;
-    }
+    NSResult ret = NSPushToCache(clientResponse, taskType);
+    NS_VERTIFY_NOT_NULL(ret == NS_OK ? (void *)1 : NULL, OC_STACK_KEEP_TRANSACTION);
 
     return OC_STACK_KEEP_TRANSACTION;
 }
@@ -116,21 +120,18 @@ OCStackApplicationResult NSConsumerMessageListener(
     NS_VERTIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
     NS_VERTIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
 
+    NS_LOG(DEBUG, "build NSProvider");
     NSProvider * provider = NSGetNSProvider(clientResponse);
-    if (!provider)
-    {
-        NS_LOG(ERROR, "getting provider is failed");
-        return OC_STACK_KEEP_TRANSACTION;
-    }
+    NS_VERTIFY_NOT_NULL(provider, OC_STACK_KEEP_TRANSACTION);
 
+    NS_LOG(DEBUG, "build NSMessage");
     NSMessage_consumer * newNoti = NSGetNSMessage(clientResponse);
-    if (!newNoti)
-    {
-        return OC_STACK_KEEP_TRANSACTION;
-    }
+    NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(
+            newNoti, OC_STACK_KEEP_TRANSACTION, OICFree(provider));
 
     if (!strcmp(newNoti->mId, NS_MESSAGE_ACCEPTANCE))
     {
+        // TODO update provider list.
         NS_LOG(DEBUG, "Receive Subscribe confirm");
         OICFree(provider->mUserData);
         OICFree(provider);
@@ -138,16 +139,11 @@ OCStackApplicationResult NSConsumerMessageListener(
         return OC_STACK_KEEP_TRANSACTION;
     }
 
-    NSTaskType taskType = TASK_CONSUMER_RECV_NOTIFICATION;
-
     NS_LOG(DEBUG, "newNoti->type == Notification");
     NSNotificationPost(provider, (NSMessage *) newNoti);
 
-    if (NS_OK != NSPushToCache(clientResponse, taskType))
-    {
-        NSRemoveMessage(newNoti);
-        return OC_STACK_KEEP_TRANSACTION;
-    }
+    NSResult ret = NSPushToCache(clientResponse, TASK_CONSUMER_RECV_MESSAGE);
+    NS_VERTIFY_NOT_NULL(ret == NS_OK ? (void *)1 : NULL, OC_STACK_KEEP_TRANSACTION);
 
     return OC_STACK_KEEP_TRANSACTION;
 }
@@ -155,17 +151,13 @@ OCStackApplicationResult NSConsumerMessageListener(
 NSResult NSPushToCache(OCClientResponse * clientResponse, NSTaskType type)
 {
     NSMessage_consumer * cachedNoti = NSGetNSMessage(clientResponse);
-    if (!cachedNoti)
-    {
-        return NS_ERROR;
-    }
+    NS_LOG(DEBUG, "build NSMessage");
+    NS_VERTIFY_NOT_NULL(cachedNoti, NS_ERROR);
+
+    NS_LOG(DEBUG, "build NSTask");
     NSTask * task = NSMakeTask(type, (void *) cachedNoti);
-    if (!task)
-    {
-        NS_LOG(ERROR, "NSTask allocation fail");
-        NSRemoveMessage(cachedNoti);
-        return NS_ERROR;
-    }
+    NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(task, NS_ERROR, NSRemoveMessage(cachedNoti));
+
     NSConsumerPushEvent(task);
 
     return NS_OK;
@@ -173,30 +165,23 @@ NSResult NSPushToCache(OCClientResponse * clientResponse, NSTaskType type)
 
 NSMessage_consumer * NSGetNSMessage(OCClientResponse * clientResponse)
 {
-    if(!clientResponse->payload)
-    {
-        return NULL;
-    }
+    NS_VERTIFY_NOT_NULL(clientResponse->payload, NULL);
+
+    NS_LOG(DEBUG, "get id");
+    OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
+    char * id = NULL;
+    bool getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_ID, &id);
+    NS_VERTIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
 
+    NS_LOG(DEBUG, "create NSMessage");
     NSMessage_consumer * retNoti = (NSMessage_consumer *)OICMalloc(sizeof(NSMessage_consumer));
-    if (!retNoti)
-    {
-        return NULL;
-    }
+    NS_VERTIFY_NOT_NULL(retNoti, NULL);
 
-    retNoti->mId = NULL;
+    retNoti->mId = id;
     retNoti->mTitle = NULL;
     retNoti->mContentText = NULL;
     retNoti->mSource = NULL;
 
-    OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
-    if (!OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_ID, &retNoti->mId))
-    {
-        NS_LOG(ERROR, "id of received notification is null");
-        OICFree(retNoti);
-        return NULL;
-    }
-
     OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_TITLE, &retNoti->mTitle);
     OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_TEXT, &retNoti->mContentText);
     OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_SOURCE, &retNoti->mSource);
@@ -207,7 +192,9 @@ NSMessage_consumer * NSGetNSMessage(OCClientResponse * clientResponse)
     NS_LOG_V(DEBUG, "Msg Content : %s", retNoti->mContentText);
     NS_LOG_V(DEBUG, "Msg Source : %s", retNoti->mSource);
 
+    NS_LOG(DEBUG, "copy target address");
     retNoti->addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
+    NS_VERTIFY_NOT_NULL(retNoti->addr, NULL);
     memcpy(retNoti->addr, clientResponse->addr, sizeof(OCDevAddr));
 
     retNoti->type = Notification;
@@ -217,35 +204,25 @@ NSMessage_consumer * NSGetNSMessage(OCClientResponse * clientResponse)
 
 NSSync * NSGetNSSyncInfo(OCClientResponse * clientResponse)
 {
-    if(!clientResponse->payload)
-    {
-        return NULL;
-    }
-    NSSync * retSync = (NSSync *)OICMalloc(sizeof(NSSync));
-    if (!retSync)
-    {
-        return NULL;
-    }
-
-    retSync->mMessageId = NULL;
-    retSync->mState = Notification_Read;
+    NS_VERTIFY_NOT_NULL(clientResponse->payload, NULL);
 
     OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
-    if (!OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_ID, &retSync->mMessageId))
-    {
-        NS_LOG(ERROR, "id of received sync is null");
-        OICFree(retSync);
-        return NULL;
-    }
-    int64_t state;
-    if (!OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_STATE, & state))
-    {
-        NS_LOG(ERROR, "state of received sync is null");
-        OICFree(retSync->mMessageId);
-        OICFree(retSync);
-        return NULL;
-    }
 
+    NS_LOG(DEBUG, "get state");
+    int64_t state = 0;
+    bool getResult = OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_STATE, & state);
+    NS_VERTIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
+
+    NS_LOG(DEBUG, "get id");
+    char * id = NULL;
+    getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_ID, &id);
+    NS_VERTIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
+
+    NS_LOG(DEBUG, "create NSSyncInfo");
+    NSSync * retSync = (NSSync *)OICMalloc(sizeof(NSSync));
+    NS_VERTIFY_NOT_NULL(retSync, NULL);
+
+    retSync->mMessageId = id;
     retSync->mState = (NSSyncTypes) state;
 
     NS_LOG_V(DEBUG, "Sync ID : %s", retSync->mMessageId);
@@ -256,22 +233,15 @@ NSSync * NSGetNSSyncInfo(OCClientResponse * clientResponse)
 
 NSProvider * NSGetNSProvider(OCClientResponse * clientResponse)
 {
+    NS_LOG(DEBUG, "create NSProvider");
     NSProvider * newProvider = (NSProvider *)OICMalloc(sizeof(NSProvider));
-    if (!newProvider)
-    {
-        NS_LOG(DEBUG, "NSProvider allocation fail");
-        return NULL;
-    }
+    NS_VERTIFY_NOT_NULL(newProvider, NULL);
 
     // TODO set id
     newProvider->mId = NULL;
     newProvider->mUserData = (void *)OICMalloc(sizeof(OCDevAddr));
-    if (!newProvider->mUserData)
-    {
-        NS_LOG(DEBUG, "OCDevAddr allocation fail");
-        OICFree(newProvider);
-        return NULL;
-    }
+    NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(newProvider, NULL, OICFree(newProvider));
+
     memcpy(newProvider->mUserData, clientResponse->addr, sizeof(OCDevAddr));
 
     return newProvider;
@@ -279,49 +249,23 @@ NSProvider * NSGetNSProvider(OCClientResponse * clientResponse)
 
 void NSConsumerNotificationTaskProcessing(NSTask * task)
 {
-    if (!task)
-    {
-        NS_LOG(ERROR, "task is null");
-        return;
-    }
+    NS_VERTIFY_NOT_NULL_V(task);
 
     NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
     if (task->taskType == TASK_CONSUMER_REQ_SUBSCRIBE)
     {
-        if (NS_OK != NSConsumerSubscribeProvider((NSProvider *)task->taskData))
-        {
-            NS_LOG(ERROR, "Subscribe fail");
-            return;
-        }
+        NS_LOG(DEBUG, "Request Subscribe");
+        NSResult ret = NSConsumerSubscribeProvider((NSProvider *)task->taskData);
+        NS_VERTIFY_NOT_NULL_V(ret == NS_OK ? (void *)1 : NULL);
     }
     else if (task->taskType == TASK_SEND_READ || task->taskType == TASK_SEND_DISMISS)
     {
-        NSMessage_consumer * nsConsumer = (NSMessage_consumer *) task->taskData;
-        if (!nsConsumer)
-        {
-            NS_LOG(ERROR, "taskData is NULL");
-            return;
-        }
-
-        OCRepPayload * payload = OCRepPayloadCreate ();
-        if (!payload)
-        {
-            NS_LOG(ERROR, "Failed to create POST payload object");
-            return;
-        }
-
-        int type = (task->taskType == TASK_SEND_READ) ? 0 : 1;
-        OCRepPayloadSetPropString(payload, "ID", (char *) nsConsumer->mId);
-        OCRepPayloadSetPropString(payload, "SOURCE", (char *) nsConsumer->mSource);
-        OCRepPayloadSetPropInt(payload, "STATE", type);
-
-        // TODO fix param for uri
-        if (NS_OK != NSConsumerPostProvider(
-                (OCDevAddr *) nsConsumer->addr, (OCPayload *) payload, "/notification/sync"))
-        {
-            NS_LOG(ERROR, "Subscribe fail");
-            return;
-        }
+        NSMessage_consumer * message = (NSMessage_consumer *) task->taskData;
+        NS_VERTIFY_NOT_NULL_V(message);
+
+        OCStackResult ret = NSSendSyncInfo(message, (task->taskType == TASK_SEND_READ) ? 0 : 1);
+        NS_VERTIFY_STACK_OK_V(ret);
+
     }
     else if (task->taskType == TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL)
     {
@@ -335,3 +279,28 @@ void NSConsumerNotificationTaskProcessing(NSTask * task)
         NS_LOG(ERROR, "Unknown type message");
     }
 }
+
+OCRepPayload * NSGetPayloadofSyncInfo(NSMessage_consumer * message, int type)
+{
+    OCRepPayload * payload = OCRepPayloadCreate();
+    NS_VERTIFY_NOT_NULL(payload, NULL);
+
+    OCRepPayloadSetPropString(payload, "ID", (char *) message->mId);
+    OCRepPayloadSetPropInt(payload, "STATE", type);
+    if (message->mSource)
+    {
+        OCRepPayloadSetPropString(payload, "SOURCE", (char *) message->mSource);
+    }
+
+    return payload;
+}
+
+OCStackResult NSSendSyncInfo(NSMessage_consumer * message, int type)
+{
+    OCRepPayload * payload = NSGetPayloadofSyncInfo(message, type);
+    NS_VERTIFY_NOT_NULL(payload, OC_STACK_ERROR);
+
+    return NSInvokeRequest(NULL, OC_REST_POST, message->addr,
+                           "/notification/sync", (OCPayload*)payload,
+                           NSConsumerPostResultCheck, NULL);
+}
index ee90226..324454e 100644 (file)
 #define NS_PRESENCE_SUBSCRIBE_QUERY "coap://224.0.1.187:5683/oic/ad?rt=oic.r.notification"
 #define NS_GET_INFORMATION_QUERY "/notification?if=oic.if.notification"
 
-OCDoHandle * getPresenceHandle()
-{
-    static OCDoHandle * g_PresenceHandle = NULL;
-
-    return g_PresenceHandle;
-}
-
 OCStackApplicationResult NSConsumerPresenceListener(
         void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
 {
@@ -128,6 +121,8 @@ OCStackApplicationResult NSIntrospectProvider(
     NS_VERTIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
 
     int64_t accepter = 0;
+    char * messageUri = NULL;
+    char * syncUri = NULL;
 
     NS_LOG_V(DEBUG, "GET response income : %s:%d",
             clientResponse->devAddr.addr, clientResponse->devAddr.port);
@@ -143,65 +138,45 @@ OCStackApplicationResult NSIntrospectProvider(
         return OC_STACK_DELETE_TRANSACTION;
     }
 
-    if (!clientResponse->payload)
-    {
-        NS_LOG(ERROR, "payload is null");
-        return OC_STACK_KEEP_TRANSACTION;
-    }
+    NS_VERTIFY_NOT_NULL(clientResponse->payload, OC_STACK_KEEP_TRANSACTION);
 
-    if (!OCRepPayloadGetPropInt((OCRepPayload *)clientResponse->payload,
-            NS_PAYLOAD_KEY_ACCEPTER, & accepter))
+    OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
+    while (payload)
     {
-        NS_LOG(ERROR, "can not seach for accepter");
-        return OC_STACK_KEEP_TRANSACTION;
+        NS_LOG_V(DEBUG, "Payload Key : %s", payload->values->name);
+        payload = payload->next;
     }
 
+    NS_LOG(DEBUG, "getting information of accepter");
+    bool getResult = OCRepPayloadGetPropInt((OCRepPayload *)clientResponse->payload,
+                                            NS_PAYLOAD_KEY_ACCEPTER, & accepter);
+    NS_VERTIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, OC_STACK_KEEP_TRANSACTION);
+
+    NS_LOG(DEBUG, "getting message URI");
+    getResult = OCRepPayloadGetPropString(
+            (OCRepPayload *)clientResponse->payload,
+            "MESSAGE_URI", & messageUri);
+    NS_VERTIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, OC_STACK_KEEP_TRANSACTION);
+
+    NS_LOG(DEBUG, "getting sync URI");
+    getResult = OCRepPayloadGetPropString((OCRepPayload *)clientResponse->payload,
+                NS_ATTRIBUTE_SYNC, & syncUri);
+    NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(getResult == true ? (void *) 1 : NULL,
+            OC_STACK_KEEP_TRANSACTION, OICFree(messageUri));
+
     NSProvider * newProvider = (NSProvider *)OICMalloc(sizeof(NSProvider));
-    if (!newProvider)
-    {
-        NS_LOG(DEBUG, "NSProvider allocation fail");
-        return OC_STACK_KEEP_TRANSACTION;
-    }
+    NS_VERTIFY_NOT_NULL(newProvider, OC_STACK_KEEP_TRANSACTION);
 
     // TODO set id
     newProvider->mId = NULL;
+    newProvider->messageUri = messageUri;
+    newProvider->syncUri = syncUri;
+
     newProvider->mUserData = (void *)OICMalloc(sizeof(OCDevAddr));
-    if (!newProvider)
-    {
-        NS_LOG(DEBUG, "OCDevAddr allocation fail");
-        OICFree(newProvider);
-        return OC_STACK_KEEP_TRANSACTION;
-    }
+    NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(newProvider->mUserData,
+                   OC_STACK_KEEP_TRANSACTION, OICFree(newProvider));
     memcpy(newProvider->mUserData, clientResponse->addr, sizeof(OCDevAddr));
 
-    {
-        OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
-        while (payload)
-        {
-            NS_LOG_V(DEBUG, "Payload Key : %s", payload->values->name);
-            payload = payload->next;
-        }
-    }
-
-    if (!OCRepPayloadGetPropString((OCRepPayload *)clientResponse->payload,
-            "MESSAGE_URI", & newProvider->messageUri))
-    {
-        OICFree(newProvider->mUserData);
-        OICFree(newProvider);
-        NS_LOG(ERROR, "can not seach for message uri");
-        return OC_STACK_KEEP_TRANSACTION;
-    }
-
-    if (!OCRepPayloadGetPropString((OCRepPayload *)clientResponse->payload,
-            NS_ATTRIBUTE_SYNC, & newProvider->syncUri))
-    {
-        OICFree(newProvider->messageUri);
-        OICFree(newProvider->mUserData);
-        OICFree(newProvider);
-        NS_LOG(ERROR, "can not seach for sync uri");
-        return OC_STACK_KEEP_TRANSACTION;
-    }
-
     if (accepter == NS_ACCEPTER_CONSUMER)
     {
         NS_LOG(DEBUG, "accepter is NS_ACCEPTER_CONSUMER, Callback to user");
@@ -213,11 +188,7 @@ OCStackApplicationResult NSIntrospectProvider(
         NS_LOG(DEBUG, "accepter is NS_ACCEPTER_PROVIDER, request subscribe");
 
         NSTask * task = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE, (void *) newProvider);
-        if (!task)
-        {
-            NS_LOG(DEBUG, "NSTask allocation fail");
-            return OC_STACK_KEEP_TRANSACTION;
-        }
+        NS_VERTIFY_NOT_NULL(task, OC_STACK_KEEP_TRANSACTION);
 
         NSConsumerPushEvent(task);
     }
@@ -227,11 +198,7 @@ OCStackApplicationResult NSIntrospectProvider(
 
 void NSConsumerDiscoveryTaskProcessing(NSTask * task)
 {
-    if (!task)
-    {
-        NS_LOG(ERROR, "task is null");
-        return;
-    }
+    NS_VERTIFY_NOT_NULL_V(task);
 
     NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
     if (task->taskType == TASK_EVENT_CONNECTED || task->taskType == TASK_CONSUMER_REQ_DISCOVER)
index abac627..16d2e67 100644 (file)
@@ -35,40 +35,21 @@ NSResult NSStartConsumer(
         NSNotificationReceivedCallback  postCb,
         NSSyncCallback syncCb)
 {
-    if (NSIsStartedConsumer())
-    {
-        NS_LOG(DEBUG, "is already started consumer service?");
-        return NS_OK;
-    }
-
-    if (!discoverCb)
-    {
-        NS_LOG(ERROR, "discoverCb is NULL");
-        return NS_ERROR;
-    }
-
-    if (!postCb)
-    {
-        NS_LOG(ERROR, "postCb is NULL");
-        return NS_ERROR;
-    }
+    bool isStartedConsumer = NSIsStartedConsumer();
+    NS_VERTIFY_NOT_NULL(isStartedConsumer == false ? (void *) 1 : NULL, NS_OK);
 
-    if (!syncCb)
-    {
-        NS_LOG(ERROR, "syncCb is NULL");
-        return NS_ERROR;
-    }
+    NS_VERTIFY_NOT_NULL(discoverCb, NS_ERROR);
+    NS_VERTIFY_NOT_NULL(postCb, NS_ERROR);
+    NS_VERTIFY_NOT_NULL(syncCb, NS_ERROR);
 
     NSSetDiscoverProviderCb(discoverCb);
     NSSetNotificationPostedCb(postCb);
     NSSetNotificationSyncCb(syncCb);
     NSSetIsStartedConsumer(true);
 
-    if (NS_OK != NSConsumerMessageHandlerInit())
-    {
-        NSStopConsumer();
-        return NS_ERROR;
-    }
+    NSResult ret = NSConsumerMessageHandlerInit();
+    NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(ret == NS_OK ? (void *) 1 : NULL,
+            NS_ERROR, NSStopConsumer());
 
     return NS_OK;
 }
@@ -88,11 +69,7 @@ NSResult NSStopConsumer()
 NSResult NSSubscribe(NSProvider * provider)
 {
     NSTask * subscribeTask = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE, (void *) provider);
-    if (!subscribeTask)
-    {
-        NS_LOG(ERROR, "NSTask allocation fail");
-        return NS_ERROR;
-    }
+    NS_VERTIFY_NOT_NULL(subscribeTask, NS_ERROR);
 
     return NSConsumerPushEvent(subscribeTask);
 }
@@ -100,11 +77,7 @@ NSResult NSSubscribe(NSProvider * provider)
 NSResult NSUnsubscribe(NSProvider * provider)
 {
     NSTask * unsubscribeTask = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL, (void *) provider);
-    if (!unsubscribeTask)
-    {
-        NS_LOG(ERROR, "NSTask allocation fail");
-        return NS_ERROR;
-    }
+    NS_VERTIFY_NOT_NULL(unsubscribeTask, NS_ERROR);
 
     return NSConsumerPushEvent(unsubscribeTask);
 }
@@ -112,11 +85,7 @@ NSResult NSUnsubscribe(NSProvider * provider)
 NSResult NSRescanProvider()
 {
     NSTask * discoverTask = NSMakeTask(TASK_CONSUMER_REQ_DISCOVER, NULL);
-    if (!discoverTask)
-    {
-        NS_LOG(ERROR, "NSTask allocation fail");
-        return NS_ERROR;
-    }
+    NS_VERTIFY_NOT_NULL(discoverTask, NS_ERROR);
 
     return NSConsumerPushEvent(discoverTask);
 }
@@ -124,11 +93,7 @@ NSResult NSRescanProvider()
 NSResult NSConsumerReadCheck(NSMessage * obj)
 {
     NSTask * readTask = NSMakeTask(TASK_SEND_READ, (void *) obj);
-    if (!readTask)
-    {
-        NS_LOG(ERROR, "NSTask allocation fail");
-        return NS_ERROR;
-    }
+    NS_VERTIFY_NOT_NULL(readTask, NS_ERROR);
 
     return NSConsumerPushEvent(readTask);
 }
@@ -136,21 +101,14 @@ NSResult NSConsumerReadCheck(NSMessage * obj)
 NSResult NSConsumerDismissCheck(NSMessage * obj)
 {
     NSTask * dismissTask = NSMakeTask(TASK_SEND_DISMISS, (void *) obj);
-    if (!dismissTask)
-    {
-        NS_LOG(ERROR, "NSTask allocation fail");
-        return NS_ERROR;
-    }
+    NS_VERTIFY_NOT_NULL(dismissTask, NS_ERROR);
 
     return NSConsumerPushEvent(dismissTask);
 }
 
 NSResult NSDropNSObject(NSMessage * obj)
 {
-    if (!obj)
-    {
-        return NS_ERROR;
-    }
+    NS_VERTIFY_NOT_NULL(obj, NS_ERROR);
 
     if (obj->mId)
     {
index 10c709b..1cb8038 100644 (file)
@@ -40,7 +40,7 @@ void NSDestroyCacheList()
 {
     NSCacheList * cache;
     cache = *(NSGetCacheList());
-    if (!cache)
+    if (cache)
     {
         NSStorageDestroy(cache);
     }
@@ -52,18 +52,15 @@ NSResult NSCacheUpdate(NSCacheList * cache, NSTask * task, NSConsumerMessageType
     noti->type = type;
 
     NSCacheElement * obj = (NSCacheElement *)OICMalloc(sizeof(NSCacheElement));
-    if (!obj)
-    {
-        NS_LOG(ERROR, "Cache allocation is failed");
-    }
+    NS_VERTIFY_NOT_NULL(obj, NS_ERROR);
+
     obj->data = (NSCacheData *) noti;
     obj->next = NULL;
 
-    if (NS_OK != NSStorageWrite(cache, obj))
-    {
-        NS_LOG(ERROR, "Cache write fail");
-        return NS_ERROR;
-    }
+    NS_LOG(DEBUG, "try to write to storage");
+    NSResult ret = NSStorageWrite(cache, obj);
+    NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(ret == NS_OK ? (void *) 1 : NULL,
+            NS_ERROR, NSRemoveMessage(noti));
 
     NSRemoveMessage(noti);
     OICFree(obj);
@@ -73,61 +70,46 @@ NSResult NSCacheUpdate(NSCacheList * cache, NSTask * task, NSConsumerMessageType
 
 void NSConsumerSubscriptionTaskProcessing(NSTask * task)
 {
-    if (!task)
-    {
-        NS_LOG(ERROR, "task is null");
-        return;
-    }
+    NS_VERTIFY_NOT_NULL_V(task);
 
     NSCacheList * cache;
     if (!*(NSGetCacheList()))
     {
         NS_LOG(DEBUG, "Cache Init");
         cache = NSStorageCreate();
+        NS_VERTIFY_NOT_NULL_V(cache);
+
         cache->cacheType = NS_CONSUMER_CACHE_MESSAGE;
-        if (!cache)
-        {
-            NS_LOG(ERROR, "Cache create fail");
-            return;
-        }
         NSSetCacheList(cache);
     }
     cache = *(NSGetCacheList());
 
+    NSResult ret = NS_ERROR;
     NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
-
     switch (task->taskType)
     {
-        case TASK_CONSUMER_RECV_NOTIFICATION:
+        case TASK_CONSUMER_RECV_MESSAGE:
         {
             NS_LOG(DEBUG, "Receive New Notification");
 
-            if (NS_OK != NSCacheUpdate(cache, task, Notification))
-            {
-                NS_LOG(ERROR, "Cache Update fail");
-                return;
-            }
+            ret = NSCacheUpdate(cache, task, Notification);
+            NS_VERTIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
             break;
         }
         case TASK_RECV_READ:
         {
             NS_LOG(DEBUG, "Receive Read Notification");
 
-            if (NS_OK != NSCacheUpdate(cache, task, Read))
-            {
-                NS_LOG(ERROR, "Cache Update fail");
-                return;
-            }
+            ret = NSCacheUpdate(cache, task, Read);
+            NS_VERTIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
             break;
         }
         case TASK_RECV_DISMISS:
         {
             NS_LOG(DEBUG, "Receive Dismiss Notification");
-            if (NS_OK != NSCacheUpdate(cache, task, Dismiss))
-            {
-                NS_LOG(ERROR, "Cache Update fail");
-                return;
-            }
+
+            ret = NSCacheUpdate(cache, task, Dismiss);
+            NS_VERTIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
             break;
         }
         default :
index 560ad84..1dd6764 100644 (file)
@@ -35,6 +35,14 @@ void NSConnectionStateListener(CATransportAdapter_t adapter,
 
 void NSAdapterStateListener(CATransportAdapter_t adapter, bool enabled);
 
+OCDoHandle * getPresenceHandle()
+{
+    static OCDoHandle g_PresenceHandle = NULL;
+
+    return & g_PresenceHandle;
+}
+
+
 NSResult NSConsumerListenerInit()
 {
     // TODO replace with comment lines when enable network monitor of IP Adapter
@@ -45,23 +53,25 @@ NSResult NSConsumerListenerInit()
 //        return NS_ERROR;
 //    }
 
-    if (OC_STACK_OK != NSInvokeRequest(NULL, OC_REST_PRESENCE, NULL,
-            NS_PRESENCE_SUBSCRIBE_QUERY, NULL, NSConsumerPresenceListener, NULL))
-    {
-        NS_LOG(ERROR, "Presence request fail");
-        return NS_ERROR;
-    }
+    NS_LOG(DEBUG, "Request to subscribe presence");
+    OCStackResult stackResult = NSInvokeRequest(getPresenceHandle(), OC_REST_PRESENCE, NULL,
+                        NS_PRESENCE_SUBSCRIBE_QUERY, NULL, NSConsumerPresenceListener, NULL);
+    NS_VERTIFY_STACK_OK(stackResult, NS_ERROR);
 
-    if (OC_STACK_OK != NSInvokeRequest(NULL, OC_REST_DISCOVER, NULL,
-            NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener, NULL))
-    {
-        NS_LOG(ERROR, "Discover request fail");
-        return NS_ERROR;
-    }
+    NS_LOG(DEBUG, "Request to discover provider");
+    stackResult = NSInvokeRequest(NULL, OC_REST_DISCOVER, NULL,
+                      NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener, NULL);
+    NS_VERTIFY_STACK_OK(stackResult, NS_ERROR);
 
     return NS_OK;
 }
 
+void NSConsumerListenerTermiate()
+{
+    CARegisterNetworkMonitorHandler(NULL, NULL);
+    OCCancel(*getPresenceHandle(), NS_QOS, NULL, 0);
+}
+
 void NSConnectionStateListener(CATransportAdapter_t adapter,
         const char *remote_address, bool connected)
 {
@@ -77,11 +87,8 @@ void NSConnectionStateListener(CATransportAdapter_t adapter,
         NS_LOG(DEBUG, "try to discover notification provider.");
 
         NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED, NULL);
-        if (!task)
-        {
-            NS_LOG(ERROR, "NSTask allocation fail.");
-            return;
-        }
+        NS_VERTIFY_NOT_NULL_V(task);
+
         NSConsumerPushEvent(task);
     }
 }
@@ -98,11 +105,8 @@ void NSAdapterStateListener(CATransportAdapter_t adapter, bool enabled)
         NS_LOG(DEBUG, "try to discover notification provider.");
 
         NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED, NULL);
-        if (!task)
-        {
-            NS_LOG(ERROR, "NSTask allocation fail.");
-            return;
-        }
+        NS_VERTIFY_NOT_NULL_V(task);
+
         NSConsumerPushEvent(task);
     }
 }
index ecf1841..fb1f55a 100644 (file)
@@ -33,6 +33,8 @@ extern "C" {
 
 NSResult NSConsumerListenerInit();
 
+void NSConsumerListenerTermiate();
+
 #ifdef __cplusplus
 }
 #endif // __cplusplus
index a2c9b53..d31d996 100644 (file)
 NSConsumerQueue * NSCreateQueue()
 {
     NSConsumerQueue * newQueue = (NSConsumerQueue *)OICMalloc(sizeof(NSConsumerQueue));
-    if (!newQueue)
-    {
-        return NULL;
-    }
+    NS_VERTIFY_NOT_NULL(newQueue, NULL);
 
     newQueue->size = 0;
     newQueue->head = NULL;
@@ -54,15 +51,8 @@ void NSDestroyQueue(NSConsumerQueue * queue)
 
 bool NSPushQueue(NSConsumerQueue * queue, NSConsumerQueueObject * object)
 {
-    if (!queue)
-    {
-        return false;
-    }
-
-    if (!object)
-    {
-        NS_LOG(ERROR, "object is null. can not insert to queue");
-    }
+    NS_VERTIFY_NOT_NULL(queue, false);
+    NS_VERTIFY_NOT_NULL(object, false);
 
     if (!(queue->head))
     {
@@ -83,21 +73,14 @@ NSConsumerQueueObject * NSPopQueue(NSConsumerQueue * queue)
 {
     NSConsumerQueueObject * retObject = NULL;
 
-    if (!queue)
-    {
-        return NULL;
-    }
+    NS_VERTIFY_NOT_NULL(queue, NULL);
+    NS_VERTIFY_NOT_NULL(queue->head, NULL);
 
     if (queue->size <= 0)
     {
         return NULL;
     }
 
-    if (!(queue->head))
-    {
-        return NULL;
-    }
-
     retObject = queue->head;
 
     queue->head = (NSConsumerQueueObject *)(retObject->next);
index 30d0d46..95ac81b 100644 (file)
@@ -71,32 +71,22 @@ NSResult NSConsumerMessageHandlerInit()
     NSConsumerThread * handle = NULL;
     NSConsumerQueue * queue = NULL;
 
-    if (NS_OK != NSConsumerListenerInit())
-    {
-        NS_LOG(ERROR, "listener init fail");
-        return NS_ERROR;
-    }
+    NS_LOG(DEBUG, "listener init");
+    NSResult ret = NSConsumerListenerInit();
+    NS_VERTIFY_NOT_NULL(ret == NS_OK ? (void *) 1 : NULL, NS_ERROR);
 
-    if (NS_OK != NSConsumerSystemInit())
-    {
-        NS_LOG(ERROR, "system init fail");
-        return NS_ERROR;
-    }
+    NS_LOG(DEBUG, "system init");
+    ret = NSConsumerSystemInit();
+    NS_VERTIFY_NOT_NULL(ret == NS_OK ? (void *) 1 : NULL, NS_ERROR);
 
+    NS_LOG(DEBUG, "queue thread init");
     handle = NSThreadInit(NSConsumerMsgHandleThreadFunc, NULL);
-    if (!handle)
-    {
-        NS_LOG(ERROR, "msg handle thread init fail");
-        return NS_ERROR;
-    }
+    NS_VERTIFY_NOT_NULL(handle, NS_ERROR);
     NSSetMsgHandleThreadHandle(handle);
 
+    NS_LOG(DEBUG, "create queue");
     queue = NSCreateQueue();
-    if (!queue)
-    {
-        NS_LOG(ERROR, "msg handle queue create fail");
-        return NS_ERROR;
-    }
+    NS_VERTIFY_NOT_NULL(queue, NS_ERROR);
     NSSetMsgHandleQueue(queue);
 
     return NS_OK;
@@ -104,11 +94,8 @@ NSResult NSConsumerMessageHandlerInit()
 
 NSResult NSConsumerPushEvent(NSTask * task)
 {
-    if (!NSThreadInit(NSConsumerMsgPushThreadFunc, (void *) task))
-    {
-        NS_LOG(ERROR, "insert to queue thread init fail");
-        return NS_ERROR;
-    }
+    NSConsumerThread * thread = NSThreadInit(NSConsumerMsgPushThreadFunc, (void *) task);
+    NS_VERTIFY_NOT_NULL(thread, NS_ERROR);
 
     return NS_OK;
 }
@@ -116,20 +103,19 @@ NSResult NSConsumerPushEvent(NSTask * task)
 void NSConsumerMessageHandlerExit()
 {
     NSDestroyCacheList();
+    NSConsumerListenerTermiate();
     NSThreadStop(*(NSGetMsgHandleThreadHandle()));
+    NSDestroyQueue(*(NSGetMsgHandleQueue()));
 }
 
 void * NSConsumerMsgHandleThreadFunc(void * threadHandle)
 {
     NSConsumerQueue * queue = NULL;
     NSConsumerQueueObject * obj = NULL;
+
+    NS_LOG(DEBUG, "create thread for consumer message handle");
     NSConsumerThread * queueHandleThread = (NSConsumerThread *) threadHandle;
-    NS_LOG(DEBUG, "created thread for consumer message handle");
-    if (!queueHandleThread)
-    {
-        NS_LOG(ERROR, "thread handle is null");
-        return NULL;
-    }
+    NS_VERTIFY_NOT_NULL(queueHandleThread, NULL);
 
     while (true)
     {
@@ -171,19 +157,14 @@ void * NSConsumerMsgPushThreadFunc(void * data)
 {
     NSConsumerQueueObject * obj = NULL;
     NSConsumerQueue * queue = NULL;
+
+    NS_LOG(DEBUG, "get queueThread handle");
     NSConsumerThread * msgHandleThread = *(NSGetMsgHandleThreadHandle());
-    if (!msgHandleThread)
-    {
-        NS_LOG(ERROR, "NSThreadHandle is null. can not insert to queue");
-        return NULL;
-    }
+    NS_VERTIFY_NOT_NULL(msgHandleThread, NULL);
 
+    NS_LOG(DEBUG, "create queue object");
     obj = (NSConsumerQueueObject *)OICMalloc(sizeof(NSConsumerQueueObject));
-    if (!obj)
-    {
-        NS_LOG(ERROR, "NSConsumerQueueObject allocation fail");
-        return NULL;
-    }
+    NS_VERTIFY_NOT_NULL(obj, NULL);
 
     obj->data = data;
     obj->next = NULL;
@@ -227,7 +208,7 @@ void NSConsumerTaskProcessing(NSTask * task)
     }
     case TASK_RECV_READ:
     case TASK_RECV_DISMISS:
-    case TASK_CONSUMER_RECV_NOTIFICATION:
+    case TASK_CONSUMER_RECV_MESSAGE:
     {
         NSConsumerSubscriptionTaskProcessing(task);
         break;
index 248bcb4..a30fa64 100644 (file)
@@ -32,49 +32,34 @@ void NSDestroyThreadHandle(NSConsumerThread *);
 
 NSConsumerThread * NSThreadInit(NSThreadFunc func, void * data)
 {
-    if (!func)
-    {
-        NS_LOG(ERROR, "thread function is null");
-        return NULL;
-    }
+    NS_VERTIFY_NOT_NULL(func, NULL);
 
     pthread_mutex_init(&g_create_mutex, NULL);
 
     NSConsumerThread * handle = (NSConsumerThread *)OICMalloc(sizeof(NSConsumerThread));
-    if (!handle)
-    {
-        NS_LOG(ERROR, "thread allocation fail");
-        return NULL;
-    }
+    NS_VERTIFY_NOT_NULL(handle, NULL);
 
     memset(handle, 0, sizeof(NSConsumerThread));
 
     pthread_mutexattr_init(&(handle->mutex_attr));
-    if (pthread_mutexattr_settype(&(handle->mutex_attr), PTHREAD_MUTEX_RECURSIVE))
-    {
-        NS_LOG(ERROR, "thread mutex_attr init fail");
-        NSDestroyThreadHandle(handle);
-        return NULL;
-    }
-
-    if (pthread_mutex_init(&(handle->mutex), &(handle->mutex_attr)))
-    {
-        NS_LOG(ERROR, "thread mutex init fail");
-        NSDestroyThreadHandle(handle);
-        return NULL;
-    }
+
+    int pthreadResult = pthread_mutexattr_settype(&(handle->mutex_attr), PTHREAD_MUTEX_RECURSIVE);
+    NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL,
+            NULL, NSDestroyThreadHandle(handle));
+
+    pthreadResult = pthread_mutex_init(&(handle->mutex), &(handle->mutex_attr));
+    NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL,
+            NULL, NSDestroyThreadHandle(handle));
 
     pthread_mutex_lock(&g_create_mutex);
 
     handle->isStarted = true;
 
-    if (pthread_create(&(handle->thread_id), NULL, func,
-            (data == NULL) ? (void *) handle : (void *)data))
-    {
-        NS_LOG(ERROR, "thread create fail");
-        NSDestroyThreadHandle(handle);
-        return NULL;
-    }
+    pthreadResult = pthread_create(&(handle->thread_id), NULL, func,
+                           (data == NULL) ? (void *) handle : (void *)data);
+    NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(pthreadResult == 0 ? (void *)1 : NULL,
+            NULL, NSDestroyThreadHandle(handle));
+
     pthread_mutex_unlock(&g_create_mutex);
 
     return handle;