TASK_RECV_SYNCINFO = 5000,
TASK_RECV_READ = 5001,
TASK_RECV_DISMISS = 5003,
- TASK_SEND_SYNCINFO = 5100,
+ TASK_SEND_SYNCINFO = 5099,
+ TASK_MAKE_SYNCINFO = 5100,
TASK_SEND_READ = 5101,
TASK_SEND_DISMISS = 5102,
NS_PROVIDER_CACHE_SUBSCRIBER = 1000,
NS_PROVIDER_CACHE_MESSAGE = 1001,
+ NS_CONSUMER_CACHE_PROVIDER = 2000,
NS_CONSUMER_CACHE_MESSAGE = 2001,
} NSCacheType;
NSMessage_consumer * newMsg = (NSMessage_consumer *)OICMalloc(sizeof(NSMessage_consumer));
NS_VERIFY_NOT_NULL(newMsg, NULL);
- newMsg->addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
+ OICStrcpy(newMsg->providerId, NS_DEVICE_ID_LENGTH, msg->providerId);
+ newMsg->_addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(newMsg, NULL, OICFree(newMsg));
- memcpy(newMsg->addr, msg->addr, sizeof(OCDevAddr));
+ memcpy(newMsg->_addr, msg->_addr, sizeof(OCDevAddr));
newMsg->messageId = msg->messageId;
newMsg->title = OICStrdup(msg->title);
NSOICFree(msg->title);
NSOICFree(msg->contentText);
NSOICFree(msg->sourceName);
- NSOICFree(msg->addr);
+ NSOICFree(msg->_addr);
NSOICFree(msg);
}
+NSProvider_internal * NSCopyProvider(NSProvider_internal * prov)
+{
+ NS_VERIFY_NOT_NULL(prov, NULL);
+
+ NSProvider_internal * newProv = (NSProvider_internal *)OICMalloc(sizeof(NSProvider_internal));
+ NS_VERIFY_NOT_NULL(newProv, NULL);
+
+ OICStrcpy(newProv->providerId, NS_DEVICE_ID_LENGTH, prov->providerId);
+ newProv->_addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
+ NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(newProv, NULL, OICFree(newProv));
+ memcpy(newProv->_addr, prov->_addr, sizeof(OCDevAddr));
+
+ newProv->messageUri = OICStrdup(prov->messageUri);
+ newProv->syncUri = OICStrdup(prov->syncUri);
+ newProv->messageHandle = prov->messageHandle;
+ newProv->syncHandle = prov->syncHandle;
+ newProv->accessPolicy = prov->accessPolicy;
+
+ return newProv;
+}
+void NSRemoveProvider(NSProvider_internal * prov)
+{
+ NSOICFree(prov->messageUri);
+ NSOICFree(prov->syncUri);
+ NSOICFree(prov->messageHandle);
+ NSOICFree(prov->syncHandle);
+ NSOICFree(prov->_addr);
+
+ NSOICFree(prov);
+}
+
OCStackResult NSInvokeRequest(OCDoHandle * handle,
OCMethod method, const OCDevAddr * addr,
const char * queryUrl, OCPayload * payload,
#include "NSStructs.h"
#include "ocstack.h"
-
#define NS_QOS OC_LOW_QOS
#define NS_RESOURCE_TYPE "oic.r.notification"
#define NS_RESOURCE_URI "/notification"
} \
}
+#define NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(obj, func) \
+ { \
+ if ((obj) == NULL) \
+ { \
+ NS_LOG_V(ERROR, "%s : %s is null", __func__, #obj); \
+ NS_LOG(ERROR, "execute deletion"); \
+ (func); \
+ return; \
+ } \
+ }
+
#define NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(obj, retVal, func) \
{ \
if ((obj) == NULL) \
} \
}
-typedef enum
-{
- Read,
- Dismiss,
- Notification,
-} NSConsumerMessageTypes;
-
typedef struct
{
- char providerId[37];
+ char providerId[NS_DEVICE_ID_LENGTH];
char * messageUri;
char * syncUri;
OCDoHandle messageHandle;
OCDoHandle syncHandle;
- OCDevAddr * addr;
+ OCDevAddr * _addr;
NSAccessPolicy accessPolicy;
} NSProvider_internal;
+typedef struct
+{
+ uint64_t messageId;
+ char providerId[NS_DEVICE_ID_LENGTH];
+ NSSyncType state;
+
+ OCDevAddr * _addr;
+} NSSyncInfo_internal;
+
typedef struct
{
// Mandatory
uint64_t messageId;
- char providerId[37];
+ char providerId[NS_DEVICE_ID_LENGTH];
//optional
NSMessageType type;
char * sourceName;
NSMediaContents mediaContents;
- OCDevAddr * addr;
- NSConsumerMessageTypes messageTypes;
+ OCDevAddr * _addr;
+ //NSConsumerMessageTypes messageTypes;
+ NSSyncType _messageTypes;
} NSMessage_consumer;
bool NSIsStartedConsumer();
NSMessage_consumer * NSCopyMessage(NSMessage_consumer *);
void NSRemoveMessage(NSMessage_consumer *);
+NSProvider_internal * NSCopyProvider(NSProvider_internal *);
+void NSRemoveProvider(NSProvider_internal *);
+
OCStackResult NSInvokeRequest(OCDoHandle * handle,
OCMethod method, const OCDevAddr * addr,
const char * queryUrl, OCPayload * payload,
NS_LOG(DEBUG, "subscribe message");
NS_LOG_V(DEBUG, "subscribe query : %s", query);
OCStackResult ret = NSInvokeRequest(&(provider_internal->messageHandle),
- OC_REST_OBSERVE, provider_internal->addr,
+ OC_REST_OBSERVE, provider_internal->_addr,
query, NULL, NSConsumerMessageListener, NULL);
NS_VERIFY_STACK_OK(ret, NS_ERROR);
NSOICFree(query);
NS_LOG(DEBUG, "subscribe sync");
NS_LOG_V(DEBUG, "subscribe query : %s", query);
ret = NSInvokeRequest(&(provider_internal->syncHandle),
- OC_REST_OBSERVE, provider_internal->addr,
+ OC_REST_OBSERVE, provider_internal->_addr,
query, NULL, NSConsumerSyncInfoListener, NULL);
NS_VERIFY_STACK_OK(ret, NS_ERROR);
NSOICFree(query);
NS_LOG(DEBUG, "get provider id");
char * pId = NULL;
getResult = OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, &pId);
+ NS_LOG_V (DEBUG, "provider id: %s", pId);
NS_VERIFY_NOT_NULL(getResult == true ? (void *) 1 : NULL, NULL);
NS_LOG(DEBUG, "get provider address");
memcpy(addr, clientResponse->addr, sizeof(OCDevAddr));
NS_LOG(DEBUG, "create NSMessage");
- NSMessage_consumer * retNoti = NSCreateMessage_internal(id, pId);
- NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(retNoti, NULL, NSGetMessagePostClean(pId, addr));
+ NSMessage_consumer * retMsg = NSCreateMessage_internal(id, pId);
+ NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(retMsg, NULL, NSGetMessagePostClean(pId, addr));
NSOICFree(pId);
- retNoti->addr = addr;
- retNoti->messageTypes = Notification;
+ retMsg->_addr = addr;
+ retMsg->_messageTypes = NS_SYNC_UNREAD;
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);
-
- OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_TYPE, (int64_t *)&retNoti->type);
- OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_DATETIME, &retNoti->dateTime);
- OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_TTL, (int64_t *)&retNoti->ttl);
-
- 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);
-
- return retNoti;
+ OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_TITLE, &retMsg->title);
+ OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_TEXT, &retMsg->contentText);
+ OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_SOURCE, &retMsg->sourceName);
+
+ OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_TYPE, (int64_t *)&retMsg->type);
+ OCRepPayloadGetPropString(payload, NS_ATTRIBUTE_DATETIME, &retMsg->dateTime);
+ OCRepPayloadGetPropInt(payload, NS_ATTRIBUTE_TTL, (int64_t *)&retMsg->ttl);
+
+ NS_LOG_V(DEBUG, "Msg Address : %s", retMsg->_addr->addr);
+ NS_LOG_V(DEBUG, "Msg ID : %ld", 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 : %ld", retMsg->ttl);
+
+ return retMsg;
}
NSSyncInfo * NSGetSyncInfoc(OCClientResponse * clientResponse)
NSMessage_consumer * NSCreateMessage_internal(uint64_t id, const char * providerId)
{
- NSMessage_consumer * retNoti = (NSMessage_consumer *)OICMalloc(sizeof(NSMessage_consumer));
- NS_VERIFY_NOT_NULL(retNoti, NULL);
-
- 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;
-
- return retNoti;
+ NSMessage_consumer * retMsg = (NSMessage_consumer *)OICMalloc(sizeof(NSMessage_consumer));
+ NS_VERIFY_NOT_NULL(retMsg, NULL);
+
+ retMsg->messageId = id;
+ OICStrcpy(retMsg->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, providerId);
+ retMsg->title = NULL;
+ retMsg->contentText = NULL;
+ retMsg->sourceName = NULL;
+ retMsg->type = NS_MESSAGE_INFO;
+ retMsg->dateTime = NULL;
+ retMsg->ttl = 0;
+ retMsg->_addr = NULL;
+
+ return retMsg;
}
NSSyncInfo * NSCreateSyncInfo_consumer(uint64_t msgId, const char * providerId, NSSyncType state)
return retSync;
}
+OCStackResult NSSendSyncInfo(NSSyncInfo * syncInfo, OCDevAddr * addr)
+{
+ OCRepPayload * payload = OCRepPayloadCreate();
+ NS_VERIFY_NOT_NULL(payload, OC_STACK_ERROR);
+
+ OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t)syncInfo->messageId);
+ OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_STATE, syncInfo->state);
+ OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, syncInfo->providerId);
+
+ return NSInvokeRequest(NULL, OC_REST_POST, addr,
+ NS_SYNC_URI, (OCPayload*)payload,
+ NSConsumerCheckPostResult, NULL);
+}
+
void NSConsumerCommunicationTaskProcessing(NSTask * task)
{
NS_VERIFY_NOT_NULL_V(task);
else if (task->taskType == TASK_SEND_SYNCINFO)
{
// TODO find target OCDevAddr using provider Id.
- //OCStackResult ret = NSSendSyncInfo((NSSyncInfo *)task->taskData, );
- //NS_VERTIFY_STACK_OK_V(ret);
+ NSSyncInfo_internal * syncInfo = (NSSyncInfo_internal *)task->taskData;
+ OCDevAddr * addr = syncInfo->_addr;
+ OCStackResult ret = NSSendSyncInfo((NSSyncInfo *)(task->taskData), addr);
+ NS_VERIFY_STACK_OK_V(ret);
+
+ NSOICFree(syncInfo->_addr);
+ NSOICFree(syncInfo);
}
else if (task->taskType == TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL)
{
NS_LOG(ERROR, "Unknown type message");
}
}
-
-OCStackResult NSSendSyncInfo(NSSyncInfo * syncInfo, OCDevAddr * addr)
-{
- OCRepPayload * payload = OCRepPayloadCreate();
- NS_VERIFY_NOT_NULL(payload, OC_STACK_ERROR);
-
- OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t)syncInfo->messageId);
- OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_STATE, syncInfo->state);
-
- return NSInvokeRequest(NULL, OC_REST_POST, addr,
- NS_SYNC_URI, (OCPayload*)payload,
- NSConsumerCheckPostResult, NULL);
-}
provider->messageHandle = NULL;
provider->syncHandle = NULL;
- NSOICFree(provider->addr);
+ NSOICFree(provider->_addr);
NSOICFree(provider);
}
addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(addr, NULL,
NSGetProviderPostClean(providerId, messageUri, syncUri, addr));
+
memcpy(addr, clientResponse->addr, sizeof(OCDevAddr));
NSProvider_internal * newProvider
newProvider->messageUri = messageUri;
newProvider->syncUri = syncUri;
newProvider->accessPolicy = (NSAccessPolicy)accepter;
- newProvider->addr = addr;
+ newProvider->_addr = addr;
newProvider->messageHandle = NULL;
newProvider->syncHandle = NULL;
syncInfo->messageId = messageId;
syncInfo->state = type;
- NSTask * syncTask = NSMakeTask(TASK_SEND_SYNCINFO, (void *) syncInfo);
+ NSTask * syncTask = NSMakeTask(TASK_MAKE_SYNCINFO, (void *) syncInfo);
NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(syncTask, NS_ERROR, NSOICFree(syncInfo));
return NSConsumerPushEvent(syncTask);
#include "oic_malloc.h"
#include "oic_string.h"
-NSCacheList ** NSGetCacheList()
+NSCacheList ** NSGetMessageCacheList()
{
- static NSCacheList * cache = NULL;
- return & cache;
+ static NSCacheList * messageCache = NULL;
+ return & messageCache;
}
-void NSSetCacheList(NSCacheList * cache)
+void NSSetMessageCacheList(NSCacheList * cache)
{
- *(NSGetCacheList()) = cache;
+ *(NSGetMessageCacheList()) = cache;
}
-void NSDestroyCacheList()
+NSCacheList ** NSGetProviderCacheList()
{
- NSCacheList * cache = *(NSGetCacheList());
+ static NSCacheList * providerCache = NULL;
+ return & providerCache;
+}
+
+void NSSetProviderCacheList(NSCacheList * cache)
+{
+ *(NSGetProviderCacheList()) = cache;
+}
+
+void NSDestroyMessageCacheList()
+{
+ NSCacheList * cache = *(NSGetMessageCacheList());
+ if (cache)
+ {
+ NSStorageDestroy(cache);
+ }
+}
+
+void NSDestroyProviderCacheList()
+{
+ NSCacheList * cache = *(NSGetProviderCacheList());
if (cache)
{
NSStorageDestroy(cache);
}
}
-NSResult NSCacheUpdate(NSCacheList * cache, NSTask * task, NSConsumerMessageTypes type)
+NSResult NSMessageCacheUpdate(NSCacheList * cache, NSMessage_consumer * msg, NSSyncType type)
{
- NSMessage_consumer * noti = (NSMessage_consumer *) task->taskData;
- NS_VERIFY_NOT_NULL(noti, NS_ERROR);
+ NS_VERIFY_NOT_NULL(msg, NS_ERROR);
- noti->type = type;
+ msg->type = type;
NSCacheElement * obj = (NSCacheElement *)OICMalloc(sizeof(NSCacheElement));
NS_VERIFY_NOT_NULL(obj, NS_ERROR);
- obj->data = (NSCacheData *) noti;
+ obj->data = (NSCacheData *) msg;
obj->next = NULL;
NS_LOG(DEBUG, "try to write to storage");
NSResult ret = NSStorageWrite(cache, obj);
NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(ret == NS_OK ? (void *) 1 : NULL,
- NS_ERROR, NSRemoveMessage(noti));
+ NS_ERROR, NSRemoveMessage(msg));
- NSRemoveMessage(noti);
+ //NSRemoveMessage(msg);
+ NSOICFree(obj);
+
+ return NS_OK;
+}
+
+NSResult NSProviderCacheUpdate(NSCacheList * cache, NSProvider_internal * provider)
+{
+ NS_VERIFY_NOT_NULL(provider, NS_ERROR);
+
+ NSCacheElement * obj = (NSCacheElement *)OICMalloc(sizeof(NSCacheElement));
+ NS_VERIFY_NOT_NULL(obj, NS_ERROR);
+
+ obj->data = (NSCacheData *) provider;
+ obj->next = NULL;
+
+ NS_LOG(DEBUG, "try to write to storage");
+ NSResult ret = NSStorageWrite(cache, obj);
+ NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(ret == NS_OK ? (void *) 1 : NULL,
+ NS_ERROR, NSRemoveProvider(provider));
+
+ //NSRemoveProvider(provider);
NSOICFree(obj);
return NS_OK;
void NSConsumerHandleProviderDiscovered(NSProvider_internal * provider)
{
- // TODO need to check for discovered provider is new and store provider.
+ // TODO need to check for discovered provider is new and store provider.[DONE]
+ NS_VERIFY_NOT_NULL_V(provider);
+
+ NSCacheList * cache = *(NSGetProviderCacheList());
+ NSCacheElement * cacheElement = NSStorageRead(cache, provider->providerId);
+ if (cacheElement)
+ {
+ NS_LOG_V (ERROR, "Provider is already discovered - ProviderID[%s]", provider->providerId);
+ return ;
+ }
+ else
+ {
+ NS_LOG (ERROR, "New provider is discovered");
+ NSResult ret = NSProviderCacheUpdate(cache, provider);
+ NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
+ }
if (provider->accessPolicy == NS_ACCESS_DENY)
{
{
NS_VERIFY_NOT_NULL_V(msg);
- // TODO change to find provider using pId.
- NSProvider * provider = NULL;
- {
- provider = (NSProvider *)OICMalloc(sizeof(NSProvider));
- NS_VERIFY_NOT_NULL_V(provider);
- OICStrcpy(provider->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, msg->providerId);
- }
+ // TODO change to find provider using pId. [DONE]
+ NSCacheList * cache = *(NSGetProviderCacheList());
+ NSCacheElement * cacheElement = NSStorageRead(cache, msg->providerId);
+ NS_VERIFY_NOT_NULL_V (cacheElement);
+
+ NSProvider * provider = (NSProvider *) cacheElement->data;
NSSubscriptionAccepted(provider);
}
void NSConsumerHandleRecvMessage(NSMessage_consumer * msg)
{
NS_VERIFY_NOT_NULL_V(msg);
- // TODO store message to cache
-// NSResult ret = NSCacheUpdate(cache, task, Notification);
-// NS_VERTIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
+ // TODO store message to cache [DONE]
+ NSCacheList * cache = *(NSGetMessageCacheList());
+ NSResult ret = NSMessageCacheUpdate(cache, msg, NS_SYNC_UNREAD);
+ NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
NSMessagePost((NSMessage *) msg);
}
{
NS_VERIFY_NOT_NULL_V(sync);
- // TODO need to check for provider is available.
- // 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);
+ // TODO need to check for provider is available. [DONE]
+ NSCacheList * providerCache = *(NSGetProviderCacheList());
+ NSCacheElement * providerCacheElement = NSStorageRead(providerCache, sync->providerId);
+ NS_VERIFY_NOT_NULL_V (providerCacheElement);
+
+ // TODO need to update msg list. [DONE]
+ char msgId[NS_DEVICE_ID_LENGTH] = { 0, };
+ snprintf(msgId, NS_DEVICE_ID_LENGTH, "%ld", sync->messageId);
+
+ NSCacheList * messageCache = *(NSGetMessageCacheList());
+ NSCacheElement * messageCacheElement = NSStorageRead(messageCache, msgId);
+ NS_VERIFY_NOT_NULL_V (messageCacheElement);
+
+ NSMessage_consumer * msg = (NSMessage_consumer *) messageCacheElement->data;
+ NSResult ret = NSMessageCacheUpdate(messageCache, msg, sync->state);
+ NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
NSNotificationSync(sync);
}
+void NSConsumerHandleMakeSyncInfo(NSSyncInfo * sync)
+{
+ NS_VERIFY_NOT_NULL_V(sync);
+
+ // TODO need to check for provider is available. [DONE]
+ NSCacheList * providerCache = *(NSGetProviderCacheList());
+ NSCacheElement * providerCacheElement = NSStorageRead(providerCache, sync->providerId);
+ NS_VERIFY_NOT_NULL_V (providerCacheElement);
+ NSProvider_internal * provider = (NSProvider_internal *) providerCacheElement->data;
+ NS_VERIFY_NOT_NULL_V (provider);
+
+ NSSyncInfo_internal * syncInfo = (NSSyncInfo_internal *)OICMalloc(sizeof(NSSyncInfo_internal));
+ NS_VERIFY_NOT_NULL_V(syncInfo);
+
+ OICStrcpy(syncInfo->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, sync->providerId);
+ syncInfo->messageId = sync->messageId;
+ syncInfo->state = sync->state;
+ syncInfo->_addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
+ NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(syncInfo->_addr, NSOICFree(syncInfo));
+ memcpy(syncInfo->_addr, provider->_addr, sizeof(OCDevAddr));
+
+ NSTask * syncTask = NSMakeTask(TASK_SEND_SYNCINFO, (void *) syncInfo);
+ NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(syncTask, NSOICFree(syncInfo));
+
+ NSConsumerPushEvent(syncTask);
+
+ NSOICFree(sync);
+}
+
void NSConsumerInternalTaskProcessing(NSTask * task)
{
NS_VERIFY_NOT_NULL_V(task);
- NSCacheList * cache = *(NSGetCacheList());
- if (!cache)
+ NSCacheList * MessageCache = *(NSGetMessageCacheList());
+ if (!MessageCache)
{
NS_LOG(DEBUG, "Cache Init");
- cache = NSStorageCreate();
- NS_VERIFY_NOT_NULL_V(cache);
+ MessageCache = NSStorageCreate();
+ NS_VERIFY_NOT_NULL_V(MessageCache);
- cache->cacheType = NS_CONSUMER_CACHE_MESSAGE;
- NSSetCacheList(cache);
+ MessageCache->cacheType = NS_CONSUMER_CACHE_MESSAGE;
+ NSSetMessageCacheList(MessageCache);
+ }
+
+ NSCacheList * ProviderCache = *(NSGetProviderCacheList());
+ if (!ProviderCache)
+ {
+ NS_LOG(DEBUG, "Cache Init");
+ ProviderCache = NSStorageCreate();
+ NS_VERIFY_NOT_NULL_V(ProviderCache);
+
+ ProviderCache->cacheType = NS_CONSUMER_CACHE_PROVIDER;
+ NSSetProviderCacheList(ProviderCache);
}
NSResult ret = NS_ERROR;
NSConsumerHandleRecvSyncInfo((NSSyncInfo *)task->taskData);
break;
}
- case TASK_RECV_READ:
+ case TASK_MAKE_SYNCINFO:
{
- NS_LOG(DEBUG, "Receive Read Notification");
-
- ret = NSCacheUpdate(cache, task, Read);
- NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
+ NS_LOG(DEBUG, "Make SyncInfo, get Provider's Addr");
+ NSConsumerHandleMakeSyncInfo((NSSyncInfo *)task->taskData);
break;
}
- case TASK_RECV_DISMISS:
- {
- NS_LOG(DEBUG, "Receive Dismiss Notification");
+ case TASK_RECV_READ:
+ {
+ NS_LOG(DEBUG, "Receive Read Notification");
- ret = NSCacheUpdate(cache, task, Dismiss);
- NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
- break;
- }
+ ret = NSMessageCacheUpdate(MessageCache, task, NS_SYNC_READ);
+ NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
+ break;
+ }
+ case TASK_RECV_DISMISS:
+ {
+ NS_LOG(DEBUG, "Receive Dismiss Notification");
+
+ ret = NSMessageCacheUpdate(MessageCache, task, NS_SYNC_DELETED);
+ NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
+ break;
+ }
default :
{
NS_LOG(ERROR, "Unknown TASK Type");
#include "NSStorageAdapter.h"
#include "NSConsumerCommunication.h"
-void NSDestroyCacheList();
+void NSDestroyMessageCacheList();
+
+void NSDestroyProviderCacheList();
void NSConsumerInternalTaskProcessing(NSTask *);
void NSConsumerMessageHandlerExit()
{
- NSDestroyCacheList();
+ NSDestroyMessageCacheList();
NSConsumerListenerTermiate();
NSThreadStop(*(NSGetMsgHandleThreadHandle()));
NSDestroyQueue(*(NSGetMsgHandleQueue()));
case TASK_CONSUMER_RECV_MESSAGE:
case TASK_CONSUMER_PROVIDER_DISCOVERED:
case TASK_CONSUMER_RECV_SUBSCRIBE_CONFIRMED:
+ case TASK_MAKE_SYNCINFO:
{
NSConsumerInternalTaskProcessing(task);
break;
void NSThreadJoin(NSConsumerThread * handle)
{
- if (handle->thread_id)
- {
- pthread_join(handle->thread_id, NULL);
- }
+ if (handle->thread_id)
+ {
+ pthread_join(handle->thread_id, NULL);
+ }
}
void NSDestroyThreadHandle(NSConsumerThread * handle)
NSCacheList * NSStorageCreate()
{
- pthread_mutex_t * mutex = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
+ pthread_mutex_t * mutex = (pthread_mutex_t *) OICMalloc(sizeof(pthread_mutex_t));
pthread_mutex_init(mutex, NULL);
NSSetCacheMutex(*mutex);
mutex = NSGetCacheMutex();
-
pthread_mutex_lock(mutex);
NSCacheList * newList = (NSCacheList *) OICMalloc(sizeof(NSCacheList));
return NSConsumerCacheWriteMessage(list, newObj);
}
+ else if (type == NS_CONSUMER_CACHE_PROVIDER)
+ {
+ pthread_mutex_unlock(mutex);
+
+ return NSConsumerCacheWriteProvider(list, newObj);
+ }
NS_LOG (ERROR, "Not Supported Type");
pthread_mutex_unlock(mutex);
pthread_mutex_lock(mutex);
+ NSCacheType type = list->cacheType;
+
if (!delId)
{
pthread_mutex_unlock(mutex);
NSCacheElement * prev = list->head;
NSCacheElement * del = list->head;
- NSCacheType type = list->cacheType;
-
if (NSConsumerCompareIdCacheData(type, del->data, delId))
{
if (del == list->head)
list->tail = del->next;
list->head = del->next;
- NSRemoveMessage((NSMessage_consumer *) del->data);
- OICFree(del);
+ if (type == NS_CONSUMER_CACHE_MESSAGE)
+ {
+ NSRemoveMessage((NSMessage_consumer *) del->data);
+ }
+ else if (type == NS_CONSUMER_CACHE_PROVIDER)
+ {
+ NSRemoveProvider((NSProvider_internal *) del->data);
+ }
+ NSOICFree(del);
pthread_mutex_unlock(mutex);
return NS_OK;
list->tail = prev;
prev->next = del->next;
- NSRemoveMessage((NSMessage_consumer *) del->data);
- OICFree(del);
+ if (type == NS_CONSUMER_CACHE_MESSAGE)
+ {
+ NSRemoveMessage((NSMessage_consumer *) del->data);
+ }
+ else if (type == NS_CONSUMER_CACHE_PROVIDER)
+ {
+ NSRemoveProvider((NSProvider_internal *) del->data);
+ }
+ NSOICFree(del);
pthread_mutex_unlock(mutex);
return NS_OK;
NSMessage_consumer * newMsgObj = (NSMessage_consumer *) newObj->data;
pthread_mutex_unlock(mutex);
- char msgId[] = {0, };
- sprintf(msgId, "%ld", newMsgObj->messageId);
+ char msgId[NS_DEVICE_ID_LENGTH] = {0, };
+ snprintf(msgId, NS_DEVICE_ID_LENGTH, "%ld", newMsgObj->messageId);
NSCacheElement * it = NSStorageRead(list, msgId);
pthread_mutex_lock(mutex);
return NS_OK;
}
+NSResult NSConsumerCacheWriteProvider(NSCacheList * list, NSCacheElement * newObj)
+{
+ pthread_mutex_t * mutex = NSGetCacheMutex();
+
+ pthread_mutex_lock(mutex);
+
+ NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1");
+ NSProvider_internal * prov = (NSProvider_internal *)newObj->data;
+ NS_LOG_V (DEBUG, "%s", prov->providerId);
+ NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1");
+
+ if (!newObj)
+ {
+ pthread_mutex_unlock(mutex);
+ NS_LOG (ERROR, "Failed to Write Provider Cache");
+ return NS_ERROR;
+ }
+
+ NSProvider_internal * newProvObj = (NSProvider_internal *) newObj->data;
+
+ pthread_mutex_unlock(mutex);
+ NSCacheElement * it = NSStorageRead(list, newProvObj->providerId);
+ pthread_mutex_lock(mutex);
+
+ if (it)
+ {
+ NSProvider_internal * provObj = (NSProvider_internal *) it->data;
+ it->data = (void *) NSCopyProvider(newProvObj);
+ if (!it->data)
+ {
+ NS_LOG (ERROR, "Failed to CopyProvider");
+ it->data = (void *) provObj;
+ pthread_mutex_unlock(mutex);
+
+ return NS_ERROR;
+ }
+ NSRemoveProvider(provObj);
+ pthread_mutex_unlock(mutex);
+
+ return NS_OK;
+ }
+
+ NSCacheElement * obj = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));
+ if (!obj)
+ {
+ NS_LOG(ERROR, "Fail to Create New Object");
+ pthread_mutex_unlock(mutex);
+
+ return NS_ERROR;
+ }
+ obj->data = (void *) NSCopyProvider(newProvObj);
+
+ NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!2");
+ prov = (NSProvider_internal *)obj->data;
+ NS_LOG_V (DEBUG, "%s", prov->providerId);
+ NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!2");
+
+ if (!obj->data)
+ {
+ NS_LOG (ERROR, "Failed to CopyProvider");
+ pthread_mutex_unlock(mutex);
+
+ return NS_ERROR;
+ }
+ obj->next = NULL;
+
+ if (!list->head)
+ {
+ list->head = obj;
+ list->tail = obj;
+ pthread_mutex_unlock(mutex);
+
+ NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!3");
+ prov = (NSProvider_internal *)list->tail->data;
+ NS_LOG_V (DEBUG, "%s", prov->providerId);
+ NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!3");
+
+ return NS_OK;
+ }
+
+ (list->tail)->next = obj;
+ list->tail = obj;
+
+ NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!4");
+ prov = (NSProvider_internal *)list->tail->data;
+ NS_LOG_V (DEBUG, "%s", prov->providerId);
+ NS_LOG (DEBUG, "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!4");
+
+ pthread_mutex_unlock(mutex);
+
+ return NS_OK;
+}
+
NSResult NSStorageDestroy(NSCacheList * list)
{
pthread_mutex_t * mutex = NSGetCacheMutex();
if (type == NS_CONSUMER_CACHE_MESSAGE)
{
-
while (iter)
{
next = (NSCacheElement *) iter->next;
NSRemoveMessage((NSMessage_consumer *) iter->data);
- OICFree(iter);
+ NSOICFree(iter);
iter = next;
}
- OICFree(list);
+ NSOICFree(list);
+ }
+ else if (type == NS_CONSUMER_CACHE_PROVIDER)
+ {
+ while (iter)
+ {
+ next = (NSCacheElement *) iter->next;
+
+ NSRemoveProvider((NSProvider_internal *) iter->data);
+ NSOICFree(iter);
+
+ iter = next;
+ }
+
+ NSOICFree(list);
}
pthread_mutex_unlock(mutex);
{
NSMessage_consumer * msg = (NSMessage_consumer *) data;
- char msgId[] = {0, };
- sprintf(msgId, "%ld", msg->messageId);
+ char msgId[NS_DEVICE_ID_LENGTH] = {0, };
+ snprintf(msgId, NS_DEVICE_ID_LENGTH, "%ld", msg->messageId);
if (!strcmp(msgId, id))
{
return true;
return false;
}
+ else if (type == NS_CONSUMER_CACHE_PROVIDER)
+ {
+ NSProvider_internal * prov = (NSProvider_internal *) data;
+
+ if (!strcmp(prov->providerId, id))
+ {
+ return true;
+ }
+
+ return false;
+ }
return false;
}
bool NSConsumerCompareIdCacheData(NSCacheType type, void * data, const char * id);
NSResult NSConsumerCacheWriteMessage(NSCacheList * list, NSCacheElement * newObj);
+NSResult NSConsumerCacheWriteProvider(NSCacheList * list, NSCacheElement * newObj);
#ifdef __cplusplus
}
OCRepPayloadSetUri(payload, NS_COLLECTION_MESSAGE_URI);\r
OCRepPayloadSetPropInt(payload, NS_ATTRIBUTE_MESSAGE_ID, 1);\r
OCRepPayloadSetPropBool(payload, NS_ATTRIBUTE_ACCPETANCE, accepted);\r
+ OCRepPayloadSetPropString(payload, NS_ATTRIBUTE_PROVIDER_ID, NSGetProviderInfo()->providerId);\r
\r
NSCacheElement * element = NSStorageRead(consumerSubList, id);\r
\r
#include "NSProviderScheduler.h"\r
#include "NSProviderResource.h"\r
#include "NSProviderMemoryCache.h"\r
+#include "NSProviderSystem.h"\r
#include "oic_string.h"\r
#include "oic_malloc.h"\r
\r