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];
78 char uuidString[UUID_STRING_SIZE];
80 OCConvertUuidToString(uuid, uuidString);
81 NSSetConsumerId(uuidString);
82 NS_LOG_V(DEBUG, "Consumer ID : %s", *NSGetConsumerId());
84 NS_LOG(DEBUG, "listener init");
85 NSResult ret = NSConsumerListenerInit();
86 NS_VERIFY_NOT_NULL(ret == NS_OK ? (void *) 1 : NULL, NS_ERROR);
88 NS_LOG(DEBUG, "system init");
89 ret = NSConsumerSystemInit();
90 NS_VERIFY_NOT_NULL(ret == NS_OK ? (void *) 1 : NULL, NS_ERROR);
92 NS_LOG(DEBUG, "queue thread init");
93 handle = NSThreadInit(NSConsumerMsgHandleThreadFunc, NULL);
94 NS_VERIFY_NOT_NULL(handle, NS_ERROR);
95 NSSetMsgHandleThreadHandle(handle);
97 NS_LOG(DEBUG, "create queue");
98 queue = NSCreateQueue();
99 NS_VERIFY_NOT_NULL(queue, NS_ERROR);
100 NSSetMsgHandleQueue(queue);
105 NSResult NSConsumerPushEvent(NSTask * task)
107 NSConsumerThread * thread = NSThreadInit(NSConsumerMsgPushThreadFunc, (void *) task);
108 NS_VERIFY_NOT_NULL(thread, NS_ERROR);
113 void NSConsumerMessageHandlerExit()
115 NSDestroyMessageCacheList();
116 NSDestroyProviderCacheList();
117 NSConsumerListenerTermiate();
118 NSThreadStop(*(NSGetMsgHandleThreadHandle()));
119 NSDestroyQueue(*(NSGetMsgHandleQueue()));
120 NSSetMsgHandleQueue(NULL);
123 void * NSConsumerMsgHandleThreadFunc(void * threadHandle)
125 NSConsumerQueue * queue = NULL;
126 NSConsumerQueueObject * obj = NULL;
128 NS_LOG(DEBUG, "create thread for consumer message handle");
129 NSConsumerThread * queueHandleThread = (NSConsumerThread *) threadHandle;
130 NS_VERIFY_NOT_NULL(queueHandleThread, NULL);
134 if (!queueHandleThread->isStarted)
136 NS_LOG(ERROR, "msg handler thread will be terminated");
140 queue = *(NSGetMsgHandleQueue());
146 if (NSIsQueueEmpty(queue))
152 NSThreadLock(queueHandleThread);
153 NS_LOG(DEBUG, "msg handler working");
154 obj = NSPopQueue(queue);
158 NSConsumerTaskProcessing((NSTask *)(obj->data));
161 NSThreadUnlock(queueHandleThread);
168 void * NSConsumerMsgPushThreadFunc(void * data)
170 NSConsumerQueueObject * obj = NULL;
171 NSConsumerQueue * queue = NULL;
173 NS_LOG(DEBUG, "get queueThread handle");
174 NSConsumerThread * msgHandleThread = *(NSGetMsgHandleThreadHandle());
175 NS_VERIFY_NOT_NULL(msgHandleThread, NULL);
177 NS_LOG(DEBUG, "create queue object");
178 obj = (NSConsumerQueueObject *)OICMalloc(sizeof(NSConsumerQueueObject));
179 NS_VERIFY_NOT_NULL(obj, NULL);
184 NSThreadLock(msgHandleThread);
186 queue = *(NSGetMsgHandleQueue());
189 NS_LOG(ERROR, "NSQueue is null. can not insert to queue");
195 NSPushQueue(queue, obj);
198 NSThreadUnlock(msgHandleThread);
203 void NSConsumerTaskProcessing(NSTask * task)
205 switch (task->taskType)
207 case TASK_EVENT_CONNECTED:
208 case TASK_EVENT_CONNECTED_TCP:
209 case TASK_CONSUMER_REQ_DISCOVER:
211 NSConsumerDiscoveryTaskProcessing(task);
214 case TASK_CONSUMER_REQ_SUBSCRIBE:
215 case TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL:
216 case TASK_SEND_SYNCINFO:
218 NSConsumerCommunicationTaskProcessing(task);
221 case TASK_RECV_SYNCINFO:
222 case TASK_CONSUMER_RECV_MESSAGE:
223 case TASK_CONSUMER_PROVIDER_DISCOVERED:
224 case TASK_CONSUMER_RECV_SUBSCRIBE_CONFIRMED:
225 case TASK_MAKE_SYNCINFO:
227 NSConsumerInternalTaskProcessing(task);
231 NS_LOG(ERROR, "Unknown type of task");
236 NSMessage_consumer * NSConsumerFindNSMessage(const char* messageId)
238 NS_VERIFY_NOT_NULL(messageId, NULL);
240 return NSMessageCacheFind(messageId);
243 NSProvider_internal * NSConsumerFindNSProvider(const char * providerId)
245 NS_VERIFY_NOT_NULL(providerId, NULL);
247 return NSProviderCacheFind(providerId);