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()));
122 void * NSConsumerMsgHandleThreadFunc(void * threadHandle)
124 NSConsumerQueue * queue = NULL;
125 NSConsumerQueueObject * obj = NULL;
127 NS_LOG(DEBUG, "create thread for consumer message handle");
128 NSConsumerThread * queueHandleThread = (NSConsumerThread *) threadHandle;
129 NS_VERIFY_NOT_NULL(queueHandleThread, NULL);
133 if (!queueHandleThread->isStarted)
135 NS_LOG(ERROR, "msg handler thread will be terminated");
139 queue = *(NSGetMsgHandleQueue());
145 if (NSIsQueueEmpty(queue))
151 NSThreadLock(queueHandleThread);
152 NS_LOG(DEBUG, "msg handler working");
153 obj = NSPopQueue(queue);
157 NSConsumerTaskProcessing((NSTask *)(obj->data));
160 NSThreadUnlock(queueHandleThread);
167 void * NSConsumerMsgPushThreadFunc(void * data)
169 NSConsumerQueueObject * obj = NULL;
170 NSConsumerQueue * queue = NULL;
172 NS_LOG(DEBUG, "get queueThread handle");
173 NSConsumerThread * msgHandleThread = *(NSGetMsgHandleThreadHandle());
174 NS_VERIFY_NOT_NULL(msgHandleThread, NULL);
176 NS_LOG(DEBUG, "create queue object");
177 obj = (NSConsumerQueueObject *)OICMalloc(sizeof(NSConsumerQueueObject));
178 NS_VERIFY_NOT_NULL(obj, NULL);
183 NSThreadLock(msgHandleThread);
185 queue = *(NSGetMsgHandleQueue());
188 NS_LOG(ERROR, "NSQueue is null. can not insert to queue");
194 NSPushQueue(queue, obj);
197 NSThreadUnlock(msgHandleThread);
202 void NSConsumerTaskProcessing(NSTask * task)
204 switch (task->taskType)
206 case TASK_EVENT_CONNECTED:
207 case TASK_CONSUMER_REQ_DISCOVER:
209 NSConsumerDiscoveryTaskProcessing(task);
212 case TASK_CONSUMER_REQ_SUBSCRIBE:
213 case TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL:
214 case TASK_SEND_SYNCINFO:
216 NSConsumerCommunicationTaskProcessing(task);
219 case TASK_RECV_SYNCINFO:
220 case TASK_CONSUMER_RECV_MESSAGE:
221 case TASK_CONSUMER_PROVIDER_DISCOVERED:
222 case TASK_CONSUMER_RECV_SUBSCRIBE_CONFIRMED:
223 case TASK_MAKE_SYNCINFO:
225 NSConsumerInternalTaskProcessing(task);
229 NS_LOG(ERROR, "Unknown type of task");
234 NSMessage_consumer * NSConsumerFindNSMessage(const char* messageId)
236 NS_VERIFY_NOT_NULL(messageId, NULL);
238 return NSMessageCacheFind(messageId);
241 NSProvider_internal * NSConsumerFindNSProvider(const char * providerId)
243 NS_VERIFY_NOT_NULL(providerId, NULL);
245 return NSProviderCacheFind(providerId);