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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
25 #include "oic_malloc.h"
26 #include "oic_string.h"
29 #include "NSStructs.h"
30 #include "NSConstants.h"
31 #include "NSConsumerCommon.h"
32 #include "NSConsumerCommunication.h"
35 #include "NSConsumerQueue.h"
37 #include "NSConsumerDiscovery.h"
38 #include "NSConsumerInternalTaskController.h"
39 #include "NSConsumerNetworkEventListener.h"
40 #include "NSConsumerQueueScheduler.h"
41 #include "NSConsumerSystem.h"
43 void * NSConsumerMsgHandleThreadFunc(void * handle);
45 void * NSConsumerMsgPushThreadFunc(void * data);
47 void NSConsumerTaskProcessing(NSTask * task);
49 NSConsumerThread ** NSGetMsgHandleThreadHandle()
51 static NSConsumerThread * handle = NULL;
55 void NSSetMsgHandleThreadHandle(NSConsumerThread * handle)
57 *(NSGetMsgHandleThreadHandle()) = handle;
60 NSConsumerQueue ** NSGetMsgHandleQueue()
62 static NSConsumerQueue * queue = NULL;
66 void NSSetMsgHandleQueue(NSConsumerQueue * queue)
68 *(NSGetMsgHandleQueue()) = queue;
71 NSResult NSConsumerMessageHandlerInit()
73 NSConsumerThread * handle = NULL;
74 NSConsumerQueue * queue = NULL;
76 uint8_t uuid[UUID_SIZE];
77 char uuidString[UUID_STRING_SIZE];
79 OCConvertUuidToString(uuid, uuidString);
80 NSSetConsumerId(uuidString);
81 NS_LOG_V(DEBUG, "Consumer ID : %s", *NSGetConsumerId());
83 NS_LOG(DEBUG, "listener init");
84 NSResult ret = NSConsumerListenerInit();
85 NS_VERIFY_NOT_NULL(ret == NS_OK ? (void *) 1 : NULL, NS_ERROR);
87 NS_LOG(DEBUG, "system init");
88 ret = NSConsumerSystemInit();
89 NS_VERIFY_NOT_NULL(ret == NS_OK ? (void *) 1 : NULL, NS_ERROR);
91 NS_LOG(DEBUG, "queue thread init");
92 handle = NSThreadInit(NSConsumerMsgHandleThreadFunc, NULL);
93 NS_VERIFY_NOT_NULL(handle, NS_ERROR);
94 NSSetMsgHandleThreadHandle(handle);
96 NS_LOG(DEBUG, "create queue");
97 queue = NSCreateQueue();
98 NS_VERIFY_NOT_NULL(queue, NS_ERROR);
99 NSSetMsgHandleQueue(queue);
104 NSResult NSConsumerPushEvent(NSTask * task)
106 NSConsumerThread * thread = NSThreadInit(NSConsumerMsgPushThreadFunc, (void *) task);
107 NS_VERIFY_NOT_NULL(thread, NS_ERROR);
112 void NSConsumerMessageHandlerExit()
114 NSDestroyMessageCacheList();
115 NSConsumerListenerTermiate();
116 NSThreadStop(*(NSGetMsgHandleThreadHandle()));
117 NSDestroyQueue(*(NSGetMsgHandleQueue()));
120 void * NSConsumerMsgHandleThreadFunc(void * threadHandle)
122 NSConsumerQueue * queue = NULL;
123 NSConsumerQueueObject * obj = NULL;
125 NS_LOG(DEBUG, "create thread for consumer message handle");
126 NSConsumerThread * queueHandleThread = (NSConsumerThread *) threadHandle;
127 NS_VERIFY_NOT_NULL(queueHandleThread, NULL);
131 if (!queueHandleThread->isStarted)
133 NS_LOG(ERROR, "msg handler thread will be terminated");
137 queue = *(NSGetMsgHandleQueue());
143 if (NSIsQueueEmpty(queue))
149 NSThreadLock(queueHandleThread);
150 NS_LOG(DEBUG, "msg handler working");
151 obj = NSPopQueue(queue);
155 NSConsumerTaskProcessing((NSTask *)(obj->data));
158 NSThreadUnlock(queueHandleThread);
165 void * NSConsumerMsgPushThreadFunc(void * data)
167 NSConsumerQueueObject * obj = NULL;
168 NSConsumerQueue * queue = NULL;
170 NS_LOG(DEBUG, "get queueThread handle");
171 NSConsumerThread * msgHandleThread = *(NSGetMsgHandleThreadHandle());
172 NS_VERIFY_NOT_NULL(msgHandleThread, NULL);
174 NS_LOG(DEBUG, "create queue object");
175 obj = (NSConsumerQueueObject *)OICMalloc(sizeof(NSConsumerQueueObject));
176 NS_VERIFY_NOT_NULL(obj, NULL);
181 NSThreadLock(msgHandleThread);
183 queue = *(NSGetMsgHandleQueue());
186 NS_LOG(ERROR, "NSQueue is null. can not insert to queue");
192 NSPushQueue(queue, obj);
195 NSThreadUnlock(msgHandleThread);
200 void NSConsumerTaskProcessing(NSTask * task)
202 switch (task->taskType)
204 case TASK_EVENT_CONNECTED:
205 case TASK_CONSUMER_REQ_DISCOVER:
207 NSConsumerDiscoveryTaskProcessing(task);
210 case TASK_CONSUMER_REQ_SUBSCRIBE:
211 case TASK_CONSUMER_REQ_SUBSCRIBE_CANCEL:
212 case TASK_SEND_SYNCINFO:
214 NSConsumerCommunicationTaskProcessing(task);
217 case TASK_RECV_SYNCINFO:
218 case TASK_CONSUMER_RECV_MESSAGE:
219 case TASK_CONSUMER_PROVIDER_DISCOVERED:
220 case TASK_CONSUMER_RECV_SUBSCRIBE_CONFIRMED:
221 case TASK_MAKE_SYNCINFO:
223 NSConsumerInternalTaskProcessing(task);
227 NS_LOG(ERROR, "Unknown type of task");