0.9.13 release -- bugfix: TIVI-2637 Sometimes app list doesn't disappear after launch...
[profile/ivi/ico-uxf-homescreen.git] / lib / system-controller / CicoSCServer.cpp
1 /*
2  * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9
10 //==========================================================================
11 /**
12  *  @file   CicoSCServer.cpp
13  *
14  *  @brief  This file implementation of CicoSCServer class
15  */
16 //==========================================================================
17
18 #include <sstream>
19 using namespace std;
20
21 #include "CicoSCServer.h"
22 #include "CicoSCCommand.h"
23 #include "CicoSCMessage.h"
24 #include "CicoLog.h"
25 #include "ico_syc_error.h"
26 #include "ico_syc_msg_cmd_def.h"
27 #include "CicoSCWindowController.h"
28 #include "CicoSCInputController.h"
29 #include "CicoSCUserManager.h"
30 #include "CicoSCUser.h"
31 #include "CicoSCResourceManager.h"
32 #include "CicoSCPolicyManager.h"
33
34 class CicoSCUwsHandler
35 {
36 public:
37     CicoSCUwsHandler()
38         : uwsContext(NULL), id(NULL), fd(-1), serviceFlag(false),
39           ecoreFdHandler(NULL), appid("") {}
40     void dump(void) const {
41         ICO_DBG("uwsContext=0x%08x fd=%d service=%s "
42                 "ecoreFdHandler=0x%08x appid=%s",
43                 uwsContext, fd, serviceFlag ? "true" : "false",
44                 ecoreFdHandler, appid.c_str());
45     }
46     struct ico_uws_context *uwsContext;
47     void*  id;
48     int    fd;
49     bool   serviceFlag;
50     Ecore_Fd_Handler *ecoreFdHandler;
51     string appid;
52 };
53
54 //==========================================================================    
55 //  private static variable
56 //==========================================================================    
57 CicoSCServer* CicoSCServer::ms_myInstance = NULL;
58
59 //--------------------------------------------------------------------------
60 /**
61  *  @brief  default constructor
62  */
63 //--------------------------------------------------------------------------
64 CicoSCServer::CicoSCServer()
65     : m_uwsContext(NULL), m_windowCtrl(NULL),
66       m_inputCtrl(NULL) , m_userMgr(NULL), m_resourceMgr(NULL),
67       m_policyMgr(NULL), m_dispatchProcessing(false)
68 {
69 }
70
71 //--------------------------------------------------------------------------
72 /**
73  *  @brief  destructor
74  */
75 //--------------------------------------------------------------------------
76 CicoSCServer::~CicoSCServer()
77 {
78     if (NULL != m_uwsContext) {
79         ico_uws_close(m_uwsContext);
80     }
81 }
82
83 //--------------------------------------------------------------------------
84 /**
85  *  @brief   get CicoSCServer instance
86  *
87  *  @return CicoSCServer instance
88  */
89 //--------------------------------------------------------------------------
90 CicoSCServer*
91 CicoSCServer::getInstance(void)
92 {
93     if (NULL == ms_myInstance) {
94         ms_myInstance = new CicoSCServer();
95     }
96
97     return ms_myInstance;
98 }
99
100 //--------------------------------------------------------------------------
101 /**
102  *  @brief   set window controller instance
103  *
104  *  @param [in] windowCtrl  controller instance
105  */
106 //--------------------------------------------------------------------------
107 void
108 CicoSCServer::setWindowCtrl(CicoSCWindowController *windowCtrl)
109 {
110     m_windowCtrl = windowCtrl;
111 }
112
113 //--------------------------------------------------------------------------
114 /**
115  *  @brief   set input controller instance
116  *
117  *  @param [in] inputCtrl   controller instance
118  */
119 //--------------------------------------------------------------------------
120 void
121 CicoSCServer::setInputCtrl(CicoSCInputController *inputCtrl)
122 {
123     m_inputCtrl= inputCtrl;
124 }
125
126 //--------------------------------------------------------------------------
127 /**
128  *  @brief   set user manager
129  *
130  *  @param [in] userMgr     user manager instance
131  */
132 //--------------------------------------------------------------------------
133 void
134 CicoSCServer::setUserMgr(CicoSCUserManager *userMgr)
135 {
136     m_userMgr = userMgr;
137 }
138
139 //--------------------------------------------------------------------------
140 /**
141  *  @brief   set resource manager instance
142  *
143  *  @param [in] resourceMgr resouce manager instance
144  */
145 //--------------------------------------------------------------------------
146 void
147 CicoSCServer::setResourceMgr(CicoSCResourceManager *resourceMgr)
148 {
149     m_resourceMgr = resourceMgr;
150 }
151
152 //--------------------------------------------------------------------------
153 /**
154  *  @brief   set policy manager instance
155  *
156  *  @param [in] policyMgr policy manager instance
157  */
158 //--------------------------------------------------------------------------
159 void
160 CicoSCServer::setPolicyMgr(CicoSCPolicyManager *policyMgr)
161 {
162     m_policyMgr = policyMgr;
163 }
164
165 //--------------------------------------------------------------------------
166 /**
167  *  @brief   startup server
168  *
169  *  @param [in] port        websocket port
170  *  @param [in] protocol    websocket protocol name
171  *
172  *  @return ICO_SYC_EOK on success, other on error
173  *  @retval ICO_SYC_EOK     success
174  *  @retval ICO_SYC_ENOSYS  error(connection fail)
175  */
176 //--------------------------------------------------------------------------
177 int
178 CicoSCServer::startup(int port, const char *protocol)
179 {
180     /* create uir string ":PORT" */
181     stringstream uri;
182     uri << ":" << port;
183
184     /* create context */
185     ICO_DBG("called: ico_uws_create_context(port=%s protocol=%s)",
186             uri.str().c_str(), protocol);
187     m_uwsContext = ico_uws_create_context(uri.str().c_str(), protocol);
188     if (NULL == m_uwsContext) {
189         ICO_ERR("ico_uws_create_context() failed.");
190         return ICO_SYC_ENOSYS;
191     }
192     ico_uws_service(m_uwsContext);
193
194     /* set callback */
195     int ret = ico_uws_set_event_cb(m_uwsContext, uwsReceiveEventCB,
196                                    (void *)this);
197     if (ret != ICO_UWS_ERR_NONE) {
198         ICO_ERR("ico_uws_set_event_cb() failed(%d).", ret);
199         return ICO_SYC_ENOSYS;
200     }
201     ico_uws_service(m_uwsContext);
202
203     return ICO_SYC_EOK;
204 }
205
206 //--------------------------------------------------------------------------
207 /**
208  *  @brief  teardown server
209  */
210 //--------------------------------------------------------------------------
211 void
212 CicoSCServer::teardown(void)
213 {
214     ICO_TRA("CicoSCServer::teardown Enter");
215     {
216         std::list<CicoSCUwsHandler*>::iterator itr;
217         itr = m_uwsHandlerList.begin();
218         for (; itr !=  m_uwsHandlerList.end(); ++itr) {
219             if (NULL != (*itr)->ecoreFdHandler) {
220                 ecore_main_fd_handler_del((*itr)->ecoreFdHandler);
221                 (*itr)->ecoreFdHandler = NULL;
222             }
223             delete(*itr);
224         }
225         m_uwsHandlerList.clear();
226     }
227
228     {
229         std::list<CicoSCMessage*>::iterator itr;
230         itr = m_sendMsgQueue.begin();
231         for (; itr != m_sendMsgQueue.end(); ++itr) {
232             delete(*itr);
233         }
234         m_sendMsgQueue.clear();
235     }
236     
237     {
238         std::list<CicoSCCommand*>::iterator itr;
239         itr = m_recvCmdQueue.begin();
240         for (; itr != m_recvCmdQueue.end(); ++itr) {
241             delete(*itr);
242         }
243     }
244
245     if (NULL != m_uwsContext) {
246         ico_uws_close(m_uwsContext);
247         m_uwsContext = NULL;
248     }
249     ICO_TRA("CicoSCServer::teardown Leave");
250 }
251
252 //--------------------------------------------------------------------------
253 /**
254  *  @brief   add poll websocket file destructor
255  *
256  *  @param [in] handler  websocket handler
257  */
258 //--------------------------------------------------------------------------
259 void
260 CicoSCServer::addPollFd(CicoSCUwsHandler *handler)
261 {
262     ICO_TRA("CicoSCServer::addPollFd Enter(fd=%d)", handler->fd);
263     Ecore_Fd_Handler_Flags flags;
264     flags = (Ecore_Fd_Handler_Flags)(ECORE_FD_READ | ECORE_FD_ERROR);
265
266     handler->ecoreFdHandler = ecore_main_fd_handler_add(handler->fd, flags,
267                                                        ecoreFdCallback,
268                                                        this, NULL, NULL);
269
270     ICO_DBG("Enqueue uwsHandler(0x%08x)", handler);
271     m_uwsHandlerList.push_back(handler);
272
273     ICO_TRA("CicoSCServer::addPollFd Leave");
274 }
275
276 //--------------------------------------------------------------------------
277 /**
278  *  @brief   delete poll websocket file destructor
279  *
280  *  @param [in] handler  websocket handler
281  */
282 //--------------------------------------------------------------------------
283 void
284 CicoSCServer::delPollFd(CicoSCUwsHandler *handler)
285 {
286     ICO_TRA("CicoSCServer::delPollFd Enter");
287
288     if (NULL == handler) {
289         ICO_WRN("handler is null");
290         ICO_TRA("CicoSCServer::delPollFd Leave");
291         return;
292     }
293
294     if (NULL != handler->ecoreFdHandler) {
295         ecore_main_fd_handler_del(handler->ecoreFdHandler);
296         handler->ecoreFdHandler = NULL;
297     }
298
299     list<CicoSCUwsHandler*>::iterator itr;
300     itr = m_uwsHandlerList.begin();
301     for (; itr != m_uwsHandlerList.end(); ++itr) {
302         if (*itr == handler) {
303             ICO_DBG("Dequeue uwsHandler(0x%08x)", *itr);
304             m_uwsHandlerList.erase(itr);
305             break;
306         }
307     }
308     delete handler;
309
310     ICO_TRA("CicoSCServer::delPollFd Leave");
311 }
312
313 //--------------------------------------------------------------------------
314 /**
315  *  @brief   dispatch receive message process and send message process
316  *
317  *  @param [in] handler  websocket handler
318  *
319  *  @return websocket handler instance on find, NULL on not found
320  */
321 //--------------------------------------------------------------------------
322 void
323 CicoSCServer::dispatch(const CicoSCUwsHandler *handler)
324 {
325 //    ICO_TRA("CicoSCServer::dispatch Enter(handler=0x%08x)", handler);
326
327     if (NULL == handler) {
328         ICO_WRN("handler is null");
329         ICO_TRA("CicoSCServer::dispatch Leave");
330         return;
331     }
332
333     ico_uws_service(handler->uwsContext);
334
335     // There is a possibility that after calling ico_uws_service function,
336     // the file is deleted.  Check whether handler not the disabled.
337     if (false == isExistUwsHandler(handler)) {
338         ICO_TRA("CicoSCServer::dispatch Leave");
339         return;
340     }
341
342     if (true == m_dispatchProcessing) {
343         ICO_TRA("CicoSCServer::dispatch Leave(disptch processing)");
344         return;
345     }
346
347     m_dispatchProcessing = true;
348     list<CicoSCCommand*>::iterator itr;
349     itr = m_recvCmdQueue.begin();
350     while(itr != m_recvCmdQueue.end()) {
351         ICO_DBG("Dequeue command(0x%08x)", (*itr)->cmdid);
352         CicoSCCommand *cmd = *itr;
353         itr = m_recvCmdQueue.erase(itr);
354         switch (cmd->cmdid & MSG_CMD_TYPE_MASK) {
355         case MSG_CMD_TYPE_WINCTRL:
356             //ICO_DBG("command : MSG_CMD_TYPE_WINCTRL");
357             m_windowCtrl->handleCommand(cmd);
358             break;
359         case MSG_CMD_TYPE_INPUTCTRL:
360             //ICO_DBG("command : MSG_CMD_TYPE_INPUTCTRL");
361             m_inputCtrl->handleCommand(cmd);
362             break;
363         case MSG_CMD_TYPE_USERMGR:
364             //ICO_DBG("command : MSG_CMD_TYPE_USERMGR");
365             m_userMgr->handleCommand(cmd);
366             break;
367         case MSG_CMD_TYPE_RESOURCEMGR:
368             //ICO_DBG("command : MSG_CMD_TYPE_RESOURCEMGR");
369             m_resourceMgr->handleCommand(*cmd);
370             break;
371         case MSG_CMD_TYPE_INPUTDEVSETTING:
372             //ICO_DBG("command : MSG_CMD_TYPE_INPUTDEVSETTING");
373             m_inputCtrl->handleCommand(cmd);
374             break;
375         default:
376             ICO_WRN("command: Unknown type");
377             break;
378         }
379         delete cmd;
380     }
381     m_dispatchProcessing = false;
382
383     if (NULL == handler->ecoreFdHandler) {
384         ICO_ERR("ecoreFdHandler is null");
385         ICO_TRA("CicoSCServer::dispatch Leave");
386         return;
387     }
388
389     Eina_Bool flag = ecore_main_fd_handler_active_get(handler->ecoreFdHandler,
390                                                       ECORE_FD_WRITE);
391     if (EINA_TRUE == flag) {
392 //        ICO_DBG("start send message");
393         list<CicoSCMessage*>::iterator send_itr;
394         send_itr = m_sendMsgQueue.begin();
395         while (send_itr != m_sendMsgQueue.end()) {
396 //            ICO_DBG("m_sendMsgQueue.size=%d", m_sendMsgQueue.size());
397             CicoSCMessage* msg = *send_itr;
398             CicoSCUwsHandler *sendHandler = findUwsHandler(msg->getSendToAppid());
399             if (handler != sendHandler) {
400                 ++send_itr;
401                 continue;
402             }
403             send_itr = m_sendMsgQueue.erase(send_itr);
404             ICO_DBG("Dequeue Message(id=%d)", msg->getId());
405             if ((NULL != sendHandler) && (true == sendHandler->serviceFlag)) {
406                 const char *data = msg->getData();
407                 ICO_DBG("<<<SEND appid=%s id=0x%08x msg=%s",
408                         sendHandler->appid.c_str(), sendHandler->id, data);
409 //                ICO_DBG("called: ico_usw_send called(context=0x%08x id=0x%08x)",
410 //                        sendHandler->uwsContext, sendHandler->id);
411                 ico_uws_send(sendHandler->uwsContext, sendHandler->id,
412                              (unsigned char *)data, strlen(data));
413
414                 delete msg;
415
416                 usleep(200);
417             }
418         }
419
420         Ecore_Fd_Handler_Flags flags;;
421         flags = (Ecore_Fd_Handler_Flags)(ECORE_FD_READ | ECORE_FD_ERROR);
422
423         ecore_main_fd_handler_active_set(handler->ecoreFdHandler, flags);
424     }
425
426 //    ICO_TRA("CicoSCServer::dispatch Leave");
427 }
428
429 //--------------------------------------------------------------------------
430 /**
431  *  @brief   send message to application client
432  *
433  *  @param [in] appid   application id of destination
434  *  @param [in] msg     message
435  *
436  *  @return ICO_SYC_EOK on success, other on error
437  */
438 //--------------------------------------------------------------------------
439 int
440 CicoSCServer::sendMessage(const string & appid, CicoSCMessage* msg)
441 {
442     ICO_TRA("CicoSCServer::sendMessage Enter(appid=%s, msg=%s)",
443             appid.c_str(), msg->getData());
444
445     msg->setSendToAppid(appid);
446     ICO_DBG("Enqueue Message(id=%d)", msg->getId());
447     m_sendMsgQueue.push_back(msg);
448
449     CicoSCUwsHandler *handler = findUwsHandler(appid);
450     if (NULL != handler) {
451         Ecore_Fd_Handler_Flags flags;
452         flags = (Ecore_Fd_Handler_Flags)(ECORE_FD_READ  |
453                                          ECORE_FD_WRITE |
454                                          ECORE_FD_ERROR);
455
456         ecore_main_fd_handler_active_set(handler->ecoreFdHandler, flags);
457
458         dispatch(handler);
459     }
460
461     ICO_TRA("CicoSCServer::sendMessage Leave(EOK)");
462     return ICO_SYC_EOK;
463 }
464
465 //--------------------------------------------------------------------------
466 /**
467  *  @brief   send message to homescreen
468  *
469  *  @param [in] msg     message
470  *
471  *  @return ICO_SYC_EOK on success, other on error
472  */
473 //--------------------------------------------------------------------------
474 int
475 CicoSCServer::sendMessageToHomeScreen(CicoSCMessage* msg)
476 {
477     // if user change processing(homescree is not running)
478     if (true == m_userMgr->isStoppingNow()) {
479         ICO_DBG("homescreen not running");
480         return ICO_SYC_ENOENT;
481     }
482
483     const CicoSCUser *loginUser = m_userMgr->getLoginUser();
484     if (NULL == loginUser) {
485         ICO_WRN("homescreen unknown");
486         return ICO_SYC_ENOENT;
487     }
488     return sendMessage(loginUser->homescreen, msg);
489 }
490
491 //--------------------------------------------------------------------------
492 /*
493  *  @brief  websocket utility callback function
494  *
495  *  @param [in] context     context
496  *  @param [in] event       event kinds
497  *  @param [in] id          client id
498  *  @param [in] detail      event detail
499  *  @param [in] data        user data
500  */
501 //--------------------------------------------------------------------------
502 void
503 CicoSCServer::uwsReceiveEventCB(const struct ico_uws_context *context,
504                                 const ico_uws_evt_e event,
505                                 const void *id,
506                                 const ico_uws_detail *detail,
507                                 void *user_data)
508 {
509     if (NULL == user_data) {
510         ICO_ERR("user_data is NULL");
511         return;
512     }
513
514     CicoSCServer* server = static_cast<CicoSCServer*>(user_data);
515     server->receiveEventCB(context, event, id, detail, user_data);
516 }
517
518 //--------------------------------------------------------------------------
519 /**
520  *  @brief   ecore file destructor callback fucntion
521  *
522  *  @param [in] data        user data
523  *  @param [in] handler     ecore file destructor handler
524  *
525  *  @return ECORE_CALLBACK_RENEW on retry , ECORE_CALLBACK_CANCEL on cancel
526  */
527 //--------------------------------------------------------------------------
528 Eina_Bool
529 CicoSCServer::ecoreFdCallback(void *data, Ecore_Fd_Handler *ecoreFdhandler)
530 {
531 //    ICO_TRA("CicoSCServer::ecoreFdCallback Enter");
532
533     CicoSCUwsHandler *handler = NULL;
534     handler =  static_cast<CicoSCServer*>(data)->findUwsHandler(ecoreFdhandler);
535     if (NULL != handler) {
536         static_cast<CicoSCServer*>(data)->dispatch(handler);
537     }
538
539 //    ICO_TRA("CicoSCServer::ecoreFdCallback Leave");
540     return ECORE_CALLBACK_RENEW;
541 }
542
543 //--------------------------------------------------------------------------
544 /**
545  *  @brief   websocket callback function
546  *
547  *  @param [in] context     websocket context
548  *  @param [in] event       changed event
549  *  @param [in] id          source applicatin id
550  *  @param [in] detail      event detail information
551  *  @param [in] user_data   user data
552  */
553 //--------------------------------------------------------------------------
554 void
555 CicoSCServer::receiveEventCB(const struct ico_uws_context *context,
556                              const ico_uws_evt_e          event,
557                              const void                   *id,
558                              const ico_uws_detail         *detail,
559                              void                         *user_data)
560 {
561 //    ICO_TRA("CicoSCServer::receiveEventCB Enter");
562
563     switch (event) {
564     case ICO_UWS_EVT_CLOSE:
565         ICO_DBG(">>>RECV ICO_UWS_EVT_CLOSE(id=0x%08x)", (int)id);
566 //        ICO_TRA("CicoSCServer::receiveEventCB Leave");
567         return;
568     case ICO_UWS_EVT_ERROR:
569         ICO_DBG(">>>RECV ICO_UWS_EVT_ERROR(id=0x%08x, err=%d)", 
570                 (int)id, detail->_ico_uws_error.code);
571 //        ICO_TRA("CicoSCServer::receiveEventCB Leave");
572         return;
573     default:
574         break;
575     }
576
577     // find handler
578     CicoSCUwsHandler *handler = findUwsHandler(context, id);
579     // If not found handler, create new handler
580     if (NULL == handler) {
581         handler = new CicoSCUwsHandler();
582         handler->uwsContext = (struct ico_uws_context*)context;
583         handler->id = (void*)(id);
584         handler->serviceFlag = false;
585     }
586
587     switch (event) {
588     case ICO_UWS_EVT_OPEN:
589         ICO_DBG(">>>RECV ICO_UWS_EVT_OPEN(id=0x%08x)", (int)id); 
590         break;
591     case ICO_UWS_EVT_CLOSE:
592         ICO_DBG(">>>RECV ICO_UWS_EVT_CLOSE(id=0x%08x)", (int)id);
593         delete handler;
594         break;
595     case ICO_UWS_EVT_RECEIVE:
596     {
597         ICO_DBG(">>>RECV ICO_UWS_EVT_RECEIVE(id=0x%08x, msg=%s, len=%d)", 
598                 (int)id, (char *)detail->_ico_uws_message.recv_data,
599                 detail->_ico_uws_message.recv_len);
600
601         // convert message to command
602         CicoSCCommand *cmd = new CicoSCCommand();
603         cmd->parseMessage((const char*)detail->_ico_uws_message.recv_data);
604
605         // update handler appid
606         if (cmd->cmdid == MSG_CMD_SEND_APPID) {
607             if (0 == cmd->appid.length()) {
608                 ICO_WRN("command argument invalid appid null");
609                 delete cmd;
610                 break;
611             }
612             handler->appid = cmd->appid;
613             handler->serviceFlag = true;
614             ICO_DBG("handler.appid=%s", handler->appid.c_str());
615
616             Ecore_Fd_Handler_Flags flags;
617             flags = (Ecore_Fd_Handler_Flags)(ECORE_FD_READ  |
618                                              ECORE_FD_WRITE |
619                                              ECORE_FD_ERROR);
620
621             ecore_main_fd_handler_active_set(handler->ecoreFdHandler, flags);
622
623             notifyConnected(handler->appid);
624             delete cmd;
625             break;
626         }
627         
628         // Enqueue command
629         ICO_DBG("Enqueue command(0x%08x)", cmd->cmdid);
630         m_recvCmdQueue.push_back(cmd);
631         break;
632     }
633     case ICO_UWS_EVT_ADD_FD:
634         ICO_DBG(">>>RECV ICO_UWS_EVT_ADD_FD(id=0x%08x, fd=%d)",
635                 (int)id, detail->_ico_uws_fd.fd);
636         handler->fd = detail->_ico_uws_fd.fd;
637         addPollFd(handler);
638         break;
639     case ICO_UWS_EVT_DEL_FD:
640         ICO_DBG(">>>RECV ICO_UWS_EVT_DEL_FD(id=0x%08x, fd=%d, appid=%s)",
641                 (int)id, detail->_ico_uws_fd.fd, handler->appid.c_str());
642         clearRecvCmdQueue(handler->appid);
643         clearSendMsgQueue(handler->appid);
644         delPollFd(handler);
645         break;
646     default:
647         break;
648     }
649 //    ICO_TRA("CicoSCServer::receiveEventCB Leave");
650 }
651
652 //--------------------------------------------------------------------------
653 /**
654  *  @brief   clear receive command queue
655  *
656  *  @param [in] appid   clear command application id
657  */
658 //--------------------------------------------------------------------------
659 void
660 CicoSCServer::clearRecvCmdQueue(const std::string & appid)
661 {
662     ICO_TRA("CicoSCServer::clearCmdQueue Enter(appid=%s)", appid.c_str());
663
664     std::list<CicoSCCommand*>::iterator itr;
665     itr = m_recvCmdQueue.begin();
666     for (; itr != m_recvCmdQueue.end(); ) {
667         if (0 == appid.compare((*itr)->appid)) {
668             ICO_DBG("Dequeue command(0x%08x)", (*itr)->cmdid);
669             delete *itr;
670             itr = m_recvCmdQueue.erase(itr);
671         }
672         else {
673             ++itr;
674         }
675     }
676
677     ICO_TRA("CicoSCServer::clearCmdQueue Leave")
678 }
679
680 //--------------------------------------------------------------------------
681 /**
682  *  @brief   clear send message queue
683  *
684  *  @param [in] appid   clear message application id
685  */
686 //--------------------------------------------------------------------------
687 void
688 CicoSCServer::clearSendMsgQueue(const std::string & appid)
689 {
690     ICO_TRA("CicoSCServer::clearMsgQueue Enter(appid=%s)", appid.c_str())
691
692     std::list<CicoSCMessage*>::iterator itr;
693     itr = m_sendMsgQueue.begin();
694     while(itr != m_sendMsgQueue.end()) {
695         if (0 == appid.compare((*itr)->getSendToAppid())) {
696             ICO_DBG("Dequeue Message(id=%d)", (*itr)->getId());
697             delete *itr;
698             itr = m_sendMsgQueue.erase(itr);
699         }
700         else {
701             ++itr;
702         }
703     }
704
705     ICO_TRA("CicoSCServer::clearMsgQueue Leave")
706 }
707
708 //--------------------------------------------------------------------------
709 /**
710  *  @brief   find websocket handler by context and id
711  *
712  *  @param [in] context     websocket context
713  *  @param [in] id          id
714  *
715  *  @return websocket handler instance on find, NULL on not found
716  */
717 //--------------------------------------------------------------------------
718 CicoSCUwsHandler*
719 CicoSCServer::findUwsHandler(const struct ico_uws_context *context,
720                              const void                   *id)
721 {
722     list<CicoSCUwsHandler*>::iterator itr;
723     itr = m_uwsHandlerList.begin();
724     for (; itr != m_uwsHandlerList.end(); ++itr) {
725 //        ICO_DBG("handler->context=%p handler->id=%p context=%p id=%p",
726 //                (*itr)->uwsContext, (*itr)->id, context, id);
727         if (((*itr)->uwsContext == context) &&
728             ((*itr)->id == id)) {
729             return *itr;
730         }
731     }
732     return NULL;
733 }
734
735 //--------------------------------------------------------------------------
736 /**
737  *  @brief   find websocket handler by ecore file destructor handler
738  *
739  *  @param [in] ecoreFdHandler  ecore file destructor handler
740  *
741  *  @return websocket handler instance on find, NULL on not found
742  */
743 //--------------------------------------------------------------------------
744 CicoSCUwsHandler*
745 CicoSCServer::findUwsHandler(const Ecore_Fd_Handler *ecoreFdHandler)
746 {
747     list<CicoSCUwsHandler*>::iterator itr;
748     itr = m_uwsHandlerList.begin();
749     for (; itr != m_uwsHandlerList.end(); ++itr) {
750         if ((*itr)->ecoreFdHandler == ecoreFdHandler) {
751             return *itr;
752         }
753     }
754     return NULL;
755 }
756
757 //--------------------------------------------------------------------------
758 /**
759  *  @brief   find websocket handler by appid
760  *
761  *  @param [in] addid   application id
762  *
763  *  @return websocket handler instance on find, NULL on not found
764  */
765 //--------------------------------------------------------------------------
766 CicoSCUwsHandler*
767 CicoSCServer::findUwsHandler(const string & appid)
768 {
769     list<CicoSCUwsHandler*>::iterator itr;
770     itr = m_uwsHandlerList.begin();
771     for (; itr != m_uwsHandlerList.end(); ++itr) {
772 //        ICO_DBG("handler->id=%p handler->appid=%s appid=%s",
773 //                (*itr)->id, (*itr)->appid.c_str(), appid.c_str());
774         if ((*itr)->appid == appid) {
775             return *itr;
776         }
777     }
778     return NULL;
779 }
780
781 //--------------------------------------------------------------------------
782 /**
783  *  @brief   find websocket handler by appid
784  *
785  *  @param [in] handler     websocket handler instance
786  *
787  *  @return true on exist, false on not exist
788  */
789 //--------------------------------------------------------------------------
790 bool
791 CicoSCServer::isExistUwsHandler(const CicoSCUwsHandler *handler)
792 {   
793     list<CicoSCUwsHandler*>::iterator itr;
794     itr = m_uwsHandlerList.begin();
795     for (; itr != m_uwsHandlerList.end(); ++itr) {
796         if (*itr == handler) {
797             return true;
798         }
799     }
800     return false;
801 }
802
803 //--------------------------------------------------------------------------
804 /**
805  *  @brief  notify information to homescreen on connected
806  *
807  *  @param [in] appid   application id
808  */
809 //--------------------------------------------------------------------------
810 void
811 CicoSCServer::notifyConnected(const std::string & appid)
812 {
813     const CicoSCUser *loginUser = m_userMgr->getLoginUser();
814     if (NULL == loginUser) {
815         ICO_WRN("homescreen unknown");
816         return;
817     }
818
819     // if appid equal homescreen
820     if (0 == loginUser->homescreen.compare(appid)) {
821         if (NULL != m_policyMgr) {
822             m_policyMgr->notifyConnected(appid);
823         }
824     }
825 }
826 // vim:set expandtab ts=4 sw=4: