fix bug on 64bit env
[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 MsgMutex mm;
146         MsgMutexLocker 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         MsgMutexLocker lock(mx);
159
160         this->insertOpenHandleSet(pMsgHandle);
161
162         if (running == 0) {
163                 cliSock.connect(MSG_SOCKET_PATH);
164
165                 int fd = cliSock.fd();
166
167                 MSG_DEBUG("Socket Fd : %d", fd);
168
169                 /* initializes ref_count = 1 */
170                 channel = g_io_channel_unix_new(fd);
171
172                 /* increments ref_count = 2 */
173                 eventSourceId = g_io_add_watch(channel, (GIOCondition)(G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL), &readSocket, NULL);
174
175                 MSG_DEBUG("Call g_io_add_watch() : %d", eventSourceId);
176         }
177
178         running++;
179         MSG_DEBUG("add Listener and [%d] are running.", running);
180 }
181
182
183 void MsgProxyListener::stop()
184 {
185         MSG_BEGIN();
186
187         MsgMutexLocker lock(mx);
188
189         if (running > 1) {
190                 running--;
191                 MSG_DEBUG("There are still running Listener. [%d] left.", running);
192         } else if (running == 1) {
193                 if (channel) {
194                         g_io_channel_unref(channel);
195                         channel = NULL;
196                 }
197
198                 if (eventSourceId > 0) {
199                         g_source_remove(eventSourceId);
200                         eventSourceId = 0;
201                 }
202
203                 running = 0;
204
205                 cliSock.close();
206                 MSG_DEBUG("client Listener is terminated.");
207         }
208
209         MSG_END();
210 }
211
212
213 bool MsgProxyListener::regSentStatusEventCB(MsgHandle* pMsgHandle, int fd, msg_sent_status_cb pfSentStatus, void *pUserParam)
214 {
215         MsgMutexLocker lock(mx);
216
217         std::list<MSG_SENT_STATUS_CB_ITEM_S>::iterator it = sentStatusCBList.begin();
218
219         for (; it != sentStatusCBList.end(); it++) {
220                 if (it->hAddr == pMsgHandle && it->pfSentStatusCB == pfSentStatus) {
221                         if (it->fd == fd) {
222                                 MSG_DEBUG("msg_sent_status_cb() callback : [%p] is already registered!!!", pfSentStatus);
223                                 return false;
224                         } else {
225                                 MSG_DEBUG("callback is registered by restarting server");
226                                 it->fd = fd;
227                                 return true;
228                         }
229                 }
230         }
231
232         MSG_SENT_STATUS_CB_ITEM_S sentStatusCB = {pMsgHandle, fd, pfSentStatus, pUserParam};
233
234         sentStatusCBList.push_back(sentStatusCB);
235
236         return true;
237 }
238
239
240 bool MsgProxyListener::regMessageIncomingEventCB(MsgHandle* pMsgHandle, int fd, msg_sms_incoming_cb pfNewMessage, int port, void *pUserParam)
241 {
242         MsgMutexLocker lock(mx);
243
244         std::list<MSG_INCOMING_CB_ITEM_S>::iterator it = newMessageCBList.begin();
245
246         for (; it != newMessageCBList.end(); it++) {
247                 if (it->hAddr == pMsgHandle && it->port == port && it->pfIncomingCB == pfNewMessage) {
248                         if (it->fd == fd) {
249                                 MSG_DEBUG("msg_sms_incoming_cb() callback : Port Number [%d] is already registered!!!", port);
250                                 return false;
251                         } else {
252                                 MSG_DEBUG("callback is registered by restarting server");
253                                 it->fd = fd;
254                                 return true;
255                         }
256                 }
257         }
258
259         MSG_INCOMING_CB_ITEM_S incomingCB = {pMsgHandle, fd, pfNewMessage, port, pUserParam};
260
261         newMessageCBList.push_back(incomingCB);
262
263         return true;
264 }
265
266
267 bool MsgProxyListener::regMMSConfMessageIncomingEventCB(MsgHandle* pMsgHandle, int fd, msg_mms_conf_msg_incoming_cb pfNewMMSConfMessage, const char *pAppId, void *pUserParam)
268 {
269         MsgMutexLocker lock(mx);
270
271         std::list<MSG_MMS_CONF_INCOMING_CB_ITEM_S>::iterator it = newMMSConfMessageCBList.begin();
272
273         for (; it != newMMSConfMessageCBList.end(); it++) {
274                 if (it->hAddr == pMsgHandle && it->pfMMSConfIncomingCB == pfNewMMSConfMessage) {
275                         if (it->fd == fd) {
276                                 if (pAppId == NULL) {
277                                         MSG_DEBUG("msg_mms_conf_msg_incoming_cb() callback is already registered!!!");
278                                         return false;
279                                 } else if (!strncmp(it->appId, pAppId, MAX_MMS_JAVA_APPID_LEN)) {
280                                         MSG_DEBUG("msg_mms_conf_msg_incoming_cb() callback : AppId [%s] is already registered!!!", pAppId);
281                                         return false;
282                                 }
283                         } else {
284                                 MSG_DEBUG("callback is registered by restarting server");
285                                 it->fd = fd;
286                                 return true;
287                         }
288                 }
289         }
290
291         MSG_MMS_CONF_INCOMING_CB_ITEM_S incomingConfCB = {pMsgHandle, fd, pfNewMMSConfMessage, {0}, pUserParam};
292
293         if (pAppId != NULL)
294                 strncpy(incomingConfCB.appId, pAppId, MAX_MMS_JAVA_APPID_LEN);
295
296         newMMSConfMessageCBList.push_back(incomingConfCB);
297
298         return true;
299 }
300
301
302 bool MsgProxyListener::regPushMessageIncomingEventCB(MsgHandle* pMsgHandle, int fd, msg_push_msg_incoming_cb pfNewPushMessage, const char *pAppId, void *pUserParam)
303 {
304         MsgMutexLocker lock(mx);
305
306         std::list<MSG_PUSH_INCOMING_CB_ITEM_S>::iterator it = newPushMessageCBList.begin();
307
308         for (; it != newPushMessageCBList.end(); it++) {
309                 if (it->hAddr == pMsgHandle && it->pfPushIncomingCB == pfNewPushMessage) {
310                         if (it->fd == fd) {
311                                 if (pAppId == NULL) {
312                                         MSG_DEBUG("msg_push_msg_incoming_cb() callback is already registered!!!");
313                                         return false;
314                                 } else if (!strncmp(it->appId, pAppId, MAX_WAPPUSH_ID_LEN)) {
315                                         MSG_DEBUG("msg_push_msg_incoming_cb() callback : AppId [%s] is already registered!!!", pAppId);
316                                         return false;
317                                 }
318                         } else {
319                                 MSG_DEBUG("callback is registered by restarting server");
320                                 it->fd = fd;
321                                 return true;
322                         }
323                 }
324         }
325
326         MSG_PUSH_INCOMING_CB_ITEM_S incomingPushCB = {pMsgHandle, fd, pfNewPushMessage, {0}, pUserParam};
327
328         if (pAppId != NULL)
329                 strncpy(incomingPushCB.appId, pAppId, MAX_WAPPUSH_ID_LEN);
330
331         newPushMessageCBList.push_back(incomingPushCB);
332
333         return true;
334 }
335
336
337 bool MsgProxyListener::regCBMessageIncomingEventCB(MsgHandle* pMsgHandle, int fd, msg_cb_incoming_cb pfNewCBMessage, bool bSave, void *pUserParam)
338 {
339         MsgMutexLocker lock(mx);
340
341         std::list<MSG_CB_INCOMING_CB_ITEM_S>::iterator it = newCBMessageCBList.begin();
342
343         for (; it != newCBMessageCBList.end(); it++) {
344                 if (it->hAddr == pMsgHandle && it->pfCBIncomingCB == pfNewCBMessage) {
345                         if (it->fd == fd) {
346                                 MSG_DEBUG("msg_CB_incoming_cb() callback : [%p] is already registered!!!", pfNewCBMessage);
347                                  it->bsave = bSave;
348                                  it->userParam = pUserParam;
349                                 return false;
350                         } else {
351                                 MSG_DEBUG("callback is registered by restarting server");
352                                 it->fd = fd;
353                                 return true;
354                         }
355                 }
356         }
357
358         MSG_CB_INCOMING_CB_ITEM_S incomingCB = {pMsgHandle, fd, pfNewCBMessage, bSave, pUserParam};
359
360         newCBMessageCBList.push_back(incomingCB);
361
362         return true;
363 }
364
365
366 bool MsgProxyListener::regReportMsgIncomingCB(MsgHandle* pMsgHandle, int fd, msg_report_msg_incoming_cb pfReportMessage, void *pUserParam)
367 {
368         MsgMutexLocker lock(mx);
369
370         std::list<MSG_REPORT_INCOMING_CB_ITEM_S>::iterator it = reportMessageCBList.begin();
371
372         for (; it != reportMessageCBList.end(); it++) {
373                 if (it->hAddr == pMsgHandle && it->pfReportMsgIncomingCB == pfReportMessage) {
374                         if (it->fd == fd) {
375                                 MSG_DEBUG("msg_report_msg_incoming_cb() callback : [%p] is already registered!!!", pfReportMessage);
376                                  it->userParam = pUserParam;
377                                 return false;
378                         } else {
379                                 MSG_DEBUG("callback is registered by restarting server");
380                                 it->fd = fd;
381                                 return true;
382                         }
383                 }
384         }
385
386         MSG_REPORT_INCOMING_CB_ITEM_S incomingCB = {pMsgHandle, fd, pfReportMessage, pUserParam};
387
388         reportMessageCBList.push_back(incomingCB);
389
390         return true;
391 }
392
393
394 bool MsgProxyListener::regSyncMLMessageIncomingEventCB(MsgHandle* pMsgHandle, int fd, msg_syncml_msg_incoming_cb pfNewSyncMLMessage, void *pUserParam)
395 {
396         MsgMutexLocker lock(mx);
397
398         std::list<MSG_SYNCML_INCOMING_CB_ITEM_S>::iterator it = newSyncMLMessageCBList.begin();
399
400         for (; it != newSyncMLMessageCBList.end(); it++) {
401                 if (it->hAddr == pMsgHandle && it->pfSyncMLIncomingCB == pfNewSyncMLMessage) {
402                         if (it->fd == fd) {
403                                 MSG_DEBUG("msg_syncml_msg_incoming_cb() callback : [%p] is already registered!!!", pfNewSyncMLMessage);
404                                 return false;
405                         } else {
406                                 MSG_DEBUG("callback is registered by restarting server");
407                                 it->fd = fd;
408                                 return true;
409                         }
410                 }
411         }
412
413         MSG_SYNCML_INCOMING_CB_ITEM_S incomingCB = {pMsgHandle, fd, pfNewSyncMLMessage, pUserParam};
414
415         newSyncMLMessageCBList.push_back(incomingCB);
416
417         return true;
418 }
419
420
421 bool MsgProxyListener::regLBSMessageIncomingEventCB(MsgHandle* pMsgHandle, int fd, msg_lbs_msg_incoming_cb pfNewLBSMsgIncoming, void *pUserParam)
422 {
423         MsgMutexLocker lock(mx);
424
425         std::list<MSG_LBS_INCOMING_CB_ITEM_S>::iterator it = newLBSMessageCBList.begin();
426
427         for (; it != newLBSMessageCBList.end(); it++) {
428                 if (it->hAddr == pMsgHandle && it->pfLBSMsgIncoming == pfNewLBSMsgIncoming) {
429                         if (it->fd == fd) {
430                                 MSG_DEBUG("msg_lbs_msg_incoming_cb() callback : [%p] is already registered!!!", pfNewLBSMsgIncoming);
431                                 return false;
432                         } else {
433                                 MSG_DEBUG("callback is registered by restarting server");
434                                 it->fd = fd;
435                                 return true;
436                         }
437                 }
438         }
439
440         MSG_LBS_INCOMING_CB_ITEM_S incomingCB = {pMsgHandle, fd, pfNewLBSMsgIncoming, pUserParam};
441
442         newLBSMessageCBList.push_back(incomingCB);
443
444         return true;
445 }
446
447
448 bool MsgProxyListener::regSyncMLMessageOperationEventCB(MsgHandle* pMsgHandle, int fd, msg_syncml_msg_operation_cb pfSyncMLMessageOperation, void *pUserParam)
449 {
450         MsgMutexLocker lock(mx);
451
452         std::list<MSG_SYNCML_OPERATION_CB_ITEM_S>::iterator it = operationSyncMLMessageCBList.begin();
453
454         for (; it != operationSyncMLMessageCBList.end(); it++) {
455                 if (it->hAddr == pMsgHandle && it->pfSyncMLOperationCB == pfSyncMLMessageOperation) {
456                         if (it->fd == fd) {
457                                 MSG_DEBUG("msg_syncml_msg_incoming_cb() callback : [%p] is already registered!!!", pfSyncMLMessageOperation);
458                                 return false;
459                         } else {
460                                 MSG_DEBUG("callback is registered by restarting server");
461                                 it->fd = fd;
462                                 return true;
463                         }
464                 }
465         }
466
467         MSG_SYNCML_OPERATION_CB_ITEM_S incomingCB = {pMsgHandle, fd, pfSyncMLMessageOperation, pUserParam};
468
469         operationSyncMLMessageCBList.push_back(incomingCB);
470
471         return true;
472 }
473
474
475 bool MsgProxyListener::regStorageChangeEventCB(MsgHandle* pMsgHandle, int fd, msg_storage_change_cb pfStorageChangeOperation, void *pUserParam)
476 {
477         MsgMutexLocker lock(mx);
478
479         std::list<MSG_STORAGE_CHANGE_CB_ITEM_S>::iterator it = storageChangeCBList.begin();
480
481         for (; it != storageChangeCBList.end(); it++) {
482                 if (it->hAddr == pMsgHandle && it->pfStorageChangeCB == pfStorageChangeOperation) {
483                         if (it->fd == fd) {
484                                 MSG_DEBUG("msg_storage_change_cb() callback : [%p] is already registered!!!", pfStorageChangeOperation);
485                                 return false;
486                         } else {
487                                 MSG_DEBUG("callback is registered by restarting server");
488                                 it->fd = fd;
489                                 return true;
490                         }
491                 }
492         }
493
494         MSG_STORAGE_CHANGE_CB_ITEM_S changeCB = {pMsgHandle, fd, pfStorageChangeOperation, pUserParam};
495
496         storageChangeCBList.push_back(changeCB);
497
498         return true;
499 }
500
501
502 bool MsgProxyListener::regThreadChangeEventCB(MsgHandle* pMsgHandle, int fd, msg_thread_change_cb pfThreadChangeOperation, void *pUserParam)
503 {
504         MsgMutexLocker lock(mx);
505
506         std::list<MSG_THREAD_CHANGE_CB_ITEM_S>::iterator it = threadChangeCBList.begin();
507
508         for (; it != threadChangeCBList.end(); it++) {
509                 if (it->hAddr == pMsgHandle && it->pfThreadChangeCB == pfThreadChangeOperation) {
510                         if (it->fd == fd) {
511                                 MSG_DEBUG("msg_thread_change_cb() callback : [%p] is already registered!!!", pfThreadChangeOperation);
512                                 return false;
513                         } else {
514                                 MSG_DEBUG("callback is registered by restarting server");
515                                 it->fd = fd;
516                                 return true;
517                         }
518                 }
519         }
520
521         MSG_THREAD_CHANGE_CB_ITEM_S changeCB = {pMsgHandle, fd, pfThreadChangeOperation, pUserParam};
522
523         threadChangeCBList.push_back(changeCB);
524
525         return true;
526 }
527
528
529 void MsgProxyListener::clearListOfClosedHandle(MsgHandle* pMsgHandle)
530 {
531         MSG_BEGIN();
532
533         MsgMutexLocker lock(mx);
534
535         /* sent status CB list */
536         std::list<MSG_SENT_STATUS_CB_ITEM_S>::iterator it = sentStatusCBList.begin();
537
538         for (; it != sentStatusCBList.end(); ) {
539                 if (it->hAddr == pMsgHandle) {
540                         sentStatusCBList.erase(it++);
541                         stop();
542                 } else {
543                         ++it;
544                 }
545         }
546
547         /* new message CB list */
548         std::list<MSG_INCOMING_CB_ITEM_S>::iterator it2 = newMessageCBList.begin();
549
550         for (; it2 != newMessageCBList.end(); ) {
551                 if (it2->hAddr == pMsgHandle) {
552                         newMessageCBList.erase(it2++);
553                         stop();
554                 } else {
555                         ++it2;
556                 }
557         }
558
559         /* MMS conf Message CB list */
560         std::list<MSG_MMS_CONF_INCOMING_CB_ITEM_S>::iterator it3 = newMMSConfMessageCBList.begin();
561
562         for (; it3 != newMMSConfMessageCBList.end(); ) {
563                 if (it3->hAddr == pMsgHandle) {
564                         newMMSConfMessageCBList.erase(it3++);
565                         stop();
566                 } else {
567                         ++it3;
568                 }
569         }
570
571         /* SyncML Message CB list */
572         std::list<MSG_SYNCML_INCOMING_CB_ITEM_S>::iterator it4 = newSyncMLMessageCBList.begin();
573
574         for (; it4 != newSyncMLMessageCBList.end(); ) {
575                 if (it4->hAddr == pMsgHandle) {
576                         newSyncMLMessageCBList.erase(it4++);
577                         stop();
578                 } else {
579                         ++it4;
580                 }
581         }
582
583         /* LBS Message CB list */
584         std::list<MSG_LBS_INCOMING_CB_ITEM_S>::iterator it5 = newLBSMessageCBList.begin();
585
586         for (; it5 != newLBSMessageCBList.end(); ) {
587                 if (it5->hAddr == pMsgHandle) {
588                         newLBSMessageCBList.erase(it5++);
589                         stop();
590                 } else {
591                         ++it5;
592                 }
593         }
594
595         /* Push Message CB list */
596         std::list<MSG_PUSH_INCOMING_CB_ITEM_S>::iterator it6 = newPushMessageCBList.begin();
597
598         for (; it6 != newPushMessageCBList.end(); ) {
599                 if (it6->hAddr == pMsgHandle) {
600                         newPushMessageCBList.erase(it6++);
601                         stop();
602                 } else {
603                         ++it6;
604                 }
605         }
606
607         /* CB Message CB list */
608         std::list<MSG_CB_INCOMING_CB_ITEM_S>::iterator it7 = newCBMessageCBList.begin();
609
610         for (; it7 != newCBMessageCBList.end(); ) {
611                 if (it7->hAddr == pMsgHandle) {
612                         newCBMessageCBList.erase(it7++);
613                         stop();
614                 } else {
615                         ++it7;
616                 }
617         }
618
619         /* Storage change Message CB list */
620         std::list<MSG_STORAGE_CHANGE_CB_ITEM_S>::iterator it8 = storageChangeCBList.begin();
621
622         for (; it8 != storageChangeCBList.end(); ) {
623                 if (it8->hAddr == pMsgHandle) {
624                         storageChangeCBList.erase(it8++);
625                         stop();
626                 } else {
627                         ++it8;
628                 }
629         }
630
631
632         /* Report message incoming CB list */
633         std::list<MSG_REPORT_INCOMING_CB_ITEM_S>::iterator it9 = reportMessageCBList.begin();
634         for (; it9 != reportMessageCBList.end(); ) {
635                 if (it9->hAddr == pMsgHandle) {
636                         reportMessageCBList.erase(it9++);
637                         stop();
638                 } else {
639                         ++it9;
640                 }
641         }
642
643
644         /* SyncML Message Operation CB list */
645         std::list<MSG_SYNCML_OPERATION_CB_ITEM_S>::iterator it10 = operationSyncMLMessageCBList.begin();
646         for (; it10 != operationSyncMLMessageCBList.end(); ) {
647                 if (it10->hAddr == pMsgHandle) {
648                         operationSyncMLMessageCBList.erase(it10++);
649                         stop();
650                 } else {
651                         ++it10;
652                 }
653         }
654
655
656         /* Thread change Message CB list */
657         std::list<MSG_THREAD_CHANGE_CB_ITEM_S>::iterator it11 = threadChangeCBList.begin();
658
659         for (; it11 != threadChangeCBList.end(); ) {
660                 if (it11->hAddr == pMsgHandle) {
661                         threadChangeCBList.erase(it11++);
662                         stop();
663                 } else {
664                         ++it11;
665                 }
666         }
667
668         /* Open Handle Set */
669         openHandleSet.erase(pMsgHandle);
670
671         MSG_END();
672 }
673
674
675 void MsgProxyListener::refreshListOfOpenedHandle(MsgHandle* pMsgHandle)
676 {
677         MSG_BEGIN();
678
679         MsgMutexLocker lock(mx);
680
681         /* sent status CB list */
682         std::list<MSG_SENT_STATUS_CB_ITEM_S>::iterator it = sentStatusCBList.begin();
683         for (; it != sentStatusCBList.end(); ++it) {
684                 if (it->hAddr == pMsgHandle) {
685                         it->fd = CUSTOM_SOCKET_ERROR;
686                         try {
687                                 pMsgHandle->regSentStatusCallback(it->pfSentStatusCB, it->userParam);
688                         } catch (MsgException& e) {
689                                 MSG_FATAL("%s", e.what());
690                         }
691                 }
692         }
693
694         /* new message CB list */
695         std::list<MSG_INCOMING_CB_ITEM_S>::iterator it2 = newMessageCBList.begin();
696         for (; it2 != newMessageCBList.end(); ++it2) {
697                 if (it2->hAddr == pMsgHandle) {
698                         it2->fd = CUSTOM_SOCKET_ERROR;
699                         try {
700                                 pMsgHandle->regSmsMessageCallback(it2->pfIncomingCB, it2->port, it2->userParam);
701                         } catch (MsgException& e) {
702                                 MSG_FATAL("%s", e.what());
703                         }
704                 }
705         }
706
707         /* MMS conf Message CB list */
708         std::list<MSG_MMS_CONF_INCOMING_CB_ITEM_S>::iterator it3 = newMMSConfMessageCBList.begin();
709         for (; it3 != newMMSConfMessageCBList.end(); ++it3) {
710                 if (it3->hAddr == pMsgHandle) {
711                         it3->fd = CUSTOM_SOCKET_ERROR;
712                         try {
713                                 pMsgHandle->regMmsConfMessageCallback(it3->pfMMSConfIncomingCB, it3->appId, it3->userParam);
714                         } catch (MsgException& e) {
715                                 MSG_FATAL("%s", e.what());
716                         }
717                 }
718         }
719
720         /* SyncML Message CB list */
721         std::list<MSG_SYNCML_INCOMING_CB_ITEM_S>::iterator it4 = newSyncMLMessageCBList.begin();
722         for (; it4 != newSyncMLMessageCBList.end(); ++it4) {
723                 if (it4->hAddr == pMsgHandle) {
724                         it4->fd = CUSTOM_SOCKET_ERROR;
725                         try {
726                                 pMsgHandle->regSyncMLMessageCallback(it4->pfSyncMLIncomingCB, it4->userParam);
727                         } catch (MsgException& e) {
728                                 MSG_FATAL("%s", e.what());
729                         }
730                 }
731         }
732
733         /* LBS Message CB list */
734         std::list<MSG_LBS_INCOMING_CB_ITEM_S>::iterator it5 = newLBSMessageCBList.begin();
735         for (; it5 != newLBSMessageCBList.end(); ++it5) {
736                 if (it5->hAddr == pMsgHandle) {
737                         it5->fd = CUSTOM_SOCKET_ERROR;
738                         try {
739                                 pMsgHandle->regLBSMessageCallback(it5->pfLBSMsgIncoming, it5->userParam);
740                         } catch (MsgException& e) {
741                                 MSG_FATAL("%s", e.what());
742                         }
743                 }
744         }
745
746         /* Push Message CB list */
747         std::list<MSG_PUSH_INCOMING_CB_ITEM_S>::iterator it6 = newPushMessageCBList.begin();
748         for (; it6 != newPushMessageCBList.end(); ++it6) {
749                 if (it6->hAddr == pMsgHandle) {
750                         it6->fd = CUSTOM_SOCKET_ERROR;
751                         try {
752                                 pMsgHandle->regPushMessageCallback(it6->pfPushIncomingCB, it6->appId, it6->userParam);
753                         } catch (MsgException& e) {
754                                 MSG_FATAL("%s", e.what());
755                         }
756                 }
757         }
758
759         /* CB Message CB list */
760         std::list<MSG_CB_INCOMING_CB_ITEM_S>::iterator it7 = newCBMessageCBList.begin();
761         for (; it7 != newCBMessageCBList.end(); ++it7) {
762                 if (it7->hAddr == pMsgHandle) {
763                         it7->fd = CUSTOM_SOCKET_ERROR;
764                         try {
765                                 pMsgHandle->regCBMessageCallback(it7->pfCBIncomingCB, it7->bsave, it7->userParam);
766                         } catch (MsgException& e) {
767                                 MSG_FATAL("%s", e.what());
768                         }
769                 }
770         }
771
772         /* Storage change Message CB list */
773         std::list<MSG_STORAGE_CHANGE_CB_ITEM_S>::iterator it8 = storageChangeCBList.begin();
774         for (; it8 != storageChangeCBList.end(); ++it8) {
775                 if (it8->hAddr == pMsgHandle) {
776                         it8->fd = CUSTOM_SOCKET_ERROR;
777                         try {
778                                 pMsgHandle->regStorageChangeCallback(it8->pfStorageChangeCB, it8->userParam);
779                         } catch (MsgException& e) {
780                                 MSG_FATAL("%s", e.what());
781                         }
782                 }
783         }
784
785         /* Report message incoming CB list */
786         std::list<MSG_REPORT_INCOMING_CB_ITEM_S>::iterator it9 = reportMessageCBList.begin();
787         for (; it9 != reportMessageCBList.end(); ++it9) {
788                 if (it9->hAddr == pMsgHandle) {
789                         it9->fd = CUSTOM_SOCKET_ERROR;
790                         try {
791                                 pMsgHandle->regReportMessageCallback(it9->pfReportMsgIncomingCB, it9->userParam);
792                         } catch (MsgException& e) {
793                                 MSG_FATAL("%s", e.what());
794                         }
795                 }
796         }
797
798         /* SyncML Message Operation CB list */
799         std::list<MSG_SYNCML_OPERATION_CB_ITEM_S>::iterator it10 = operationSyncMLMessageCBList.begin();
800         for (; it10 != operationSyncMLMessageCBList.end(); ++it10) {
801                 if (it10->hAddr == pMsgHandle) {
802                         it10->fd = CUSTOM_SOCKET_ERROR;
803                         try {
804                                 pMsgHandle->regSyncMLMessageOperationCallback(it10->pfSyncMLOperationCB, it10->userParam);
805                         } catch (MsgException& e) {
806                                 MSG_FATAL("%s", e.what());
807                         }
808                 }
809         }
810
811         /* Thread change Message CB list */
812         std::list<MSG_THREAD_CHANGE_CB_ITEM_S>::iterator it11 = threadChangeCBList.begin();
813         for (; it11 != threadChangeCBList.end(); ++it11) {
814                 if (it11->hAddr == pMsgHandle) {
815                         it11->fd = CUSTOM_SOCKET_ERROR;
816                         try {
817                                 pMsgHandle->regThreadChangeCallback(it11->pfThreadChangeCB, it11->userParam);
818                         } catch (MsgException& e) {
819                                 MSG_FATAL("%s", e.what());
820                         }
821                 }
822         }
823
824         MSG_END();
825 }
826
827
828 void MsgProxyListener::handleEvent(const MSG_EVENT_S* pMsgEvent)
829 {
830         MSG_BEGIN();
831
832         if (!pMsgEvent)
833                 THROW(MsgException::INVALID_PARAM, "pMsgEvent is NULL");
834
835         if (pMsgEvent->eventType == MSG_EVENT_PLG_SENT_STATUS_CNF) {
836                 unsigned long chInfo[3] = {0}; /*3 reqid, status, object */
837
838                 memcpy(&chInfo, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)), sizeof(chInfo));
839
840                 msg_struct_s status = {0, };
841                 MSG_SENT_STATUS_S statusData = {(msg_request_id_t)chInfo[0], (msg_network_status_t)chInfo[1]};
842
843                 status.type = MSG_STRUCT_SENT_STATUS_INFO;
844                 status.data = (void *)&statusData;
845
846                 mx.lock();
847
848                 MsgSentStatusCBList::iterator it = sentStatusCBList.begin();
849
850                 for ( ; it != sentStatusCBList.end() ; it++) {
851                         MsgHandle* pHandle = it->hAddr;
852
853                         msg_sent_status_cb pfunc = it->pfSentStatusCB;
854
855                         void* param = it->userParam;
856
857                         pfunc((msg_handle_t)pHandle, (msg_struct_t)&status, param);
858                 }
859
860                 mx.unlock();
861         } else if (pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_MSG_IND) {
862                 MSG_MESSAGE_INFO_S msgInfo;
863                 memset(&msgInfo, 0x00, sizeof(MSG_MESSAGE_INFO_S));
864
865                 msgInfo.addressList = NULL;
866                 unique_ptr<MSG_ADDRESS_INFO_S*, void(*)(MSG_ADDRESS_INFO_S**)> addressListBuf(&msgInfo.addressList, unique_ptr_deleter);
867
868                 MsgDecodeMsgInfo((char *)pMsgEvent->data, &msgInfo);
869
870                 int portKey = (msgInfo.msgPort.valid)? msgInfo.msgPort.dstPort: 0;
871
872                 mx.lock();
873
874                 MsgNewMessageCBList::iterator it = newMessageCBList.begin();
875                 MsgNewMessageCBList matchList;
876
877                 for ( ; it != newMessageCBList.end() ; it++) {
878                         if (portKey == it->port) {
879                                 matchList.push_back(*it);
880                         }
881                 }
882
883                 mx.unlock();
884
885                 it = matchList.begin();
886
887                 for ( ; it != matchList.end(); it++ ) {
888                         MsgHandle* pHandle = it->hAddr;
889
890                         MSG_MESSAGE_HIDDEN_S msgHidden = {0, };
891
892                         msgHidden.pData = NULL;
893                         msgHidden.pMmsData = NULL;
894
895                         /* Allocate memory for address list of message */
896                         msg_struct_list_s *addr_list = (msg_struct_list_s *)new msg_struct_list_s;
897
898                         addr_list->nCount = 0;
899                         addr_list->msg_struct_info = (msg_struct_t *)calloc(MAX_TO_ADDRESS_CNT, sizeof(MSG_ADDRESS_INFO_S *));
900                         if (addr_list->msg_struct_info == NULL)
901                                 continue;
902
903                         msg_struct_s *pTmp = NULL;
904
905                         for (int i = 0; i < MAX_TO_ADDRESS_CNT; i++) {
906                                 addr_list->msg_struct_info[i] = (msg_struct_t)new msg_struct_s;
907                                 pTmp = (msg_struct_s *)addr_list->msg_struct_info[i];
908                                 pTmp->type = MSG_STRUCT_ADDRESS_INFO;
909                                 pTmp->data = new MSG_ADDRESS_INFO_S;
910                                 memset(pTmp->data, 0x00, sizeof(MSG_ADDRESS_INFO_S));
911
912                                 addr_list->msg_struct_info[i] = (msg_struct_t)pTmp;
913                         }
914
915                         msgHidden.addr_list = addr_list;
916
917                         try {
918                                 pHandle->convertMsgStruct(&msgInfo, &msgHidden);
919                         }
920                         catch (MsgException& e) {
921                                 MSG_FATAL("%s", e.what());
922                         }
923
924                         msg_struct_s msg = {0, };
925                         msg.type = MSG_STRUCT_MESSAGE_INFO;
926                         msg.data = &msgHidden;
927
928                         msg_sms_incoming_cb pfunc = it->pfIncomingCB;
929
930                         void* param = it->userParam;
931
932                         pfunc((msg_handle_t)pHandle, (msg_struct_t) &msg, param);
933
934                         delete [] (char*)msgHidden.pData;
935                         if (msgHidden.pMmsData != NULL)
936                                 delete [] (char*)msgHidden.pMmsData;
937
938                         /* address Memory Free */
939                         if (msgHidden.addr_list!= NULL) {
940                                 for (int i = 0; i < MAX_TO_ADDRESS_CNT; i++) {
941                                         msg_struct_s * addrInfo = (msg_struct_s *)msgHidden.addr_list->msg_struct_info[i];
942                                         delete (MSG_ADDRESS_INFO_S *)addrInfo->data;
943                                         addrInfo->data = NULL;
944                                         delete (msg_struct_s *)msgHidden.addr_list->msg_struct_info[i];
945                                         msgHidden.addr_list->msg_struct_info[i] = NULL;
946                                 }
947
948                                 g_free(msgHidden.addr_list->msg_struct_info);
949
950                                 delete msgHidden.addr_list;
951                                 msgHidden.addr_list = NULL;
952                         }
953                 }
954
955         } else if (pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_MMS_CONF) {
956                 MSG_MESSAGE_INFO_S msgInfo;
957                 memset(&msgInfo, 0x00, sizeof(MSG_MESSAGE_INFO_S));
958
959                 msgInfo.addressList = NULL;
960                 unique_ptr<MSG_ADDRESS_INFO_S*, void(*)(MSG_ADDRESS_INFO_S**)> addressListBuf(&msgInfo.addressList, unique_ptr_deleter);
961
962                 MsgDecodeMsgInfo((char *)pMsgEvent->data, &msgInfo);
963
964
965                 MMS_RECV_DATA_S* pMmsRecvData = (MMS_RECV_DATA_S*)msgInfo.msgData;
966
967                 char* appIdKey = (pMmsRecvData->msgAppId.valid)? pMmsRecvData->msgAppId.appId: NULL;
968
969                 mx.lock();
970
971                 MsgNewMMSConfMessageCBList::iterator it = newMMSConfMessageCBList.begin();
972                 MsgNewMMSConfMessageCBList matchList;
973
974                 for ( ; it != newMMSConfMessageCBList.end(); it++) {
975                         if (appIdKey) {
976                                 if (!strcmp(appIdKey, it->appId))
977                                         matchList.push_back(*it);
978                         } else {
979                                 /* (appIdKey == NULL && it->appId[0] == 0) */
980                                 if (it->appId[0] == 0)
981                                         matchList.push_back(*it);
982                         }
983                 }
984
985                 mx.unlock();
986
987                 /* Contents of msgData removed and replaced to retrievedFilePath for convertMsgStruct */
988                 /* it is moved from UpdateMessage in MmsPluginStorage.cpp */
989                 char tempFileName[MSG_FILENAME_LEN_MAX+1] = {0}; /* check MSG_FILENAME_LEN_MAX */
990
991                 strncpy(tempFileName, pMmsRecvData->retrievedFilePath, MSG_FILENAME_LEN_MAX);
992
993                 memset(msgInfo.msgData, 0, MAX_MSG_DATA_LEN+1);
994                 memcpy(msgInfo.msgData, tempFileName + strlen(MSG_DATA_PATH), MAX_MSG_DATA_LEN);
995
996                 it = matchList.begin();
997
998                 for ( ; it != matchList.end(); it++) {
999                         MsgHandle* pHandle = it->hAddr;
1000
1001                         MSG_MESSAGE_HIDDEN_S msgHidden = {0, };
1002
1003                         msgHidden.pData = NULL;
1004                         msgHidden.pMmsData = NULL;
1005
1006                         /* Allocate memory for address list of message */
1007                         msg_struct_list_s *addr_list = (msg_struct_list_s *)new msg_struct_list_s;
1008
1009                         addr_list->nCount = 0;
1010                         addr_list->msg_struct_info = (msg_struct_t *)calloc(MAX_TO_ADDRESS_CNT, sizeof(MSG_ADDRESS_INFO_S *));
1011                         if (addr_list->msg_struct_info == NULL)
1012                                 continue;
1013
1014                         msg_struct_s *pTmp = NULL;
1015
1016                         for (int i = 0; i < MAX_TO_ADDRESS_CNT; i++) {
1017                                 addr_list->msg_struct_info[i] = (msg_struct_t)new msg_struct_s;
1018                                 pTmp = (msg_struct_s *)addr_list->msg_struct_info[i];
1019                                 pTmp->type = MSG_STRUCT_ADDRESS_INFO;
1020                                 pTmp->data = new MSG_ADDRESS_INFO_S;
1021                                 memset(pTmp->data, 0x00, sizeof(MSG_ADDRESS_INFO_S));
1022
1023                                 addr_list->msg_struct_info[i] = (msg_struct_t)pTmp;
1024                         }
1025
1026                         msgHidden.addr_list = addr_list;
1027
1028                         try {
1029                                 pHandle->convertMsgStruct(&msgInfo, &msgHidden);
1030                         }
1031                         catch (MsgException& e) {
1032                                 MSG_FATAL("%s", e.what());
1033                         }
1034
1035                         msg_struct_s msg = {0, };
1036                         msg.type = MSG_STRUCT_MESSAGE_INFO;
1037                         msg.data = &msgHidden;
1038
1039                         msg_mms_conf_msg_incoming_cb pfunc = it->pfMMSConfIncomingCB;
1040
1041                         void* param = it->userParam;
1042                         pfunc((msg_handle_t)pHandle, (msg_struct_t) &msg, param);
1043
1044                         delete [] (char*)msgHidden.pData;
1045                         if (msgHidden.pMmsData != NULL)
1046                                 delete [] (char*)msgHidden.pMmsData;
1047
1048                         /* address Memory Free */
1049                         if (msgHidden.addr_list != NULL) {
1050                                 for (int i = 0; i < MAX_TO_ADDRESS_CNT; i++) {
1051                                         msg_struct_s * addrInfo = (msg_struct_s *)msgHidden.addr_list->msg_struct_info[i];
1052                                         delete (MSG_ADDRESS_INFO_S *)addrInfo->data;
1053                                         addrInfo->data = NULL;
1054                                         delete (msg_struct_s *)msgHidden.addr_list->msg_struct_info[i];
1055                                         msgHidden.addr_list->msg_struct_info[i] = NULL;
1056                                 }
1057
1058                                 g_free(msgHidden.addr_list->msg_struct_info);
1059
1060                                 delete msgHidden.addr_list;
1061                                 msgHidden.addr_list = NULL;
1062                         }
1063
1064                         /* Here the retrieved message will be deleted from native storage. */
1065                         /* as of now, msg which have appId is considered as JAVA MMS message and it will be sent and handled in JAVA app. */
1066                         if (appIdKey) {
1067                                 MSG_DEBUG("delete received JAVA MMS message:%s from native storage", tempFileName);
1068                                 pHandle->deleteMessage(msgInfo.msgId);
1069                         }
1070                 }
1071         } else if (pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_SYNCML_MSG_IND) {
1072                 MSG_SYNCML_MESSAGE_DATA_S* pSyncMLData = (MSG_SYNCML_MESSAGE_DATA_S *)pMsgEvent->data;
1073
1074                 MSG_DEBUG("msgType [%d]", pSyncMLData->syncmlType);
1075
1076                 mx.lock();
1077
1078                 MsgNewSyncMLMessageCBList::iterator it = newSyncMLMessageCBList.begin();
1079
1080                 for ( ; it != newSyncMLMessageCBList.end(); it++) {
1081                         MsgHandle* pHandle = it->hAddr;
1082
1083                         msg_syncml_msg_incoming_cb pfunc = it->pfSyncMLIncomingCB;
1084
1085                         void* param = it->userParam;
1086
1087                         pfunc((msg_handle_t)pHandle, pSyncMLData->syncmlType, pSyncMLData->pushBody, pSyncMLData->pushBodyLen, pSyncMLData->wspHeader, pSyncMLData->wspHeaderLen, pSyncMLData->simIndex, param);
1088                 }
1089
1090                 mx.unlock();
1091         } else if (pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_LBS_MSG_IND) {
1092                 MSG_LBS_MESSAGE_DATA_S* pLBSData = (MSG_LBS_MESSAGE_DATA_S *)pMsgEvent->data;
1093
1094                 mx.lock();
1095
1096                 MsgNewLBSMessageCBList::iterator it = newLBSMessageCBList.begin();
1097
1098                 for ( ; it != newLBSMessageCBList.end(); it++) {
1099                         MsgHandle* pHandle = it->hAddr;
1100
1101                         msg_lbs_msg_incoming_cb pfunc = it->pfLBSMsgIncoming;
1102
1103                         void* param = it->userParam;
1104
1105                         pfunc((msg_handle_t)pHandle, pLBSData->pushHeader, pLBSData->pushBody, pLBSData->pushBodyLen, param);
1106                 }
1107
1108                 mx.unlock();
1109         } else if (pMsgEvent->eventType == MSG_EVENT_SYNCML_OPERATION) {
1110                 int msgId;
1111                 int extId;
1112
1113                 memcpy(&msgId, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)), sizeof(int));
1114                 memcpy(&extId, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)+sizeof(int)), sizeof(int));
1115
1116                 MSG_DEBUG("msgId [%d]", msgId);
1117                 MSG_DEBUG("extId [%d]", extId);
1118
1119                 mx.lock();
1120
1121                 MsgOperationSyncMLMessageCBList::iterator it = operationSyncMLMessageCBList.begin();
1122
1123                 for ( ; it != operationSyncMLMessageCBList.end(); it++) {
1124                         MsgHandle* pHandle = it->hAddr;
1125
1126                         msg_syncml_msg_operation_cb pfunc = it->pfSyncMLOperationCB;
1127
1128                         void* param = it->userParam;
1129
1130                         pfunc((msg_handle_t)pHandle, msgId, extId, param);
1131                 }
1132
1133                 mx.unlock();
1134         } else if (pMsgEvent->eventType == MSG_EVENT_PLG_STORAGE_CHANGE_IND) {
1135                 msg_storage_change_type_t storageChangeType;
1136                 msg_id_list_s msgIdList;
1137                 memset(&msgIdList, 0x00, sizeof(msg_id_list_s));
1138
1139                 /* Decode event data */
1140                 memcpy(&storageChangeType, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)), sizeof(msg_storage_change_type_t));
1141                 memcpy(&msgIdList.nCount, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)+sizeof(msg_storage_change_type_t)), sizeof(int));
1142
1143                 if (msgIdList.nCount > 0)
1144                         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));
1145                 else
1146                         msgIdList.msgIdList = NULL;
1147
1148                 MSG_DEBUG("storageChangeType [%d], msgIdList.nCount [%d]", storageChangeType, msgIdList.nCount);
1149
1150                 mx.lock();
1151
1152                 MsgStorageChangeCBList::iterator it = storageChangeCBList.begin();
1153
1154                 for ( ; it != storageChangeCBList.end(); it++) {
1155                         MsgHandle* pHandle = it->hAddr;
1156
1157                         msg_storage_change_cb pfunc = it->pfStorageChangeCB;
1158
1159                         void* param = it->userParam;
1160
1161                         pfunc((msg_handle_t)pHandle, storageChangeType, (msg_id_list_s*)&msgIdList, param);
1162                 }
1163
1164                 mx.unlock();
1165         } else if (pMsgEvent->eventType == MSG_EVENT_PLG_THREAD_CHANGE_IND) {
1166                 msg_storage_change_type_t storageChangeType;
1167                 msg_thread_id_t threadId;
1168
1169                 /* Decode event data */
1170                 memcpy(&storageChangeType, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)), sizeof(msg_storage_change_type_t));
1171                 memcpy(&threadId, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)+sizeof(msg_storage_change_type_t)), sizeof(msg_thread_id_t));
1172
1173                 MSG_DEBUG("storageChangeType [%d], threadId [%d]", storageChangeType, threadId);
1174
1175                 mx.lock();
1176
1177                 MsgThreadChangeCBList::iterator it = threadChangeCBList.begin();
1178
1179                 for ( ; it != threadChangeCBList.end(); it++) {
1180                         MsgHandle* pHandle = it->hAddr;
1181
1182                         msg_thread_change_cb pfunc = it->pfThreadChangeCB;
1183
1184                         void* param = it->userParam;
1185
1186                         pfunc((msg_handle_t)pHandle, storageChangeType, threadId, param);
1187                 }
1188
1189                 mx.unlock();
1190         } else if (pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_CB_MSG_IND) {
1191                 MSG_CB_MSG_S *pCbMsg = (MSG_CB_MSG_S *)pMsgEvent->data;
1192
1193                 mx.lock();
1194
1195                 MsgNewCBMessageCBList::iterator it = newCBMessageCBList.begin();
1196
1197                 for ( ; it != newCBMessageCBList.end(); it++) {
1198                         MsgHandle* pHandle = it->hAddr;
1199                         msg_struct_s msg = {0, };
1200
1201                         msg.type = MSG_STRUCT_CB_MSG;
1202                         msg.data = pCbMsg;
1203
1204                         msg_cb_incoming_cb pfunc = it->pfCBIncomingCB;
1205
1206                         void* param = it->userParam;
1207
1208                         pfunc((msg_handle_t)pHandle, (msg_struct_t) &msg, param);
1209                 }
1210
1211                 mx.unlock();
1212         } else if (pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_PUSH_MSG_IND) {
1213                 MSG_PUSH_MESSAGE_DATA_S* pPushData = (MSG_PUSH_MESSAGE_DATA_S *)pMsgEvent->data;
1214
1215                 mx.lock();
1216
1217                 MsgNewPushMessageCBList::iterator it = newPushMessageCBList.begin();
1218
1219                 for ( ; it != newPushMessageCBList.end(); it++) {
1220                         MsgHandle* pHandle = it->hAddr;
1221
1222                         msg_push_msg_incoming_cb pfunc = it->pfPushIncomingCB;
1223
1224                         void* param = it->userParam;
1225
1226                         if (!strncmp(it->appId, pPushData->pushAppId, MAX_WAPPUSH_ID_LEN))
1227                                 pfunc((msg_handle_t)pHandle, pPushData->pushHeader, pPushData->pushBody, pPushData->pushBodyLen, param);
1228                 }
1229
1230                 mx.unlock();
1231         } else if (pMsgEvent->eventType == MSG_EVENT_PLG_REPORT_MSG_INCOMING_IND) {
1232                 msg_report_type_t reportType;
1233                 msg_message_id_t msgId;
1234                 int addr_len;
1235                 char *addr_val;
1236
1237                 /* Decode event data */
1238                 memcpy(&reportType, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)), sizeof(msg_report_type_t));
1239                 memcpy(&msgId, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)+sizeof(msg_report_type_t)), sizeof(msg_message_id_t));
1240                 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));
1241                 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));
1242                 addr_val[addr_len] = '\0';
1243
1244                 MSG_SEC_DEBUG("reportType [%d], msgId [%d], Address Length [%d], Address Value [%s]", reportType, msgId, addr_len, addr_val);
1245
1246                 mx.lock();
1247
1248                 MsgReportMessageCBList::iterator it = reportMessageCBList.begin();
1249
1250                 for ( ; it != reportMessageCBList.end(); it++) {
1251                         MsgHandle* pHandle = it->hAddr;
1252
1253                         msg_report_msg_incoming_cb pfunc = it->pfReportMsgIncomingCB;
1254
1255                         void* param = it->userParam;
1256
1257                         pfunc((msg_handle_t)pHandle, reportType, msgId, addr_len, addr_val, param);
1258                 }
1259
1260                 mx.unlock();
1261         }
1262
1263         MSG_END();
1264 }
1265
1266
1267 int MsgProxyListener::getRemoteFd()
1268 {
1269         return cliSock.getRemoteFd();
1270 }
1271
1272
1273 int MsgProxyListener::readFromSocket(char** buf, unsigned int* len)
1274 {
1275         return cliSock.read(buf, len);
1276 }
1277
1278
1279 void MsgProxyListener::resetProxyListener()
1280 {
1281         MSG_DEBUG("client Listener reset");
1282         MsgMutexLocker lock(mx);
1283
1284         if (channel) {
1285                 g_io_channel_unref(channel);
1286                 channel = NULL;
1287         }
1288
1289         if (eventSourceId > 0) {
1290                 g_source_remove(eventSourceId);
1291                 eventSourceId = 0;
1292         }
1293
1294         running = 0;
1295
1296         cliSock.close();
1297
1298         handle_set::iterator it = openHandleSet.begin();
1299         for (; it != openHandleSet.end(); it++) {
1300                 MSG_DEBUG("disconnect socket for opened handle [%p]", it);
1301                 MsgHandle *handle = (MsgHandle *)*it;
1302                 handle->disconnectSocket();
1303         }
1304 }
1305
1306
1307 void MsgProxyListener::refreshProxyListener()
1308 {
1309         MSG_DEBUG("refresh proxy listener");
1310         MsgMutexLocker lock(mx);
1311
1312         handle_set::iterator it = openHandleSet.begin();
1313         for (; it != openHandleSet.end(); it++) {
1314                 MsgHandle *handle = (MsgHandle *)*it;
1315                 handle->openHandle();
1316                 refreshListOfOpenedHandle(handle);
1317         }
1318 }
1319
1320
1321 void MsgProxyListener::clearProxyCBLists()
1322 {
1323         MSG_DEBUG("clear proxy callback list");
1324
1325         sentStatusCBList.clear();
1326         newMessageCBList.clear();
1327         newMMSConfMessageCBList.clear();
1328         newSyncMLMessageCBList.clear();
1329         newLBSMessageCBList.clear();
1330         newPushMessageCBList.clear();
1331         newCBMessageCBList.clear();
1332         storageChangeCBList.clear();
1333         threadChangeCBList.clear();
1334         reportMessageCBList.clear();
1335 }
1336
1337
1338 void MsgProxyListener::insertOpenHandleSet(MsgHandle* pMsgHandle)
1339 {
1340         MSG_DEBUG("try to insert opened handle. handle=[%p]", pMsgHandle);
1341
1342         MsgMutexLocker lock(mx);
1343
1344         handle_set::iterator it = openHandleSet.find(pMsgHandle);
1345         if (it == openHandleSet.end()) {
1346                 openHandleSet.insert(pMsgHandle);
1347                 MSG_DEBUG("New handle is added. current count = [%d]", openHandleSet.size());
1348         }
1349 }
1350
1351
1352 void MsgProxyListener::clearOpenHandleSet()
1353 {
1354         MSG_DEBUG("clear opened handle set");
1355         openHandleSet.clear();
1356 }
1357
1358 #ifdef CHECK_SENT_STATUS_CALLBACK
1359 int MsgProxyListener::getSentStatusCbCnt()
1360 {
1361         int cbCnt = 0;
1362
1363         cbCnt = sentStatusCBList.size();
1364
1365         MSG_DEBUG("registered sent status callback count : [%d]", cbCnt);
1366
1367         return cbCnt;
1368 }
1369 #endif