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 "NSConsumerScheduler.h"
27 #include "oic_malloc.h"
28 #include "oic_string.h"
31 #include "NSStructs.h"
32 #include "NSConstants.h"
33 #include "NSConsumerCommon.h"
34 #include "NSConsumerCommunication.h"
37 #include "NSConsumerQueue.h"
39 #include "NSConsumerDiscovery.h"
40 #include "NSConsumerInternalTaskController.h"
41 #include "NSConsumerNetworkEventListener.h"
42 #include "NSConsumerSystem.h"
45 #include "NSConsumerMQPlugin.h"
48 void * NSConsumerMsgHandleThreadFunc(void * handle);
50 void * NSConsumerMsgPushThreadFunc(void * data);
52 void NSConsumerTaskProcessing(NSTask * task);
54 NSConsumerThread ** NSGetMsgHandleThreadHandle()
56 static NSConsumerThread * handle = NULL;
60 void NSSetMsgHandleThreadHandle(NSConsumerThread * handle)
62 *(NSGetMsgHandleThreadHandle()) = handle;
65 NSConsumerQueue ** NSGetMsgHandleQueue()
67 static NSConsumerQueue * queue = NULL;
71 void NSSetMsgHandleQueue(NSConsumerQueue * queue)
73 *(NSGetMsgHandleQueue()) = queue;
76 NSResult NSConsumerMessageHandlerInit()
78 NSConsumerThread * handle = NULL;
79 NSConsumerQueue * queue = NULL;
81 char * consumerUuid = (char *)OCGetServerInstanceIDString();
82 NS_VERIFY_NOT_NULL(consumerUuid, NS_ERROR);
84 NSSetConsumerId(consumerUuid);
85 NS_LOG_V(INFO_PRIVATE, "Consumer ID : %s", *NSGetConsumerId());
87 NS_LOG(DEBUG, "listener init");
88 NSResult ret = NSConsumerListenerInit();
89 NS_VERIFY_NOT_NULL(ret == NS_OK ? (void *) 1 : NULL, NS_ERROR);
91 NS_LOG(DEBUG, "system init");
92 ret = NSConsumerSystemInit();
93 NS_VERIFY_NOT_NULL(ret == NS_OK ? (void *) 1 : NULL, NS_ERROR);
95 NS_LOG(DEBUG, "create queue");
96 queue = NSCreateQueue();
97 NS_VERIFY_NOT_NULL(queue, NS_ERROR);
98 NSSetMsgHandleQueue(queue);
100 NS_LOG(DEBUG, "queue thread init");
101 handle = NSThreadInit(NSConsumerMsgHandleThreadFunc, NULL);
102 NS_VERIFY_NOT_NULL(handle, NS_ERROR);
103 NSSetMsgHandleThreadHandle(handle);
108 NSResult NSConsumerPushEvent(NSTask * task)
110 NSConsumerThread * thread = NSThreadInit(NSConsumerMsgPushThreadFunc, (void *) task);
111 NS_VERIFY_NOT_NULL(thread, NS_ERROR);
113 NSDestroyThreadHandle(thread);
119 void NSConsumerMessageHandlerExit()
122 NSConsumerListenerTermiate();
123 NSCancelAllSubscription();
125 NSConsumerThread * thread = *(NSGetMsgHandleThreadHandle());
126 NSThreadStop(thread);
127 NSSetMsgHandleThreadHandle(NULL);
129 NSConsumerQueue * queue = *(NSGetMsgHandleQueue());
130 NSDestroyQueue(queue);
131 NSSetMsgHandleQueue(NULL);
133 NSDestroyInternalCachedList();
136 void * NSConsumerMsgHandleThreadFunc(void * threadHandle)
138 NSConsumerQueue * queue = *(NSGetMsgHandleQueue());;
139 NSConsumerQueueObject * obj = NULL;
141 NS_LOG(DEBUG, "create thread for consumer message handle");
142 NSConsumerThread * queueHandleThread = (NSConsumerThread *) threadHandle;
143 NS_VERIFY_NOT_NULL(queueHandleThread, NULL);
149 queue = *(NSGetMsgHandleQueue());
154 if (!queueHandleThread->isStarted && NSIsQueueEmpty(queue))
156 NS_LOG(ERROR, "msg handler thread will be terminated");
160 if (NSIsQueueEmpty(queue))
166 NSThreadLock(queueHandleThread);
167 NS_LOG(DEBUG, "msg handler working");
168 obj = NSPopQueue(queue);
172 NSConsumerTaskProcessing((NSTask *)(obj->data));
176 NSThreadUnlock(queueHandleThread);
183 void * NSConsumerMsgPushThreadFunc(void * data)
185 NSConsumerQueueObject * obj = NULL;
186 NSConsumerQueue * queue = NULL;
188 NS_LOG(DEBUG, "get queueThread handle");
189 NSConsumerThread * msgHandleThread = *(NSGetMsgHandleThreadHandle());
190 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(msgHandleThread, NULL, NSOICFree(data));
192 NS_LOG(DEBUG, "create queue object");
193 obj = (NSConsumerQueueObject *)OICMalloc(sizeof(NSConsumerQueueObject));
194 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING(obj, NULL, NSOICFree(data));
199 NSThreadLock(msgHandleThread);
201 queue = *(NSGetMsgHandleQueue());
204 NS_LOG(ERROR, "NSQueue is null. can not insert to queue");
210 NSPushConsumerQueue(queue, obj);
213 NSThreadUnlock(msgHandleThread);
218 void NSProviderDeletedPostClean(
219 NSTask * task, NSProvider_internal * prov1, NSProvider_internal * prov2)
221 if (task && task->taskData)
223 if (task->taskType == TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL)
225 NSRemoveProvider((NSProvider *) task->taskData);
227 else if (task->taskType == TASK_CONSUMER_PROVIDER_DELETED)
229 NSOICFree(task->taskData);
236 NSRemoveProvider_internal(prov1);
241 NSRemoveProvider_internal(prov2);
245 void NSConsumerTaskProcessing(NSTask * task)
247 switch (task->taskType)
249 case TASK_EVENT_CONNECTED:
250 case TASK_EVENT_CONNECTED_TCP:
251 case TASK_CONSUMER_REQ_DISCOVER:
253 NSConsumerDiscoveryTaskProcessing(task);
256 case TASK_CONSUMER_REQ_SUBSCRIBE:
258 NSProvider_internal * prov =
259 NSConsumerFindNSProvider(((NSProvider *)task->taskData)->providerId);
260 NS_VERIFY_NOT_NULL_V(prov);
261 NSTask * subTask = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE, prov);
262 NS_VERIFY_NOT_NULL_V(subTask);
263 NSConsumerCommunicationTaskProcessing(subTask);
265 NSRemoveProvider((NSProvider *)task->taskData);
269 case TASK_SEND_SYNCINFO:
270 case TASK_CONSUMER_REQ_TOPIC_LIST:
271 case TASK_CONSUMER_SELECT_TOPIC_LIST:
273 NSConsumerCommunicationTaskProcessing(task);
276 case TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL:
277 case TASK_CONSUMER_PROVIDER_DELETED:
279 NSProvider_internal * data = NULL;
281 if (task->taskType == TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL)
283 data = NSConsumerFindNSProvider(((NSProvider *) task->taskData)->providerId);
284 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(
285 data, NSProviderDeletedPostClean(task, NULL, NULL));
287 else if (task->taskType == TASK_CONSUMER_PROVIDER_DELETED)
289 data = NSFindProviderFromAddr((OCDevAddr *) task->taskData);
290 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(
291 data, NSProviderDeletedPostClean(task, NULL, NULL));
294 NSProvider_internal * data2 = NSCopyProvider_internal(data);
295 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(
296 data2, NSProviderDeletedPostClean(task, data, NULL));
298 NSTask * conTask = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL, data);
299 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(
300 conTask, NSProviderDeletedPostClean(task, data, data2));
301 NSConsumerCommunicationTaskProcessing(conTask);
303 NSTask * conTask2 = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL, data2);
304 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(
305 conTask, NSProviderDeletedPostClean(task, NULL, data2));
306 NSConsumerInternalTaskProcessing(conTask2);
308 NSProviderDeletedPostClean(task, NULL, NULL);
311 case TASK_RECV_SYNCINFO:
312 case TASK_CONSUMER_RECV_MESSAGE:
313 case TASK_CONSUMER_SENT_REQ_OBSERVE:
314 case TASK_CONSUMER_RECV_PROVIDER_CHANGED:
315 case TASK_MAKE_SYNCINFO:
316 case TASK_CONSUMER_REQ_TOPIC_URI:
317 case TASK_CONSUMER_RECV_TOPIC_LIST:
319 NSConsumerInternalTaskProcessing(task);
322 case TASK_CONSUMER_PROVIDER_DISCOVERED:
324 NSTask * getTopicTask = (NSTask *)OICMalloc(sizeof(NSTask));
325 NS_VERIFY_NOT_NULL_WITH_POST_CLEANING_V(getTopicTask,
326 NSRemoveProvider_internal((void *) task->taskData));
327 getTopicTask->nextTask = NULL;
328 getTopicTask->taskData =
329 (void *) NSCopyProvider_internal((NSProvider_internal *) task->taskData);
330 getTopicTask->taskType = TASK_CONSUMER_REQ_TOPIC_LIST;
331 NSConsumerCommunicationTaskProcessing(getTopicTask);
332 NSConsumerInternalTaskProcessing(task);
336 case TASK_MQ_REQ_SUBSCRIBE:
338 NSConsumerMQTaskProcessing(task);
344 NS_LOG(ERROR, "Unknown type of task");
350 NSProvider_internal * NSConsumerFindNSProvider(const char * providerId)
352 NS_VERIFY_NOT_NULL(providerId, NULL);
354 return NSProviderCacheFind(providerId);