added cacheadapter interface and memorycache of notification.
[platform/upstream/iotivity.git] / service / notification / src / provider / NSProviderNotification.c
1 //******************************************************************
2 //
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "NSProviderNotification.h"
22
23 NSResult NSInitMessageList()
24 {
25     messageList = NSCacheCreate();
26     messageList->cacheType = NS_PROVIDER_CACHE_MESSAGE;
27     return NS_OK;
28 }
29
30 NSResult NSGetMessagePayload(NSMessage *msg, OCRepPayload** msgPayload)
31 {
32
33     *msgPayload = OCRepPayloadCreate();
34
35     if (!*msgPayload)
36     {
37         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
38         return NS_ERROR;
39     }
40
41     OCRepPayloadSetUri(*msgPayload, NSGetNotificationMessageUri());
42     OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_ID, msg->mId);
43     OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_TITLE, msg->mTitle);
44     OCRepPayloadSetPropString(*msgPayload, NS_ATTRIBUTE_TEXT, msg->mContentText);
45
46     return NS_OK;
47 }
48
49 NSResult NSGetSyncPayload(NSSync *sync, OCRepPayload** syncPayload)
50 {
51
52     *syncPayload = OCRepPayloadCreate();
53
54     if (!*syncPayload)
55     {
56         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
57         return NS_ERROR;
58     }
59
60     OCRepPayloadSetUri(*syncPayload, NSGetNotificationSyncUri());
61     OCRepPayloadSetPropString(*syncPayload, NS_ATTRIBUTE_ID, sync->mMessageId);
62     OCRepPayloadSetPropInt(*syncPayload, NS_ATTRIBUTE_STATE, sync->mState);
63
64     return NS_OK;
65 }
66
67 NSResult NSSendMessage(NSMessage *msg)
68 {
69     OIC_LOG(DEBUG, NOTIFICATION_TAG, "Send Notification Message to consumer");
70     int i;
71     // Set Resource and get resource handle
72     OCResourceHandle rHandle;
73     OCObservationId obArray[255] =
74     { 0, };
75     int obCount = 0;
76     if (NSPutMessageResource(msg, &rHandle) != NS_OK)
77     {
78         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Fail to put notification resource"));
79         return NS_ERROR;
80     }
81
82     // Send Notification to subscribers
83
84     if (consumerSubList->head == NULL)
85     {
86         printf("printf - no observers (consumerSubList->head == NULL)\n");
87         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("no observers"));
88         return NS_ERROR;
89     }
90
91     OCRepPayload* payload;
92     printf("printf - no observers - 1\n");
93     if (NSGetMessagePayload(msg, &payload) != NS_OK)
94     {
95         printf("printf - no observers - 2\n");
96         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
97         return NS_ERROR;
98     }
99
100     printf("printf - no observers - 3\n");
101     NSCacheElement * it = consumerSubList->head;
102     printf("printf - no observers - 4\n");
103     while (it)
104     {
105         printf("printf - no observers - 5\n");
106         NSCacheSubData * subData = (NSCacheSubData *) it->data;
107         printf("NS_ subData->id = %s\n", subData->id);
108         printf("NS_ subData->messageId = %d\n", subData->messageObId);
109         printf("NS_ subData->obID = %d\n", subData->syncObId);
110         printf("NS_ subData->isWhite = %d\n", subData->isWhite);
111
112         printf("printf - no observers - 6\n");
113         if (subData->isWhite)
114         {
115             printf("printf - no observers - 7\n");
116             obArray[obCount++] = subData->messageObId;
117             printf("printf - no observers - 8\n");
118         }
119
120         it = it->next;
121     }
122     printf("printf - no observers - 9\n");
123     for (i = 0; i < obCount; ++i)
124     {
125         printf("NS_ -------------------------------------------------------message\n");
126         printf("NS_ whiteList->idList[%d] = %d\n", i, obArray[i]);
127         printf("NS_ -------------------------------------------------------message\n");
128     }
129     printf("printf - no observers - 10\n");
130     // Notify message to subscribers
131
132     OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray, obCount, payload,
133             OC_LOW_QOS);
134     printf("NS_ message ocstackResult = %d\n", ocstackResult);
135
136     if (ocstackResult != OC_STACK_OK)
137     {
138         printf("printf - no observers - 11\n");
139         OIC_LOG(ERROR, NOTIFICATION_TAG, "fail to send message");
140         OCRepPayloadDestroy(payload);
141         return NS_ERROR;
142
143     }
144     printf("printf - no observers - 12\n");
145     OCRepPayloadDestroy(payload);
146
147     return NS_OK;
148 }
149
150 NSResult NSSendSync(NSSync *sync)
151 {
152     OIC_LOG(DEBUG, NOTIFICATION_TAG, "Send Notification Sync to consumer");
153
154     OCObservationId obArray[255] = { 0, };
155     int obCount = 0;
156     int i;
157
158     OCResourceHandle rHandle;
159     if (NSPutSyncResource(sync, &rHandle) != NS_OK)
160     {
161         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Fail to put sync resource"));
162         return NS_ERROR;
163     }
164
165     NSCacheElement * it = consumerSubList->head;
166
167     while (it)
168     {
169         NSCacheSubData * subData = (NSCacheSubData *) it->data;
170         if (subData->isWhite)
171         {
172             obArray[obCount++] = subData->syncObId;
173         }
174
175         it = it->next;
176
177     }
178
179     // Send sync to subscribers
180
181     OCRepPayload* payload;
182     if (NSGetSyncPayload(sync, &payload) != NS_OK)
183     {
184         OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Failed to allocate payload"));
185         return NS_ERROR;
186     }
187
188     // Notify sync to subscribers
189
190     for (i = 0; i < obCount; ++i)
191     {
192         printf("NS_ -------------------------------------------------------message\n");
193         printf("NS_ whiteList->idList[%d] = %d\n", i, obArray[i]);
194         printf("NS_ -------------------------------------------------------message\n");
195     }
196
197     OCStackResult ocstackResult = OCNotifyListOfObservers(rHandle, obArray,
198             obCount, payload, OC_LOW_QOS);
199
200     printf("NS_ sync ocstackResult = %d\n", ocstackResult);
201     if (ocstackResult != OC_STACK_OK)
202     {
203         OIC_LOG(ERROR, NOTIFICATION_TAG, "fail to send Sync");
204         OCRepPayloadDestroy(payload);
205         return NS_ERROR;
206
207     }
208     OCRepPayloadDestroy(payload);
209
210     return NS_OK;
211 }
212
213 void * NSNotificationSchedule(void *ptr)
214 {
215     if (ptr == NULL)
216     {
217         OIC_LOG(DEBUG, NOTIFICATION_TAG, "Create NSNotifiactionSchedule");
218     }
219
220     while (NSIsRunning[NOTIFICATION_SCHEDULER])
221     {
222         sem_wait(&NSSemaphore[NOTIFICATION_SCHEDULER]);
223         pthread_mutex_lock(&NSMutex[NOTIFICATION_SCHEDULER]);
224
225         if (NSHeadMsg[NOTIFICATION_SCHEDULER] != NULL)
226         {
227             NSTask *node = NSHeadMsg[NOTIFICATION_SCHEDULER];
228             NSHeadMsg[NOTIFICATION_SCHEDULER] = node->nextTask;
229
230             switch (node->taskType)
231             {
232                 case TASK_SEND_NOTIFICATION:
233                 {
234                     NSMessage * nsMsg = node->taskData;
235                     NSSendMessage(nsMsg);
236                     break;
237                 }
238                 case TASK_SEND_READ:
239                     NSSendSync((NSSync*) node->taskData);
240                     break;
241                 case TASK_RECV_READ:
242                     NSSendSync((NSSync*) node->taskData);
243                     NSPushQueue(RESPONSE_SCHEDULER, TASK_CB_SYNC, node->taskData);
244                     break;
245
246                 default:
247                     OIC_LOG(ERROR, NOTIFICATION_TAG, "Unknown type message");
248                     break;
249
250             }
251             OICFree(node);
252         }
253
254         pthread_mutex_unlock(&NSMutex[NOTIFICATION_SCHEDULER]);
255
256     }
257     return NULL;
258 }