1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "NSConstants.h"
22 #include "NSConsumerCommon.h"
23 #include "NSConsumerInternalTaskController.h"
24 #include "NSStructs.h"
26 #include "oic_malloc.h"
27 #include "oic_string.h"
29 NSCacheList ** NSGetMessageCacheList()
31 static NSCacheList * messageCache = NULL;
32 return & messageCache;
35 void NSSetMessageCacheList(NSCacheList * cache)
37 *(NSGetMessageCacheList()) = cache;
40 NSCacheList ** NSGetProviderCacheList()
42 static NSCacheList * providerCache = NULL;
43 return & providerCache;
46 void NSSetProviderCacheList(NSCacheList * cache)
48 *(NSGetProviderCacheList()) = cache;
51 void NSDestroyMessageCacheList()
53 NSCacheList * cache = *(NSGetMessageCacheList());
56 NSStorageDestroy(cache);
59 NSSetMessageCacheList(NULL);
62 void NSDestroyProviderCacheList()
64 NSCacheList * cache = *(NSGetProviderCacheList());
67 NSStorageDestroy(cache);
70 NSSetProviderCacheList(NULL);
73 NSMessage * NSMessageCacheFind(const char * messageId)
75 NS_VERIFY_NOT_NULL(messageId, NULL);
77 NSCacheList * MessageCache = *(NSGetMessageCacheList());
80 NS_LOG(DEBUG, "Message Cache Init");
81 MessageCache = NSStorageCreate();
82 NS_VERIFY_NOT_NULL(MessageCache, NULL);
84 MessageCache->cacheType = NS_CONSUMER_CACHE_MESSAGE;
85 NSSetMessageCacheList(MessageCache);
88 NSCacheElement * cacheElement = NSStorageRead(MessageCache, messageId);
89 NS_VERIFY_NOT_NULL(cacheElement, NULL);
91 return NSCopyMessage(((NSStoreMessage *) cacheElement->data)->msg);
94 NSProvider_internal * NSProviderCacheFind(const char * providerId)
96 NS_VERIFY_NOT_NULL(providerId, NULL);
98 NSCacheList * ProviderCache = *(NSGetProviderCacheList());
101 NS_LOG(DEBUG, "Provider Cache Init");
102 ProviderCache = NSStorageCreate();
103 NS_VERIFY_NOT_NULL(ProviderCache, NULL);
105 ProviderCache->cacheType = NS_CONSUMER_CACHE_PROVIDER;
106 NSSetProviderCacheList(ProviderCache);
109 NSCacheElement * cacheElement = NSStorageRead(ProviderCache, providerId);
110 NS_VERIFY_NOT_NULL(cacheElement, NULL);
112 return NSCopyProvider((NSProvider_internal *) cacheElement->data);
115 void NSRemoveCacheElementMessage(NSCacheElement * obj)
117 NSRemoveMessage(((NSStoreMessage *)obj->data)->msg);
118 NSOICFree(obj->data);
122 NSResult NSMessageCacheUpdate(NSMessage * msg, NSSyncType type)
124 NS_VERIFY_NOT_NULL(msg, NS_ERROR);
126 NSCacheList * MessageCache = *(NSGetMessageCacheList());
129 NS_LOG(DEBUG, "Message Cache Init");
130 MessageCache = NSStorageCreate();
131 NS_VERIFY_NOT_NULL(MessageCache, NS_ERROR);
133 MessageCache->cacheType = NS_CONSUMER_CACHE_MESSAGE;
134 NSSetMessageCacheList(MessageCache);
137 NSStoreMessage * sMsg = (NSStoreMessage *)OICMalloc(sizeof(NSStoreMessage));
138 NS_VERIFY_NOT_NULL(sMsg, NS_ERROR);
140 NSCacheElement * obj = (NSCacheElement *)OICMalloc(sizeof(NSCacheElement));
141 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(obj, NS_ERROR, NSOICFree(sMsg));
144 sMsg->msg = NSCopyMessage(msg);
146 obj->data = (NSCacheData *) sMsg;
149 NS_LOG(DEBUG, "try to write to storage");
150 NSResult ret = NSStorageWrite(MessageCache, obj);
151 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(ret == NS_OK ? (void *) 1 : NULL,
152 NS_ERROR, NSRemoveCacheElementMessage(obj));
154 NSRemoveCacheElementMessage(obj);
156 NS_LOG(DEBUG, "Update message done");
160 NSResult NSProviderCacheUpdate(NSProvider_internal * provider)
162 NSCacheList * ProviderCache = *(NSGetProviderCacheList());
165 NS_LOG(DEBUG, "Provider Cache Init");
166 ProviderCache = NSStorageCreate();
167 NS_VERIFY_NOT_NULL(ProviderCache, NS_ERROR);
169 ProviderCache->cacheType = NS_CONSUMER_CACHE_PROVIDER;
170 NSSetProviderCacheList(ProviderCache);
173 NS_VERIFY_NOT_NULL(provider, NS_ERROR);
175 NSCacheElement * obj = (NSCacheElement *)OICMalloc(sizeof(NSCacheElement));
176 NS_VERIFY_NOT_NULL(obj, NS_ERROR);
178 obj->data = (NSCacheData *) provider;
181 NS_LOG(DEBUG, "try to write to storage");
182 NSResult ret = NSStorageWrite(ProviderCache, obj);
183 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(ret == NS_OK ? (void *) 1 : NULL,
184 NS_ERROR, NSOICFree(obj));
189 void NSCancelAllSubscription()
191 NSCacheList * ProviderCache = *(NSGetProviderCacheList());
194 NS_LOG(DEBUG, "Provider Cache Init");
195 ProviderCache = NSStorageCreate();
196 NS_VERIFY_NOT_NULL_V(ProviderCache);
198 ProviderCache->cacheType = NS_CONSUMER_CACHE_PROVIDER;
199 NSSetProviderCacheList(ProviderCache);
202 NSCacheElement * obj = NULL;
203 while ((obj = NSPopProviderCacheList(ProviderCache)))
205 NS_LOG(DEBUG, "build NSTask");
206 NSTask * task = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL, (void *) obj->data);
207 NS_VERIFY_NOT_NULL_V(task);
209 NSConsumerPushEvent(task);
213 void NSConsumerHandleProviderDiscovered(NSProvider_internal * provider)
215 NS_VERIFY_NOT_NULL_V(provider);
218 bool isSubscribing = false;
219 NSProvider_internal * providerCacheData = NSProviderCacheFind(provider->providerId);
221 if (providerCacheData == NULL)
227 providerCacheData->accessPolicy = provider->accessPolicy;
228 NSProviderConnectionInfo * infos = providerCacheData->connection;
229 OCTransportAdapter newAdapter = provider->connection->addr->adapter;
232 isSubscribing |= infos->isSubscribing;
233 if (infos->addr->adapter == newAdapter && infos->isSubscribing == true)
235 NS_LOG_V(DEBUG, "This provider already discovered : %s:%d",
236 infos->addr->addr, infos->addr->port);
237 NS_LOG_V(DEBUG, "Subscription : %d", infos->isSubscribing);
244 NSResult ret = NSProviderCacheUpdate(provider);
245 NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
247 if (isAdded == false)
249 NS_LOG(DEBUG, "New provider is discovered");
253 provider = providerCacheData;
254 NS_LOG(DEBUG, "provider's connection is updated.");
257 if (provider->accessPolicy == NS_SELECTION_CONSUMER && isSubscribing == false)
259 NS_LOG(DEBUG, "accepter is NS_ACCEPTER_CONSUMER, Callback to user");
260 NSDiscoveredProvider((NSProvider *) provider);
264 NS_LOG(DEBUG, "accepter is NS_ACCEPTER_PROVIDER, request subscribe");
265 NSProvider_internal * subProvider = NSCopyProvider(provider);
266 NSTask * task = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE, (void *) subProvider);
267 NS_VERIFY_NOT_NULL_V(task);
269 NSConsumerPushEvent(task);
272 NSRemoveProvider(providerCacheData);
275 void NSConsumerHandleProviderDeleted(NSProvider_internal * provider)
277 NS_VERIFY_NOT_NULL_V(provider);
279 NSCacheList * providerCache = *(NSGetProviderCacheList());
280 NS_VERIFY_NOT_NULL_V(providerCache);
282 NSResult ret = NSStorageDelete(providerCache, provider->providerId);
283 NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *)1 : NULL);
286 void NSConsumerHandleRecvSubscriptionConfirmed(NSMessage * msg)
288 NS_VERIFY_NOT_NULL_V(msg);
290 NS_LOG_V(DEBUG, "confirmed by : %s", msg->providerId);
291 NSProvider_internal * provider = NSProviderCacheFind(msg->providerId);
292 NS_VERIFY_NOT_NULL_V(provider);
294 if (provider->connection->next == NULL)
296 NS_LOG(DEBUG, "call back to user");
297 NSSubscriptionAccepted((NSProvider *) provider);
301 void NSConsumerHandleRecvMessage(NSMessage * msg)
303 NS_VERIFY_NOT_NULL_V(msg);
305 NSResult ret = NSMessageCacheUpdate(msg, NS_SYNC_UNREAD);
306 NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
308 NSMessagePost((NSMessage *) msg);
311 void NSConsumerHandleRecvSyncInfo(NSSyncInfo * sync)
313 NS_VERIFY_NOT_NULL_V(sync);
315 char msgId[NS_DEVICE_ID_LENGTH] = { 0, };
316 snprintf(msgId, NS_DEVICE_ID_LENGTH, "%lld", (long long int)sync->messageId);
318 NSMessage * msg = NSMessageCacheFind(msgId);
319 NS_VERIFY_NOT_NULL_V(msg);
321 NSResult ret = NSMessageCacheUpdate(msg, sync->state);
322 NS_VERIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
323 NSRemoveMessage(msg);
325 NSNotificationSync(sync);
328 void NSConsumerHandleMakeSyncInfo(NSSyncInfo * sync)
330 NS_VERIFY_NOT_NULL_V(sync);
332 NSProvider_internal * provider = NSProviderCacheFind(sync->providerId);
333 NS_VERIFY_NOT_NULL_V (provider);
335 NSProviderConnectionInfo * connections = NSCopyProviderConnections(provider->connection);
336 NS_VERIFY_NOT_NULL_V (connections);
338 NSSyncInfo_internal * syncInfo = (NSSyncInfo_internal *)OICMalloc(sizeof(NSSyncInfo_internal));
339 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(syncInfo, NSRemoveConnections(connections));
341 OICStrcpy(syncInfo->providerId, sizeof(char) * NS_DEVICE_ID_LENGTH, sync->providerId);
342 syncInfo->messageId = sync->messageId;
343 syncInfo->state = sync->state;
344 syncInfo->connection = connections;
346 NSTask * syncTask = NSMakeTask(TASK_SEND_SYNCINFO, (void *) syncInfo);
347 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(syncTask, NSOICFree(syncInfo));
349 NSConsumerPushEvent(syncTask);
352 void NSConsumerInternalTaskProcessing(NSTask * task)
354 NS_VERIFY_NOT_NULL_V(task);
356 NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
357 switch (task->taskType)
359 case TASK_CONSUMER_RECV_SUBSCRIBE_CONFIRMED:
361 NS_LOG(DEBUG, "Receive Subscribe confirm from provider.");
362 NSConsumerHandleRecvSubscriptionConfirmed((NSMessage *)task->taskData);
363 NSRemoveMessage((NSMessage *)task->taskData);
366 case TASK_CONSUMER_RECV_MESSAGE:
368 NS_LOG(DEBUG, "Receive New Notification");
369 NSConsumerHandleRecvMessage((NSMessage *)task->taskData);
370 NSRemoveMessage((NSMessage *)task->taskData);
373 case TASK_CONSUMER_PROVIDER_DISCOVERED:
375 NS_LOG(DEBUG, "Receive New Provider is discovered.");
376 NSConsumerHandleProviderDiscovered((NSProvider_internal *)task->taskData);
377 NSRemoveProvider((NSProvider_internal *)task->taskData);
380 case TASK_RECV_SYNCINFO:
382 NS_LOG(DEBUG, "Receive SyncInfo.");
383 NSConsumerHandleRecvSyncInfo((NSSyncInfo *)task->taskData);
384 NSOICFree(task->taskData);
387 case TASK_MAKE_SYNCINFO:
389 NS_LOG(DEBUG, "Make SyncInfo, get Provider's Addr");
390 NSConsumerHandleMakeSyncInfo((NSSyncInfo *)task->taskData);
391 NSOICFree(task->taskData);
394 case TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL:
396 NS_LOG(DEBUG, "Make Subscribe cancel from provider.");
397 NSConsumerHandleProviderDeleted((NSProvider_internal *)task->taskData);
398 NSRemoveProvider((NSProvider_internal *)task->taskData);
403 NS_LOG(ERROR, "Unknown TASK Type");