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 "NSProviderNotification.h"
23 NSResult NSSetMessagePayload(NSMessage *msg, OCRepPayload** msgPayload)
25 NS_LOG(DEBUG, "NSSetMessagePayload - IN");
27 *msgPayload = OCRepPayloadCreate();
31 NS_LOG(ERROR, "Failed to allocate payload");
35 OCRepPayloadSetUri(*msgPayload, NS_COLLECTION_MESSAGE_URI);
36 OCRepPayloadSetPropInt(*msgPayload, NS_ATTRIBUTE_MESSAGE_ID, msg->messageId);
37 OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_PROVIDER_ID, msg->providerId);
39 NSDuplicateSetPropertyInt(msgPayload, NS_ATTRIBUTE_TYPE, msg->type);
40 NSDuplicateSetPropertyInt(msgPayload, NS_ATTRIBUTE_MESSAGE_ID, msg->ttl);
41 NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_DATETIME, msg->dateTime);
42 NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_TITLE, msg->title);
43 NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_TEXT, msg->contentText);
44 NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_SOURCE, msg->sourceName);
45 NSDuplicateSetPropertyString(msgPayload, NS_ATTRIBUTE_TOPIC_NAME, msg->topic);
47 NS_LOG(DEBUG, "NSSetMessagePayload - OUT");
51 NSResult NSSetSyncPayload(NSSyncInfo *sync, OCRepPayload** syncPayload)
53 NS_LOG(DEBUG, "NSSetSyncPayload - IN");
55 *syncPayload = OCRepPayloadCreate();
59 NS_LOG(ERROR, "Failed to allocate payload");
63 OCRepPayloadSetUri(*syncPayload, NS_COLLECTION_SYNC_URI);
65 OCRepPayloadSetPropString(*syncPayload, NS_ATTRIBUTE_PROVIDER_ID, sync->providerId);
66 OCRepPayloadSetPropInt(*syncPayload, NS_ATTRIBUTE_MESSAGE_ID, sync->messageId);
67 OCRepPayloadSetPropInt(*syncPayload, NS_ATTRIBUTE_STATE, sync->state);
69 NS_LOG(DEBUG, "NSSetSyncPayload - OUT");
73 NSResult NSSendNotification(NSMessage *msg)
75 NS_LOG(DEBUG, "NSSendMessage - IN");
77 OCResourceHandle rHandle;
78 OCObservationId obArray[255] = { 0, };
81 if (NSPutMessageResource(msg, &rHandle) != NS_OK)
83 NS_LOG(ERROR, "fail to Put notification resource");
87 if (consumerSubList->head == NULL)
89 NS_LOG(ERROR, "SubList->head is NULL, empty SubList");
93 OCRepPayload* payload = NULL;
95 if (NSSetMessagePayload(msg, &payload) != NS_OK)
97 NS_LOG(ERROR, "fail to Get message payload");
101 NSCacheElement * it = consumerSubList->head;
105 NSCacheSubData * subData = (NSCacheSubData *) it->data;
106 NS_LOG_V(DEBUG, "message subData->id = %s", subData->id);
107 NS_LOG_V(DEBUG, "subData->messageId = %d", subData->messageObId);
108 NS_LOG_V(DEBUG, "subData->cloud_messageId = %d", subData->remote_messageObId);
109 NS_LOG_V(DEBUG, "subData->syncId = %d", subData->syncObId);
110 NS_LOG_V(DEBUG, "subData->cloud_syncId = %d", subData->remote_syncObId);
111 NS_LOG_V(DEBUG, "subData->isWhite = %d", subData->isWhite);
113 if (subData->isWhite)
115 if(subData->messageObId != 0)
119 NS_LOG_V(DEBUG, "this is topic message: %s", msg->topic);
121 if(NSProviderIsTopicSubScribed(consumerTopicList->head, subData->id, msg->topic))
123 obArray[obCount++] = subData->messageObId;
128 obArray[obCount++] = subData->messageObId;
132 #if(defined WITH_CLOUD && defined RD_CLIENT)
133 if(subData->remote_messageObId != 0)
137 NS_LOG_V(DEBUG, "this is topic message via remote server: %s", msg->topic);
138 if(NSProviderIsTopicSubScribed(consumerTopicList->head, subData->id, msg->topic))
140 obArray[obCount++] = subData->remote_messageObId;
145 obArray[obCount++] = subData->remote_messageObId;
154 for (i = 0; i < obCount; ++i)
156 NS_LOG(DEBUG, "-------------------------------------------------------message\n");
157 NS_LOG_V(DEBUG, "SubScription WhiteList[%d] = %d", i, obArray[i]);
158 NS_LOG(DEBUG, "-------------------------------------------------------message\n");
163 NS_LOG(ERROR, "observer count is zero");
167 OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray, obCount, payload,
170 NS_LOG_V(DEBUG, "Message ocstackResult = %d", ocstackResult);
172 if (ocstackResult != OC_STACK_OK)
174 NS_LOG(ERROR, "fail to send message");
175 OCRepPayloadDestroy(payload);
179 OCRepPayloadDestroy(payload);
181 NS_LOG(DEBUG, "NSSendMessage - OUT");
186 NSResult NSSendSync(NSSyncInfo *sync)
188 NS_LOG(DEBUG, "NSSendSync - IN");
190 OCObservationId obArray[255] = { 0, };
194 OCResourceHandle rHandle;
195 if (NSPutSyncResource(sync, &rHandle) != NS_OK)
197 NS_LOG(ERROR, PCF("Fail to put sync resource"));
201 NSCacheElement * it = consumerSubList->head;
205 NSCacheSubData * subData = (NSCacheSubData *) it->data;
206 NS_LOG_V(DEBUG, "sync subData->id = %s", subData->id);
207 NS_LOG_V(DEBUG, "subData->messageId = %d", subData->messageObId);
208 NS_LOG_V(DEBUG, "subData->cloud_messageId = %d", subData->remote_messageObId);
209 NS_LOG_V(DEBUG, "subData->syncId = %d", subData->syncObId);
210 NS_LOG_V(DEBUG, "subData->cloud_syncId = %d", subData->remote_syncObId);
211 NS_LOG_V(DEBUG, "subData->isWhite = %d", subData->isWhite);
213 if (subData->isWhite)
215 if(subData->syncObId != 0)
217 obArray[obCount++] = subData->syncObId;
220 #if(defined WITH_CLOUD && defined RD_CLIENT)
221 if(subData->remote_syncObId != 0)
223 obArray[obCount++] = subData->remote_syncObId;
230 OCRepPayload* payload;
231 if (NSSetSyncPayload(sync, &payload) != NS_OK)
233 NS_LOG(ERROR, "Failed to allocate payload");
237 for (i = 0; i < obCount; ++i)
239 NS_LOG(DEBUG, "-------------------------------------------------------message\n");
240 NS_LOG_V(DEBUG, "Sync WhiteList[%d] = %d", i, obArray[i]);
241 NS_LOG(DEBUG, "-------------------------------------------------------message\n");
244 OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray,
245 obCount, payload, OC_LOW_QOS);
247 NS_LOG_V(DEBUG, "Sync ocstackResult = %d", ocstackResult);
249 if (ocstackResult != OC_STACK_OK)
251 NS_LOG(ERROR, "fail to send Sync");
252 OCRepPayloadDestroy(payload);
256 OCRepPayloadDestroy(payload);
258 NS_LOG(DEBUG, "NSSendSync - OUT");
262 void * NSNotificationSchedule(void *ptr)
266 NS_LOG(DEBUG, "Create NSNotifiactionSchedule");
269 while (NSIsRunning[NOTIFICATION_SCHEDULER])
271 sem_wait(&NSSemaphore[NOTIFICATION_SCHEDULER]);
272 pthread_mutex_lock(&NSMutex[NOTIFICATION_SCHEDULER]);
274 if (NSHeadMsg[NOTIFICATION_SCHEDULER] != NULL)
276 NSTask *node = NSHeadMsg[NOTIFICATION_SCHEDULER];
277 NSHeadMsg[NOTIFICATION_SCHEDULER] = node->nextTask;
279 switch (node->taskType)
281 case TASK_SEND_NOTIFICATION:
283 NS_LOG(DEBUG, "CASE TASK_SEND_NOTIFICATION : ");
284 NSSendNotification((NSMessage *)node->taskData);
285 NSFreeMessage((NSMessage *)node->taskData);
289 NS_LOG(DEBUG, "CASE TASK_SEND_READ : ");
290 NSSendSync((NSSyncInfo*) node->taskData);
291 NSFreeSync((NSSyncInfo*) node->taskData);
294 NS_LOG(DEBUG, "CASE TASK_RECV_READ : ");
295 NSSendSync((NSSyncInfo*) node->taskData);
296 NSPushQueue(CALLBACK_RESPONSE_SCHEDULER, TASK_CB_SYNC, node->taskData);
299 NS_LOG(ERROR, "Unknown type message");
306 pthread_mutex_unlock(&NSMutex[NOTIFICATION_SCHEDULER]);
310 NS_LOG(INFO, "Destroy NSNotificationSchedule");