Modified consumer internal function call flow. 93/8693/8
authorKIM JungYong <jyong2.kim@samsung.com>
Thu, 16 Jun 2016 07:31:10 +0000 (16:31 +0900)
committerUze Choi <uzchoi@samsung.com>
Fri, 17 Jun 2016 02:18:50 +0000 (02:18 +0000)
When receive provider discover and message and syncinfo,
callback is called by communication module until this patch.
but, a proper way is callback should be calling by internalTaskControl module.
so, execution of callback logic is moved to internalTaskControl module.

Public NSProvider structure is modified.
Query Paramater(Consumer Id) is inclued on the request for subscription to providers.

Change-Id: Ib4e0db852d68889b90b66f880a052ed1453c2aca
Signed-off-by: KIM JungYong <jyong2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/8693
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
13 files changed:
service/notification/examples/linux/notificationconsumer.c
service/notification/include/NSCommon.h
service/notification/src/common/NSConstants.h
service/notification/src/common/NSUtil.c
service/notification/src/consumer/NSConsumerCommon.c
service/notification/src/consumer/NSConsumerCommon.h
service/notification/src/consumer/NSConsumerCommunication.c
service/notification/src/consumer/NSConsumerCommunication.h
service/notification/src/consumer/NSConsumerDiscovery.c
service/notification/src/consumer/NSConsumerInterface.c
service/notification/src/consumer/NSConsumerInternalTaskController.c
service/notification/src/consumer/NSConsumerInternalTaskController.h
service/notification/src/consumer/NSConsumerQueueScheduler.c

index 46d0eb8..bd3ba48 100644 (file)
@@ -7,8 +7,9 @@
 
 void onNotificationPosted(NSProvider * provider, NSMessage * notification)
 {
-    printf("Notification from : %s:%d\n", ((OCDevAddr *)provider->mUserData)->addr,
-            ((OCDevAddr *)provider->mUserData)->port);
+    (void) provider;
+//    printf("Notification from : %s:%d\n", ((OCDevAddr *)provider->mUserData)->addr,
+//            ((OCDevAddr *)provider->mUserData)->port);
     printf("id : %ld\n", notification->messageId);
     printf("title : %s\n", notification->title);
     printf("content : %s\n", notification->contentText);
@@ -26,8 +27,9 @@ void onDiscoverNotification(NSProvider * provider)
 
 void onNotificationSync(NSProvider * provider, NSSyncInfo * sync)
 {
-    printf("Sync from : %s:%d\n", ((OCDevAddr *)provider->mUserData)->addr,
-            ((OCDevAddr *)provider->mUserData)->port);
+    (void) provider;
+//    printf("Sync from : %s:%d\n", ((OCDevAddr *)provider->mUserData)->addr,
+//            ((OCDevAddr *)provider->mUserData)->port);
     printf("Sync ID : %ld\n", sync->messageId);
     printf("Sync STATE : %d\n", sync->state);
 }
index 319dff0..1a28092 100644 (file)
@@ -107,12 +107,7 @@ typedef struct
  */
 typedef struct
 {
-    char * mId;
-    char * mUserData;
-    char * messageUri;
-    char * syncUri;
-    OCDoHandle messageHandle;
-    OCDoHandle syncHandle;
+    char providerId[37];
 
 } NSProvider;
 
index 34cd51f..9c91439 100644 (file)
@@ -116,14 +116,16 @@ typedef enum eTaskType
     TASK_SEND_NOTIFICATION = 4000,
     TASK_SEND_PENDING_NOTI = 4001,
 
-    TASK_RECV_READ = 5000,
-    TASK_RECV_DISMISS = 5001,
-    TASK_SEND_READ = 5002,
-    TASK_SEND_DISMISS = 5003,
+    TASK_RECV_SYNCINFO = 5000,
+    TASK_RECV_READ = 5001,
+    TASK_RECV_DISMISS = 5003,
+    TASK_SEND_READ = 5101,
+    TASK_SEND_DISMISS = 5102,
 
     TASK_CONSUMER_REQ_DISCOVER = 8001,
     TASK_CONSUMER_REQ_SUBSCRIBE = 8002,
     TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL = 8003,
+    TASK_CONSUMER_RECV_SUBSCRIBE_CONFIRMED = 8004,
     TASK_CONSUMER_RECV_MESSAGE = 8101,
 
     TASK_CONSUMER_PROVIDER_DISCOVERED = 8201,
index dae0647..6cc3bc9 100755 (executable)
@@ -272,7 +272,7 @@ NSSyncInfo * NSGetSyncInfo(OCPayload * payload)
     OICFree(providerId);
 
     NS_LOG_V(DEBUG, "Provider ID : %s", retSync->providerId);
-    NS_LOG_V(DEBUG, "Sync ID : %lld", retSync->messageId);
+    NS_LOG_V(DEBUG, "Sync ID : %ld", retSync->messageId);
     NS_LOG_V(DEBUG, "Sync State : %d", (int) retSync->state);
 
     NS_LOG(DEBUG, "NSGetSyncInfo - OUT");
index 6eab997..5c9ee73 100644 (file)
 #include "oic_malloc.h"
 #include "oic_string.h"
 
+#define NS_QUERY_CONSUMER_ID "consumerid"
+
+char ** NSGetConsumerId()
+{
+    static char * g_consumerId = NULL;
+    return & g_consumerId;
+}
+
+void NSSetConsumerId(char * cId)
+{
+    NS_VERTIFY_NOT_NULL_V(cId);
+    char ** consumerId = NSGetConsumerId();
+    NSOICFree(*consumerId);
+    *consumerId = (char *)OICMalloc(sizeof(char) * NS_DEVICE_ID_LENGTH);
+    NS_VERTIFY_NOT_NULL_V(*consumerId);
+
+    OICStrcpy(*consumerId, sizeof(char) * NS_DEVICE_ID_LENGTH, cId);
+}
+
+char * NSGetQuery(const char * uri)
+{
+    NS_VERTIFY_NOT_NULL(uri, NULL);
+
+    char * consumerId = OICStrdup(*NSGetConsumerId());
+    NS_VERTIFY_NOT_NULL(consumerId, NULL);
+
+    size_t uriLen = strlen(uri) + 1;
+    size_t qKeyLen = sizeof(NS_QUERY_CONSUMER_ID);
+    size_t queryLen = NS_DEVICE_ID_LENGTH + uriLen + qKeyLen + 2;
+
+    char * retQuery = (char *)OICMalloc(sizeof(char) * queryLen);
+    NS_VERTIFY_NOT_NULL(retQuery, NULL);
+
+    size_t index = 0;
+    OICStrcpy((retQuery + index), uriLen, uri);
+    index += uriLen - 1;
+    OICStrcpy((retQuery + index), 2, "?");
+    index += 1;
+    OICStrcpy((retQuery + index), qKeyLen, NS_QUERY_CONSUMER_ID);
+    index += qKeyLen - 1;
+    OICStrcpy((retQuery + index), 2, "=");
+    index += 1;
+    OICStrcpy((retQuery + index), NS_DEVICE_ID_LENGTH, consumerId);
+
+    NSOICFree(consumerId);
+
+    return retQuery;
+}
+
 bool * NSGetBoneIsStartedConsumer()
 {
     static bool g_isStartedConsumer = false;
@@ -108,21 +157,21 @@ void NSNotificationSync(NSProvider * provider, NSSyncInfo * sync)
     NS_VERTIFY_NOT_NULL_V(thread);
 }
 
-NSNotificationReceivedCallback  * NSGetBoneNotificationPostedCb()
+NSNotificationReceivedCallback  * NSGetBoneMessagePostedCb()
 {
     static NSNotificationReceivedCallback  g_postCb = NULL;
 
     return & g_postCb;
 }
 
-void NSSetNotificationPostedCb(NSNotificationReceivedCallback  cb)
+void NSSetMessagePostedCb(NSNotificationReceivedCallback  cb)
 {
-    * NSGetBoneNotificationPostedCb() = cb;
+    * NSGetBoneMessagePostedCb() = cb;
 }
 
-NSNotificationReceivedCallback  NSGetNotificationPostedCb()
+NSNotificationReceivedCallback  NSGetMessagePostedCb()
 {
-    return * NSGetBoneNotificationPostedCb();
+    return * NSGetBoneMessagePostedCb();
 }
 
 typedef struct
@@ -131,23 +180,23 @@ typedef struct
     NSMessage * msg;
 } NSMessageData;
 
-void * NSNotificationPostFunc(void * obj)
+void * NSMessagePostFunc(void * obj)
 {
     NSMessageData * msgData = (NSMessageData *) obj;
 
-    NSGetNotificationPostedCb()((NSProvider *) msgData->provider,
+    NSGetMessagePostedCb()((NSProvider *) msgData->provider,
             (NSMessage *) msgData->msg);
     return NULL;
 }
 
-void NSNotificationPost(NSProvider * provider, NSMessage * msg)
+void NSMessagePost(NSProvider * provider, NSMessage * msg)
 {
     NSMessageData * obj = (NSMessageData *)OICMalloc(sizeof(NSMessageData));
     NS_VERTIFY_NOT_NULL_V(obj);
     obj->provider = provider;
     obj->msg = msg;
 
-    NSConsumerThread * thread = NSThreadInit(NSNotificationPostFunc, (void *) obj);
+    NSConsumerThread * thread = NSThreadInit(NSMessagePostFunc, (void *) obj);
     NS_VERTIFY_NOT_NULL_V(thread);
 }
 
@@ -183,33 +232,13 @@ NSMessage_consumer * NSCopyMessage(NSMessage_consumer * msg)
 }
 void NSRemoveMessage(NSMessage_consumer * msg)
 {
-    if (msg->messageId)
-    {
-        msg->messageId = 0;
-    }
-    if (msg->title)
-    {
-        OICFree(msg->title);
-        msg->title = NULL;
-    }
-    if (msg->contentText)
-    {
-        OICFree(msg->contentText);
-        msg->contentText = NULL;
-    }
-    if (msg->sourceName)
-    {
-        OICFree(msg->sourceName);
-        msg->sourceName = NULL;
-    }
-    if (msg->addr)
-    {
-        OICFree(msg->addr);
-        msg->addr = NULL;
-    }
-
-    OICFree(msg);
-    msg = NULL;
+    msg->messageId = 0;
+    NSOICFree(msg->title);
+    NSOICFree(msg->contentText);
+    NSOICFree(msg->sourceName);
+    NSOICFree(msg->addr);
+
+    NSOICFree(msg);
 }
 
 OCStackResult NSInvokeRequest(OCDoHandle * handle,
index 06d9494..4bd82e3 100644 (file)
@@ -32,6 +32,7 @@ extern "C" {
 #include "NSStructs.h"
 #include "ocstack.h"
 
+
 #define NS_QOS OC_LOW_QOS
 #define NS_RESOURCE_TYPE "oic.r.notification"
 #define NS_RESOURCE_URI "/notification"
@@ -40,50 +41,76 @@ extern "C" {
 #define NS_RESOURCE_QUERY "/oic/res"
 
 #define NS_DISCOVER_QUERY "/oic/res?rt=oic.r.notification"
+#define NS_DEVICE_ID_LENGTH 37
 
-#define NS_VERTIFY_NOT_NULL_V(obj) { if ((obj) == NULL) \
+#define NS_VERTIFY_NOT_NULL_V(obj) \
     { \
-        NS_LOG_V(ERROR, "%s : obj is null", __func__); \
-        return; \
-    }}
-
-#define NS_VERTIFY_NOT_NULL(obj, retVal) { if ((obj) == NULL) \
+        if ((obj) == NULL) \
+        { \
+            NS_LOG_V(ERROR, "%s : obj is null", __func__); \
+            return; \
+        } \
+    }
+
+#define NS_VERTIFY_NOT_NULL(obj, retVal) \
     { \
-        NS_LOG_V(ERROR, "%s : obj is null", __func__); \
-        return (retVal); \
-    }}
-
-#define NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(obj, retVal, func) { \
-    if ((obj) == NULL) \
+        if ((obj) == NULL) \
+        { \
+            NS_LOG_V(ERROR, "%s : obj is null", __func__); \
+            return (retVal); \
+        } \
+    }
+
+#define NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(obj, retVal, func) \
     { \
-        NS_LOG_V(ERROR, "%s : obj is null", __func__); \
-        NS_LOG(ERROR, "execute deletion"); \
-        (func); \
-        return (retVal); \
-    }}
-
-#define NS_VERTIFY_STACK_OK_V(obj) { OCStackResult _ret = (obj); \
-    if ( _ret != OC_STACK_OK) \
+        if ((obj) == NULL) \
+        { \
+            NS_LOG_V(ERROR, "%s : obj is null", __func__); \
+            NS_LOG(ERROR, "execute deletion"); \
+            (func); \
+            return (retVal); \
+        } \
+    }
+
+#define NS_VERTIFY_STACK_OK_V(obj) \
     { \
-        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) \
+        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) \
     { \
-        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) \
+        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) \
     { \
-        NS_LOG_V(ERROR, "%s : obj is not OC_STACK_OK : %d", __func__, _ret); \
-        (func); \
-        return (retVal); \
-    }}
+        OCStackResult _ret = (obj); \
+        if ( _ret != OC_STACK_OK) \
+        { \
+            NS_LOG_V(ERROR, "%s : obj is not OC_STACK_OK : %d", __func__, _ret); \
+            (func); \
+            return (retVal); \
+        } \
+    }
+
+#define NSOICFree(obj) \
+    { \
+        if ((obj)) \
+        { \
+            OICFree((obj)); \
+            (obj) = NULL; \
+        } \
+    }
 
 typedef enum
 {
@@ -92,12 +119,26 @@ typedef enum
     Notification,
 } NSConsumerMessageTypes;
 
+typedef struct
+{
+    char providerId[37];
+
+    char * messageUri;
+    char * syncUri;
+
+    OCDoHandle messageHandle;
+    OCDoHandle syncHandle;
+    OCDevAddr * addr;
+    NSAccessPolicy accessPolicy;
+
+} NSProvider_internal;
+
 typedef struct
 {
     // Mandatory
     uint64_t messageId;
     char * providerId;
-    
+
     //optional
     NSMessageType type;
     char * dateTime;
@@ -107,9 +148,7 @@ typedef struct
     char * sourceName;
     NSMediaContents mediaContents;
 
-    //Optional
     OCDevAddr * addr;
-    char * syncUri;
     NSConsumerMessageTypes messageTypes;
 } NSMessage_consumer;
 
@@ -119,12 +158,17 @@ void NSSetIsStartedConsumer(bool setValue);
 void NSSetDiscoverProviderCb(NSProviderDiscoveredCallback cb);
 void NSDiscoveredProvider(NSProvider * handle);
 
-void NSSetNotificationPostedCb(NSNotificationReceivedCallback  cb);
-void NSNotificationPost(NSProvider * provider, NSMessage * obj);
+void NSSetMessagePostedCb(NSNotificationReceivedCallback  cb);
+void NSMessagePost(NSProvider * provider, NSMessage * obj);
 
 void NSSetNotificationSyncCb(NSSyncCallback cb);
 void NSNotificationSync(NSProvider * provider, NSSyncInfo * sync);
 
+char ** NSGetConsumerId();
+void NSSetConsumerId(char * cId);
+
+char * NSGetQuery(const char * uri);
+
 NSTask * NSMakeTask(NSTaskType, void *);
 
 NSResult NSConsumerPushEvent(NSTask *);
index c8410ca..f4ecec2 100644 (file)
 #include "oic_string.h"
 #include "ocpayload.h"
 
-const char NS_MESSAGE_ACCEPTANCE[] = "0000-0000-0000-0000";
+#define NS_SYNC_URI "/notification/sync"
 
-NSMessage_consumer * NSGetNSMessage(OCClientResponse * clientResponse);
-NSSyncInfo * NSGetNSSyncInfo(OCClientResponse * clientResponse);
-NSProvider * NSGetNSProvider(OCClientResponse * clientResponse);
-OCRepPayload * NSGetPayloadofSyncInfo(NSMessage_consumer * message, int type);
+unsigned long NS_MESSAGE_ACCEPTANCE = 1;
 
+NSMessage_consumer * NSCreateMessage_internal(uint64_t msgId, const char * providerId);
+NSSyncInfo * NSCreateSyncInfo_consumer(uint64_t msgId, const char * providerId, NSSyncType state);
 
-NSResult NSPushToCache(OCClientResponse * clientResponse, NSTaskType type);
+NSMessage_consumer * NSGetMessage(OCClientResponse * clientResponse);
+NSSyncInfo * NSGetSyncInfoc(OCClientResponse * clientResponse);
 
+OCRepPayload * NSGetofSyncInfoPayload(NSMessage_consumer * message, int type);
 OCStackResult NSSendSyncInfo(NSMessage_consumer * message, int type);
 
+// TODO it seem to not to be this file
+NSResult NSPushToCache(OCClientResponse * clientResponse, NSTaskType type);
+
 NSResult NSConsumerSubscribeProvider(NSProvider * provider)
 {
-    OCStackResult ret = NSInvokeRequest(&(provider->messageHandle),
-                          OC_REST_OBSERVE, (OCDevAddr *) provider->mUserData,
-                          provider->messageUri, NULL, NSConsumerMessageListener, NULL);
+    NSProvider_internal * provider_internal = (NSProvider_internal *) provider;
+    NS_VERTIFY_NOT_NULL(provider_internal, NS_ERROR);
+
+    NS_LOG(DEBUG, "get subscribe message query");
+    char * query = NSGetQuery(provider_internal->messageUri);
+    NS_VERTIFY_NOT_NULL(query, NS_ERROR);
+
+    NS_LOG(DEBUG, "subscribe message");
+    OCStackResult ret = NSInvokeRequest(&(provider_internal->messageHandle),
+                          OC_REST_OBSERVE, provider_internal->addr,
+                          provider_internal->messageUri, NULL, NSConsumerMessageListener, NULL);
     NS_VERTIFY_STACK_OK(ret, NS_ERROR);
+    NSOICFree(query);
 
-    ret = NSInvokeRequest(&(provider->syncHandle),
-                          OC_REST_OBSERVE, (OCDevAddr *) provider->mUserData,
-                          provider->syncUri, NULL, NSConsumerSyncInfoListener, NULL);
+    NS_LOG(DEBUG, "get subscribe sync query");
+    query = NSGetQuery(provider_internal->syncUri);
+    NS_VERTIFY_NOT_NULL(query, NS_ERROR);
+
+    NS_LOG(DEBUG, "subscribe sync");
+    ret = NSInvokeRequest(&(provider_internal->syncHandle),
+                          OC_REST_OBSERVE, provider_internal->addr,
+                          provider_internal->syncUri, NULL, NSConsumerSyncInfoListener, NULL);
     NS_VERTIFY_STACK_OK(ret, NS_ERROR);
+    NSOICFree(query);
 
     return NS_OK;
 }
 
-OCStackApplicationResult NSConsumerPostResultCheck(
+OCStackApplicationResult NSConsumerCheckPostResult(
         void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
 {
     (void) ctx;
@@ -64,13 +83,9 @@ OCStackApplicationResult NSConsumerPostResultCheck(
     return OC_STACK_KEEP_TRANSACTION;
 }
 
-NSResult NSConsumerPostProvider(OCDevAddr * addr, OCPayload * payload, const char * uri)
+void NSRemoveSyncInfoObj(NSSyncInfo * sync)
 {
-    OCStackResult ret = NSInvokeRequest(NULL, OC_REST_POST, addr, uri, payload,
-                                        NSConsumerPostResultCheck, NULL);
-    NS_VERTIFY_STACK_OK(ret, NS_ERROR);
-
-    return NS_OK;
+    NSOICFree(sync);
 }
 
 OCStackApplicationResult NSConsumerSyncInfoListener(
@@ -82,32 +97,18 @@ OCStackApplicationResult NSConsumerSyncInfoListener(
     NS_VERTIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
     NS_VERTIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
 
-    NSSyncInfo * newNoti = NULL;
+    NS_LOG(DEBUG, "get NSSyncInfo");
+    NSSyncInfo * newSync = NSGetSyncInfoc(clientResponse);
+    NS_VERTIFY_NOT_NULL(newSync, OC_STACK_KEEP_TRANSACTION);
 
-    NS_LOG(ERROR, "get provider");
-    NSProvider * provider = NSGetNSProvider(clientResponse);
-    NS_VERTIFY_NOT_NULL(provider, OC_STACK_KEEP_TRANSACTION);
+    NSTaskType taskType = TASK_RECV_SYNCINFO;
 
-    newNoti = NSGetNSSyncInfo(clientResponse);
-    NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(
-            newNoti, OC_STACK_KEEP_TRANSACTION, OICFree(provider));
-
-    NSTaskType taskType = TASK_RECV_READ;
-
-    if (newNoti->state != NS_SYNC_READ)
-    {
-        NS_LOG(DEBUG, "newNoti->type : Dismiss");
-        taskType = TASK_RECV_DISMISS;
-    }
-    else
-    {
-        NS_LOG(DEBUG, "newNoti->type : Read");
-    }
-
-    NSNotificationSync(provider, newNoti);
+    NS_LOG(DEBUG, "build NSTask");
+    NSTask * task = NSMakeTask(taskType, (void *) newSync);
+    NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(task,
+               OC_STACK_KEEP_TRANSACTION, NSRemoveSyncInfoObj(newSync));
 
-    NSResult ret = NSPushToCache(clientResponse, taskType);
-    NS_VERTIFY_NOT_NULL(ret == NS_OK ? (void *)1 : NULL, OC_STACK_KEEP_TRANSACTION);
+    NSConsumerPushEvent(task);
 
     return OC_STACK_KEEP_TRANSACTION;
 }
@@ -121,37 +122,34 @@ 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);
-    NS_VERTIFY_NOT_NULL(provider, OC_STACK_KEEP_TRANSACTION);
-
     NS_LOG(DEBUG, "build NSMessage");
-    NSMessage_consumer * newNoti = NSGetNSMessage(clientResponse);
-    NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(
-            newNoti, OC_STACK_KEEP_TRANSACTION, OICFree(provider));
+    NSMessage_consumer * newNoti = NSGetMessage(clientResponse);
+    NS_VERTIFY_NOT_NULL(newNoti, OC_STACK_KEEP_TRANSACTION);
+
+    NSTaskType type = TASK_CONSUMER_RECV_MESSAGE;
 
-    if (newNoti->messageId == 0)
+    if (newNoti->messageId == NS_MESSAGE_ACCEPTANCE)
     {
-        // TODO update provider list.
         NS_LOG(DEBUG, "Receive Subscribe confirm");
-        OICFree(provider->mUserData);
-        OICFree(provider);
-        NSRemoveMessage(newNoti);
-        return OC_STACK_KEEP_TRANSACTION;
+        type = TASK_CONSUMER_RECV_SUBSCRIBE_CONFIRMED;
+    }
+    else
+    {
+        NS_LOG(DEBUG, "Receive new message");
     }
 
-    NS_LOG(DEBUG, "newNoti->type == Notification");
-    NSNotificationPost(provider, (NSMessage *) newNoti);
+    NS_LOG(DEBUG, "build NSTask");
+    NSTask * task = NSMakeTask(type, (void *) newNoti);
+    NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(task, NS_ERROR, NSRemoveMessage(newNoti));
 
-    NSResult ret = NSPushToCache(clientResponse, TASK_CONSUMER_RECV_MESSAGE);
-    NS_VERTIFY_NOT_NULL(ret == NS_OK ? (void *)1 : NULL, OC_STACK_KEEP_TRANSACTION);
+    NSConsumerPushEvent(task);
 
     return OC_STACK_KEEP_TRANSACTION;
 }
 
 NSResult NSPushToCache(OCClientResponse * clientResponse, NSTaskType type)
 {
-    NSMessage_consumer * cachedNoti = NSGetNSMessage(clientResponse);
+    NSMessage_consumer * cachedNoti = NSGetMessage(clientResponse);
     NS_LOG(DEBUG, "build NSMessage");
     NS_VERTIFY_NOT_NULL(cachedNoti, NS_ERROR);
 
@@ -164,12 +162,18 @@ NSResult NSPushToCache(OCClientResponse * clientResponse, NSTaskType type)
     return NS_OK;
 }
 
-NSMessage_consumer * NSGetNSMessage(OCClientResponse * clientResponse)
+void NSGetMessagePostClean(char * pId, OCDevAddr * addr)
+{
+    NSOICFree(pId);
+    NSOICFree(addr);
+}
+
+NSMessage_consumer * NSGetMessage(OCClientResponse * clientResponse)
 {
     NS_VERTIFY_NOT_NULL(clientResponse->payload, NULL);
+    OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
 
     NS_LOG(DEBUG, "get msg id");
-    OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
     uint64_t id = NULL;
     bool getResult = OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t *)&id);
     NS_VERTIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
@@ -179,16 +183,20 @@ NSMessage_consumer * NSGetNSMessage(OCClientResponse * clientResponse)
     getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, &pId);
     NS_VERTIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
 
+    NS_LOG(DEBUG, "get provider address");
+    OCDevAddr * addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
+    NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(addr, NULL, NSGetMessagePostClean(pId, addr));
+    memcpy(addr, clientResponse->addr, sizeof(OCDevAddr));
+
     NS_LOG(DEBUG, "create NSMessage");
-    NSMessage_consumer * retNoti = (NSMessage_consumer *)OICMalloc(sizeof(NSMessage_consumer));
-    NS_VERTIFY_NOT_NULL(retNoti, NULL);
+    NSMessage_consumer * retNoti = NSCreateMessage_internal(id, pId);
+    NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(retNoti, NULL, NSGetMessagePostClean(pId, addr));
+    NSOICFree(pId);
 
-    retNoti->messageId = id;
-    retNoti->providerId = pId;
-    retNoti->title = NULL;
-    retNoti->contentText = NULL;
-    retNoti->sourceName = NULL;
+    retNoti->addr = addr;
+    retNoti->messageTypes = Notification;
 
+    NS_LOG(DEBUG, "get msg optional field");
     OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_TITLE, &retNoti->title);
     OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_TEXT, &retNoti->contentText);
     OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_SOURCE, &retNoti->sourceName);
@@ -197,39 +205,27 @@ NSMessage_consumer * NSGetNSMessage(OCClientResponse * clientResponse)
     OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_DATETIME, &retNoti->dateTime);
     OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_TTL, (int64_t *)&retNoti->ttl);
 
-    NS_LOG_V(DEBUG, "Msg Address : %s", clientResponse->addr->addr);
-    NS_LOG_V(DEBUG, "Msg ID : %ld", retNoti->messageId);
-    NS_LOG_V(DEBUG, "Msg Title : %s", retNoti->title);
+    NS_LOG_V(DEBUG, "Msg Address : %s", retNoti->addr->addr);
+    NS_LOG_V(DEBUG, "Msg ID      : %ld", retNoti->messageId);
+    NS_LOG_V(DEBUG, "Msg Title   : %s", retNoti->title);
     NS_LOG_V(DEBUG, "Msg Content : %s", retNoti->contentText);
-    NS_LOG_V(DEBUG, "Msg Source : %s", retNoti->sourceName);
-    NS_LOG_V(DEBUG, "Msg Type : %d", retNoti->type);
-    NS_LOG_V(DEBUG, "Msg Date : %s", retNoti->dateTime);
-    NS_LOG_V(DEBUG, "Msg ttl : %ld", retNoti->ttl);
-
-    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->messageTypes = Notification;
+    NS_LOG_V(DEBUG, "Msg Source  : %s", retNoti->sourceName);
+    NS_LOG_V(DEBUG, "Msg Type    : %d", retNoti->type);
+    NS_LOG_V(DEBUG, "Msg Date    : %s", retNoti->dateTime);
+    NS_LOG_V(DEBUG, "Msg ttl     : %ld", retNoti->ttl);
 
     return retNoti;
 }
 
-NSSyncInfo * NSGetNSSyncInfo(OCClientResponse * clientResponse)
+NSSyncInfo * NSGetSyncInfoc(OCClientResponse * clientResponse)
 {
     NS_VERTIFY_NOT_NULL(clientResponse->payload, NULL);
 
     OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
 
-    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 msg id");
     uint64_t id = NULL;
-    getResult = OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t *)&id);
+    bool getResult = OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t *)&id);
     NS_VERTIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
 
     NS_LOG(DEBUG, "get provider id");
@@ -237,14 +233,15 @@ NSSyncInfo * NSGetNSSyncInfo(OCClientResponse * clientResponse)
     getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, &pId);
     NS_VERTIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
 
+    NS_LOG(DEBUG, "get state");
+    int64_t state = 0;
+    getResult = OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_STATE, & state);
+    NS_VERTIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
+
     NS_LOG(DEBUG, "create NSSyncInfo");
-    NSSyncInfo * retSync = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
+    NSSyncInfo * retSync = NSCreateSyncInfo_consumer(id, pId, (NSSyncType)state);
     NS_VERTIFY_NOT_NULL(retSync, NULL);
 
-    retSync->messageId = id;
-    retSync->state = (NSSyncType) state;
-    (retSync->providerId)[0] = '\0';
-
     NS_LOG_V(DEBUG, "Sync ID : %ld", retSync->messageId);
     NS_LOG_V(DEBUG, "Sync State : %d", (int) retSync->state);
     NS_LOG_V(DEBUG, "Sync Provider ID : %s", retSync->providerId);
@@ -252,23 +249,37 @@ NSSyncInfo * NSGetNSSyncInfo(OCClientResponse * clientResponse)
     return retSync;
 }
 
-NSProvider * NSGetNSProvider(OCClientResponse * clientResponse)
+NSMessage_consumer * NSCreateMessage_internal(uint64_t id, const char * providerId)
 {
-    NS_LOG(DEBUG, "create NSProvider");
-    NSProvider * newProvider = (NSProvider *)OICMalloc(sizeof(NSProvider));
-    NS_VERTIFY_NOT_NULL(newProvider, NULL);
+    NSMessage_consumer * retNoti = (NSMessage_consumer *)OICMalloc(sizeof(NSMessage_consumer));
+    NS_VERTIFY_NOT_NULL(retNoti, NULL);
 
-    // TODO set id
-    newProvider->mId = NULL;
-    newProvider->mUserData = (void *)OICMalloc(sizeof(OCDevAddr));
-    NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(newProvider, NULL, OICFree(newProvider));
+    retNoti->messageId = id;
+    OICStrcpy(retNoti->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, providerId);
+    retNoti->title = NULL;
+    retNoti->contentText = NULL;
+    retNoti->sourceName = NULL;
+    retNoti->type = NS_MESSAGE_INFO;
+    retNoti->dateTime = NULL;
+    retNoti->ttl = 0;
+    retNoti->addr = NULL;
 
-    memcpy(newProvider->mUserData, clientResponse->addr, sizeof(OCDevAddr));
+    return retNoti;
+}
 
-    return newProvider;
+NSSyncInfo * NSCreateSyncInfo_consumer(uint64_t msgId, const char * providerId, NSSyncType state)
+{
+    NSSyncInfo * retSync = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
+    NS_VERTIFY_NOT_NULL(retSync, NULL);
+
+    retSync->messageId = msgId;
+    retSync->state = state;
+    OICStrcpy(retSync->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, providerId);
+
+    return retSync;
 }
 
-void NSConsumerNotificationTaskProcessing(NSTask * task)
+void NSConsumerCommunicationTaskProcessing(NSTask * task)
 {
     NS_VERTIFY_NOT_NULL_V(task);
 
@@ -290,7 +301,7 @@ void NSConsumerNotificationTaskProcessing(NSTask * task)
     }
     else if (task->taskType == TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL)
     {
-        NSProvider * provider = (NSProvider *)task->taskData;
+        NSProvider_internal * provider = (NSProvider_internal *)task->taskData;
 
         OCCancel(provider->messageHandle, NS_QOS, NULL, 0);
         OCCancel(provider->syncHandle, NS_QOS, NULL, 0);
@@ -301,27 +312,23 @@ void NSConsumerNotificationTaskProcessing(NSTask * task)
     }
 }
 
-OCRepPayload * NSGetPayloadofSyncInfo(NSMessage_consumer * message, int type)
+OCRepPayload * NSGetofSyncInfoPayload(NSMessage_consumer * message, int type)
 {
     OCRepPayload * payload = OCRepPayloadCreate();
     NS_VERTIFY_NOT_NULL(payload, NULL);
 
-    OCRepPayloadSetPropInt(payload, "ID", (int64_t)message->messageId);
-    OCRepPayloadSetPropInt(payload, "STATE", type);
-    if (message->sourceName)
-    {
-        OCRepPayloadSetPropString(payload, "SOURCE", (char *) message->sourceName);
-    }
+    OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t)message->messageId);
+    OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_STATE, type);
 
     return payload;
 }
 
 OCStackResult NSSendSyncInfo(NSMessage_consumer * message, int type)
 {
-    OCRepPayload * payload = NSGetPayloadofSyncInfo(message, type);
+    OCRepPayload * payload = NSGetofSyncInfoPayload(message, type);
     NS_VERTIFY_NOT_NULL(payload, OC_STACK_ERROR);
 
     return NSInvokeRequest(NULL, OC_REST_POST, message->addr,
-                           "/notification/sync", (OCPayload*)payload,
-                           NSConsumerPostResultCheck, NULL);
+                           NS_SYNC_URI, (OCPayload*)payload,
+                           NSConsumerCheckPostResult, NULL);
 }
index 8fcf483..31ddfb7 100644 (file)
@@ -31,10 +31,9 @@ extern "C" {
 #include "NSStructs.h"
 #include "ocstack.h"
 
-void NSConsumerNotificationTaskProcessing(NSTask *);
+void NSConsumerCommunicationTaskProcessing(NSTask *);
 
 NSResult NSConsumerSubscribeProvider(NSProvider *);
-NSResult NSConsumerPostProvider(OCDevAddr *, OCPayload *, const char *);
 
 OCStackApplicationResult NSConsumerMessageListener(void *, OCDoHandle, OCClientResponse *);
 
index f8ce820..549c76e 100644 (file)
 #include "NSConstants.h"
 #include "ocpayload.h"
 #include "oic_malloc.h"
+#include "oic_string.h"
 
-#define NS_PAYLOAD_KEY_ACCEPTER "ACCEPTER"
 #define NS_DISCOVER_QUERY "/oic/res?rt=oic.r.notification"
 #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"
 
+NSProvider_internal * NSGetProvider(OCClientResponse * clientResponse);
+
 OCStackApplicationResult NSConsumerPresenceListener(
         void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
 {
@@ -64,6 +66,7 @@ OCStackApplicationResult NSConsumerPresenceListener(
 
     else if (payload->trigger == OC_PRESENCE_TRIGGER_CREATE)
     {
+        NS_LOG(DEBUG, "started presence or resource is created.");
         NSInvokeRequest(NULL, OC_REST_DISCOVER, clientResponse->addr,
             NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener, NULL);
     }
@@ -78,6 +81,7 @@ OCStackApplicationResult NSProviderDiscoverListener(
     (void) handle;
 
     NS_VERTIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
+    NS_VERTIFY_NOT_NULL(clientResponse->payload, OC_STACK_KEEP_TRANSACTION);
     NS_VERTIFY_STACK_OK(clientResponse->result, OC_STACK_KEEP_TRANSACTION);
 
     NS_LOG_V(DEBUG, "Discover income : %s:%d",
@@ -92,11 +96,6 @@ OCStackApplicationResult NSProviderDiscoverListener(
         return OC_STACK_DELETE_TRANSACTION;
     }
 
-    if (!clientResponse->payload)
-    {
-        return OC_STACK_KEEP_TRANSACTION;
-    }
-
     OCResourcePayload * resource = ((OCDiscoveryPayload *)clientResponse->payload)->resources;
     while (resource)
     {
@@ -111,6 +110,18 @@ OCStackApplicationResult NSProviderDiscoverListener(
     return OC_STACK_KEEP_TRANSACTION;
 }
 
+void NSRemoveProviderObj(NSProvider_internal * provider)
+{
+    NSOICFree(provider->messageUri);
+    NSOICFree(provider->syncUri);
+
+    provider->messageHandle = NULL;
+    provider->syncHandle = NULL;
+    NSOICFree(provider->addr);
+
+    NSOICFree(provider);
+}
+
 OCStackApplicationResult NSIntrospectProvider(
         void * ctx, OCDoHandle handle, OCClientResponse * clientResponse)
 {
@@ -120,10 +131,6 @@ OCStackApplicationResult NSIntrospectProvider(
     NS_VERTIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION);
     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);
     NS_LOG_V(DEBUG, "GET response result : %d",
@@ -138,7 +145,30 @@ OCStackApplicationResult NSIntrospectProvider(
         return OC_STACK_DELETE_TRANSACTION;
     }
 
-    NS_VERTIFY_NOT_NULL(clientResponse->payload, OC_STACK_KEEP_TRANSACTION);
+    NSProvider_internal * newProvider = NSGetProvider(clientResponse);
+    NS_VERTIFY_NOT_NULL(newProvider, OC_STACK_KEEP_TRANSACTION);
+
+    NS_LOG(DEBUG, "build NSTask");
+    NSTask * task = NSMakeTask(TASK_CONSUMER_PROVIDER_DISCOVERED, (void *) newProvider);
+    NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(task, NS_ERROR, NSRemoveProviderObj(newProvider));
+
+    NSConsumerPushEvent(task);
+
+    return OC_STACK_KEEP_TRANSACTION;
+}
+
+void NSGetProviderPostClean(char * pId, char * mUri, char * sUri, OCDevAddr * addr)
+{
+    NSOICFree(pId);
+    NSOICFree(mUri);
+    NSOICFree(sUri);
+    NSOICFree(addr);
+}
+
+NSProvider_internal * NSGetProvider(OCClientResponse * clientResponse)
+{
+    NS_LOG(DEBUG, "create NSProvider");
+    NS_VERTIFY_NOT_NULL(clientResponse->payload, NULL);
 
     OCRepPayload * payload = (OCRepPayload *)clientResponse->payload;
     while (payload)
@@ -147,53 +177,52 @@ OCStackApplicationResult NSIntrospectProvider(
         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);
+    payload = (OCRepPayload *)clientResponse->payload;
 
-    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));
-    NS_VERTIFY_NOT_NULL(newProvider, OC_STACK_KEEP_TRANSACTION);
-
-    // TODO set id
-    newProvider->mId = NULL;
+    char * providerId = NULL;
+    char * messageUri = NULL;
+    char * syncUri = NULL;
+    int64_t accepter = 0;
+    OCDevAddr * addr = NULL;
+
+    NS_LOG(DEBUG, "get information of accepter");
+    bool getResult = OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_POLICY, & accepter);
+    NS_VERTIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
+
+    NS_LOG(DEBUG, "get provider ID");
+    getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, & providerId);
+    NS_VERTIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
+
+    NS_LOG(DEBUG, "get message URI");
+    getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_MESSAGE, & messageUri);
+    NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(getResult == true ? (void *) 1 : NULL, NULL,
+            NSGetProviderPostClean(providerId, messageUri, syncUri, addr));
+
+    NS_LOG(DEBUG, "get sync URI");
+    getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_SYNC, & syncUri);
+    NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(getResult == true ? (void *) 1 : NULL, NULL,
+            NSGetProviderPostClean(providerId, messageUri, syncUri, addr));
+
+    NS_LOG(DEBUG, "get provider address");
+    addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
+    NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(addr, NULL,
+           NSGetProviderPostClean(providerId, messageUri, syncUri, addr));
+    memcpy(addr, clientResponse->addr, sizeof(OCDevAddr));
+
+    NSProvider_internal * newProvider
+        = (NSProvider_internal *)OICMalloc(sizeof(NSProvider_internal));
+    NS_VERTIFY_NOT_NULL(newProvider, NULL);
+
+    OICStrcpy(newProvider->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, providerId);
+    NSOICFree(providerId);
     newProvider->messageUri = messageUri;
     newProvider->syncUri = syncUri;
+    newProvider->accessPolicy = (NSAccessPolicy)accepter;
+    newProvider->addr = addr;
+    newProvider->messageHandle = NULL;
+    newProvider->syncHandle = NULL;
 
-    newProvider->mUserData = (void *)OICMalloc(sizeof(OCDevAddr));
-    NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(newProvider->mUserData,
-                   OC_STACK_KEEP_TRANSACTION, OICFree(newProvider));
-    memcpy(newProvider->mUserData, clientResponse->addr, sizeof(OCDevAddr));
-
-    if (accepter == NS_ACCESS_DENY)
-    {
-        NS_LOG(DEBUG, "accepter is NS_ACCEPTER_CONSUMER, Callback to user");
-
-        NSDiscoveredProvider(newProvider);
-    }
-    else
-    {
-        NS_LOG(DEBUG, "accepter is NS_ACCEPTER_PROVIDER, request subscribe");
-
-        NSTask * task = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE, (void *) newProvider);
-        NS_VERTIFY_NOT_NULL(task, OC_STACK_KEEP_TRANSACTION);
-
-        NSConsumerPushEvent(task);
-    }
-
-    return OC_STACK_KEEP_TRANSACTION;
+    return newProvider;
 }
 
 void NSConsumerDiscoveryTaskProcessing(NSTask * task)
@@ -211,4 +240,3 @@ void NSConsumerDiscoveryTaskProcessing(NSTask * task)
         NS_LOG(ERROR, "Unknown type message");
     }
 }
-
index cb211d6..51ca8ce 100644 (file)
@@ -43,7 +43,7 @@ NSResult NSStartConsumer(
     NS_VERTIFY_NOT_NULL(syncCb, NS_ERROR);
 
     NSSetDiscoverProviderCb(discoverCb);
-    NSSetNotificationPostedCb(postCb);
+    NSSetMessagePostedCb(postCb);
     NSSetNotificationSyncCb(syncCb);
     NSSetIsStartedConsumer(true);
 
@@ -57,7 +57,7 @@ NSResult NSStartConsumer(
 NSResult NSStopConsumer()
 {
     NSSetDiscoverProviderCb(NULL);
-    NSSetNotificationPostedCb(NULL);
+    NSSetMessagePostedCb(NULL);
     NSSetNotificationSyncCb(NULL);
     NSSetIsStartedConsumer(false);
 
@@ -110,30 +110,11 @@ NSResult NSDropNSObject(NSMessage * obj)
 {
     NS_VERTIFY_NOT_NULL(obj, NS_ERROR);
 
-    if (obj->messageId)
-    {
-        obj->messageId = 0;
-    }
-
-    if (obj->title)
-    {
-        OICFree(obj->title);
-        obj->title = NULL;
-    }
-
-    if (obj->contentText)
-    {
-        OICFree(obj->contentText);
-        obj->contentText = NULL;
-    }
-
-    if (obj->sourceName)
-    {
-        OICFree(obj->sourceName);
-        obj->sourceName = NULL;
-    }
-
-    OICFree(obj);
+    obj->messageId = 0;
+    NSOICFree(obj->title);
+    NSOICFree(obj->contentText);
+    NSOICFree(obj->sourceName);
+    NSOICFree(obj);
 
     return NS_OK;
 }
index 1ba1d90..4dd03f4 100644 (file)
@@ -38,8 +38,7 @@ void NSSetCacheList(NSCacheList * cache)
 
 void NSDestroyCacheList()
 {
-    NSCacheList * cache;
-    cache = *(NSGetCacheList());
+    NSCacheList * cache = *(NSGetCacheList());
     if (cache)
     {
         NSStorageDestroy(cache);
@@ -49,6 +48,8 @@ void NSDestroyCacheList()
 NSResult NSCacheUpdate(NSCacheList * cache, NSTask * task, NSConsumerMessageTypes type)
 {
     NSMessage_consumer * noti = (NSMessage_consumer *) task->taskData;
+    NS_VERTIFY_NOT_NULL(noti, NS_ERROR);
+
     noti->type = type;
 
     NSCacheElement * obj = (NSCacheElement *)OICMalloc(sizeof(NSCacheElement));
@@ -63,12 +64,57 @@ NSResult NSCacheUpdate(NSCacheList * cache, NSTask * task, NSConsumerMessageType
             NS_ERROR, NSRemoveMessage(noti));
 
     NSRemoveMessage(noti);
-    OICFree(obj);
+    NSOICFree(obj);
 
     return NS_OK;
 }
 
-void NSConsumerSubscriptionTaskProcessing(NSTask * task)
+void NSConsumerHandleProviderDiscovered(NSProvider_internal * provider)
+{
+    // TODO need to check for discovered provider is new.
+
+    if (provider->accessPolicy == NS_ACCESS_DENY)
+    {
+        NS_LOG(DEBUG, "accepter is NS_ACCEPTER_CONSUMER, Callback to user");
+        NSDiscoveredProvider((NSProvider *) provider);
+    }
+    else
+    {
+        NS_LOG(DEBUG, "accepter is NS_ACCEPTER_PROVIDER, request subscribe");
+        NSTask * task = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE, (void *) provider);
+        NS_VERTIFY_NOT_NULL_V(task);
+
+        NSConsumerPushEvent(task);
+    }
+}
+
+void NSConsumerHandleRecvSubscriptionConfirmed(NSMessage_consumer * msg)
+{
+    // TODO need to check for provider is new and callback to upper apis.
+    (void) msg;
+}
+
+void NSConsumerHandleRecvMessage(NSMessage_consumer * msg)
+{
+    NSMessagePost(NULL, (NSMessage *) msg);
+
+//    NSResult ret = NSCacheUpdate(cache, task, Notification);
+//    NS_VERTIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
+}
+
+void NSConsumerHandleRecvSyncInfo(NSSyncInfo * sync)
+{
+    NS_VERTIFY_NOT_NULL_V(sync);
+
+    // TODO need to check for provider is available.
+    NSNotificationSync(NULL, sync);
+
+    // TODO need to update msg list.
+//        NSResult ret = NSPushToCache(clientResponse, taskType);
+//        NS_VERTIFY_NOT_NULL(ret == NS_OK ? (void *)1 : NULL, OC_STACK_KEEP_TRANSACTION);
+}
+
+void NSConsumerInternalTaskProcessing(NSTask * task)
 {
     NS_VERTIFY_NOT_NULL_V(task);
 
@@ -87,12 +133,29 @@ void NSConsumerSubscriptionTaskProcessing(NSTask * task)
     NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
     switch (task->taskType)
     {
+        case TASK_CONSUMER_RECV_SUBSCRIBE_CONFIRMED:
+        {
+            NS_LOG(DEBUG, "Receive Subscribe confirm from provider.");
+            NSConsumerHandleRecvSubscriptionConfirmed((NSMessage_consumer *)task->taskData);
+            break;
+        }
         case TASK_CONSUMER_RECV_MESSAGE:
         {
             NS_LOG(DEBUG, "Receive New Notification");
+            NSConsumerHandleRecvMessage((NSMessage_consumer *)task->taskData);
 
-            ret = NSCacheUpdate(cache, task, Notification);
-            NS_VERTIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
+            break;
+        }
+        case TASK_CONSUMER_PROVIDER_DISCOVERED:
+        {
+            NS_LOG(DEBUG, "Receive New Provider is discovdered.");
+            NSConsumerHandleProviderDiscovered((NSProvider_internal *)task->taskData);
+            break;
+        }
+        case TASK_RECV_SYNCINFO:
+        {
+            NS_LOG(DEBUG, "Receive SyncInfo.");
+            NSConsumerHandleRecvSyncInfo((NSSyncInfo *)task->taskData);
             break;
         }
         case TASK_RECV_READ:
index 4d69688..6578b90 100644 (file)
@@ -31,7 +31,7 @@ extern "C" {
 
 void NSDestroyCacheList();
 
-void NSConsumerSubscriptionTaskProcessing(NSTask *);
+void NSConsumerInternalTaskProcessing(NSTask *);
 
 #ifdef __cplusplus
 }
index 95ac81b..9af64e1 100644 (file)
@@ -23,6 +23,8 @@
 #include <unistd.h>
 
 #include "oic_malloc.h"
+#include "oic_string.h"
+#include "ocrandom.h"
 
 #include "NSStructs.h"
 #include "NSConstants.h"
@@ -71,6 +73,13 @@ NSResult NSConsumerMessageHandlerInit()
     NSConsumerThread * handle = NULL;
     NSConsumerQueue * queue = NULL;
 
+    uint8_t uuid[UUID_SIZE];
+    char uuidString[UUID_STRING_SIZE];
+    OCGenerateUuid(uuid);
+    OCConvertUuidToString(uuid, uuidString);
+    NSSetConsumerId(uuidString);
+    NS_LOG_V(DEBUG, "Consumer ID : %s", *NSGetConsumerId());
+
     NS_LOG(DEBUG, "listener init");
     NSResult ret = NSConsumerListenerInit();
     NS_VERTIFY_NOT_NULL(ret == NS_OK ? (void *) 1 : NULL, NS_ERROR);
@@ -203,14 +212,17 @@ void NSConsumerTaskProcessing(NSTask * task)
     case TASK_SEND_READ:
     case TASK_SEND_DISMISS:
     {
-        NSConsumerNotificationTaskProcessing(task);
+        NSConsumerCommunicationTaskProcessing(task);
         break;
     }
     case TASK_RECV_READ:
     case TASK_RECV_DISMISS:
+    case TASK_RECV_SYNCINFO:
     case TASK_CONSUMER_RECV_MESSAGE:
+    case TASK_CONSUMER_PROVIDER_DISCOVERED:
+    case TASK_CONSUMER_RECV_SUBSCRIBE_CONFIRMED:
     {
-        NSConsumerSubscriptionTaskProcessing(task);
+        NSConsumerInternalTaskProcessing(task);
         break;
     }
     default: