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