From a9368e1fdad242443530e4c3aacb4099c2e6bcc4 Mon Sep 17 00:00:00 2001 From: "jaesick.shin" Date: Wed, 9 Nov 2016 13:23:59 +0900 Subject: [PATCH] [IoT-1523] modify example about MQ this patch include, 1. modify example for MQ. 2. change discovery scheduler to subscription scheduler about MQ task. 3. add defined WITH_MQ part. 4. When sendNotification worked, I changed the location to publish MQ. Change-Id: I836eed003a4da883fd2722ffead343d0bbb97660 Signed-off-by: jaesick.shin Reviewed-on: https://gerrit.iotivity.org/gerrit/14139 Tested-by: jenkins-iotivity Reviewed-by: Uze Choi --- .../examples/linux/notificationprovider.c | 22 +++++++++++-- .../src/provider/NSProviderInterface.c | 4 ++- .../notification/src/provider/NSProviderListener.c | 9 +++++- .../notification/src/provider/NSProviderListener.h | 2 ++ .../src/provider/NSProviderNotification.c | 36 ++++++++++++++-------- .../src/provider/NSProviderSubscription.c | 16 ++++++++-- .../notification/src/provider/NSProviderSystem.c | 1 + 7 files changed, 71 insertions(+), 19 deletions(-) diff --git a/service/notification/examples/linux/notificationprovider.c b/service/notification/examples/linux/notificationprovider.c index c864669..221013a 100644 --- a/service/notification/examples/linux/notificationprovider.c +++ b/service/notification/examples/linux/notificationprovider.c @@ -46,6 +46,11 @@ char UID[50] = {'\0',}; char ACCESS_TOKEN[50] = {'\0',}; #endif +#ifdef WITH_MQ +char CLOUD_ADDRESS[100] = "10.113.64.52:5686"; +char CLOUD_TOPIC[100] = "/oic/ps/notification"; +#endif + char mainConsumer[37] = {'\0',}; extern char *strdup(const char *s); @@ -181,7 +186,8 @@ int main() printf("8. NSProviderUnselectTopic(); \n"); printf("9. NSProviderGetConsumerTopics(); \n"); printf("10. NSProviderGetTopics(); \n"); - printf("11. NSStopProvider() \n"); + printf("11. NSProviderSubscribeMQService() \n"); + printf("0. NSStopProvider() \n"); #ifdef WITH_CLOUD printf("21. NSProviderEnableRemoteService (after login) \n"); printf("22. NSProviderDisableRemoteService (after login) \n"); @@ -249,6 +255,7 @@ int main() input(body); printf("topic : "); + input(topic); printf("app - mTitle : %s \n", title); @@ -321,9 +328,20 @@ int main() } break; +#if WITH_MQ case 11: - NSStopProvider(); + printf("11. Subscribe MQ Service\n"); + + printf("Remote Server Address: "); + //input(CLOUD_ADDRESS); + + printf("Topic[notificationtest]: "); + //input(CLOUD_TOPIC); + + NSProviderSubscribeMQService(CLOUD_ADDRESS, CLOUD_TOPIC); break; +#endif + #ifdef WITH_CLOUD case 21: printf("Enable Remote Service\n"); diff --git a/service/notification/src/provider/NSProviderInterface.c b/service/notification/src/provider/NSProviderInterface.c index dad01ba..83fa73b 100644 --- a/service/notification/src/provider/NSProviderInterface.c +++ b/service/notification/src/provider/NSProviderInterface.c @@ -220,7 +220,9 @@ NSResult NSProviderSubscribeMQService(const char * serverAddress, const char * t topicAddr->serverAddr = NSGetQueryAddress(serverAddress); topicAddr->topicName = OICStrdup(topicName); - NSPushQueue(DISCOVERY_SCHEDULER, TASK_MQ_REQ_SUBSCRIBE, (void *) topicAddr); + NS_LOG_V(DEBUG, "input Topic Name : %s", topicAddr->topicName); + + NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_MQ_REQ_SUBSCRIBE, (void *) topicAddr); pthread_mutex_unlock(&nsInitMutex); NS_LOG(DEBUG, "NSProviderSubscribeMQService - OUT"); diff --git a/service/notification/src/provider/NSProviderListener.c b/service/notification/src/provider/NSProviderListener.c index e6c528f..c3b4667 100644 --- a/service/notification/src/provider/NSProviderListener.c +++ b/service/notification/src/provider/NSProviderListener.c @@ -332,6 +332,8 @@ OCStackApplicationResult NSProviderMQListener(void * ctx, OCDoHandle handle, (void) ctx; (void) handle; + NS_LOG_V(DEBUG, "clientResponse->sequenceNumber = %d", clientResponse->sequenceNumber); + if (clientResponse->sequenceNumber == OC_OBSERVE_REGISTER) { NS_LOG(DEBUG, "MQ OC_OBSERVE_RIGSTER"); @@ -397,6 +399,8 @@ OCStackApplicationResult NSProviderMQListener(void * ctx, OCDoHandle handle, OCStackApplicationResult NSProviderIntrospectMQTopic(void * ctx, OCDoHandle handle, OCClientResponse * clientResponse) { + NS_LOG(DEBUG, "NSProviderIntrospectMQTopic - IN"); + (void) handle; NS_VERIFY_NOT_NULL(clientResponse, OC_STACK_KEEP_TRANSACTION); @@ -421,6 +425,8 @@ OCStackApplicationResult NSProviderIntrospectMQTopic(void * ctx, OCDoHandle hand NS_ATTIRBUTE_MQ_TOPICLIST, & topicList, dimensions); char * interestTopicName = (char *) ctx; + + NS_LOG_V(DEBUG, "interestTopicName = %s", interestTopicName); for (size_t i = 0; i < dimensions[0]; ++i) { NS_LOG_V(DEBUG, "found MQ topic : %s", topicList[i]); @@ -434,7 +440,7 @@ OCStackApplicationResult NSProviderIntrospectMQTopic(void * ctx, OCDoHandle hand cbdata.cd = NULL; OCStackResult ret = OCDoResource(NULL, OC_REST_OBSERVE, topicList[i], - clientResponse->addr, NULL, CT_DEFAULT, OC_LOW_QOS, &cbdata, NULL, 0); + clientResponse->addr, NULL, CT_DEFAULT, OC_HIGH_QOS, &cbdata, NULL, 0); if (!NSOCResultToSuccess(ret)) { @@ -444,6 +450,7 @@ OCStackApplicationResult NSProviderIntrospectMQTopic(void * ctx, OCDoHandle hand } } + NS_LOG(DEBUG, "NSProviderIntrospectMQTopic - OUT"); return OC_STACK_KEEP_TRANSACTION; } diff --git a/service/notification/src/provider/NSProviderListener.h b/service/notification/src/provider/NSProviderListener.h index 1c1e14a..ea4a48c 100644 --- a/service/notification/src/provider/NSProviderListener.h +++ b/service/notification/src/provider/NSProviderListener.h @@ -51,10 +51,12 @@ void NSProviderConnectionStateListener(const CAEndpoint_t * info, bool isConnect void NSProviderAdapterStateListener(CATransportAdapter_t adapter, bool enabled); +#ifdef WITH_MQ OCStackApplicationResult NSProviderIntrospectMQTopic(void * ctx, OCDoHandle handle, OCClientResponse * clientResponse); OCStackApplicationResult NSProviderPublishTopicCB(void *ctx, OCDoHandle handle, OCClientResponse *clientResponse); +#endif #endif /* _NS_PROVIDER_LISTENER__H_ */ diff --git a/service/notification/src/provider/NSProviderNotification.c b/service/notification/src/provider/NSProviderNotification.c index f069d7d..affc533 100644 --- a/service/notification/src/provider/NSProviderNotification.c +++ b/service/notification/src/provider/NSProviderNotification.c @@ -19,6 +19,8 @@ //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #include "NSProviderNotification.h" +#include "NSProviderListener.h" +#include "NSProviderSystem.h" NSResult NSSetMessagePayload(NSMessage *msg, OCRepPayload** msgPayload) { @@ -79,6 +81,7 @@ NSResult NSSetSyncPayload(NSSyncInfo *sync, OCRepPayload** syncPayload) #ifdef WITH_MQ OCStackResult NSProviderPublishTopic(OCRepPayload * payload, OCClientResponseHandler response) { + NS_LOG(DEBUG, "NSProviderPublishTopic - IN"); OCCallbackData cbData; memset(&cbData, 0, sizeof(OCCallbackData)); cbData.cb = response; @@ -95,6 +98,15 @@ OCStackResult NSProviderPublishTopic(OCRepPayload * payload, OCClientResponseHan OCRepPayloadSetPropObject(publishPayload, NS_ATTRIBUTE_MQ_MESSAGE, payload); NSMQServerInfo * serverInfo = NSGetMQServerInfo(); + + if (!serverInfo) + { + NS_LOG(DEBUG, "serverInfo is not NULL"); + NS_LOG_V(DEBUG, "serverInfo->serverUri = %s", serverInfo->serverUri); + } + + NS_LOG(DEBUG, "NSProviderPublishTopic - OUT"); + return OCDoResource(NULL, OC_REST_POST, serverInfo->serverUri, serverInfo->devAddr, (OCPayload *)publishPayload, CT_ADAPTER_TCP, OC_LOW_QOS, &cbData, NULL, 0); @@ -115,17 +127,24 @@ NSResult NSSendNotification(NSMessage *msg) return NS_ERROR; } - if (consumerSubList->head == NULL) + OCRepPayload* payload = NULL; + + if (NSSetMessagePayload(msg, &payload) != NS_OK) { - NS_LOG(ERROR, "SubList->head is NULL, empty SubList"); + NS_LOG(ERROR, "fail to Get message payload"); return NS_ERROR; } - OCRepPayload* payload = NULL; +#ifdef WITH_MQ + if (NSGetMQServerInfo()) + { + NSProviderPublishTopic(payload, NSProviderPublishTopicCB); + } +#endif - if (NSSetMessagePayload(msg, &payload) != NS_OK) + if (consumerSubList->head == NULL) { - NS_LOG(ERROR, "fail to Get message payload"); + NS_LOG(ERROR, "SubList->head is NULL, empty SubList"); return NS_ERROR; } @@ -178,14 +197,7 @@ NSResult NSSendNotification(NSMessage *msg) } #endif -#ifdef WITH_MQ - if (!NSGetMQServerInfo()) - { - NSProviderPublishTopic(payload, NSProviderPublishTopicCB); - } -#endif } - it = it->next; } diff --git a/service/notification/src/provider/NSProviderSubscription.c b/service/notification/src/provider/NSProviderSubscription.c index 8474de3..423491e 100644 --- a/service/notification/src/provider/NSProviderSubscription.c +++ b/service/notification/src/provider/NSProviderSubscription.c @@ -19,6 +19,7 @@ //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #include "NSProviderSubscription.h" +#include "NSProviderListener.h" NSResult NSInitSubscriptionList() { @@ -351,14 +352,23 @@ void NSProviderMQSubscription(NSMQTopicAddress * topicAddr) char * serverUri = topicAddr->serverAddr; char * topicName = topicAddr->topicName; + NS_LOG_V(DEBUG, "input Topic Name2 : %s", topicAddr->topicName); + OCDevAddr * addr = NSChangeAddress(serverUri); OCCallbackData cbdata = { NULL, NULL, NULL }; cbdata.cb = NSProviderIntrospectMQTopic; - cbdata.context = topicName; + cbdata.context = OICStrdup(topicName); cbdata.cd = OICFree; - OCStackResult ret = OCDoResource(NULL, OC_REST_GET, serverUri, addr, - NULL, CT_DEFAULT, OC_LOW_QOS, &cbdata, NULL, 0); + char requestUri[100] = "coap+tcp://"; + + NS_LOG_V(DEBUG, "requestUri1 = %s", requestUri); + OICStrcat(requestUri, strlen(requestUri)+strlen(serverUri)+1, serverUri); + NS_LOG_V(DEBUG, "requestUri2 = %s", requestUri); + OICStrcat(requestUri, strlen(requestUri)+ strlen("/oic/ps") + 1, "/oic/ps"); + NS_LOG_V(DEBUG, "requestUri3 = %s", requestUri); + OCStackResult ret = OCDoResource(NULL, OC_REST_GET, requestUri, addr, + NULL, CT_DEFAULT, OC_HIGH_QOS, &cbdata, NULL, 0); NSOCResultToSuccess(ret); diff --git a/service/notification/src/provider/NSProviderSystem.c b/service/notification/src/provider/NSProviderSystem.c index 0018737..089a5b7 100644 --- a/service/notification/src/provider/NSProviderSystem.c +++ b/service/notification/src/provider/NSProviderSystem.c @@ -156,6 +156,7 @@ void NSSetMQServerInfo(const char * serverUri, OCDevAddr * devAddr) { if (!mqServerInfo) { + NS_LOG(DEBUG, "setMqServer"); mqServerInfo = (NSMQServerInfo *)OICMalloc(sizeof(NSMQServerInfo)); mqServerInfo->serverUri = OICStrdup(serverUri); mqServerInfo->devAddr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr)); -- 2.7.4