9e60ef66b524012c0d5c0acb284ada688e92e6b3
[platform/core/messaging/msg-service.git] / proxy / MsgProxyListener.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15 */
16
17 #include <errno.h>
18
19 #include "MsgDebug.h"
20 #include "MsgCppTypes.h"
21 #include "MsgException.h"
22 #include "MsgUtilFunction.h"
23 #include "MsgUtilFile.h"
24 #include "MsgProxyListener.h"
25 #include "MsgGconfWrapper.h"
26
27 void MsgServerRestartCb(keynode_t *key, void* data)
28 {
29         bool bReady = false;
30         MSG_DEBUG("Message Service Running State Changed");
31         /* server is currently booting and service is not available until the end of booting */
32         if (MsgSettingGetBool(VCONFKEY_MSG_SERVER_READY, &bReady) != MSG_SUCCESS)
33                 MSG_INFO("MsgSettingGetBool() is failed");
34
35         MSG_INFO("Message Service Running State Changed bReady:(%d)", bReady);
36
37         /* bReady false indicates that server has restarted. Hence the proxylistener needs to be reset */
38         if (bReady == false) {
39                 MSG_DEBUG("Message Service Is Restarted");
40                 MsgProxyListener::instance()->resetProxyListener();
41         } else {
42                 MSG_DEBUG("Message Service Is ready again. Refreshing ProxyListener");
43                 MsgProxyListener::instance()->refreshProxyListener();
44         }
45 }
46
47 gboolean readSocket(GIOChannel *source, GIOCondition condition, gpointer data)
48 {
49         MSG_BEGIN();
50 #if 0
51         if ((G_IO_ERR & condition) || (G_IO_HUP & condition) || (G_IO_NVAL & condition)) {
52                 MSG_DEBUG("IO condition Error!!! [%d]", condition);
53
54                 MsgProxyListener::instance()->stop();
55                 return FALSE;
56         }
57 #endif
58         if (G_IO_ERR & condition) {
59                 MSG_ERR("IO Error!!! [%d]", condition);
60                 MsgProxyListener::instance()->clearProxyCBLists();
61                 MsgProxyListener::instance()->clearOpenHandleSet();
62                 return FALSE;
63         }
64
65         if (G_IO_HUP & condition) {
66                 MSG_ERR("socket fd Error!!! [%d]", condition);
67                 MsgProxyListener::instance()->resetProxyListener();
68                 return FALSE;
69         }
70
71         if (G_IO_NVAL & condition) {
72                 MSG_ERR("Invaild socket Error!!! [%d]", condition);
73                 MsgProxyListener::instance()->clearProxyCBLists();
74                 MsgProxyListener::instance()->clearOpenHandleSet();
75                 return FALSE;
76         }
77
78         char* buf = NULL;
79         unique_ptr<char*, void(*)(char**)> eventBuf(&buf, unique_ptr_deleter);
80         unsigned int len = 0;
81
82         int n = MsgProxyListener::instance()->readFromSocket(&buf, &len);
83
84         if (n > 0) {
85                 MSG_DEBUG(">>Receiving %d bytes", n);
86                 MsgProxyListener::instance()->handleEvent((MSG_EVENT_S*)buf);
87         } else if (n == 0) {
88                 MSG_WARN("Server closed connection");
89                 return FALSE;
90         } else /* dataSize < 0 */ {
91                 MSG_DEBUG("Data is not for Listener");
92         }
93
94         MSG_END();
95
96         return TRUE;
97 }
98
99
100 /*==================================================================================================
101                                      IMPLEMENTATION OF MsgListenerThread - Member Functions
102 ==================================================================================================*/
103 MsgProxyListener* MsgProxyListener::pInstance = NULL;
104
105
106 MsgProxyListener::MsgProxyListener() : running(0)
107 {
108         clearProxyCBLists();
109         clearOpenHandleSet();
110
111         msg_error_t err = MSG_SUCCESS;
112         err = MsgSettingRegVconfCBCommon(VCONFKEY_MSG_SERVER_READY, MsgServerRestartCb);
113         if (err != MSG_SUCCESS) {
114                 if (err == MSG_ERR_PERMISSION_DENIED) {
115                         THROW(MsgException::SECURITY_ERROR, "It must have privilege to access vconf key");
116                 } else {
117                         THROW(MsgException::SERVER_READY_ERROR, "vconf register fail");
118                 }
119         }
120
121         channel = NULL;
122         eventSourceId = 0;
123 }
124
125
126 MsgProxyListener::~MsgProxyListener()
127 {
128         clearProxyCBLists();
129         clearOpenHandleSet();
130
131         msg_error_t err = MSG_SUCCESS;
132         err = MsgSettingRemoveVconfCBCommon(VCONFKEY_MSG_SERVER_READY, MsgServerRestartCb);
133         if (err != MSG_SUCCESS) {
134                 if (err == MSG_ERR_PERMISSION_DENIED) {
135                         MSG_DEBUG("It must have privilege to access vconf key");
136                 } else {
137                         MSG_DEBUG("vconf register fail");
138                 }
139         }
140 }
141
142
143 MsgProxyListener* MsgProxyListener::instance()
144 {
145         static Mutex mm;
146         MutexLocker lock(mm);
147
148         if (!pInstance) {
149                 pInstance = new MsgProxyListener();
150         }
151
152         return pInstance;
153 }
154
155
156 void MsgProxyListener::start(MsgHandle* pMsgHandle)
157 {
158         MutexLocker lock(mx);
159
160         this->insertOpenHandleSet(pMsgHandle);
161
162         if (running == 0) {
163                 cliSock.connect(MSG_SOCKET_PATH);
164                 /* wake up the waiting thread */
165                 cv.signal();
166
167                 int fd = cliSock.fd();
168
169                 MSG_DEBUG("Socket Fd : %d", fd);
170
171                 /* initializes ref_count = 1 */
172                 channel = g_io_channel_unix_new(fd);
173
174                 /* increments ref_count = 2 */
175                 eventSourceId = g_io_add_watch(channel, (GIOCondition)(G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL), &readSocket, NULL);
176
177                 MSG_DEBUG("Call g_io_add_watch() : %d", eventSourceId);
178         }
179
180         running++;
181         MSG_DEBUG("add Listener and [%d] are running.", running);
182 }
183
184
185 void MsgProxyListener::stop()
186 {
187         MSG_BEGIN();
188
189         MutexLocker lock(mx);
190
191         if (running > 1) {
192                 running--;
193                 MSG_DEBUG("There are still running Listener. [%d] left.", running);
194         } else if (running == 1) {
195                 if (channel) {
196                         g_io_channel_unref(channel);
197                         channel = NULL;
198                 }
199
200                 if (eventSourceId > 0) {
201                         g_source_remove(eventSourceId);
202                         eventSourceId = 0;
203                 }
204
205                 running = 0;
206
207                 cliSock.close();
208                 MSG_DEBUG("client Listener is terminated.");
209         }
210
211         MSG_END();
212 }
213
214
215 bool MsgProxyListener::regSentStatusEventCB(MsgHandle* pMsgHandle, int fd, msg_sent_status_cb pfSentStatus, void *pUserParam)
216 {
217         MutexLocker lock(mx);
218
219         std::list<MSG_SENT_STATUS_CB_ITEM_S>::iterator it = sentStatusCBList.begin();
220
221         for (; it != sentStatusCBList.end(); it++) {
222                 if (it->hAddr == pMsgHandle && it->pfSentStatusCB == pfSentStatus) {
223                         if (it->fd == fd) {
224                                 MSG_DEBUG("msg_sent_status_cb() callback : [%p] is already registered!!!", pfSentStatus);
225                                 return false;
226                         } else {
227                                 MSG_DEBUG("callback is registered by restarting server");
228                                 it->fd = fd;
229                                 return true;
230                         }
231                 }
232         }
233
234         MSG_SENT_STATUS_CB_ITEM_S sentStatusCB = {pMsgHandle, fd, pfSentStatus, pUserParam};
235
236         sentStatusCBList.push_back(sentStatusCB);
237
238         return true;
239 }
240
241
242 bool MsgProxyListener::regMessageIncomingEventCB(MsgHandle* pMsgHandle, int fd, msg_sms_incoming_cb pfNewMessage, int port, void *pUserParam)
243 {
244         MutexLocker lock(mx);
245
246         std::list<MSG_INCOMING_CB_ITEM_S>::iterator it = newMessageCBList.begin();
247
248         for (; it != newMessageCBList.end(); it++) {
249                 if (it->hAddr == pMsgHandle && it->port == port && it->pfIncomingCB == pfNewMessage) {
250                         if (it->fd == fd) {
251                                 MSG_DEBUG("msg_sms_incoming_cb() callback : Port Number [%d] is already registered!!!", port);
252                                 return false;
253                         } else {
254                                 MSG_DEBUG("callback is registered by restarting server");
255                                 it->fd = fd;
256                                 return true;
257                         }
258                 }
259         }
260
261         MSG_INCOMING_CB_ITEM_S incomingCB = {pMsgHandle, fd, pfNewMessage, port, pUserParam};
262
263         newMessageCBList.push_back(incomingCB);
264
265         return true;
266 }
267
268
269 bool MsgProxyListener::regMMSConfMessageIncomingEventCB(MsgHandle* pMsgHandle, int fd, msg_mms_conf_msg_incoming_cb pfNewMMSConfMessage, const char *pAppId, void *pUserParam)
270 {
271         MutexLocker lock(mx);
272
273         std::list<MSG_MMS_CONF_INCOMING_CB_ITEM_S>::iterator it = newMMSConfMessageCBList.begin();
274
275         for (; it != newMMSConfMessageCBList.end(); it++) {
276                 if (it->hAddr == pMsgHandle && it->pfMMSConfIncomingCB == pfNewMMSConfMessage) {
277                         if (it->fd == fd) {
278                                 if (pAppId == NULL) {
279                                         MSG_DEBUG("msg_mms_conf_msg_incoming_cb() callback is already registered!!!");
280                                         return false;
281                                 } else if (!strncmp(it->appId, pAppId, MAX_MMS_JAVA_APPID_LEN)) {
282                                         MSG_DEBUG("msg_mms_conf_msg_incoming_cb() callback : AppId [%s] is already registered!!!", pAppId);
283                                         return false;
284                                 }
285                         } else {
286                                 MSG_DEBUG("callback is registered by restarting server");
287                                 it->fd = fd;
288                                 return true;
289                         }
290                 }
291         }
292
293         MSG_MMS_CONF_INCOMING_CB_ITEM_S incomingConfCB = {pMsgHandle, fd, pfNewMMSConfMessage, {0}, pUserParam};
294
295         if (pAppId != NULL)
296                 strncpy(incomingConfCB.appId, pAppId, MAX_MMS_JAVA_APPID_LEN);
297
298         newMMSConfMessageCBList.push_back(incomingConfCB);
299
300         return true;
301 }
302
303
304 bool MsgProxyListener::regPushMessageIncomingEventCB(MsgHandle* pMsgHandle, int fd, msg_push_msg_incoming_cb pfNewPushMessage, const char *pAppId, void *pUserParam)
305 {
306         MutexLocker lock(mx);
307
308         std::list<MSG_PUSH_INCOMING_CB_ITEM_S>::iterator it = newPushMessageCBList.begin();
309
310         for (; it != newPushMessageCBList.end(); it++) {
311                 if (it->hAddr == pMsgHandle && it->pfPushIncomingCB == pfNewPushMessage) {
312                         if (it->fd == fd) {
313                                 if (pAppId == NULL) {
314                                         MSG_DEBUG("msg_push_msg_incoming_cb() callback is already registered!!!");
315                                         return false;
316                                 } else if (!strncmp(it->appId, pAppId, MAX_WAPPUSH_ID_LEN)) {
317                                         MSG_DEBUG("msg_push_msg_incoming_cb() callback : AppId [%s] is already registered!!!", pAppId);
318                                         return false;
319                                 }
320                         } else {
321                                 MSG_DEBUG("callback is registered by restarting server");
322                                 it->fd = fd;
323                                 return true;
324                         }
325                 }
326         }
327
328         MSG_PUSH_INCOMING_CB_ITEM_S incomingPushCB = {pMsgHandle, fd, pfNewPushMessage, {0}, pUserParam};
329
330         if (pAppId != NULL)
331                 strncpy(incomingPushCB.appId, pAppId, MAX_WAPPUSH_ID_LEN);
332
333         newPushMessageCBList.push_back(incomingPushCB);
334
335         return true;
336 }
337
338
339 bool MsgProxyListener::regCBMessageIncomingEventCB(MsgHandle* pMsgHandle, int fd, msg_cb_incoming_cb pfNewCBMessage, bool bSave, void *pUserParam)
340 {
341         MutexLocker lock(mx);
342
343         std::list<MSG_CB_INCOMING_CB_ITEM_S>::iterator it = newCBMessageCBList.begin();
344
345         for (; it != newCBMessageCBList.end(); it++) {
346                 if (it->hAddr == pMsgHandle && it->pfCBIncomingCB == pfNewCBMessage) {
347                         if (it->fd == fd) {
348                                 MSG_DEBUG("msg_CB_incoming_cb() callback : [%p] is already registered!!!", pfNewCBMessage);
349                                  it->bsave = bSave;
350                                  it->userParam = pUserParam;
351                                 return false;
352                         } else {
353                                 MSG_DEBUG("callback is registered by restarting server");
354                                 it->fd = fd;
355                                 return true;
356                         }
357                 }
358         }
359
360         MSG_CB_INCOMING_CB_ITEM_S incomingCB = {pMsgHandle, fd, pfNewCBMessage, bSave, pUserParam};
361
362         newCBMessageCBList.push_back(incomingCB);
363
364         return true;
365 }
366
367
368 bool MsgProxyListener::regReportMsgIncomingCB(MsgHandle* pMsgHandle, int fd, msg_report_msg_incoming_cb pfReportMessage, void *pUserParam)
369 {
370         MutexLocker lock(mx);
371
372         std::list<MSG_REPORT_INCOMING_CB_ITEM_S>::iterator it = reportMessageCBList.begin();
373
374         for (; it != reportMessageCBList.end(); it++) {
375                 if (it->hAddr == pMsgHandle && it->pfReportMsgIncomingCB == pfReportMessage) {
376                         if (it->fd == fd) {
377                                 MSG_DEBUG("msg_report_msg_incoming_cb() callback : [%p] is already registered!!!", pfReportMessage);
378                                  it->userParam = pUserParam;
379                                 return false;
380                         } else {
381                                 MSG_DEBUG("callback is registered by restarting server");
382                                 it->fd = fd;
383                                 return true;
384                         }
385                 }
386         }
387
388         MSG_REPORT_INCOMING_CB_ITEM_S incomingCB = {pMsgHandle, fd, pfReportMessage, pUserParam};
389
390         reportMessageCBList.push_back(incomingCB);
391
392         return true;
393 }
394
395
396 bool MsgProxyListener::regSyncMLMessageIncomingEventCB(MsgHandle* pMsgHandle, int fd, msg_syncml_msg_incoming_cb pfNewSyncMLMessage, void *pUserParam)
397 {
398         MutexLocker lock(mx);
399
400         std::list<MSG_SYNCML_INCOMING_CB_ITEM_S>::iterator it = newSyncMLMessageCBList.begin();
401
402         for (; it != newSyncMLMessageCBList.end(); it++) {
403                 if (it->hAddr == pMsgHandle && it->pfSyncMLIncomingCB == pfNewSyncMLMessage) {
404                         if (it->fd == fd) {
405                                 MSG_DEBUG("msg_syncml_msg_incoming_cb() callback : [%p] is already registered!!!", pfNewSyncMLMessage);
406                                 return false;
407                         } else {
408                                 MSG_DEBUG("callback is registered by restarting server");
409                                 it->fd = fd;
410                                 return true;
411                         }
412                 }
413         }
414
415         MSG_SYNCML_INCOMING_CB_ITEM_S incomingCB = {pMsgHandle, fd, pfNewSyncMLMessage, pUserParam};
416
417         newSyncMLMessageCBList.push_back(incomingCB);
418
419         return true;
420 }
421
422
423 bool MsgProxyListener::regLBSMessageIncomingEventCB(MsgHandle* pMsgHandle, int fd, msg_lbs_msg_incoming_cb pfNewLBSMsgIncoming, void *pUserParam)
424 {
425         MutexLocker lock(mx);
426
427         std::list<MSG_LBS_INCOMING_CB_ITEM_S>::iterator it = newLBSMessageCBList.begin();
428
429         for (; it != newLBSMessageCBList.end(); it++) {
430                 if (it->hAddr == pMsgHandle && it->pfLBSMsgIncoming == pfNewLBSMsgIncoming) {
431                         if (it->fd == fd) {
432                                 MSG_DEBUG("msg_lbs_msg_incoming_cb() callback : [%p] is already registered!!!", pfNewLBSMsgIncoming);
433                                 return false;
434                         } else {
435                                 MSG_DEBUG("callback is registered by restarting server");
436                                 it->fd = fd;
437                                 return true;
438                         }
439                 }
440         }
441
442         MSG_LBS_INCOMING_CB_ITEM_S incomingCB = {pMsgHandle, fd, pfNewLBSMsgIncoming, pUserParam};
443
444         newLBSMessageCBList.push_back(incomingCB);
445
446         return true;
447 }
448
449
450 bool MsgProxyListener::regSyncMLMessageOperationEventCB(MsgHandle* pMsgHandle, int fd, msg_syncml_msg_operation_cb pfSyncMLMessageOperation, void *pUserParam)
451 {
452         MutexLocker lock(mx);
453
454         std::list<MSG_SYNCML_OPERATION_CB_ITEM_S>::iterator it = operationSyncMLMessageCBList.begin();
455
456         for (; it != operationSyncMLMessageCBList.end(); it++) {
457                 if (it->hAddr == pMsgHandle && it->pfSyncMLOperationCB == pfSyncMLMessageOperation) {
458                         if (it->fd == fd) {
459                                 MSG_DEBUG("msg_syncml_msg_incoming_cb() callback : [%p] is already registered!!!", pfSyncMLMessageOperation);
460                                 return false;
461                         } else {
462                                 MSG_DEBUG("callback is registered by restarting server");
463                                 it->fd = fd;
464                                 return true;
465                         }
466                 }
467         }
468
469         MSG_SYNCML_OPERATION_CB_ITEM_S incomingCB = {pMsgHandle, fd, pfSyncMLMessageOperation, pUserParam};
470
471         operationSyncMLMessageCBList.push_back(incomingCB);
472
473         return true;
474 }
475
476
477 bool MsgProxyListener::regStorageChangeEventCB(MsgHandle* pMsgHandle, int fd, msg_storage_change_cb pfStorageChangeOperation, void *pUserParam)
478 {
479         MutexLocker lock(mx);
480
481         std::list<MSG_STORAGE_CHANGE_CB_ITEM_S>::iterator it = storageChangeCBList.begin();
482
483         for (; it != storageChangeCBList.end(); it++) {
484                 if (it->hAddr == pMsgHandle && it->pfStorageChangeCB == pfStorageChangeOperation) {
485                         if (it->fd == fd) {
486                                 MSG_DEBUG("msg_storage_change_cb() callback : [%p] is already registered!!!", pfStorageChangeOperation);
487                                 return false;
488                         } else {
489                                 MSG_DEBUG("callback is registered by restarting server");
490                                 it->fd = fd;
491                                 return true;
492                         }
493                 }
494         }
495
496         MSG_STORAGE_CHANGE_CB_ITEM_S changeCB = {pMsgHandle, fd, pfStorageChangeOperation, pUserParam};
497
498         storageChangeCBList.push_back(changeCB);
499
500         return true;
501 }
502
503
504 void MsgProxyListener::clearListOfClosedHandle(MsgHandle* pMsgHandle)
505 {
506         MSG_BEGIN();
507
508         MutexLocker lock(mx);
509
510         /* sent status CB list */
511         std::list<MSG_SENT_STATUS_CB_ITEM_S>::iterator it = sentStatusCBList.begin();
512
513         for (; it != sentStatusCBList.end(); ) {
514                 if (it->hAddr == pMsgHandle) {
515                         sentStatusCBList.erase(it++);
516                         stop();
517                 } else {
518                         ++it;
519                 }
520         }
521
522         /* new message CB list */
523         std::list<MSG_INCOMING_CB_ITEM_S>::iterator it2 = newMessageCBList.begin();
524
525         for (; it2 != newMessageCBList.end(); ) {
526                 if (it2->hAddr == pMsgHandle) {
527                         newMessageCBList.erase(it2++);
528                         stop();
529                 } else {
530                         ++it2;
531                 }
532         }
533
534         /* MMS conf Message CB list */
535         std::list<MSG_MMS_CONF_INCOMING_CB_ITEM_S>::iterator it3 = newMMSConfMessageCBList.begin();
536
537         for (; it3 != newMMSConfMessageCBList.end(); ) {
538                 if (it3->hAddr == pMsgHandle) {
539                         newMMSConfMessageCBList.erase(it3++);
540                         stop();
541                 } else {
542                         ++it3;
543                 }
544         }
545
546         /* SyncML Message CB list */
547         std::list<MSG_SYNCML_INCOMING_CB_ITEM_S>::iterator it4 = newSyncMLMessageCBList.begin();
548
549         for (; it4 != newSyncMLMessageCBList.end(); ) {
550                 if (it4->hAddr == pMsgHandle) {
551                         newSyncMLMessageCBList.erase(it4++);
552                         stop();
553                 } else {
554                         ++it4;
555                 }
556         }
557
558         /* LBS Message CB list */
559         std::list<MSG_LBS_INCOMING_CB_ITEM_S>::iterator it5 = newLBSMessageCBList.begin();
560
561         for (; it5 != newLBSMessageCBList.end(); ) {
562                 if (it5->hAddr == pMsgHandle) {
563                         newLBSMessageCBList.erase(it5++);
564                         stop();
565                 } else {
566                         ++it5;
567                 }
568         }
569
570         /* Push Message CB list */
571         std::list<MSG_PUSH_INCOMING_CB_ITEM_S>::iterator it6 = newPushMessageCBList.begin();
572
573         for (; it6 != newPushMessageCBList.end(); ) {
574                 if (it6->hAddr == pMsgHandle) {
575                         newPushMessageCBList.erase(it6++);
576                         stop();
577                 } else {
578                         ++it6;
579                 }
580         }
581
582         /* CB Message CB list */
583         std::list<MSG_CB_INCOMING_CB_ITEM_S>::iterator it7 = newCBMessageCBList.begin();
584
585         for (; it7 != newCBMessageCBList.end(); ) {
586                 if (it7->hAddr == pMsgHandle) {
587                         newCBMessageCBList.erase(it7++);
588                         stop();
589                 } else {
590                         ++it7;
591                 }
592         }
593
594         /* Storage change Message CB list */
595         std::list<MSG_STORAGE_CHANGE_CB_ITEM_S>::iterator it8 = storageChangeCBList.begin();
596
597         for (; it8 != storageChangeCBList.end(); ) {
598                 if (it8->hAddr == pMsgHandle) {
599                         storageChangeCBList.erase(it8++);
600                         stop();
601                 } else {
602                         ++it8;
603                 }
604         }
605
606
607         /* Report message incoming CB list */
608         std::list<MSG_REPORT_INCOMING_CB_ITEM_S>::iterator it9 = reportMessageCBList.begin();
609         for (; it9 != reportMessageCBList.end(); ) {
610                 if (it9->hAddr == pMsgHandle) {
611                         reportMessageCBList.erase(it9++);
612                         stop();
613                 } else {
614                         ++it9;
615                 }
616         }
617
618
619         /* SyncML Message Operation CB list */
620         std::list<MSG_SYNCML_OPERATION_CB_ITEM_S>::iterator it10 = operationSyncMLMessageCBList.begin();
621         for (; it10 != operationSyncMLMessageCBList.end(); ) {
622                 if (it10->hAddr == pMsgHandle) {
623                         operationSyncMLMessageCBList.erase(it10++);
624                         stop();
625                 } else {
626                         ++it10;
627                 }
628         }
629
630         /* Open Handle Set */
631         openHandleSet.erase(pMsgHandle);
632
633         MSG_END();
634 }
635
636
637 void MsgProxyListener::refreshListOfOpenedHandle(MsgHandle* pMsgHandle)
638 {
639         MSG_BEGIN();
640
641         MutexLocker lock(mx);
642
643         /* sent status CB list */
644         std::list<MSG_SENT_STATUS_CB_ITEM_S>::iterator it = sentStatusCBList.begin();
645         for (; it != sentStatusCBList.end(); ++it) {
646                 if (it->hAddr == pMsgHandle) {
647                         it->fd = CUSTOM_SOCKET_ERROR;
648                         try {
649                                 pMsgHandle->regSentStatusCallback(it->pfSentStatusCB, it->userParam);
650                         } catch (MsgException& e) {
651                                 MSG_FATAL("%s", e.what());
652                         }
653                 }
654         }
655
656         /* new message CB list */
657         std::list<MSG_INCOMING_CB_ITEM_S>::iterator it2 = newMessageCBList.begin();
658         for (; it2 != newMessageCBList.end(); ++it2) {
659                 if (it2->hAddr == pMsgHandle) {
660                         it2->fd = CUSTOM_SOCKET_ERROR;
661                         try {
662                                 pMsgHandle->regSmsMessageCallback(it2->pfIncomingCB, it2->port, it2->userParam);
663                         } catch (MsgException& e) {
664                                 MSG_FATAL("%s", e.what());
665                         }
666                 }
667         }
668
669         /* MMS conf Message CB list */
670         std::list<MSG_MMS_CONF_INCOMING_CB_ITEM_S>::iterator it3 = newMMSConfMessageCBList.begin();
671         for (; it3 != newMMSConfMessageCBList.end(); ++it3) {
672                 if (it3->hAddr == pMsgHandle) {
673                         it3->fd = CUSTOM_SOCKET_ERROR;
674                         try {
675                                 pMsgHandle->regMmsConfMessageCallback(it3->pfMMSConfIncomingCB, it3->appId, it3->userParam);
676                         } catch (MsgException& e) {
677                                 MSG_FATAL("%s", e.what());
678                         }
679                 }
680         }
681
682         /* SyncML Message CB list */
683         std::list<MSG_SYNCML_INCOMING_CB_ITEM_S>::iterator it4 = newSyncMLMessageCBList.begin();
684         for (; it4 != newSyncMLMessageCBList.end(); ++it4) {
685                 if (it4->hAddr == pMsgHandle) {
686                         it4->fd = CUSTOM_SOCKET_ERROR;
687                         try {
688                                 pMsgHandle->regSyncMLMessageCallback(it4->pfSyncMLIncomingCB, it4->userParam);
689                         } catch (MsgException& e) {
690                                 MSG_FATAL("%s", e.what());
691                         }
692                 }
693         }
694
695         /* LBS Message CB list */
696         std::list<MSG_LBS_INCOMING_CB_ITEM_S>::iterator it5 = newLBSMessageCBList.begin();
697         for (; it5 != newLBSMessageCBList.end(); ++it5) {
698                 if (it5->hAddr == pMsgHandle) {
699                         it5->fd = CUSTOM_SOCKET_ERROR;
700                         try {
701                                 pMsgHandle->regLBSMessageCallback(it5->pfLBSMsgIncoming, it5->userParam);
702                         } catch (MsgException& e) {
703                                 MSG_FATAL("%s", e.what());
704                         }
705                 }
706         }
707
708         /* Push Message CB list */
709         std::list<MSG_PUSH_INCOMING_CB_ITEM_S>::iterator it6 = newPushMessageCBList.begin();
710         for (; it6 != newPushMessageCBList.end(); ++it6) {
711                 if (it6->hAddr == pMsgHandle) {
712                         it6->fd = CUSTOM_SOCKET_ERROR;
713                         try {
714                                 pMsgHandle->regPushMessageCallback(it6->pfPushIncomingCB, it6->appId, it6->userParam);
715                         } catch (MsgException& e) {
716                                 MSG_FATAL("%s", e.what());
717                         }
718                 }
719         }
720
721         /* CB Message CB list */
722         std::list<MSG_CB_INCOMING_CB_ITEM_S>::iterator it7 = newCBMessageCBList.begin();
723         for (; it7 != newCBMessageCBList.end(); ++it7) {
724                 if (it7->hAddr == pMsgHandle) {
725                         it7->fd = CUSTOM_SOCKET_ERROR;
726                         try {
727                                 pMsgHandle->regCBMessageCallback(it7->pfCBIncomingCB, it7->bsave, it7->userParam);
728                         } catch (MsgException& e) {
729                                 MSG_FATAL("%s", e.what());
730                         }
731                 }
732         }
733
734         /* Storage change Message CB list */
735         std::list<MSG_STORAGE_CHANGE_CB_ITEM_S>::iterator it8 = storageChangeCBList.begin();
736         for (; it8 != storageChangeCBList.end(); ++it8) {
737                 if (it8->hAddr == pMsgHandle) {
738                         it8->fd = CUSTOM_SOCKET_ERROR;
739                         try {
740                                 pMsgHandle->regStorageChangeCallback(it8->pfStorageChangeCB, it8->userParam);
741                         } catch (MsgException& e) {
742                                 MSG_FATAL("%s", e.what());
743                         }
744                 }
745         }
746
747         /* Report message incoming CB list */
748         std::list<MSG_REPORT_INCOMING_CB_ITEM_S>::iterator it9 = reportMessageCBList.begin();
749         for (; it9 != reportMessageCBList.end(); ++it9) {
750                 if (it9->hAddr == pMsgHandle) {
751                         it9->fd = CUSTOM_SOCKET_ERROR;
752                         try {
753                                 pMsgHandle->regReportMessageCallback(it9->pfReportMsgIncomingCB, it9->userParam);
754                         } catch (MsgException& e) {
755                                 MSG_FATAL("%s", e.what());
756                         }
757                 }
758         }
759
760         /* SyncML Message Operation CB list */
761         std::list<MSG_SYNCML_OPERATION_CB_ITEM_S>::iterator it10 = operationSyncMLMessageCBList.begin();
762         for (; it10 != operationSyncMLMessageCBList.end(); ++it10) {
763                 if (it10->hAddr == pMsgHandle) {
764                         it10->fd = CUSTOM_SOCKET_ERROR;
765                         try {
766                                 pMsgHandle->regSyncMLMessageOperationCallback(it10->pfSyncMLOperationCB, it10->userParam);
767                         } catch (MsgException& e) {
768                                 MSG_FATAL("%s", e.what());
769                         }
770                 }
771         }
772
773         MSG_END();
774 }
775
776
777 void MsgProxyListener::handleEvent(const MSG_EVENT_S* pMsgEvent)
778 {
779         MSG_BEGIN();
780
781         if (!pMsgEvent)
782                 THROW(MsgException::INVALID_PARAM, "pMsgEvent is NULL");
783
784         if (pMsgEvent->eventType == MSG_EVENT_PLG_SENT_STATUS_CNF) {
785                 unsigned int chInfo[3] = {0}; /*3 reqid, status, object */
786
787                 memcpy(&chInfo, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)), sizeof(chInfo));
788
789                 msg_struct_s status = {0, };
790                 MSG_SENT_STATUS_S statusData = {(msg_request_id_t)chInfo[0], (msg_network_status_t)chInfo[1]};
791
792                 status.type = MSG_STRUCT_SENT_STATUS_INFO;
793                 status.data = (void *)&statusData;
794
795                 mx.lock();
796
797                 MsgSentStatusCBList::iterator it = sentStatusCBList.begin();
798
799                 for ( ; it != sentStatusCBList.end() ; it++) {
800                         MsgHandle* pHandle = it->hAddr;
801
802                         msg_sent_status_cb pfunc = it->pfSentStatusCB;
803
804                         void* param = it->userParam;
805
806                         pfunc((msg_handle_t)pHandle, (msg_struct_t)&status, param);
807                 }
808
809                 mx.unlock();
810         } else if (pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_MSG_IND) {
811                 MSG_MESSAGE_INFO_S msgInfo;
812                 memset(&msgInfo, 0x00, sizeof(MSG_MESSAGE_INFO_S));
813
814                 msgInfo.addressList = NULL;
815                 unique_ptr<MSG_ADDRESS_INFO_S*, void(*)(MSG_ADDRESS_INFO_S**)> addressListBuf(&msgInfo.addressList, unique_ptr_deleter);
816
817                 MsgDecodeMsgInfo((char *)pMsgEvent->data, &msgInfo);
818
819                 int portKey = (msgInfo.msgPort.valid)? msgInfo.msgPort.dstPort: 0;
820
821                 mx.lock();
822
823                 MsgNewMessageCBList::iterator it = newMessageCBList.begin();
824                 MsgNewMessageCBList matchList;
825
826                 for ( ; it != newMessageCBList.end() ; it++) {
827                         if (portKey == it->port) {
828                                 matchList.push_back(*it);
829                         }
830                 }
831
832                 mx.unlock();
833
834                 it = matchList.begin();
835
836                 for ( ; it != matchList.end(); it++ ) {
837                         MsgHandle* pHandle = it->hAddr;
838
839                         MSG_MESSAGE_HIDDEN_S msgHidden = {0, };
840
841                         msgHidden.pData = NULL;
842                         msgHidden.pMmsData = NULL;
843
844                         /* Allocate memory for address list of message */
845                         msg_struct_list_s *addr_list = (msg_struct_list_s *)new msg_struct_list_s;
846
847                         addr_list->nCount = 0;
848                         addr_list->msg_struct_info = (msg_struct_t *)calloc(MAX_TO_ADDRESS_CNT, sizeof(MSG_ADDRESS_INFO_S *));
849                         if (addr_list->msg_struct_info == NULL)
850                                 continue;
851
852                         msg_struct_s *pTmp = NULL;
853
854                         for (int i = 0; i < MAX_TO_ADDRESS_CNT; i++) {
855                                 addr_list->msg_struct_info[i] = (msg_struct_t)new msg_struct_s;
856                                 pTmp = (msg_struct_s *)addr_list->msg_struct_info[i];
857                                 pTmp->type = MSG_STRUCT_ADDRESS_INFO;
858                                 pTmp->data = new MSG_ADDRESS_INFO_S;
859                                 memset(pTmp->data, 0x00, sizeof(MSG_ADDRESS_INFO_S));
860
861                                 addr_list->msg_struct_info[i] = (msg_struct_t)pTmp;
862                         }
863
864                         msgHidden.addr_list = addr_list;
865
866                         try {
867                                 pHandle->convertMsgStruct(&msgInfo, &msgHidden);
868                         }
869                         catch (MsgException& e) {
870                                 MSG_FATAL("%s", e.what());
871                         }
872
873                         msg_struct_s msg = {0, };
874                         msg.type = MSG_STRUCT_MESSAGE_INFO;
875                         msg.data = &msgHidden;
876
877                         msg_sms_incoming_cb pfunc = it->pfIncomingCB;
878
879                         void* param = it->userParam;
880
881                         pfunc((msg_handle_t)pHandle, (msg_struct_t) &msg, param);
882
883                         delete [] (char*)msgHidden.pData;
884                         if (msgHidden.pMmsData != NULL)
885                                 delete [] (char*)msgHidden.pMmsData;
886
887                         /* address Memory Free */
888                         if (msgHidden.addr_list!= NULL) {
889                                 for (int i = 0; i < MAX_TO_ADDRESS_CNT; i++) {
890                                         msg_struct_s * addrInfo = (msg_struct_s *)msgHidden.addr_list->msg_struct_info[i];
891                                         delete (MSG_ADDRESS_INFO_S *)addrInfo->data;
892                                         addrInfo->data = NULL;
893                                         delete (msg_struct_s *)msgHidden.addr_list->msg_struct_info[i];
894                                         msgHidden.addr_list->msg_struct_info[i] = NULL;
895                                 }
896
897                                 g_free(msgHidden.addr_list->msg_struct_info);
898
899                                 delete msgHidden.addr_list;
900                                 msgHidden.addr_list = NULL;
901                         }
902                 }
903
904         } else if (pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_MMS_CONF) {
905                 MSG_MESSAGE_INFO_S msgInfo;
906                 memset(&msgInfo, 0x00, sizeof(MSG_MESSAGE_INFO_S));
907
908                 msgInfo.addressList = NULL;
909                 unique_ptr<MSG_ADDRESS_INFO_S*, void(*)(MSG_ADDRESS_INFO_S**)> addressListBuf(&msgInfo.addressList, unique_ptr_deleter);
910
911                 MsgDecodeMsgInfo((char *)pMsgEvent->data, &msgInfo);
912
913
914                 MMS_RECV_DATA_S* pMmsRecvData = (MMS_RECV_DATA_S*)msgInfo.msgData;
915
916                 char* appIdKey = (pMmsRecvData->msgAppId.valid)? pMmsRecvData->msgAppId.appId: NULL;
917
918                 mx.lock();
919
920                 MsgNewMMSConfMessageCBList::iterator it = newMMSConfMessageCBList.begin();
921                 MsgNewMMSConfMessageCBList matchList;
922
923                 for ( ; it != newMMSConfMessageCBList.end(); it++) {
924                         if (appIdKey) {
925                                 if (!strcmp(appIdKey, it->appId))
926                                         matchList.push_back(*it);
927                         } else {
928                                 /* (appIdKey == NULL && it->appId[0] == 0) */
929                                 if (it->appId[0] == 0)
930                                         matchList.push_back(*it);
931                         }
932                 }
933
934                 mx.unlock();
935
936                 /* Contents of msgData removed and replaced to retrievedFilePath for convertMsgStruct */
937                 /* it is moved from UpdateMessage in MmsPluginStorage.cpp */
938                 char tempFileName[MSG_FILENAME_LEN_MAX+1] = {0}; /* check MSG_FILENAME_LEN_MAX */
939
940                 strncpy(tempFileName, pMmsRecvData->retrievedFilePath, MSG_FILENAME_LEN_MAX);
941
942                 memset(msgInfo.msgData, 0, MAX_MSG_DATA_LEN+1);
943                 memcpy(msgInfo.msgData, tempFileName + strlen(MSG_DATA_PATH), MAX_MSG_DATA_LEN);
944
945                 it = matchList.begin();
946
947                 for ( ; it != matchList.end(); it++) {
948                         MsgHandle* pHandle = it->hAddr;
949
950                         MSG_MESSAGE_HIDDEN_S msgHidden = {0, };
951
952                         msgHidden.pData = NULL;
953                         msgHidden.pMmsData = NULL;
954
955                         /* Allocate memory for address list of message */
956                         msg_struct_list_s *addr_list = (msg_struct_list_s *)new msg_struct_list_s;
957
958                         addr_list->nCount = 0;
959                         addr_list->msg_struct_info = (msg_struct_t *)calloc(MAX_TO_ADDRESS_CNT, sizeof(MSG_ADDRESS_INFO_S *));
960                         if (addr_list->msg_struct_info == NULL)
961                                 continue;
962
963                         msg_struct_s *pTmp = NULL;
964
965                         for (int i = 0; i < MAX_TO_ADDRESS_CNT; i++) {
966                                 addr_list->msg_struct_info[i] = (msg_struct_t)new msg_struct_s;
967                                 pTmp = (msg_struct_s *)addr_list->msg_struct_info[i];
968                                 pTmp->type = MSG_STRUCT_ADDRESS_INFO;
969                                 pTmp->data = new MSG_ADDRESS_INFO_S;
970                                 memset(pTmp->data, 0x00, sizeof(MSG_ADDRESS_INFO_S));
971
972                                 addr_list->msg_struct_info[i] = (msg_struct_t)pTmp;
973                         }
974
975                         msgHidden.addr_list = addr_list;
976
977                         try {
978                                 pHandle->convertMsgStruct(&msgInfo, &msgHidden);
979                         }
980                         catch (MsgException& e) {
981                                 MSG_FATAL("%s", e.what());
982                         }
983
984                         msg_struct_s msg = {0, };
985                         msg.type = MSG_STRUCT_MESSAGE_INFO;
986                         msg.data = &msgHidden;
987
988                         msg_mms_conf_msg_incoming_cb pfunc = it->pfMMSConfIncomingCB;
989
990                         void* param = it->userParam;
991                         pfunc((msg_handle_t)pHandle, (msg_struct_t) &msg, param);
992
993                         delete [] (char*)msgHidden.pData;
994                         if (msgHidden.pMmsData != NULL)
995                                 delete [] (char*)msgHidden.pMmsData;
996
997                         /* address Memory Free */
998                         if (msgHidden.addr_list != NULL) {
999                                 for (int i = 0; i < MAX_TO_ADDRESS_CNT; i++) {
1000                                         msg_struct_s * addrInfo = (msg_struct_s *)msgHidden.addr_list->msg_struct_info[i];
1001                                         delete (MSG_ADDRESS_INFO_S *)addrInfo->data;
1002                                         addrInfo->data = NULL;
1003                                         delete (msg_struct_s *)msgHidden.addr_list->msg_struct_info[i];
1004                                         msgHidden.addr_list->msg_struct_info[i] = NULL;
1005                                 }
1006
1007                                 g_free(msgHidden.addr_list->msg_struct_info);
1008
1009                                 delete msgHidden.addr_list;
1010                                 msgHidden.addr_list = NULL;
1011                         }
1012
1013                         /* Here the retrieved message will be deleted from native storage. */
1014                         /* as of now, msg which have appId is considered as JAVA MMS message and it will be sent and handled in JAVA app. */
1015                         if (appIdKey) {
1016                                 MSG_DEBUG("delete received JAVA MMS message:%s from native storage", tempFileName);
1017                                 pHandle->deleteMessage(msgInfo.msgId);
1018                         }
1019                 }
1020         } else if (pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_SYNCML_MSG_IND) {
1021                 MSG_SYNCML_MESSAGE_DATA_S* pSyncMLData = (MSG_SYNCML_MESSAGE_DATA_S *)pMsgEvent->data;
1022
1023                 MSG_DEBUG("msgType [%d]", pSyncMLData->syncmlType);
1024
1025                 mx.lock();
1026
1027                 MsgNewSyncMLMessageCBList::iterator it = newSyncMLMessageCBList.begin();
1028
1029                 for ( ; it != newSyncMLMessageCBList.end(); it++) {
1030                         MsgHandle* pHandle = it->hAddr;
1031
1032                         msg_syncml_msg_incoming_cb pfunc = it->pfSyncMLIncomingCB;
1033
1034                         void* param = it->userParam;
1035
1036                         pfunc((msg_handle_t)pHandle, pSyncMLData->syncmlType, pSyncMLData->pushBody, pSyncMLData->pushBodyLen, pSyncMLData->wspHeader, pSyncMLData->wspHeaderLen, pSyncMLData->simIndex, param);
1037                 }
1038
1039                 mx.unlock();
1040         } else if (pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_LBS_MSG_IND) {
1041                 MSG_LBS_MESSAGE_DATA_S* pLBSData = (MSG_LBS_MESSAGE_DATA_S *)pMsgEvent->data;
1042
1043                 mx.lock();
1044
1045                 MsgNewLBSMessageCBList::iterator it = newLBSMessageCBList.begin();
1046
1047                 for ( ; it != newLBSMessageCBList.end(); it++) {
1048                         MsgHandle* pHandle = it->hAddr;
1049
1050                         msg_lbs_msg_incoming_cb pfunc = it->pfLBSMsgIncoming;
1051
1052                         void* param = it->userParam;
1053
1054                         pfunc((msg_handle_t)pHandle, pLBSData->pushHeader, pLBSData->pushBody, pLBSData->pushBodyLen, param);
1055                 }
1056
1057                 mx.unlock();
1058         } else if (pMsgEvent->eventType == MSG_EVENT_SYNCML_OPERATION) {
1059                 int msgId;
1060                 int extId;
1061
1062                 memcpy(&msgId, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)), sizeof(int));
1063                 memcpy(&extId, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)+sizeof(int)), sizeof(int));
1064
1065                 MSG_DEBUG("msgId [%d]", msgId);
1066                 MSG_DEBUG("extId [%d]", extId);
1067
1068                 mx.lock();
1069
1070                 MsgOperationSyncMLMessageCBList::iterator it = operationSyncMLMessageCBList.begin();
1071
1072                 for ( ; it != operationSyncMLMessageCBList.end(); it++) {
1073                         MsgHandle* pHandle = it->hAddr;
1074
1075                         msg_syncml_msg_operation_cb pfunc = it->pfSyncMLOperationCB;
1076
1077                         void* param = it->userParam;
1078
1079                         pfunc((msg_handle_t)pHandle, msgId, extId, param);
1080                 }
1081
1082                 mx.unlock();
1083         } else if (pMsgEvent->eventType == MSG_EVENT_PLG_STORAGE_CHANGE_IND) {
1084                 msg_storage_change_type_t storageChangeType;
1085                 msg_id_list_s msgIdList;
1086                 memset(&msgIdList, 0x00, sizeof(msg_id_list_s));
1087
1088                 /* Decode event data */
1089                 memcpy(&storageChangeType, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)), sizeof(msg_storage_change_type_t));
1090                 memcpy(&msgIdList.nCount, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)+sizeof(msg_storage_change_type_t)), sizeof(int));
1091
1092                 if (msgIdList.nCount > 0)
1093                         msgIdList.msgIdList = (msg_message_id_t*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)+sizeof(msg_storage_change_type_t)+sizeof(int));
1094                 else
1095                         msgIdList.msgIdList = NULL;
1096
1097                 MSG_DEBUG("storageChangeType [%d], msgIdList.nCount [%d]", storageChangeType, msgIdList.nCount);
1098
1099                 mx.lock();
1100
1101                 MsgStorageChangeCBList::iterator it = storageChangeCBList.begin();
1102
1103                 for ( ; it != storageChangeCBList.end(); it++) {
1104                         MsgHandle* pHandle = it->hAddr;
1105
1106                         msg_storage_change_cb pfunc = it->pfStorageChangeCB;
1107
1108                         void* param = it->userParam;
1109
1110                         pfunc((msg_handle_t)pHandle, storageChangeType, (msg_id_list_s*)&msgIdList, param);
1111                 }
1112
1113                 mx.unlock();
1114         } else if (pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_CB_MSG_IND) {
1115                 MSG_CB_MSG_S *pCbMsg = (MSG_CB_MSG_S *)pMsgEvent->data;
1116
1117                 mx.lock();
1118
1119                 MsgNewCBMessageCBList::iterator it = newCBMessageCBList.begin();
1120
1121                 for ( ; it != newCBMessageCBList.end(); it++) {
1122                         MsgHandle* pHandle = it->hAddr;
1123                         msg_struct_s msg = {0, };
1124
1125                         msg.type = MSG_STRUCT_CB_MSG;
1126                         msg.data = pCbMsg;
1127
1128                         msg_cb_incoming_cb pfunc = it->pfCBIncomingCB;
1129
1130                         void* param = it->userParam;
1131
1132                         pfunc((msg_handle_t)pHandle, (msg_struct_t) &msg, param);
1133                 }
1134
1135                 mx.unlock();
1136         } else if (pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_PUSH_MSG_IND) {
1137                 MSG_PUSH_MESSAGE_DATA_S* pPushData = (MSG_PUSH_MESSAGE_DATA_S *)pMsgEvent->data;
1138
1139                 mx.lock();
1140
1141                 MsgNewPushMessageCBList::iterator it = newPushMessageCBList.begin();
1142
1143                 for ( ; it != newPushMessageCBList.end(); it++) {
1144                         MsgHandle* pHandle = it->hAddr;
1145
1146                         msg_push_msg_incoming_cb pfunc = it->pfPushIncomingCB;
1147
1148                         void* param = it->userParam;
1149
1150                         if (!strncmp(it->appId, pPushData->pushAppId, MAX_WAPPUSH_ID_LEN))
1151                                 pfunc((msg_handle_t)pHandle, pPushData->pushHeader, pPushData->pushBody, pPushData->pushBodyLen, param);
1152                 }
1153
1154                 mx.unlock();
1155         } else if (pMsgEvent->eventType == MSG_EVENT_PLG_REPORT_MSG_INCOMING_IND) {
1156                 msg_report_type_t reportType;
1157                 msg_message_id_t msgId;
1158                 int addr_len;
1159                 char *addr_val;
1160
1161                 /* Decode event data */
1162                 memcpy(&reportType, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)), sizeof(msg_report_type_t));
1163                 memcpy(&msgId, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)+sizeof(msg_report_type_t)), sizeof(msg_message_id_t));
1164                 memcpy(&addr_len, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)+sizeof(msg_report_type_t)+sizeof(msg_message_id_t)), sizeof(int));
1165                 addr_val = (char*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)+sizeof(msg_report_type_t)+sizeof(msg_message_id_t)+sizeof(int));
1166                 addr_val[addr_len] = '\0';
1167
1168                 MSG_SEC_DEBUG("reportType [%d], msgId [%d], Address Length [%d], Address Value [%s]", reportType, msgId, addr_len, addr_val);
1169
1170                 mx.lock();
1171
1172                 MsgReportMessageCBList::iterator it = reportMessageCBList.begin();
1173
1174                 for ( ; it != reportMessageCBList.end(); it++) {
1175                         MsgHandle* pHandle = it->hAddr;
1176
1177                         msg_report_msg_incoming_cb pfunc = it->pfReportMsgIncomingCB;
1178
1179                         void* param = it->userParam;
1180
1181                         pfunc((msg_handle_t)pHandle, reportType, msgId, addr_len, addr_val, param);
1182                 }
1183
1184                 mx.unlock();
1185         }
1186
1187         MSG_END();
1188 }
1189
1190
1191 int MsgProxyListener::getRemoteFd()
1192 {
1193         return cliSock.getRemoteFd();
1194 }
1195
1196
1197 int MsgProxyListener::readFromSocket(char** buf, unsigned int* len)
1198 {
1199         return cliSock.read(buf, len);
1200 }
1201
1202
1203 void MsgProxyListener::resetProxyListener()
1204 {
1205         MSG_DEBUG("client Listener reset");
1206         MutexLocker lock(mx);
1207
1208         if (channel) {
1209                 g_io_channel_unref(channel);
1210                 channel = NULL;
1211         }
1212
1213         if (eventSourceId > 0) {
1214                 g_source_remove(eventSourceId);
1215                 eventSourceId = 0;
1216         }
1217
1218         running = 0;
1219
1220         cliSock.close();
1221
1222         handle_set::iterator it = openHandleSet.begin();
1223         for (; it != openHandleSet.end(); it++) {
1224                 MSG_DEBUG("disconnect socket for opened handle [%p]", it);
1225                 MsgHandle *handle = (MsgHandle *)*it;
1226                 handle->disconnectSocket();
1227         }
1228 }
1229
1230
1231 void MsgProxyListener::refreshProxyListener()
1232 {
1233         MSG_DEBUG("refresh proxy listener");
1234         MutexLocker lock(mx);
1235
1236         handle_set::iterator it = openHandleSet.begin();
1237         for (; it != openHandleSet.end(); it++) {
1238                 MsgHandle *handle = (MsgHandle *)*it;
1239                 handle->openHandle();
1240                 refreshListOfOpenedHandle(handle);
1241         }
1242 }
1243
1244
1245 void MsgProxyListener::clearProxyCBLists()
1246 {
1247         MSG_DEBUG("clear proxy callback list");
1248
1249         sentStatusCBList.clear();
1250         newMessageCBList.clear();
1251         newMMSConfMessageCBList.clear();
1252         newSyncMLMessageCBList.clear();
1253         newLBSMessageCBList.clear();
1254         newPushMessageCBList.clear();
1255         newCBMessageCBList.clear();
1256         newSyncMLMessageCBList.clear();
1257         storageChangeCBList.clear();
1258         reportMessageCBList.clear();
1259 }
1260
1261
1262 void MsgProxyListener::insertOpenHandleSet(MsgHandle* pMsgHandle)
1263 {
1264         MSG_DEBUG("try to insert opened handle. handle=[%p]", pMsgHandle);
1265
1266         MutexLocker lock(mx);
1267
1268         handle_set::iterator it = openHandleSet.find(pMsgHandle);
1269         if (it == openHandleSet.end()) {
1270                 openHandleSet.insert(pMsgHandle);
1271                 MSG_DEBUG("New handle is added. current count = [%d]", openHandleSet.size());
1272         }
1273 }
1274
1275
1276 void MsgProxyListener::clearOpenHandleSet()
1277 {
1278         MSG_DEBUG("clear opened handle set");
1279         openHandleSet.clear();
1280 }
1281
1282 #ifdef CHECK_SENT_STATUS_CALLBACK
1283 int MsgProxyListener::getSentStatusCbCnt()
1284 {
1285         int cbCnt = 0;
1286
1287         cbCnt = sentStatusCBList.size();
1288
1289         MSG_DEBUG("registered sent status callback count : [%d]", cbCnt);
1290
1291         return cbCnt;
1292 }
1293 #endif