From fef266b1c75e567c02bc705c93aa92d477a9a62e Mon Sep 17 00:00:00 2001 From: "ch79.cho" Date: Mon, 18 Jul 2016 17:34:11 +0900 Subject: [PATCH] add subscriber list for remote server The changes are modified to send messages to the subscribers from local and cloud concurrently. The service stores subscriber list from remote server seperately as well as local subscribers. Change-Id: Ide908a34a2c22360865df1e1f95dd131eb6d51e4 Signed-off-by: ch79.cho Reviewed-on: https://gerrit.iotivity.org/gerrit/9435 Tested-by: jenkins-iotivity Reviewed-by: Uze Choi --- .../examples/linux/notificationprovider.c | 106 +++++++++++---------- service/notification/include/NSCommon.h | 2 - service/notification/src/common/NSStructs.h | 12 ++- .../src/provider/NSProviderInterface.c | 4 +- .../src/provider/NSProviderNotification.c | 25 ++++- .../src/provider/NSProviderSubscription.c | 41 ++++++-- .../notification/src/provider/NSProviderSystem.c | 28 ++++++ .../provider/cache/linux/NSProviderMemoryCache.c | 21 +++- 8 files changed, 163 insertions(+), 76 deletions(-) diff --git a/service/notification/examples/linux/notificationprovider.c b/service/notification/examples/linux/notificationprovider.c index f4313e1..94c06ce 100644 --- a/service/notification/examples/linux/notificationprovider.c +++ b/service/notification/examples/linux/notificationprovider.c @@ -1,22 +1,22 @@ -/****************************************************************** - * - * Copyright 2015 Samsung Electronics All Rights Reserved. - * - * - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ******************************************************************/ +//****************************************************************** +// +// Copyright 2016 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #include #include @@ -32,6 +32,10 @@ #define TAG "NSProviderExample" +// Input the following values to publish resource to cloud +char REMOTE_SERVER_ADDRESS[50]; +char REMOTE_SERVER_SESSION[50]; + extern char *strdup(const char *s); bool isExit = false; @@ -45,7 +49,7 @@ void* OCProcessThread(void * ptr) { if (OCProcess() != OC_STACK_OK) { - OIC_LOG(ERROR, TAG, "OCStack process error"); + printf("OCStack process error"); return NULL; } } @@ -55,7 +59,7 @@ void* OCProcessThread(void * ptr) void subscribeRequestCallback(NSConsumer *consumer) { - OIC_LOG(INFO, TAG, "consumer requested to subscribe"); + printf("consumer requested to subscribe"); printf("NS_APP Consumer Device ID: %s\n", consumer->consumerId); @@ -64,7 +68,7 @@ void subscribeRequestCallback(NSConsumer *consumer) void syncCallback(NSSyncInfo *sync) { - OIC_LOG(INFO, TAG, "sync requested"); + printf("sync requested"); printf("NS_APP Sync State: %d\n", sync->state); } @@ -75,27 +79,26 @@ OCStackApplicationResult CloudLoginoutCallback(void *ctx, int CtxValue = 0x99; if (ctx != (void *)CtxValue) { - OIC_LOG(INFO, TAG, "Invalid Cloud Login/out callback received"); + printf("Invalid Cloud Login/out callback received"); } - OIC_LOG(INFO, TAG, "Login/out response received"); + printf("Login/out response received"); if (clientResponse->payload != NULL && clientResponse->payload->type == PAYLOAD_TYPE_REPRESENTATION) { - OIC_LOG(INFO, TAG, "PAYLOAD_TYPE_REPRESENTATION received"); + printf("PAYLOAD_TYPE_REPRESENTATION received"); OCRepPayloadValue *val = ((OCRepPayload *)clientResponse->payload)->values; - OIC_LOG(INFO, TAG, "Get payload values"); + printf("Get payload values"); while (val) { - OIC_LOG_V(INFO, TAG, "key: %s / Value: %s", val->name, val->str); + printf("key: %s / Value: %s", val->name, val->str); val = val->next; } - char *serverAddress = "coap+tcp://52.69.149.85:5683"; - NSProviderEnableRemoteService(serverAddress); + NSProviderEnableRemoteService(REMOTE_SERVER_ADDRESS); } return OC_STACK_KEEP_TRANSACTION; @@ -112,13 +115,7 @@ int main() int num; pthread_t processThread; - // cloud host address - const char *host = "coap+tcp://52.69.149.85:5683"; - // cloud auth session - const char *auth_session = "gZDRuDyYapZXIcrs"; - - - OIC_LOG(INFO, TAG, "NSStartProvider()"); + printf("NSStartProvider()"); // open oic_db static OCPersistentStorage ps = {server_fopen, fread, fwrite, fclose, unlink}; @@ -126,7 +123,7 @@ int main() if (OCInit(NULL, 0, OC_CLIENT_SERVER) != OC_STACK_OK) { - OIC_LOG(INFO, TAG, "OCStack init error"); + printf("OCStack init error"); return 0; } @@ -141,7 +138,7 @@ int main() printf("3. NSSendNotification() \n"); printf("4. NSRead \n"); printf("5. NSStopProvider() \n"); - printf("6. NSGetConsumerList \n"); + printf("11. NSCloudLogin \n"); printf("12. NSCloudLogout \n"); printf("0. Exit() \n"); @@ -156,15 +153,15 @@ int main() switch (num) { case 1: - OIC_LOG(INFO, TAG, "NSStartProvider(Accepter: Provider)"); + printf("NSStartProvider(Accepter: Provider)"); NSStartProvider(NS_ACCESS_ALLOW, subscribeRequestCallback, syncCallback); break; case 2: - OIC_LOG(INFO, TAG, "NSStartProvider(Accepter: Consumer)"); + printf("NSStartProvider(Accepter: Consumer)"); NSStartProvider(NS_ACCESS_DENY, subscribeRequestCallback, syncCallback); break; case 3: - OIC_LOG(INFO, TAG, "NSSendNotification()"); + printf("NSSendNotification()"); char title[100]; char body[100]; @@ -191,7 +188,7 @@ int main() break; case 4: - OIC_LOG(INFO, TAG, "NSRead"); + printf("NSRead"); NSSyncInfo * sync = (NSSyncInfo*) OICMalloc(sizeof(NSSyncInfo)); sync->messageId = OICStrdup("dev_001"); @@ -202,27 +199,32 @@ int main() case 5: NSStopProvider(); break; - case 6: - OIC_LOG(INFO, TAG, "NSGetConsumerList"); - break; + case 11: - OIC_LOG(INFO, TAG, "NSCloudLogin"); + printf("NSCloudLogin"); + + printf("Cloud Address: "); + gets(REMOTE_SERVER_ADDRESS); + + printf("Session Code: "); + gets(REMOTE_SERVER_SESSION); + - NSCloudLogin(host, auth_session, CloudLoginoutCallback); - OIC_LOG(INFO, TAG, "OCCloudLogin requested"); + NSCloudLogin(REMOTE_SERVER_ADDRESS, REMOTE_SERVER_SESSION, CloudLoginoutCallback); + printf("OCCloudLogin requested"); break; case 12: - OIC_LOG(INFO, TAG, "NSCloudLogout"); + printf("NSCloudLogout"); - NSCloudLogout(host, auth_session, CloudLoginoutCallback); - OIC_LOG(INFO, TAG, "OCCloudLogout requested"); + NSCloudLogout(REMOTE_SERVER_ADDRESS, REMOTE_SERVER_SESSION, CloudLoginoutCallback); + printf("OCCloudLogout requested"); break; case 0: NSStopProvider(); isExit = true; break; default: - OIC_LOG(INFO, TAG, "Under Construction"); + printf("Under Construction"); break; } diff --git a/service/notification/include/NSCommon.h b/service/notification/include/NSCommon.h index 1a28092..e5c64bb 100644 --- a/service/notification/include/NSCommon.h +++ b/service/notification/include/NSCommon.h @@ -27,8 +27,6 @@ #ifndef _NS_COMMON_H_ #define _NS_COMMON_H_ -#include - #define NS_ATTRIBUTE_POLICY "ACCEPTER" #define NS_ATTRIBUTE_MESSAGE "MESSAGE_URI" #define NS_ATTRIBUTE_SYNC "SYNC_URI" diff --git a/service/notification/src/common/NSStructs.h b/service/notification/src/common/NSStructs.h index e51f4ff..4ade4b1 100644 --- a/service/notification/src/common/NSStructs.h +++ b/service/notification/src/common/NSStructs.h @@ -52,15 +52,17 @@ typedef struct typedef struct { - char id[37]; // ip - int syncObId; - int messageObId; - bool isWhite; + char id[37]; + int syncObId; // sync resource observer ID for local consumer + int messageObId; // message resource observer ID for local consumer + int remote_syncObId; //sync observer ID subscribed through remote server + int remote_messageObId; //message observer ID subscribed through remote server + bool isWhite; // access state -> True: allowed / False: blocked } NSCacheSubData; typedef struct { - char * id; // ip ? ? ? + char * id; int messageType; // noti = 1, read = 2, dismiss = 3 NSMessage * nsMessage; } NSCacheMsgData; diff --git a/service/notification/src/provider/NSProviderInterface.c b/service/notification/src/provider/NSProviderInterface.c index 6fa5967..53baa56 100644 --- a/service/notification/src/provider/NSProviderInterface.c +++ b/service/notification/src/provider/NSProviderInterface.c @@ -152,6 +152,8 @@ NSResult NSProviderEnableRemoteService(char *serverAddress) } NS_LOG_V(DEBUG, "Remote server address: %s", serverAddress); + NSSetRemoteServerAddress(serverAddress); + NSPushQueue(DISCOVERY_SCHEDULER, TASK_PUBLISH_RESOURCE, serverAddress); pthread_mutex_unlock(&nsInitMutex); @@ -170,6 +172,7 @@ NSResult NSProviderDisableRemoteService(char *serverAddress) return NS_FAIL; } NS_LOG_V(DEBUG, "Remote server address: %s", serverAddress); + NSDeleteRemoteServerAddress(serverAddress); pthread_mutex_unlock(&nsInitMutex); NS_LOG(DEBUG, "NSProviderDisableRemoteService - OUT"); @@ -312,4 +315,3 @@ void * NSInterfaceSchedule(void * ptr) NS_LOG(DEBUG, "Destroy NSResponseSchedule"); return NULL; } - diff --git a/service/notification/src/provider/NSProviderNotification.c b/service/notification/src/provider/NSProviderNotification.c index b3fd1f8..a2a60f1 100644 --- a/service/notification/src/provider/NSProviderNotification.c +++ b/service/notification/src/provider/NSProviderNotification.c @@ -113,14 +113,20 @@ NSResult NSSendNotification(NSMessage *msg) while (it) { NSCacheSubData * subData = (NSCacheSubData *) it->data; - NS_LOG_V(DEBUG, "subData->id = %s", subData->id); + NS_LOG_V(DEBUG, "message subData->id = %s", subData->id); NS_LOG_V(DEBUG, "subData->messageId = %d", subData->messageObId); - NS_LOG_V(DEBUG, "subData->obID = %d", subData->syncObId); + NS_LOG_V(DEBUG, "subData->cloud_messageId = %d", subData->remote_messageObId); + NS_LOG_V(DEBUG, "subData->syncId = %d", subData->syncObId); + NS_LOG_V(DEBUG, "subData->cloud_syncId = %d", subData->remote_syncObId); NS_LOG_V(DEBUG, "subData->isWhite = %d", subData->isWhite); if (subData->isWhite) { - obArray[obCount++] = subData->messageObId; + if(subData->messageObId != 0) + obArray[obCount++] = subData->messageObId; + + if(subData->remote_messageObId != 0) + obArray[obCount++] = subData->remote_messageObId; } it = it->next; } @@ -177,9 +183,20 @@ NSResult NSSendSync(NSSyncInfo *sync) while (it) { NSCacheSubData * subData = (NSCacheSubData *) it->data; + NS_LOG_V(DEBUG, "sync subData->id = %s", subData->id); + NS_LOG_V(DEBUG, "subData->messageId = %d", subData->messageObId); + NS_LOG_V(DEBUG, "subData->cloud_messageId = %d", subData->remote_messageObId); + NS_LOG_V(DEBUG, "subData->syncId = %d", subData->syncObId); + NS_LOG_V(DEBUG, "subData->cloud_syncId = %d", subData->remote_syncObId); + NS_LOG_V(DEBUG, "subData->isWhite = %d", subData->isWhite); + if (subData->isWhite) { - obArray[obCount++] = subData->syncObId; + if(subData->syncObId != 0) + obArray[obCount++] = subData->syncObId; + + if(subData->remote_syncObId != 0) + obArray[obCount++] = subData->remote_syncObId; } it = it->next; } diff --git a/service/notification/src/provider/NSProviderSubscription.c b/service/notification/src/provider/NSProviderSubscription.c index c770bca..361a2e8 100644 --- a/service/notification/src/provider/NSProviderSubscription.c +++ b/service/notification/src/provider/NSProviderSubscription.c @@ -82,6 +82,8 @@ NSResult NSSendAccessPolicyResponse(OCEntityHandlerRequest *entityHandlerRequest return NS_ERROR; } + NS_LOG_V(DEBUG, "NS Provider ID: %s", NSGetProviderInfo()->providerId); + OCRepPayloadSetUri(payload, NS_ROOT_URI); OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, NSGetProviderInfo()->providerId); OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_POLICY, NSGetSubscriptionAccepter()); @@ -128,17 +130,26 @@ void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, NSResour NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData)); OICStrcpy(subData->id, UUID_STRING_SIZE, id); + NS_LOG_V(DEBUG, "SubList ID = [%s]", subData->id); + NS_LOG_V(DEBUG, "Consumer Address: %s", entityHandlerRequest->devAddr.addr); + if(NSIsRemoteServerAddress(entityHandlerRequest->devAddr.addr)) + { + NS_LOG(DEBUG, "Requested by remote server"); + subData->remote_messageObId = entityHandlerRequest->obsInfo.obsId; + NS_LOG_V(DEBUG, "SubList message observation ID = [%d]", subData->remote_messageObId); + } + else + { + NS_LOG(DEBUG, "Requested by local consumer"); + subData->messageObId = entityHandlerRequest->obsInfo.obsId; + NS_LOG_V(DEBUG, "SubList message observation ID = [%d]", subData->messageObId); + } subData->isWhite = false; - subData->messageObId = entityHandlerRequest->obsInfo.obsId; - subData->syncObId = 0; element->data = (void*) subData; element->next = NULL; - NS_LOG_V(DEBUG, "SubList IP[ID] = [%s]", subData->id); - NS_LOG_V(DEBUG, "SubList message observation ID = [%d]", subData->messageObId); - if (NSStorageWrite(consumerSubList, element) != NS_OK) { NS_LOG(DEBUG, "fail to write cache"); @@ -160,19 +171,29 @@ void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, NSResour NS_LOG(DEBUG, "resourceType == NS_RESOURCE_SYNC"); NSCacheElement * element = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement)); NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData)); + OICStrcpy(subData->id, UUID_STRING_SIZE, id); + NS_LOG_V(DEBUG, "SubList ID = [%s]", subData->id); + NS_LOG_V(DEBUG, "Consumer Address: %s", entityHandlerRequest->devAddr.addr); + if(NSIsRemoteServerAddress(entityHandlerRequest->devAddr.addr)) + { + NS_LOG(DEBUG, "Requested by remote server"); + subData->remote_syncObId = entityHandlerRequest->obsInfo.obsId; + NS_LOG_V(DEBUG, "SubList sync observation ID = [%d]", subData->remote_syncObId); + } + else + { + NS_LOG(DEBUG, "Requested by local consumer"); + subData->syncObId = entityHandlerRequest->obsInfo.obsId; + NS_LOG_V(DEBUG, "SubList sync observation ID = [%d]", subData->syncObId); + } subData->isWhite = false; - subData->syncObId = entityHandlerRequest->obsInfo.obsId; - subData->messageObId = 0; element->data = (void*) subData; element->next = NULL; - NS_LOG_V(DEBUG, "SubList IP[ID] = [%s]", subData->id); - NS_LOG_V(DEBUG, "SubList sync observation ID = [%d]", subData->syncObId); - if (NSStorageWrite(consumerSubList, element) != NS_OK) { NS_LOG(ERROR, "Fail to write cache"); diff --git a/service/notification/src/provider/NSProviderSystem.c b/service/notification/src/provider/NSProviderSystem.c index a9a69aa..2a6c6ee 100644 --- a/service/notification/src/provider/NSProviderSystem.c +++ b/service/notification/src/provider/NSProviderSystem.c @@ -19,7 +19,10 @@ //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #include "NSProviderSystem.h" +#define MAX_SERVER_ADDRESS 32 static NSConnectionState NSProviderConnectionState; +static char NSRemoteServerAddress[MAX_SERVER_ADDRESS] = {0,}; + NSProviderInfo * providerInfo; void NSSetProviderConnectionState(NSConnectionState state) @@ -36,6 +39,31 @@ NSConnectionState NSGetProviderConnectionState() return NSProviderConnectionState; } +void NSSetRemoteServerAddress(char *serverAddress) +{ + + OICStrcpy(NSRemoteServerAddress, MAX_SERVER_ADDRESS, serverAddress); +} + +void NSDeleteRemoteServerAddress(char *serverAddress) +{ + NS_LOG_V(DEBUG, "Delete cloud address: %s", serverAddress); + + memset(NSRemoteServerAddress, 0, MAX_SERVER_ADDRESS); +} + +bool NSIsRemoteServerAddress(char *serverAddress) +{ + NS_LOG_V(DEBUG, "Check server address: %s", serverAddress); + + if(serverAddress != NULL) + { + return strstr(NSRemoteServerAddress, serverAddress); + } + + return false; +} + void NSInitProviderInfo() { NS_LOG(DEBUG, "NSInitProviderInfo"); diff --git a/service/notification/src/provider/cache/linux/NSProviderMemoryCache.c b/service/notification/src/provider/cache/linux/NSProviderMemoryCache.c index 8ce8f97..fdf978b 100755 --- a/service/notification/src/provider/cache/linux/NSProviderMemoryCache.c +++ b/service/notification/src/provider/cache/linux/NSProviderMemoryCache.c @@ -99,6 +99,8 @@ NSResult NSCacheUpdateSubScriptionState(NSCacheList * list, char * id, bool stat NS_LOG_V(DEBUG, "currData_ID = %s", itData->id); NS_LOG_V(DEBUG, "currData_MsgObID = %d", itData->messageObId); NS_LOG_V(DEBUG, "currData_SyncObID = %d", itData->syncObId); + NS_LOG_V(DEBUG, "currData_Cloud_MsgObID = %d", itData->remote_messageObId); + NS_LOG_V(DEBUG, "currData_Cloud_SyncObID = %d", itData->remote_syncObId); NS_LOG_V(DEBUG, "currData_IsWhite = %d", itData->isWhite); NS_LOG_V(DEBUG, "update state = %d", state); @@ -156,23 +158,38 @@ NSResult NSStorageWrite(NSCacheList * list, NSCacheElement * newObj) NS_LOG_V(DEBUG, "currData_ID = %s", itData->id); NS_LOG_V(DEBUG, "currData_MsgObID = %d", itData->messageObId); NS_LOG_V(DEBUG, "currData_SyncObID = %d", itData->syncObId); + NS_LOG_V(DEBUG, "currData_Cloud_MsgObID = %d", itData->remote_messageObId); + NS_LOG_V(DEBUG, "currData_Cloud_SyncObID = %d", itData->remote_syncObId); NS_LOG_V(DEBUG, "currData_IsWhite = %d", itData->isWhite); NS_LOG_V(DEBUG, "subData_ID = %s", subData->id); NS_LOG_V(DEBUG, "subData_MsgObID = %d", subData->messageObId); NS_LOG_V(DEBUG, "subData_SyncObID = %d", subData->syncObId); + NS_LOG_V(DEBUG, "subData_Cloud_MsgObID = %d", subData->remote_messageObId); + NS_LOG_V(DEBUG, "subData_Cloud_SyncObID = %d", subData->remote_syncObId); NS_LOG_V(DEBUG, "subData_IsWhite = %d", subData->isWhite); - if (itData->messageObId == 0) + if (subData->messageObId != 0) { itData->messageObId = subData->messageObId; } - if (itData->syncObId == 0) + if (subData->syncObId != 0) { itData->syncObId = subData->syncObId; } + if (subData->remote_messageObId != 0) + { + itData->remote_messageObId = subData->remote_messageObId; + } + + if (subData->remote_syncObId != 0) + { + itData->remote_syncObId = subData->remote_syncObId; + NS_LOG_V(DEBUG, "sync id cached: %d", itData->remote_syncObId); + } + NS_LOG(DEBUG, "Update Data - OUT"); pthread_mutex_unlock(&NSCacheMutex); -- 2.7.4