Modify Consumer example & Logic to copy provider
authorYounghyunJoo <yh_.joo@samsung.com>
Wed, 24 Aug 2016 04:57:41 +0000 (13:57 +0900)
committerUze Choi <uzchoi@samsung.com>
Wed, 24 Aug 2016 06:03:25 +0000 (06:03 +0000)
- Modify example code to test Topic
- Modify Logic to copy provider during processing topic

Change-Id: I4a6ca0d2d0c203fcfb76af00cc4c05b745d498e0
Signed-off-by: YounghyunJoo <yh_.joo@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/10783
Reviewed-by: Uze Choi <uzchoi@samsung.com>
Tested-by: Uze Choi <uzchoi@samsung.com>
service/notification/examples/linux/notificationconsumer.c
service/notification/src/consumer/NSConsumerCommon.c
service/notification/src/consumer/NSConsumerCommunication.c
service/notification/src/consumer/NSConsumerInterface.c
service/notification/src/consumer/NSConsumerInternalTaskController.c
service/notification/src/consumer/NSConsumerScheduler.c

index f715eac..d538b07 100644 (file)
@@ -38,6 +38,8 @@
 #define CLOUD_IOTIVITYNS_SESSION ""                // refer to IoTivity Cloud Module Sample
 #endif
 
+NSProvider * g_provider = NULL;
+
 void onDiscoverNotification(NSProvider * provider)
 {
     printf("notification resource discovered\n");
@@ -45,6 +47,20 @@ void onDiscoverNotification(NSProvider * provider)
     printf("startSubscribing\n");
 }
 
+void printProviderTopicList(NSProvider *provider)
+{
+    printf("printProviderTopicList\n");
+    if (provider->topicLL)
+    {
+        NSTopicLL * iter = provider->topicLL;
+        while (iter)
+        {
+            printf("Topic Name: %s\t Topic State: %d\n", iter->topicName, iter->state);
+            iter = iter->next;
+        }
+    }
+}
+
 void onProviderChanged(NSProvider * provider, NSResponse response)
 {
     printf("Provider changed: %d\n", response);
@@ -53,49 +69,9 @@ void onProviderChanged(NSProvider * provider, NSResponse response)
     if (response == NS_TOPIC)
     {
         printf ("Provider Topic Updated\n");
-        if (provider->topicLL)
-        {
-            NSTopicLL * iter = provider->topicLL;
-            while (iter)
-            {
-                printf("Topic Name: %s\t Topic State: %d\n", iter->topicName, iter->state);
-                iter = iter->next;
-            }
-        }
-
-        printf("3. Get Topics\n");
-        printf("4. Select Topics\n");
-        printf("input: ");
-
-        int num = 0;
-        char dummy = '\0';
-        scanf("%d", &num);
-        fflush(stdin);
-        scanf("%c", &dummy);
-        fflush(stdin);
 
-        switch (num)
-        {
-            case 3:
-                printf("3. Get Topics\n");
-                NSConsumerGetInterestTopics(provider);
-                break;
-            case 4:
-                printf("4. Select Topics\n");
-                if (provider->topicLL)
-                {
-                    NSTopicLL * iter = provider->topicLL;
-                    int i = 0;
-                    while (iter)
-                    {
-                        iter->state = (i++)%2;
-                        printf("Topic Name: %s\t Topic State: %d\n", iter->topicName, iter->state);
-                        iter = iter->next;
-                    }
-                }
-                NSConsumerSelectInterestTopics(provider);
-                break;
-        }
+        printProviderTopicList(provider);
+        g_provider = provider;
     }
 }
 
@@ -118,7 +94,6 @@ void onNotificationSync(NSSyncInfo * sync)
     printf("Sync STATE : %d\n", sync->state);
 }
 
-
 #ifdef WITH_CLOUD
 OCStackApplicationResult handleLoginoutCB(void *ctx,
         OCDoHandle handle,
@@ -209,6 +184,8 @@ int main(void)
 
         printf("1. Start Consumer\n");
         printf("2. Stop Consumer\n");
+        printf("3. Get Topics\n");
+        printf("4. Select Topics\n");
         printf("5. Exit\n");
 
         printf("Input: ");
@@ -228,6 +205,29 @@ int main(void)
                 printf("2. Stop Consumer");
                 NSStopConsumer();
                 break;
+            case 3:
+                printf("3. Get Topics\n");
+                if(g_provider)
+                {
+                    NSConsumerGetInterestTopics(g_provider);
+                }
+                break;
+            case 4:
+                printf("4. Select Topics\n");
+
+                if (g_provider && g_provider->topicLL)
+                {
+                    NSTopicLL * iter = g_provider->topicLL;
+                    int i = 0;
+                    while (iter)
+                    {
+                        iter->state = (i++)%2;
+                        printf("Topic Name: %s\t Topic State: %d\n", iter->topicName, iter->state);
+                        iter = iter->next;
+                    }
+                    NSConsumerSelectInterestTopics(g_provider);
+                }
+                break;
             case 5:
                 printf("5. Exit");
                 isExit = true;
@@ -236,6 +236,5 @@ int main(void)
                 break;
         }
     }
-
     return 0;
 }
index ea12a52..f5af451 100644 (file)
@@ -412,6 +412,7 @@ NSTopicLL * NSCopyTopicLL(NSTopicLL * topicHead)
 
     NSTopicLL * iter = topicHead;
 
+    NS_LOG_V(DEBUG, "[NSCopyTopicLL] Name:%s\t State:%d", iter->topicName, iter->state);
     NSTopicLL * newTopicHead = NSCopyTopicNode(iter);
     NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(newTopicHead, NULL, NSRemoveTopicLL(newTopicHead));
 
@@ -419,6 +420,7 @@ NSTopicLL * NSCopyTopicLL(NSTopicLL * topicHead)
 
     while (iter)
     {
+        NS_LOG_V(DEBUG, "[NSCopyTopicLL] Name:%s\t State:%d", iter->topicName, iter->state);
         NSTopicLL * newTopicNode = NSCopyTopicNode(iter);
         NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(newTopicNode, NULL, NSRemoveTopicLL(newTopicHead));
 
index 3d7e75b..5c0bb29 100644 (file)
@@ -471,13 +471,17 @@ void NSConsumerCommunicationTaskProcessing(NSTask * task)
         while (iter)
         {
             topicLLSize ++;
+            NS_LOG_V(DEBUG, "[%d] Topic Name:%s\tTopic State:%d",
+                                        topicLLSize, iter->topicName, iter->state);
             iter = (NSTopicLL *) iter->next;
         }
 
         OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_CONSUMER_ID, *NSGetConsumerId());
+        NS_LOG_V(DEBUG, "NS_ATTRIBUTE_CONSUMER_ID: %s", *NSGetConsumerId());
 
         iter = topicLL;
         int iterSize = 0;
+        NS_LOG_V(DEBUG, "DimensionSize: %d", topicLLSize);
 
         OCRepPayload ** topicPayload = NULL;
         if (topicLLSize > 0)
@@ -492,10 +496,13 @@ void NSConsumerCommunicationTaskProcessing(NSTask * task)
                                             iter->topicName);
                 OCRepPayloadSetPropInt(topicPayload[iterSize], NS_ATTRIBUTE_TOPIC_SELECTION,
                                             iter->state);
+                NS_LOG_V(DEBUG, "NS_ATTRIBUTE_TOPIC_NAME: %s", iter->topicName);
+                NS_LOG_V(DEBUG, "NS_ATTRIBUTE_TOPIC_SELECTION: %d", iter->state);
                 iterSize++;
                 iter = iter->next;
             }
             size_t dimensions[3] = {topicLLSize, 0, 0};
+
             OCRepPayloadSetPropObjectArrayAsOwner(payload, NS_ATTRIBUTE_TOPIC_LIST,
                                                     topicPayload, dimensions);
         }
index fa6cb4d..48732cd 100644 (file)
@@ -167,6 +167,7 @@ NSResult NSConsumerGetInterestTopics(NSProvider * provider)
 
     NS_VERIFY_NOT_NULL(provider, NS_ERROR);
 
+    NS_LOG_V(DEBUG, "NSProvider ID: %s", provider->providerId);
     NSProvider_internal * prov = NSConsumerFindNSProvider(provider->providerId);
     NS_VERIFY_NOT_NULL(prov, NS_ERROR);
     NSSelector selector = prov->accessPolicy;
index 1ec6a7f..a1718e6 100644 (file)
@@ -322,7 +322,9 @@ void NSConsumerHandleRecvProviderChanged(NSMessage * msg)
     if (provider->connection->next == NULL && provider->accessPolicy == NS_SELECTION_CONSUMER)
     {
         NS_LOG(DEBUG, "call back to user");
-        NSProviderChanged((NSProvider *) provider, (NSResponse) msg->messageId);
+        NSProvider * prov = NSCopyProvider(provider);
+        NSProviderChanged(prov, (NSResponse) msg->messageId);
+        NSRemoveProvider_internal(provider);
     }
 }
 
@@ -397,11 +399,9 @@ void NSConsumerHandleRecvTopicLL(NSProvider_internal * provider)
     NSResult ret = NSProviderCacheUpdate(provider);
     NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
 
-    if (provider->connection->next == NULL)
-    {
-        NS_LOG(DEBUG, "call back to user");
-        NSProviderChanged((NSProvider *) provider, (NSResponse) NS_TOPIC);
-    }
+    NS_LOG(DEBUG, "call back to user");
+    NSProvider * prov = NSCopyProvider(provider);
+    NSProviderChanged((NSProvider *) prov, (NSResponse) NS_TOPIC);
 }
 
 void NSConsumerInternalTaskProcessing(NSTask * task)
index 55bc75e..8c8f5c9 100644 (file)
@@ -271,9 +271,16 @@ void NSConsumerTaskProcessing(NSTask * task)
         case TASK_CONSUMER_GET_TOPIC_LIST:
         case TASK_CONSUMER_SELECT_TOPIC_LIST:
         {
+            NSProvider * provider = (NSProvider *)task->taskData;
             NSProvider_internal * prov =
-                    NSConsumerFindNSProvider(((NSProvider *)task->taskData)->providerId);
+                    NSConsumerFindNSProvider(provider->providerId);
             NS_VERIFY_NOT_NULL_V(prov);
+            if (task->taskType == TASK_CONSUMER_SELECT_TOPIC_LIST)
+            {
+                NSRemoveTopicLL(prov->topicLL);
+                prov->topicLL = NSCopyTopicLL((NSTopicLL *)provider->topicLL);
+            }
+
             NSTask * topTask = NSMakeTask(task->taskType, prov);
             NS_VERIFY_NOT_NULL_V(topTask);
             NSConsumerCommunicationTaskProcessing(topTask);