Added logic for discovering Provider in TCP transport 29/9229/4
authorKIM JungYong <jyong2.kim@samsung.com>
Fri, 8 Jul 2016 01:19:20 +0000 (10:19 +0900)
committerUze Choi <uzchoi@samsung.com>
Mon, 11 Jul 2016 09:22:25 +0000 (09:22 +0000)
Before: Consumer tries to discovers provider in UDP transport,
        when connection changed Event is called thru CAUtil.
After: discover provider in TCP transport also for the same event
 - This is independent from initial discovery by service start

# Some compile warnings printed are resolved
# Some string defines for cloud TCP

Change-Id: Ic183ce90aaa1146f6c5910c6040280c370a0d40c
Signed-off-by: KIM JungYong <jyong2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/9229
Reviewed-by: Uze Choi <uzchoi@samsung.com>
Tested-by: Uze Choi <uzchoi@samsung.com>
13 files changed:
service/notification/examples/linux/notificationconsumer.c
service/notification/src/common/NSConstants.h
service/notification/src/common/NSStructs.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/NSConsumerDiscovery.c
service/notification/src/consumer/NSConsumerInterface.c
service/notification/src/consumer/NSConsumerInternalTaskController.c
service/notification/src/consumer/NSConsumerNetworkEventListener.c
service/notification/src/consumer/NSConsumerScheduler.c
service/notification/src/consumer/cache/linux/NSConsumerMemoryCache.c

index 2ecf765..029ea02 100644 (file)
@@ -20,7 +20,7 @@ void onSubscriptionAccepted(NSProvider * provider)
 
 void onNotificationPosted(NSMessage * notification)
 {
-    printf("id : %lld\n", notification->messageId);
+    printf("id : %lu\n", notification->messageId);
     printf("title : %s\n", notification->title);
     printf("content : %s\n", notification->contentText);
     printf("source : %s\n", notification->sourceName);
@@ -29,7 +29,7 @@ void onNotificationPosted(NSMessage * notification)
 
 void onNotificationSync(NSSyncInfo * sync)
 {
-    printf("Sync ID : %lld\n", sync->messageId);
+    printf("Sync ID : %lu\n", sync->messageId);
     printf("Sync STATE : %d\n", sync->state);
 }
 
index b8299b1..551d792 100644 (file)
@@ -135,7 +135,8 @@ typedef enum eTaskType
     TASK_CONSUMER_RECV_CONFIRM = 8206,
 
     TASK_EVENT_CONNECTED = 9000,
-    TASK_EVENT_DISCONNECTED = 9001,
+    TASK_EVENT_CONNECTED_TCP = 9001,
+    TASK_EVENT_DISCONNECTED = 9002,
 
     TASK_CB_SUBSCRIPTION = 10000,
     TASK_CB_SYNC = 10001,
index 6703e01..e51f4ff 100644 (file)
@@ -31,8 +31,8 @@
 typedef struct _nsTask\r
 {\r
     NSTaskType taskType;\r
-    void* taskData;\r
-    struct _nsTask* nextTask;\r
+    void * taskData;\r
+    struct _nsTask * nextTask;\r
 } NSTask;\r
 \r
 typedef void * NSCacheData;\r
@@ -69,8 +69,8 @@ typedef struct
 {\r
     OCResourceHandle handle;\r
     int accepter;\r
-    char* message_uri;\r
-    char* sync_uri;\r
+    char * message_uri;\r
+    char * sync_uri;\r
 } NSNotificationResource;\r
 \r
 typedef struct\r
@@ -93,8 +93,8 @@ typedef struct
 typedef struct\r
 {\r
     OCResourceHandle handle;\r
-    char* id;\r
-    char* state;\r
+    char * id;\r
+    char * state;\r
 } NSSyncResource;\r
 \r
 typedef struct\r
index 6945199..8ed546f 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 : %lu", retSync->messageId);
     NS_LOG_V(DEBUG, "Sync State : %d", (int) retSync->state);
 
     NS_LOG(DEBUG, "NSGetSyncInfo - OUT");
index d879de0..26a9321 100644 (file)
@@ -268,7 +268,7 @@ void NSRemoveProvider(NSProvider_internal * prov)
 OCStackResult NSInvokeRequest(OCDoHandle * handle,
         OCMethod method, const OCDevAddr * addr,
         const char * queryUrl, OCPayload * payload,
-        void * callbackFunc, void * callbackData)
+        void * callbackFunc, void * callbackData, OCConnectivityType type)
 {
     OCCallbackData cbdata;
 
@@ -277,5 +277,5 @@ OCStackResult NSInvokeRequest(OCDoHandle * handle,
     cbdata.cd = NULL;
 
     return OCDoResource(handle, method, queryUrl, addr,
-            payload, CT_DEFAULT, NS_QOS, &cbdata, NULL, 0);
+            payload, type, NS_QOS, &cbdata, NULL, 0);
 }
index d05304b..85f9b69 100644 (file)
@@ -198,7 +198,7 @@ void NSRemoveProvider(NSProvider_internal *);
 OCStackResult NSInvokeRequest(OCDoHandle * handle,
         OCMethod method, const OCDevAddr * addr,
         const char * queryUrl, OCPayload * payload,
-        void * callbackFunc, void * callbackData);
+        void * callbackFunc, void * callbackData, OCConnectivityType type);
 
 #ifdef __cplusplus
 }
index f944caf..9ec8e0b 100644 (file)
@@ -35,12 +35,6 @@ NSSyncInfo * NSCreateSyncInfo_consumer(uint64_t msgId, const char * providerId,
 NSMessage_consumer * NSGetMessage(OCClientResponse * clientResponse);
 NSSyncInfo * NSGetSyncInfoc(OCClientResponse * clientResponse);
 
-OCRepPayload * NSGetofSyncInfoPayload(NSMessage_consumer * message, int type);
-OCStackResult NSSendSyncInfoUsingMessage(NSMessage_consumer * message, int type);
-
-// TODO it seem to not to be this file
-NSResult NSPushToCache(OCClientResponse * clientResponse, NSTaskType type);
-
 NSResult NSConsumerSubscribeProvider(NSProvider * provider)
 {
     NSProvider_internal * provider_internal = (NSProvider_internal *) provider;
@@ -54,7 +48,7 @@ NSResult NSConsumerSubscribeProvider(NSProvider * provider)
     NS_LOG_V(DEBUG, "subscribe query : %s", query);
     OCStackResult ret = NSInvokeRequest(&(provider_internal->i_messageHandle),
                           OC_REST_OBSERVE, provider_internal->i_addr,
-                          query, NULL, NSConsumerMessageListener, NULL);
+                          query, NULL, NSConsumerMessageListener, NULL, CT_DEFAULT);
     NS_VERIFY_STACK_OK(ret, NS_ERROR);
     NSOICFree(query);
 
@@ -66,7 +60,7 @@ NSResult NSConsumerSubscribeProvider(NSProvider * provider)
     NS_LOG_V(DEBUG, "subscribe query : %s", query);
     ret = NSInvokeRequest(&(provider_internal->i_syncHandle),
                           OC_REST_OBSERVE, provider_internal->i_addr,
-                          query, NULL, NSConsumerSyncInfoListener, NULL);
+                          query, NULL, NSConsumerSyncInfoListener, NULL, CT_DEFAULT);
     NS_VERIFY_STACK_OK(ret, NS_ERROR);
     NSOICFree(query);
 
@@ -149,21 +143,6 @@ OCStackApplicationResult NSConsumerMessageListener(
     return OC_STACK_KEEP_TRANSACTION;
 }
 
-NSResult NSPushToCache(OCClientResponse * clientResponse, NSTaskType type)
-{
-    NSMessage_consumer * cachedNoti = NSGetMessage(clientResponse);
-    NS_LOG(DEBUG, "build NSMessage");
-    NS_VERIFY_NOT_NULL(cachedNoti, NS_ERROR);
-
-    NS_LOG(DEBUG, "build NSTask");
-    NSTask * task = NSMakeTask(type, (void *) cachedNoti);
-    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(task, NS_ERROR, NSRemoveMessage(cachedNoti));
-
-    NSConsumerPushEvent(task);
-
-    return NS_OK;
-}
-
 void NSGetMessagePostClean(char * pId, OCDevAddr * addr)
 {
     NSOICFree(pId);
@@ -209,13 +188,13 @@ NSMessage_consumer * NSGetMessage(OCClientResponse * clientResponse)
     OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_TTL, (int64_t *)&retMsg->ttl);
 
     NS_LOG_V(DEBUG, "Msg Address : %s", retMsg->i_addr->addr);
-    NS_LOG_V(DEBUG, "Msg ID      : %lld", retMsg->messageId);
+    NS_LOG_V(DEBUG, "Msg ID      : %lu", retMsg->messageId);
     NS_LOG_V(DEBUG, "Msg Title   : %s", retMsg->title);
     NS_LOG_V(DEBUG, "Msg Content : %s", retMsg->contentText);
     NS_LOG_V(DEBUG, "Msg Source  : %s", retMsg->sourceName);
     NS_LOG_V(DEBUG, "Msg Type    : %d", retMsg->type);
     NS_LOG_V(DEBUG, "Msg Date    : %s", retMsg->dateTime);
-    NS_LOG_V(DEBUG, "Msg ttl     : %lld", retMsg->ttl);
+    NS_LOG_V(DEBUG, "Msg ttl     : %lu", retMsg->ttl);
 
     return retMsg;
 }
@@ -245,7 +224,7 @@ NSSyncInfo * NSGetSyncInfoc(OCClientResponse * clientResponse)
     NSSyncInfo * retSync = NSCreateSyncInfo_consumer(id, pId, (NSSyncType)state);
     NS_VERIFY_NOT_NULL(retSync, NULL);
 
-    NS_LOG_V(DEBUG, "Sync ID : %lld", retSync->messageId);
+    NS_LOG_V(DEBUG, "Sync ID : %lu", retSync->messageId);
     NS_LOG_V(DEBUG, "Sync State : %d", (int) retSync->state);
     NS_LOG_V(DEBUG, "Sync Provider ID : %s", retSync->providerId);
 
@@ -293,7 +272,7 @@ OCStackResult NSSendSyncInfo(NSSyncInfo * syncInfo, OCDevAddr * addr)
 
     return NSInvokeRequest(NULL, OC_REST_POST, addr,
                            NS_SYNC_URI, (OCPayload*)payload,
-                           NSConsumerCheckPostResult, NULL);
+                           NSConsumerCheckPostResult, NULL, addr->adapter);
 }
 
 void NSConsumerCommunicationTaskProcessing(NSTask * task)
index df72f70..a87fb0b 100644 (file)
@@ -30,6 +30,7 @@
 
 #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_PRESENCE_SUBSCRIBE_QUERY_TCP "/oic/ad?rt=oic.r.notification"
 #define NS_GET_INFORMATION_QUERY "/notification?if=oic.if.notification"
 
 NSProvider_internal * NSGetProvider(OCClientResponse * clientResponse);
@@ -68,7 +69,8 @@ OCStackApplicationResult NSConsumerPresenceListener(
     {
         NS_LOG(DEBUG, "started presence or resource is created.");
         NSInvokeRequest(NULL, OC_REST_DISCOVER, clientResponse->addr,
-            NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener, NULL);
+            NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener, NULL,
+            clientResponse->addr->adapter);
     }
 
     return OC_STACK_KEEP_TRANSACTION;
@@ -99,10 +101,11 @@ OCStackApplicationResult NSProviderDiscoverListener(
     OCResourcePayload * resource = ((OCDiscoveryPayload *)clientResponse->payload)->resources;
     while (resource)
     {
-        if (!strcmp(resource->uri, NS_RESOURCE_URI))
+        if (strstr(resource->uri, NS_RESOURCE_URI))
         {
             NSInvokeRequest(NULL, OC_REST_GET, clientResponse->addr,
-                    NS_RESOURCE_URI, NULL, NSIntrospectProvider, NULL);
+                    resource->uri, NULL, NSIntrospectProvider, NULL,
+                    clientResponse->addr->adapter);
         }
         resource = resource->next;
     }
@@ -212,7 +215,8 @@ NSProvider_internal * NSGetProvider(OCClientResponse * clientResponse)
 
     NSProvider_internal * newProvider
         = (NSProvider_internal *)OICMalloc(sizeof(NSProvider_internal));
-    NS_VERIFY_NOT_NULL(newProvider, NULL);
+    NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(newProvider, NULL,
+          NSGetProviderPostClean(providerId, messageUri, syncUri, addr));
 
     OICStrcpy(newProvider->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, providerId);
     NSOICFree(providerId);
@@ -233,11 +237,31 @@ void NSConsumerDiscoveryTaskProcessing(NSTask * task)
     NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
     if (task->taskType == TASK_EVENT_CONNECTED || task->taskType == TASK_CONSUMER_REQ_DISCOVER)
     {
-        NSInvokeRequest(NULL, OC_REST_DISCOVER, NULL, NS_DISCOVER_QUERY,
-                NULL, NSProviderDiscoverListener, NULL);
+        OCDevAddr * addr = (OCDevAddr *) task->taskData;
+
+        NS_LOG(DEBUG, "Request discover [UDP]");
+        NSInvokeRequest(NULL, OC_REST_DISCOVER, addr, NS_DISCOVER_QUERY,
+                NULL, NSProviderDiscoverListener, NULL, addr->adapter);
+    }
+    else if (task->taskType == TASK_EVENT_CONNECTED_TCP)
+    {
+        NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(task->taskData, NSOICFree(task));
+        OCDevAddr * addr = (OCDevAddr *) task->taskData;
+
+        NS_LOG(DEBUG, "Request discover [TCP]");
+        NSInvokeRequest(NULL, OC_REST_DISCOVER, addr, NS_DISCOVER_QUERY,
+                NULL, NSProviderDiscoverListener, NULL, addr->adapter);
+
+        NS_LOG(DEBUG, "Subscribe presence [TCP]");
+        NSInvokeRequest(NULL, OC_REST_PRESENCE, addr, NS_PRESENCE_SUBSCRIBE_QUERY_TCP,
+                NULL, NSConsumerPresenceListener, NULL, addr->adapter);
+
+        NSOICFree(task->taskData);
     }
     else
     {
         NS_LOG(ERROR, "Unknown type message");
     }
+
+    NSOICFree(task);
 }
index 37b4c00..617454d 100644 (file)
@@ -116,7 +116,7 @@ NSProvider * NSConsumerGetProvider(const char * providerId)
 NSMessage * NSConsumerGetMessage(uint64_t messageId)
 {
     char msgId[NS_DEVICE_ID_LENGTH] = { 0, };
-    snprintf(msgId, NS_DEVICE_ID_LENGTH, "%lld", messageId);
+    snprintf(msgId, NS_DEVICE_ID_LENGTH, "%lu", messageId);
 
     return (NSMessage *) NSConsumerFindNSMessage(msgId);
 }
index 25e7b71..828c946 100644 (file)
@@ -176,7 +176,7 @@ void NSConsumerHandleProviderDiscovered(NSProvider_internal * provider)
     NS_VERIFY_NOT_NULL_V(provider);
 
     NSProvider_internal * providerCacheData = NSProviderCacheFind(provider->providerId);
-    NS_VERIFY_NOT_NULL_V(!providerCacheData);
+    NS_VERIFY_NOT_NULL_V(providerCacheData == NULL ? (void *)1 : NULL);
 
     NS_LOG (ERROR, "New provider is discovered");
     NSResult ret = NSProviderCacheUpdate(provider);
@@ -226,7 +226,7 @@ void NSConsumerHandleRecvSyncInfo(NSSyncInfo * sync)
     NS_VERIFY_NOT_NULL_V(provider);
 
     char msgId[NS_DEVICE_ID_LENGTH] = { 0, };
-    snprintf(msgId, NS_DEVICE_ID_LENGTH, "%lld", sync->messageId);
+    snprintf(msgId, NS_DEVICE_ID_LENGTH, "%lu", sync->messageId);
 
     NSMessage_consumer * msg = NSMessageCacheFind(msgId);
     NS_VERIFY_NOT_NULL_V(msg);
@@ -266,7 +266,6 @@ void NSConsumerInternalTaskProcessing(NSTask * task)
 {
     NS_VERIFY_NOT_NULL_V(task);
 
-    NSResult ret = NS_ERROR;
     NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
     switch (task->taskType)
     {
index ca78381..40e6261 100644 (file)
@@ -24,6 +24,7 @@
 #include "NSConstants.h"
 #include "NSConsumerCommon.h"
 #include "cautilinterface.h"
+#include "oic_malloc.h"
 
 #include "NSConsumerDiscovery.h"
 #include "NSConsumerNetworkEventListener.h"
@@ -55,12 +56,14 @@ NSResult NSConsumerListenerInit()
 
     NS_LOG(DEBUG, "Request to subscribe presence");
     OCStackResult stackResult = NSInvokeRequest(getPresenceHandle(), OC_REST_PRESENCE, NULL,
-                        NS_PRESENCE_SUBSCRIBE_QUERY, NULL, NSConsumerPresenceListener, NULL);
+                        NS_PRESENCE_SUBSCRIBE_QUERY, NULL, NSConsumerPresenceListener,
+                        NULL, CT_DEFAULT);
     NS_VERIFY_STACK_OK(stackResult, NS_ERROR);
 
     NS_LOG(DEBUG, "Request to discover provider");
     stackResult = NSInvokeRequest(NULL, OC_REST_DISCOVER, NULL,
-                      NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener, NULL);
+                      NS_DISCOVER_QUERY, NULL, NSProviderDiscoverListener,
+                      NULL, CT_DEFAULT);
     NS_VERIFY_STACK_OK(stackResult, NS_ERROR);
 
     return NS_OK;
@@ -82,12 +85,26 @@ void NSConnectionStateListener(CATransportAdapter_t adapter,
     (void) adapter;
     (void) remote_address;
 
+    NSTaskType type = TASK_EVENT_CONNECTED;
+    OCDevAddr * addr = NULL;
     if (connected)
     {
-        NS_LOG(DEBUG, "try to discover notification provider.");
-
-        NSTask * task = NSMakeTask(TASK_EVENT_CONNECTED, NULL);
-        NS_VERIFY_NOT_NULL_V(task);
+        if (adapter == CA_ADAPTER_TCP)
+        {
+            type = TASK_EVENT_CONNECTED_TCP;
+            NS_LOG(DEBUG, "try to discover notification provider : TCP.");
+            // TODO convet to OCDevAddr;
+            // addr = .....
+        }
+        else if (adapter == CA_ADAPTER_IP)
+        {
+            NS_LOG(DEBUG, "try to discover notification provider.");
+            // TODO convet to OCDevAddr;
+            // addr = .....
+        }
+
+        NSTask * task = NSMakeTask(type, addr);
+        NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(task, NSOICFree(addr));
 
         NSConsumerPushEvent(task);
     }
index e58f3bb..64a5196 100644 (file)
@@ -204,6 +204,7 @@ void NSConsumerTaskProcessing(NSTask * task)
     switch (task->taskType)
     {
     case TASK_EVENT_CONNECTED:
+    case TASK_EVENT_CONNECTED_TCP:
     case TASK_CONSUMER_REQ_DISCOVER:
     {
         NSConsumerDiscoveryTaskProcessing(task);
index e4f2b43..b031faf 100644 (file)
@@ -210,7 +210,7 @@ NSResult NSConsumerCacheWriteMessage(NSCacheList * list, NSCacheElement * newObj
 
     pthread_mutex_unlock(mutex);
     char msgId[NS_DEVICE_ID_LENGTH] = {0, };
-    snprintf(msgId, NS_DEVICE_ID_LENGTH, "%lld", newMsgObj->messageId);
+    snprintf(msgId, NS_DEVICE_ID_LENGTH, "%lu", newMsgObj->messageId);
     NSCacheElement * it = NSStorageRead(list, msgId);
     pthread_mutex_lock(mutex);
 
@@ -416,7 +416,7 @@ bool NSConsumerCompareIdCacheData(NSCacheType type, void * data, const char * id
         NSMessage_consumer * msg = (NSMessage_consumer *) data;
 
         char msgId[NS_DEVICE_ID_LENGTH] = {0, };
-        snprintf(msgId, NS_DEVICE_ID_LENGTH, "%lld", msg->messageId);
+        snprintf(msgId, NS_DEVICE_ID_LENGTH, "%lu", msg->messageId);
         if (!strcmp(msgId, id))
         {
             return true;