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"
44 void * NSConsumerMsgHandleThreadFunc(void * handle);
46 void * NSConsumerMsgPushThreadFunc(void * data);
48 void NSConsumerTaskProcessing(NSTask * task);
50 NSConsumerThread ** NSGetMsgHandleThreadHandle()
52 static NSConsumerThread * handle = NULL;
56 void NSSetMsgHandleThreadHandle(NSConsumerThread * handle)
58 *(NSGetMsgHandleThreadHandle()) = handle;
61 NSConsumerQueue ** NSGetMsgHandleQueue()
63 static NSConsumerQueue * queue = NULL;
67 void NSSetMsgHandleQueue(NSConsumerQueue * queue)
69 *(NSGetMsgHandleQueue()) = queue;
72 NSResult NSConsumerMessageHandlerInit()
74 NSConsumerThread * handle = NULL;
75 NSConsumerQueue * queue = NULL;
77 uint8_t uuid[UUID_SIZE] = {0,};
78 char uuidString[UUID_STRING_SIZE] = {0,};
79 OCRandomUuidResult randomRet = OCGenerateUuid(uuid);
80 NS_VERIFY_NOT_NULL(randomRet == RAND_UUID_OK ? (void *) 1 : NULL, NS_ERROR);
81 randomRet = OCConvertUuidToString(uuid, uuidString);
82 NS_VERIFY_NOT_NULL(randomRet == RAND_UUID_OK ? (void *) 1 : NULL, NS_ERROR);
84 NSSetConsumerId(uuidString);
85 NS_LOG_V(DEBUG, "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, "queue thread init");
96 handle = NSThreadInit(NSConsumerMsgHandleThreadFunc, NULL);
97 NS_VERIFY_NOT_NULL(handle, NS_ERROR);
98 NSSetMsgHandleThreadHandle(handle);
100 NS_LOG(DEBUG, "create queue");
101 queue = NSCreateQueue();
102 NS_VERIFY_NOT_NULL(queue, NS_ERROR);
103 NSSetMsgHandleQueue(queue);
108 NSResult NSConsumerPushEvent(NSTask * task)
110 NSConsumerThread * thread = NSThreadInit(NSConsumerMsgPushThreadFunc, (void *) task);
111 NS_VERIFY_NOT_NULL(thread, NS_ERROR);
116 void NSConsumerMessageHandlerExit()
119 NSConsumerListenerTermiate();
120 NSCancelAllSubscription();
121 NSThreadStop(*(NSGetMsgHandleThreadHandle()));
122 NSDestroyQueue(*(NSGetMsgHandleQueue()));
123 NSSetMsgHandleQueue(NULL);
125 NSDestroyMessageCacheList();
126 NSDestroyProviderCacheList();
129 void * NSConsumerMsgHandleThreadFunc(void * threadHandle)
131 NSConsumerQueue * queue = *(NSGetMsgHandleQueue());;
132 NSConsumerQueueObject * obj = NULL;
134 NS_LOG(DEBUG, "create thread for consumer message handle");
135 NSConsumerThread * queueHandleThread = (NSConsumerThread *) threadHandle;
136 NS_VERIFY_NOT_NULL(queueHandleThread, NULL);
142 queue = *(NSGetMsgHandleQueue());
147 if (!queueHandleThread->isStarted && NSIsQueueEmpty(queue))
149 NS_LOG(ERROR, "msg handler thread will be terminated");
153 if (NSIsQueueEmpty(queue))
159 NSThreadLock(queueHandleThread);
160 NS_LOG(DEBUG, "msg handler working");
161 obj = NSPopQueue(queue);
165 NSConsumerTaskProcessing((NSTask *)(obj->data));
168 NSThreadUnlock(queueHandleThread);
175 void * NSConsumerMsgPushThreadFunc(void * data)
177 NSConsumerQueueObject * obj = NULL;
178 NSConsumerQueue * queue = NULL;
180 NS_LOG(DEBUG, "get queueThread handle");
181 NSConsumerThread * msgHandleThread = *(NSGetMsgHandleThreadHandle());
182 NS_VERIFY_NOT_NULL(msgHandleThread, NULL);
184 NS_LOG(DEBUG, "create queue object");
185 obj = (NSConsumerQueueObject *)OICMalloc(sizeof(NSConsumerQueueObject));
186 NS_VERIFY_NOT_NULL(obj, NULL);
191 NSThreadLock(msgHandleThread);
193 queue = *(NSGetMsgHandleQueue());
196 NS_LOG(ERROR, "NSQueue is null. can not insert to queue");
202 NSPushQueue(queue, obj);
205 NSThreadUnlock(msgHandleThread);
210 void NSConsumerTaskProcessing(NSTask * task)
212 switch (task->taskType)
214 case TASK_EVENT_CONNECTED:
215 case TASK_EVENT_CONNECTED_TCP:
216 case TASK_CONSUMER_REQ_DISCOVER:
218 NSConsumerDiscoveryTaskProcessing(task);
221 case TASK_CONSUMER_REQ_SUBSCRIBE:
222 case TASK_SEND_SYNCINFO:
223 case TASK_CONSUMER_REQ_TOPIC_LIST:
224 case TASK_CONSUMER_GET_TOPIC_LIST:
225 case TASK_CONSUMER_SELECT_TOPIC_LIST:
227 NSConsumerCommunicationTaskProcessing(task);
230 case TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL:
232 NSProvider_internal * data = NSCopyProvider((NSProvider_internal *)task->taskData);
233 NS_VERIFY_NOT_NULL_V(data);
234 NSTask * conTask = NSMakeTask(TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL, data);
235 NS_VERIFY_NOT_NULL_V(conTask);
236 NSConsumerCommunicationTaskProcessing(task);
237 NSConsumerInternalTaskProcessing(conTask);
240 case TASK_RECV_SYNCINFO:
241 case TASK_CONSUMER_RECV_MESSAGE:
242 case TASK_CONSUMER_PROVIDER_DISCOVERED:
243 case TASK_CONSUMER_RECV_SUBSCRIBE_CONFIRMED:
244 case TASK_MAKE_SYNCINFO:
245 case TASK_CONSUMER_REQ_TOPIC_URI:
246 case TASK_CONSUMER_RECV_TOPIC_LIST:
248 NSConsumerInternalTaskProcessing(task);
253 NS_LOG(ERROR, "Unknown type of task");
259 NSMessage * NSConsumerFindNSMessage(const char* messageId)
261 NS_VERIFY_NOT_NULL(messageId, NULL);
263 return NSMessageCacheFind(messageId);
266 NSProvider_internal * NSConsumerFindNSProvider(const char * providerId)
268 NS_VERIFY_NOT_NULL(providerId, NULL);
270 return NSProviderCacheFind(providerId);