utilities/serviceipcserver/serviceipcserver.cpp
changeset 16 3c88a81ff781
equal deleted inserted replaced
14:6aeb7a756187 16:3c88a81ff781
       
     1 /**
       
     2    This file is part of CWRT package **
       
     3 
       
     4    Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). **
       
     5 
       
     6    This program is free software: you can redistribute it and/or modify
       
     7    it under the terms of the GNU (Lesser) General Public License as
       
     8    published by the Free Software Foundation, version 2.1 of the License.
       
     9    This program is distributed in the hope that it will be useful, but
       
    10    WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
       
    12    (Lesser) General Public License for more details. You should have
       
    13    received a copy of the GNU (Lesser) General Public License along
       
    14    with this program. If not, see <http://www.gnu.org/licenses/>.
       
    15 */
       
    16 
       
    17 
       
    18 #include "serviceipcserver.h"
       
    19 #include "serviceipcserver_p.h"
       
    20 #include "serviceipcserverfactory.h"
       
    21 #include "serviceipcobserver.h"
       
    22 #include "serviceipcserversession.h"
       
    23 
       
    24 namespace WRT
       
    25 {
       
    26 
       
    27 const int KServerShutdownDelay=10000;
       
    28 /*!
       
    29  \class ServiceFwIPCServer
       
    30  Service Framework IPC Server public class
       
    31  */
       
    32 
       
    33 /*!
       
    34     Constructor
       
    35     @param aBackend backend to instantiate
       
    36     @param aObserver engine observer to call back to handle new requests
       
    37     @param aParent QObject's parent
       
    38 */
       
    39 ServiceFwIPCServer::ServiceFwIPCServer( MServiceIPCObserver* aObserver, 
       
    40                                         QObject* aParent, 
       
    41                                         TServiceIPCBackends aBackend ) 
       
    42     : QObject(aParent )
       
    43     , m_Observer( aObserver )
       
    44 {
       
    45     d = ServiceFwIPCServerFactory::createBackend( aParent,aBackend );
       
    46     d->q = this;
       
    47     m_Timer = new QTimer();
       
    48     connect(m_Timer, SIGNAL(timeout()), this, SIGNAL(handleExit()));
       
    49 }
       
    50 
       
    51 /*!
       
    52  Destructor
       
    53  */
       
    54 ServiceFwIPCServer::~ServiceFwIPCServer()
       
    55 {
       
    56     delete d;
       
    57     if(m_Timer){
       
    58         if (m_Timer->isActive()) {
       
    59             m_Timer->stop();          
       
    60         }
       
    61         delete m_Timer;
       
    62         m_Timer = NULL;
       
    63     }
       
    64 }
       
    65 
       
    66 /*!
       
    67  Start listening for new service requests
       
    68  @param aServerName name of the server
       
    69  @return bool if connected
       
    70  */
       
    71 bool ServiceFwIPCServer::listen(const QString& aServerName)
       
    72 {
       
    73     return d->listen(aServerName);
       
    74 }
       
    75 
       
    76 /*!
       
    77  Shutdown the server and stop serving clients 
       
    78  @return void
       
    79  */
       
    80 void ServiceFwIPCServer::disconnect()
       
    81 {
       
    82     d->disconnect();
       
    83 }
       
    84 
       
    85 /*!
       
    86  Start the server shutdown timer
       
    87  @return void
       
    88  */
       
    89 void ServiceFwIPCServer::startTimer()
       
    90 {
       
    91    if (m_Timer){        
       
    92        m_Timer->start(KServerShutdownDelay);
       
    93    }
       
    94 }
       
    95 
       
    96 
       
    97 /*!
       
    98  Stop the server shutdown timer
       
    99  @return void
       
   100  */
       
   101 void ServiceFwIPCServer::stopTimer()
       
   102 {
       
   103     if (m_Timer){
       
   104         if (m_Timer->isActive()){
       
   105             m_Timer->stop();
       
   106         }
       
   107     }
       
   108 }
       
   109 
       
   110 /*!
       
   111  IPC server lifetime should be configurable 
       
   112  @param aKeepLife to keep or disconnect IPC server when all clients are shutdown. 
       
   113  @return void
       
   114  */
       
   115 void ServiceFwIPCServer::configIpcServerLifetime(bool aKeepServer)
       
   116 {
       
   117     d->configIpcServerLifetime(aKeepServer);
       
   118 }
       
   119 
       
   120 /*!
       
   121  * Send a message to all broadcast clients connected to this server
       
   122  * @param aMessage the contents of the message
       
   123  */ 
       
   124 void ServiceFwIPCServer::broadcast( const QByteArray& aMessage )
       
   125 {
       
   126     QHash<int, ServiceIPCSession*> sessions = d->getBroadcastSessions(); 
       
   127     for (int i = 0; i < sessions.count(); ++i) {
       
   128         ServiceIPCSession* session = sessions[i];
       
   129         if ((session->getReadyToSend()) && (session->messageListIsEmpty())) {
       
   130             session->write(aMessage);
       
   131             session->completeRequest();
       
   132             session->setReadyToSend(false);
       
   133         }
       
   134         else {
       
   135             //queue the aMessage to be sent
       
   136             session->appendMessageList(aMessage);
       
   137         }
       
   138     }
       
   139 }
       
   140  
       
   141 /*!
       
   142  * Send a message to a particular client connected to this server
       
   143  * @param aSessionId, id of a session to broadcast to
       
   144  * @param aMessage the contents of the message
       
   145  */ 
       
   146 void ServiceFwIPCServer::sendMessage( qint32 aSessionId, QByteArray& aMessage )
       
   147 {
       
   148     QHash<int, ServiceIPCSession*> sessions = d->getBroadcastSessions(); 
       
   149     ServiceIPCSession* session = sessions.value(aSessionId);
       
   150     if (session != NULL) {
       
   151         if ((session->getReadyToSend()) && (session->messageListIsEmpty())) { 
       
   152             session->write(aMessage);
       
   153             session->completeRequest();
       
   154             session->setReadyToSend(false);
       
   155         }
       
   156         else {
       
   157             //queue the aMessage to be sent
       
   158             session->appendMessageList(aMessage);
       
   159         }
       
   160     }
       
   161 }
       
   162                                 
       
   163 }
       
   164 
       
   165 /*!
       
   166  \fn WRT::ServiceFwIPCServer::handleExit()
       
   167 
       
   168  Handle server shutdown\n
       
   169  Emitted when all client sessions were closed and server should shutdown
       
   170  @return void
       
   171  */
       
   172 // END OF FILE