apply push/cb function
[platform/core/messaging/msg-service.git] / proxy / MsgProxyListener.cpp
1 /*
2 * Copyright 2012  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
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 "MsgUtilFile.h"
23 #include "MsgProxyListener.h"
24 #include "MsgGconfWrapper.h"
25
26
27 gboolean readSocket(GIOChannel *source, GIOCondition condition, gpointer data)
28 {
29         MSG_BEGIN();
30
31         if (G_IO_ERR & condition)
32         {
33                 MSG_DEBUG("IO Error!!! [%d]", condition);
34
35                 MsgProxyListener::instance()->stop();
36                 return FALSE;
37         }
38
39         if (G_IO_HUP & condition)
40         {
41                 MSG_DEBUG("socket fd Error!!! [%d]", condition);
42
43                 MsgProxyListener::instance()->stop();
44                 return FALSE;
45         }
46
47         if (G_IO_NVAL & condition)
48         {
49                 MSG_DEBUG("Invaild socket Error!!! [%d]", condition);
50
51                 MsgProxyListener::instance()->stop();
52                 return FALSE;
53         }
54
55         char* buf = NULL;
56         int len;
57
58         int n = MsgProxyListener::instance()->readFromSocket(&buf, &len);
59
60         if (n > 0)
61         {
62                 MSG_DEBUG(">>Receiving %d bytes", n);
63                 MsgProxyListener::instance()->handleEvent((MSG_EVENT_S*)buf);
64         }
65         else if (n == 0)
66         {
67                 MSG_DEBUG("Server closed connection");
68                 MsgProxyListener::instance()->stop();
69                 return FALSE;
70         }
71         else // dataSize < 0
72         {
73                 MSG_DEBUG("Data is not for Listener");
74         }
75
76         if (buf)
77         {
78                 delete [] buf;
79                 buf = NULL;
80         }
81
82         MSG_END();
83
84         return TRUE;
85 }
86
87
88 /*==================================================================================================
89                                      IMPLEMENTATION OF MsgListenerThread - Member Functions
90 ==================================================================================================*/
91 MsgProxyListener* MsgProxyListener::pInstance = NULL;
92
93
94 MsgProxyListener::MsgProxyListener() : running(0)
95 {
96         sentStatusCBList.clear();
97         newMessageCBList.clear();
98         newMMSConfMessageCBList.clear();
99         newSyncMLMessageCBList.clear();
100         newLBSMessageCBList.clear();
101 }
102
103
104 MsgProxyListener::~MsgProxyListener()
105 {
106         sentStatusCBList.clear();
107         newMessageCBList.clear();
108         newMMSConfMessageCBList.clear();
109         newSyncMLMessageCBList.clear();
110         newLBSMessageCBList.clear();
111 }
112
113
114 MsgProxyListener* MsgProxyListener::instance()
115 {
116         static Mutex mm;
117         MutexLocker lock(mm);
118
119         if (!pInstance)
120                 pInstance = new MsgProxyListener();
121
122         return pInstance;
123 }
124
125
126 void MsgProxyListener::start()
127 {
128         if (running == 0)
129         {
130                 mx.lock();
131                 cliSock.connect(MSG_SOCKET_PATH);
132                 cv.signal(); // wake up the waiting thread
133                 mx.unlock();
134
135                 int fd = cliSock.fd();
136
137                 MSG_DEBUG("Socket Fd : %d", fd);
138
139                 channel = g_io_channel_unix_new(fd); // initializes ref_count = 1
140
141                 eventSourceId = g_io_add_watch(channel, (GIOCondition)(G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL), &readSocket, NULL); // increments ref_count =2
142
143                 MSG_DEBUG("Call g_io_add_watch() : %d", eventSourceId);
144         }
145
146         running++;
147         MSG_DEBUG("add Listener and [%d] are running.", running);
148 }
149
150
151 void MsgProxyListener::stop()
152 {
153         MSG_BEGIN();
154
155         if (running > 1)
156         {
157                 running--;
158                 MSG_DEBUG("There are still running Listener. [%d] left.", running);
159         }
160         else if (running == 1)
161         {
162                 MutexLocker lock(mx);
163
164                 running--;
165
166                 g_io_channel_unref(channel); // decrements ref_count = 1
167
168                 g_source_remove(eventSourceId);
169
170                 cliSock.close();
171
172                 MSG_DEBUG("client Listener is terminated.");
173         }
174
175         MSG_END();
176 }
177
178
179 bool MsgProxyListener::regSentStatusEventCB(MsgHandle* pMsgHandle, msg_sent_status_cb pfSentStatus, void *pUserParam)
180 {
181         MutexLocker lock(mx);
182
183         std::list<MSG_SENT_STATUS_CB_ITEM_S>::iterator it = sentStatusCBList.begin();
184
185         for (; it != sentStatusCBList.end(); it++)
186         {
187                 if (it->hAddr == pMsgHandle && it->pfSentStatusCB == pfSentStatus)
188                 {
189                         MSG_DEBUG("msg_sent_status_cb() callback : [%p] is already registered!!!", pfSentStatus);
190
191                         it->userParam = pUserParam;
192
193                         return false;
194                 }
195         }
196
197         MSG_SENT_STATUS_CB_ITEM_S sentStatusCB = {pMsgHandle, pfSentStatus, pUserParam};
198
199         sentStatusCBList.push_back(sentStatusCB);
200
201         return true;
202 }
203
204
205 bool MsgProxyListener::regMessageIncomingEventCB(MsgHandle* pMsgHandle, msg_sms_incoming_cb pfNewMessage, int port, void *pUserParam)
206 {
207         MutexLocker lock(mx);
208
209         std::list<MSG_INCOMING_CB_ITEM_S>::iterator it = newMessageCBList.begin();
210
211         for (; it != newMessageCBList.end(); it++)
212         {
213                 if (it->port == port && it->pfIncomingCB == pfNewMessage)
214                 {
215                         MSG_DEBUG("msg_sms_incoming_cb() callback : Port Number [%d] is already registered!!!", port);
216
217                         it->userParam = pUserParam;
218
219                         return false;
220                 }
221         }
222
223         MSG_INCOMING_CB_ITEM_S incomingCB = {pMsgHandle, pfNewMessage, port, pUserParam};
224
225         newMessageCBList.push_back(incomingCB);
226
227         return true;
228 }
229
230
231 bool MsgProxyListener::regMMSConfMessageIncomingEventCB(MsgHandle* pMsgHandle, msg_mms_conf_msg_incoming_cb pfNewMMSConfMessage, const char *pAppId, void *pUserParam)
232 {
233         MutexLocker lock(mx);
234
235         std::list<MSG_MMS_CONF_INCOMING_CB_ITEM_S>::iterator it = newMMSConfMessageCBList.begin();
236
237         for (; it != newMMSConfMessageCBList.end(); it++)
238         {
239                 if (it->pfMMSConfIncomingCB == pfNewMMSConfMessage)
240                 {
241
242                         if(pAppId == NULL)
243                         {
244                                 MSG_DEBUG("msg_mms_conf_msg_incoming_cb() callback is already registered!!!");
245
246                                 return false;
247                         }
248                         else if(!strncmp(it->appId, pAppId, MAX_MMS_JAVA_APPID_LEN))
249                         {
250                                 MSG_DEBUG("msg_mms_conf_msg_incoming_cb() callback : AppId [%s] is already registered!!!", pAppId);
251
252                                 it->userParam = pUserParam;
253
254                                 return false;
255                         }
256                 }
257         }
258
259         MSG_MMS_CONF_INCOMING_CB_ITEM_S incomingConfCB = {pMsgHandle, pfNewMMSConfMessage, {0}, pUserParam};
260
261         if (pAppId != NULL)
262                 strncpy(incomingConfCB.appId, pAppId, MAX_MMS_JAVA_APPID_LEN);
263
264         newMMSConfMessageCBList.push_back(incomingConfCB);
265
266         return true;
267 }
268
269
270 bool MsgProxyListener::regPushMessageIncomingEventCB(MsgHandle* pMsgHandle, msg_push_msg_incoming_cb pfNewPushMessage, const char *pAppId, void *pUserParam)
271 {
272         MutexLocker lock(mx);
273
274         std::list<MSG_PUSH_INCOMING_CB_ITEM_S>::iterator it = newPushMessageCBList.begin();
275
276         for (; it != newPushMessageCBList.end(); it++)
277         {
278                 if (it->pfPushIncomingCB == pfNewPushMessage)
279                 {
280
281                         if(pAppId == NULL)
282                         {
283                                 MSG_DEBUG("msg_push_msg_incoming_cb() callback is already registered!!!");
284
285                                 return false;
286                         }
287                         else if(!strncmp(it->appId, pAppId, MAX_WAPPUSH_ID_LEN))
288                         {
289                                 MSG_DEBUG("msg_push_msg_incoming_cb() callback : AppId [%s] is already registered!!!", pAppId);
290
291                                 it->userParam = pUserParam;
292
293                                 return false;
294                         }
295                 }
296         }
297
298         MSG_PUSH_INCOMING_CB_ITEM_S incomingPushCB = {pMsgHandle, pfNewPushMessage, {0}, pUserParam};
299
300         if (pAppId != NULL)
301                 strncpy(incomingPushCB.appId, pAppId, MAX_WAPPUSH_ID_LEN);
302
303         newPushMessageCBList.push_back(incomingPushCB);
304
305         return true;
306 }
307
308 bool MsgProxyListener::regCBMessageIncomingEventCB(MsgHandle* pMsgHandle, msg_cb_incoming_cb pfNewCBMessage, bool bSave, void *pUserParam)
309 {
310         MutexLocker lock(mx);
311
312         std::list<MSG_CB_INCOMING_CB_ITEM_S>::iterator it = newCBMessageCBList.begin();
313
314         for (; it != newCBMessageCBList.end(); it++)
315         {
316                 if (it->pfCBIncomingCB == pfNewCBMessage)
317                 {
318                         MSG_DEBUG("msg_CB_incoming_cb() callback : [%p] is already registered!!!", pfNewCBMessage);
319
320                         it->bsave = bSave;
321                         it->userParam = pUserParam;
322
323                         return false;
324                 }
325         }
326
327         MSG_CB_INCOMING_CB_ITEM_S incomingCB = {pMsgHandle, pfNewCBMessage, bSave, pUserParam};
328
329         newCBMessageCBList.push_back(incomingCB);
330
331         return true;
332 }
333
334 bool MsgProxyListener::regSyncMLMessageIncomingEventCB(MsgHandle* pMsgHandle, msg_syncml_msg_incoming_cb pfNewSyncMLMessage, void *pUserParam)
335 {
336         MutexLocker lock(mx);
337
338         std::list<MSG_SYNCML_INCOMING_CB_ITEM_S>::iterator it = newSyncMLMessageCBList.begin();
339
340         for (; it != newSyncMLMessageCBList.end(); it++)
341         {
342                 if (it->pfSyncMLIncomingCB == pfNewSyncMLMessage)
343                 {
344                         MSG_DEBUG("msg_syncml_msg_incoming_cb() callback : [%p] is already registered!!!", pfNewSyncMLMessage);
345
346                         it->userParam = pUserParam;
347
348                         return false;
349                 }
350         }
351
352         MSG_SYNCML_INCOMING_CB_ITEM_S incomingCB = {pMsgHandle, pfNewSyncMLMessage, pUserParam};
353
354         newSyncMLMessageCBList.push_back(incomingCB);
355
356         return true;
357 }
358
359
360 bool MsgProxyListener::regLBSMessageIncomingEventCB(MsgHandle* pMsgHandle, msg_lbs_msg_incoming_cb pfNewLBSMsgIncoming, void *pUserParam)
361 {
362         MutexLocker lock(mx);
363
364         std::list<MSG_LBS_INCOMING_CB_ITEM_S>::iterator it = newLBSMessageCBList.begin();
365
366         for (; it != newLBSMessageCBList.end(); it++)
367         {
368                 if (it->pfLBSMsgIncoming == pfNewLBSMsgIncoming)
369                 {
370                         MSG_DEBUG("msg_lbs_msg_incoming_cb() callback : [%p] is already registered!!!", pfNewLBSMsgIncoming);
371
372                         it->userParam = pUserParam;
373
374                         return false;
375                 }
376         }
377
378         MSG_LBS_INCOMING_CB_ITEM_S incomingCB = {pMsgHandle, pfNewLBSMsgIncoming, pUserParam};
379
380         newLBSMessageCBList.push_back(incomingCB);
381
382         return true;
383 }
384
385
386 bool MsgProxyListener::regSyncMLMessageOperationEventCB(MsgHandle* pMsgHandle, msg_syncml_msg_operation_cb pfSyncMLMessageOperation, void *pUserParam)
387 {
388         MutexLocker lock(mx);
389
390         std::list<MSG_SYNCML_OPERATION_CB_ITEM_S>::iterator it = operationSyncMLMessageCBList.begin();
391
392         for (; it != operationSyncMLMessageCBList.end(); it++)
393         {
394                 if (it->pfSyncMLOperationCB == pfSyncMLMessageOperation)
395                 {
396                         MSG_DEBUG("msg_syncml_msg_incoming_cb() callback : [%p] is already registered!!!", pfSyncMLMessageOperation);
397
398                         it->userParam = pUserParam;
399
400                         return false;
401                 }
402         }
403
404         MSG_SYNCML_OPERATION_CB_ITEM_S incomingCB = {pMsgHandle, pfSyncMLMessageOperation, pUserParam};
405
406         operationSyncMLMessageCBList.push_back(incomingCB);
407
408         return true;
409 }
410
411
412 bool MsgProxyListener::regStorageChangeEventCB(MsgHandle* pMsgHandle, msg_storage_change_cb pfStorageChangeOperation, void *pUserParam)
413 {
414         MutexLocker lock(mx);
415
416         std::list<MSG_STORAGE_CHANGE_CB_ITEM_S>::iterator it = storageChangeCBList.begin();
417
418         for (; it != storageChangeCBList.end(); it++)
419         {
420                 if (it->pfStorageChangeCB == pfStorageChangeOperation)
421                 {
422                         MSG_DEBUG("msg_storage_change_cb() callback : [%p] is already registered!!!", pfStorageChangeOperation);
423
424                         it->userParam = pUserParam;
425
426                         return false;
427                 }
428         }
429
430         MSG_STORAGE_CHANGE_CB_ITEM_S changeCB = {pMsgHandle, pfStorageChangeOperation, pUserParam};
431
432         storageChangeCBList.push_back(changeCB);
433
434         return true;
435 }
436
437
438 void MsgProxyListener::clearListOfClosedHandle(MsgHandle* pMsgHandle)
439 {
440         MSG_BEGIN();
441
442         // sent status CB list
443         std::list<MSG_SENT_STATUS_CB_ITEM_S>::iterator it = sentStatusCBList.begin();
444
445         for (; it != sentStatusCBList.end(); it++)
446         {
447                 if (it->hAddr == pMsgHandle)
448                 {
449                         sentStatusCBList.erase(it);
450                         it = sentStatusCBList.begin();
451
452                         //Stop client Listener
453                         stop();
454                 }
455         }
456
457         // new message CB list
458         std::list<MSG_INCOMING_CB_ITEM_S>::iterator it2 = newMessageCBList.begin();
459
460         for (; it2 != newMessageCBList.end(); it2++)
461         {
462                 if (it2->hAddr == pMsgHandle)
463                 {
464                         newMessageCBList.erase(it2);
465                         it2 = newMessageCBList.begin();
466
467                         //Stop client Listener
468                         stop();
469                 }
470         }
471
472         // MMS conf Message CB list
473         std::list<MSG_MMS_CONF_INCOMING_CB_ITEM_S>::iterator it3 = newMMSConfMessageCBList.begin();
474
475         for (; it3 != newMMSConfMessageCBList.end(); it3++)
476         {
477                 if (it3->hAddr == pMsgHandle)
478                 {
479                         newMMSConfMessageCBList.erase(it3);
480                         it3 = newMMSConfMessageCBList.begin();
481
482                         //Stop client Listener
483                         stop();
484                 }
485         }
486
487         // SyncML Message CB list
488         std::list<MSG_SYNCML_INCOMING_CB_ITEM_S>::iterator it4 = newSyncMLMessageCBList.begin();
489
490         for (; it4 != newSyncMLMessageCBList.end(); it4++)
491         {
492                 if (it4->hAddr == pMsgHandle)
493                 {
494                         newSyncMLMessageCBList.erase(it4);
495                         it4 = newSyncMLMessageCBList.begin();
496
497                         //Stop client Listener
498                         stop();
499                 }
500         }
501
502         // LBS Message CB list
503         std::list<MSG_LBS_INCOMING_CB_ITEM_S>::iterator it5 = newLBSMessageCBList.begin();
504
505         for (; it5 != newLBSMessageCBList.end(); it5++)
506         {
507                 if (it5->hAddr == pMsgHandle)
508                 {
509                         newLBSMessageCBList.erase(it5);
510                         it5 = newLBSMessageCBList.begin();
511
512                         //Stop client Listener
513                         stop();
514                 }
515         }
516
517         // Push Message CB list
518         std::list<MSG_PUSH_INCOMING_CB_ITEM_S>::iterator it6 = newPushMessageCBList.begin();
519
520         for (; it6 != newPushMessageCBList.end(); it6++)
521         {
522                 if (it6->hAddr == pMsgHandle)
523                 {
524                         newPushMessageCBList.erase(it6);
525                         it6 = newPushMessageCBList.begin();
526
527                         //Stop client Listener
528                         stop();
529                 }
530
531         }
532
533         // CB Message CB list
534         std::list<MSG_CB_INCOMING_CB_ITEM_S>::iterator it7 = newCBMessageCBList.begin();
535
536         bool bSave = false;
537         for (; it7 != newCBMessageCBList.end(); it7++)
538         {
539
540                 if (it7->hAddr == pMsgHandle)
541                 {
542
543                         newCBMessageCBList.erase(it7);
544                         it7 = newCBMessageCBList.begin();
545
546                         //Stop client Listener
547                         stop();
548                 }
549                 else
550                 {
551                         if(it7->bsave == true)
552                                 bSave = true;
553                 }
554         }
555
556         if(!bSave)
557                 MsgSettingSetBool(CB_SAVE, bSave);
558
559         // Storage change Message CB list
560         std::list<MSG_STORAGE_CHANGE_CB_ITEM_S>::iterator it8 = storageChangeCBList.begin();
561
562         for (; it8 != storageChangeCBList.end(); it8++)
563         {
564                 if (it8->hAddr == pMsgHandle)
565                 {
566                         storageChangeCBList.erase(it8);
567                         it8 = storageChangeCBList.begin();
568
569                         //Stop client Listener
570                         stop();
571                 }
572         }
573
574         MSG_END();
575 }
576
577 void MsgProxyListener::handleEvent(const MSG_EVENT_S* pMsgEvent)
578 {
579         MSG_BEGIN();
580
581         if (!pMsgEvent)
582                 THROW(MsgException::INVALID_PARAM, "pMsgEvent is NULL");
583
584         if (pMsgEvent->eventType == MSG_EVENT_PLG_SENT_STATUS_CNF)
585         {
586                 unsigned int chInfo[3] = {0}; //3// reqid, status, object
587
588                 memcpy(&chInfo, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)), sizeof(chInfo));
589
590                 msg_struct_s status = {0,};
591                 MSG_SENT_STATUS_S statusData = {(msg_request_id_t)chInfo[0], (msg_network_status_t)chInfo[1]};
592
593                 status.type = MSG_STRUCT_SENT_STATUS_INFO;
594                 status.data = (void *)&statusData;
595
596                 mx.lock();
597
598                 MsgSentStatusCBList::iterator it = sentStatusCBList.begin();
599
600                 for( ; it != sentStatusCBList.end() ; it++)
601                 {
602                         MsgHandle* pHandle = it->hAddr;
603
604                         msg_sent_status_cb pfunc = it->pfSentStatusCB;
605
606                         void* param = it->userParam;
607
608                         pfunc((msg_handle_t)pHandle, (msg_struct_t)&status, param);
609                 }
610
611                 mx.unlock();
612         }
613         else if ( pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_MSG_IND )
614         {
615                 MSG_MESSAGE_INFO_S *pMsgInfo = (MSG_MESSAGE_INFO_S *)pMsgEvent->data;
616                 int portKey = (pMsgInfo->msgPort.valid)? pMsgInfo->msgPort.dstPort: 0;
617
618                 mx.lock();
619
620                 MsgNewMessageCBList::iterator it = newMessageCBList.begin();
621                 MsgNewMessageCBList matchList;
622
623                 for( ; it != newMessageCBList.end() ; it++)
624                 {
625                         if( portKey == it->port)
626                         {
627                                 matchList.push_back(*it);
628                         }
629                 }
630
631                 mx.unlock();
632
633                 it = matchList.begin();
634
635                 for( ; it != matchList.end(); it++ )
636                 {
637                         MsgHandle* pHandle = it->hAddr;
638
639                         MSG_MESSAGE_INFO_S *pMsgInfo = (MSG_MESSAGE_INFO_S *)pMsgEvent->data;
640                         MSG_MESSAGE_HIDDEN_S msgHidden = {0,};
641
642                         msgHidden.pData = NULL;
643                         msgHidden.pMmsData = NULL;
644
645                         /* Allocate memory for address list of message */
646                         msg_struct_list_s *addr_list = (msg_struct_list_s *)new msg_struct_list_s;
647
648                         addr_list->nCount = 0;
649                         addr_list->msg_struct_info = (msg_struct_t *)new char[sizeof(MSG_ADDRESS_INFO_S *)*MAX_TO_ADDRESS_CNT];
650
651                         msg_struct_s *pTmp = NULL;
652
653                         for (int i = 0; i < MAX_TO_ADDRESS_CNT; i++) {
654                                 addr_list->msg_struct_info[i] = (msg_struct_t)new char[sizeof(msg_struct_s)];
655                                 pTmp = (msg_struct_s *)addr_list->msg_struct_info[i];
656                                 pTmp->type = MSG_STRUCT_ADDRESS_INFO;
657                                 pTmp->data = new MSG_ADDRESS_INFO_S;
658                                 memset(pTmp->data, 0x00, sizeof(MSG_ADDRESS_INFO_S));
659
660                                 addr_list->msg_struct_info[i] = (msg_struct_t)pTmp;
661                         }
662
663                         msgHidden.addr_list = addr_list;
664
665                         pHandle->convertMsgStruct(pMsgInfo, &msgHidden);
666
667                         msg_struct_s msg = {0,};
668                         msg.type = MSG_STRUCT_MESSAGE_INFO;
669                         msg.data = &msgHidden;
670
671                         msg_sms_incoming_cb pfunc = it->pfIncomingCB;
672
673                         void* param = it->userParam;
674
675                         pfunc((msg_handle_t)pHandle, (msg_struct_t) &msg, param);
676
677                         delete [] (char*)msgHidden.pData;
678                         if (msgHidden.pMmsData != NULL)
679                                 delete [] (char*)msgHidden.pMmsData;
680
681                         // address Memory Free
682                         if (msgHidden.addr_list!= NULL)
683                         {
684                                 for(int i=0; i<MAX_TO_ADDRESS_CNT; i++) {
685                                         msg_struct_s * addrInfo = (msg_struct_s *)msgHidden.addr_list->msg_struct_info[i];
686                                         delete (MSG_ADDRESS_INFO_S *)addrInfo->data;
687                                         addrInfo->data = NULL;
688                                         delete (msg_struct_s *)msgHidden.addr_list->msg_struct_info[i];
689                                         msgHidden.addr_list->msg_struct_info[i] = NULL;
690                                 }
691
692                                 delete [] msgHidden.addr_list->msg_struct_info;
693
694                                 delete msgHidden.addr_list;
695                                 msgHidden.addr_list = NULL;
696                         }
697
698                 }
699
700         }
701         else if ( pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_MMS_CONF )
702         {
703                 MSG_MESSAGE_INFO_S *pMsgInfo = (MSG_MESSAGE_INFO_S *)pMsgEvent->data;
704                 MMS_RECV_DATA_S* pMmsRecvData = ( MMS_RECV_DATA_S*)pMsgInfo->msgData;
705
706                 char* appIdKey = (pMmsRecvData->msgAppId.valid)? pMmsRecvData->msgAppId.appId: NULL;
707
708                 mx.lock();
709
710                 MsgNewMMSConfMessageCBList::iterator it = newMMSConfMessageCBList.begin();
711                 MsgNewMMSConfMessageCBList matchList;
712
713                 for( ; it != newMMSConfMessageCBList.end() ; it++)
714                 {
715                         if(appIdKey)
716                         {
717                                 if(!strcmp(appIdKey, it->appId))
718                                         matchList.push_back(*it);
719                         }
720                         else//(appIdKey == NULL && it->appId[0] == 0)
721                         {
722                                 if(it->appId[0] == 0)
723                                         matchList.push_back(*it);
724                         }
725                 }
726
727                 mx.unlock();
728
729                 // Contents of msgData removed and replaced to retrievedFilePath for convertMsgStruct
730                 // it is moved from UpdateMessage in MmsPluginStorage.cpp
731                 char tempFileName[MSG_FILENAME_LEN_MAX+1] = {0};  // check MSG_FILENAME_LEN_MAX
732
733                 strncpy(tempFileName, pMmsRecvData->retrievedFilePath, MSG_FILENAME_LEN_MAX);
734
735                 memset(pMsgInfo->msgData, 0, MAX_MSG_DATA_LEN+1);
736                 memcpy(pMsgInfo->msgData, tempFileName + strlen(MSG_DATA_PATH), strlen(tempFileName));
737
738                 it = matchList.begin();
739
740                 for( ; it != matchList.end() ; it++)
741                 {
742                         MsgHandle* pHandle = it->hAddr;
743
744                         MSG_MESSAGE_INFO_S *pMsgInfo = (MSG_MESSAGE_INFO_S *)pMsgEvent->data;
745                         MSG_MESSAGE_HIDDEN_S msgHidden = {0,};
746
747                         msgHidden.pData = NULL;
748                         msgHidden.pMmsData = NULL;
749
750                         /* Allocate memory for address list of message */
751                         msg_struct_list_s *addr_list = (msg_struct_list_s *)new msg_struct_list_s;
752
753                         addr_list->nCount = 0;
754                         addr_list->msg_struct_info = (msg_struct_t *)new char[sizeof(MSG_ADDRESS_INFO_S *)*MAX_TO_ADDRESS_CNT];
755
756                         msg_struct_s *pTmp = NULL;
757
758                         for (int i = 0; i < MAX_TO_ADDRESS_CNT; i++) {
759                                 addr_list->msg_struct_info[i] = (msg_struct_t)new char[sizeof(msg_struct_s)];
760                                 pTmp = (msg_struct_s *)addr_list->msg_struct_info[i];
761                                 pTmp->type = MSG_STRUCT_ADDRESS_INFO;
762                                 pTmp->data = new MSG_ADDRESS_INFO_S;
763                                 memset(pTmp->data, 0x00, sizeof(MSG_ADDRESS_INFO_S));
764
765                                 addr_list->msg_struct_info[i] = (msg_struct_t)pTmp;
766                         }
767
768                         msgHidden.addr_list = addr_list;
769
770                         pHandle->convertMsgStruct(pMsgInfo, &msgHidden);
771
772                         msg_struct_s msg = {0,};
773                         msg.type = MSG_STRUCT_MESSAGE_INFO;
774                         msg.data = &msgHidden;
775
776                         msg_mms_conf_msg_incoming_cb pfunc = it->pfMMSConfIncomingCB;
777
778                         void* param = it->userParam;
779                         pfunc((msg_handle_t)pHandle, (msg_struct_t) &msg, param);
780
781                         delete [] (char*)msgHidden.pData;
782                         if (msgHidden.pMmsData != NULL)
783                                 delete [] (char*)msgHidden.pMmsData;
784
785                         // address Memory Free
786                         if (msgHidden.addr_list!= NULL)
787                         {
788                                 for(int i=0; i<MAX_TO_ADDRESS_CNT; i++) {
789                                         msg_struct_s * addrInfo = (msg_struct_s *)msgHidden.addr_list->msg_struct_info[i];
790                                         delete (MSG_ADDRESS_INFO_S *)addrInfo->data;
791                                         addrInfo->data = NULL;
792                                         delete (msg_struct_s *)msgHidden.addr_list->msg_struct_info[i];
793                                         msgHidden.addr_list->msg_struct_info[i] = NULL;
794                                 }
795
796                                 delete [] msgHidden.addr_list->msg_struct_info;
797
798                                 delete msgHidden.addr_list;
799                                 msgHidden.addr_list = NULL;
800                         }
801
802                         // Here the retrieved message will be deleted from native storage.
803                         // as of now, msg which have appId is considered as JAVA MMS message and it will be sent and handled in JAVA app.
804                         if(appIdKey)
805                         {
806                                 MSG_DEBUG("delete received JAVA MMS message:%s from native storage",tempFileName);
807                                 pHandle->deleteMessage(pMsgInfo->msgId);
808                         }
809                 }
810         }
811         else if ( pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_SYNCML_MSG_IND )
812         {
813                 MSG_SYNCML_MESSAGE_DATA_S* pSyncMLData = (MSG_SYNCML_MESSAGE_DATA_S *)pMsgEvent->data;
814
815                 MSG_DEBUG("msgType [%d]", pSyncMLData->syncmlType);
816
817                 mx.lock();
818
819                 MsgNewSyncMLMessageCBList::iterator it = newSyncMLMessageCBList.begin();
820
821                 for( ; it != newSyncMLMessageCBList.end() ; it++)
822                 {
823                         MsgHandle* pHandle = it->hAddr;
824
825                         msg_syncml_msg_incoming_cb pfunc = it->pfSyncMLIncomingCB;
826
827                         void* param = it->userParam;
828
829                         pfunc((msg_handle_t)pHandle, pSyncMLData->syncmlType, pSyncMLData->pushBody, pSyncMLData->pushBodyLen, pSyncMLData->wspHeader, pSyncMLData->wspHeaderLen, param);
830                 }
831
832                 mx.unlock();
833         }
834         else if ( pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_LBS_MSG_IND )
835         {
836                 MSG_LBS_MESSAGE_DATA_S* pLBSData = (MSG_LBS_MESSAGE_DATA_S *)pMsgEvent->data;
837
838                 mx.lock();
839
840                 MsgNewLBSMessageCBList::iterator it = newLBSMessageCBList.begin();
841
842                 for( ; it != newLBSMessageCBList.end() ; it++)
843                 {
844                         MsgHandle* pHandle = it->hAddr;
845
846                         msg_lbs_msg_incoming_cb pfunc = it->pfLBSMsgIncoming;
847
848                         void* param = it->userParam;
849
850                         pfunc((msg_handle_t)pHandle, pLBSData->pushHeader, pLBSData->pushBody, pLBSData->pushBodyLen, param);
851                 }
852
853                 mx.unlock();
854         }
855         else if ( pMsgEvent->eventType == MSG_EVENT_SYNCML_OPERATION )
856         {
857                 int msgId;
858                 int extId;
859
860                 memcpy(&msgId, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)), sizeof(int));
861                 memcpy(&extId, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)+sizeof(int)), sizeof(int));
862
863                 MSG_DEBUG("msgId [%d]", msgId);
864                 MSG_DEBUG("extId [%d]", extId);
865
866                 mx.lock();
867
868                 MsgOperationSyncMLMessageCBList::iterator it = operationSyncMLMessageCBList.begin();
869
870                 for( ; it != operationSyncMLMessageCBList.end() ; it++)
871                 {
872                         MsgHandle* pHandle = it->hAddr;
873
874                         msg_syncml_msg_operation_cb pfunc = it->pfSyncMLOperationCB;
875
876                         void* param = it->userParam;
877
878                         pfunc((msg_handle_t)pHandle, msgId, extId, param);
879                 }
880
881                 mx.unlock();
882         }
883         else if (pMsgEvent->eventType == MSG_EVENT_PLG_STORAGE_CHANGE_IND)
884         {
885                 msg_storage_change_type_t storageChangeType;
886                 msg_id_list_s msgIdList;
887                 memset(&msgIdList, 0x00, sizeof(msg_id_list_s));
888
889                 // Decode event data
890                 memcpy(&storageChangeType, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)), sizeof(msg_storage_change_type_t));
891                 memcpy(&msgIdList.nCount, (void*)((char*)pMsgEvent+sizeof(MSG_EVENT_TYPE_T)+sizeof(msg_error_t)+sizeof(msg_storage_change_type_t)), sizeof(int));
892
893                 if(msgIdList.nCount > 0)
894                         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));
895                 else
896                         msgIdList.msgIdList = NULL;
897
898                 MSG_DEBUG("storageChangeType [%d], msgIdList.nCount [%d]", storageChangeType, msgIdList.nCount);
899
900                 mx.lock();
901
902                 MsgStorageChangeCBList::iterator it = storageChangeCBList.begin();
903
904                 for( ; it != storageChangeCBList.end() ; it++)
905                 {
906                         MsgHandle* pHandle = it->hAddr;
907
908                         msg_storage_change_cb pfunc = it->pfStorageChangeCB;
909
910                         void* param = it->userParam;
911
912                         pfunc((msg_handle_t)pHandle, storageChangeType, (msg_id_list_s*)&msgIdList, param);
913                 }
914
915                 mx.unlock();
916         }
917
918         else if (pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_CB_MSG_IND)
919         {
920                 MSG_CB_MSG_S *pCbMsg = (MSG_CB_MSG_S *)pMsgEvent->data;
921
922                 mx.lock();
923
924                 MsgNewCBMessageCBList::iterator it = newCBMessageCBList.begin();
925
926                 for( ; it != newCBMessageCBList.end() ; it++)
927                 {
928                         MsgHandle* pHandle = it->hAddr;
929                         msg_struct_s msg = {0,};
930
931                         msg.type = MSG_STRUCT_CB_MSG;
932                         msg.data = pCbMsg;
933
934                         msg_cb_incoming_cb pfunc = it->pfCBIncomingCB;
935
936                         void* param = it->userParam;
937
938                         pfunc((msg_handle_t)pHandle, (msg_struct_t) &msg, param);
939                 }
940
941                 mx.unlock();
942         }
943
944         else if ( pMsgEvent->eventType == MSG_EVENT_PLG_INCOMING_PUSH_MSG_IND )
945         {
946                 MSG_PUSH_MESSAGE_DATA_S* pPushData = (MSG_PUSH_MESSAGE_DATA_S *)pMsgEvent->data;
947
948                 mx.lock();
949
950                 MsgNewPushMessageCBList::iterator it = newPushMessageCBList.begin();
951
952                 for( ; it != newPushMessageCBList.end() ; it++)
953                 {
954                         MsgHandle* pHandle = it->hAddr;
955
956                         msg_push_msg_incoming_cb pfunc = it->pfPushIncomingCB;
957
958                         void* param = it->userParam;
959
960                         pfunc((msg_handle_t)pHandle, pPushData->pushHeader, pPushData->pushBody, pPushData->pushBodyLen, param);
961                 }
962
963                 mx.unlock();
964         }
965
966         MSG_END();
967 }
968
969
970 int  MsgProxyListener::getRemoteFd()
971 {
972         MutexLocker lock(mx);
973
974         int tmpFd = cliSock.getRemoteFd();
975
976         MSG_DEBUG("listener fd [%d]", tmpFd);
977
978         if( tmpFd == -1 )
979         {
980                 cv.wait(mx.pMutex());
981         }
982
983         return cliSock.getRemoteFd();
984 }
985
986
987 int MsgProxyListener::readFromSocket(char** buf, int* len)
988 {
989         return cliSock.read(buf, len);
990 }