utilities/serviceipcclient/serviceipcclient.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 "serviceipcclient.h"
       
    19 #include "serviceipc.h"
       
    20 
       
    21 namespace WRT
       
    22 {
       
    23 
       
    24 /*!
       
    25  \class ServiceIPCClient
       
    26   This class is an extension to the current ServiceIPCClient interface
       
    27   with the additional functionality of receiving messages from the server 
       
    28  */
       
    29 
       
    30 /*!
       
    31  Destructor
       
    32  */
       
    33 ServiceIPCClient::~ServiceIPCClient()
       
    34 {
       
    35     delete m_syncIPC;
       
    36     delete m_asyncIPC;
       
    37     delete m_broadcastIPC;
       
    38 }
       
    39 
       
    40 /*!
       
    41  Constructor 
       
    42  @param aBackend IPC backend to use
       
    43  @param aParent Parent to this QObject
       
    44  */
       
    45 ServiceIPCClient::ServiceIPCClient(QObject* aParent, TServiceIPCBackends aBackend) :
       
    46                                    QObject(aParent)
       
    47 {  
       
    48     m_syncIPC = new ServiceFwIPC(aParent, aBackend);
       
    49     m_asyncIPC = new ServiceFwIPC(aParent, aBackend);
       
    50     m_broadcastIPC = new ServiceFwIPC(aParent, aBackend);
       
    51     
       
    52     QObject::connect(m_asyncIPC, SIGNAL(readyRead()), this, SLOT(handleAsyncMsg()));
       
    53     QObject::connect(m_broadcastIPC, SIGNAL(readyRead()), this, SLOT(handleBroadcastMsg()));
       
    54 }
       
    55 
       
    56 /*!
       
    57  Connect to the server
       
    58  @param aServerName name of the server to connect to
       
    59  @return true if connected, false if not
       
    60  */
       
    61 bool ServiceIPCClient::connect(const QString& aServerName)
       
    62 {
       
    63     bool ret;
       
    64     ret = m_syncIPC->connect(aServerName);
       
    65     if (ret) {
       
    66         int sessionId;
       
    67         ret = m_syncIPC->getSessionId(sessionId);
       
    68         if (ret) {
       
    69             m_sessionId = sessionId;
       
    70             ret = m_asyncIPC->connect(aServerName);
       
    71             if (ret) {
       
    72                 ret = m_asyncIPC->setSessionId(sessionId);
       
    73                 if (ret) {
       
    74                     ret = m_broadcastIPC->connect(aServerName);
       
    75                     if (ret) {
       
    76                         ret = m_broadcastIPC->setSessionId(sessionId);
       
    77                         if (!ret) {
       
    78                             //async. IPC set session ID fail, disconnect broadcast IPC
       
    79                             m_broadcastIPC->disconnect();
       
    80                         }
       
    81                         else {
       
    82                             subscribeBroadcastMsg();
       
    83                         }
       
    84                     }
       
    85                 }
       
    86                 if (!ret)  {
       
    87                     //async. IPC set session ID fail or broadcast IPC connect fail, disconnect async IPC
       
    88                     m_asyncIPC->disconnect();
       
    89                 }
       
    90             }
       
    91         }
       
    92         if (!ret) {
       
    93             m_syncIPC->disconnect(); //get sessionId failed or async. IPC connect fail, disconnect sync IPC
       
    94         }
       
    95     }
       
    96     return ret;
       
    97 }
       
    98 
       
    99 /*!
       
   100  Disconnect from the server
       
   101  @return void
       
   102  */
       
   103 void ServiceIPCClient::disconnect()
       
   104 {
       
   105     m_syncIPC->disconnect();
       
   106     m_asyncIPC->disconnect();
       
   107     m_broadcastIPC->disconnect();
       
   108 }
       
   109 
       
   110 /*!
       
   111  Starts the service
       
   112  @param aServerName name of the server
       
   113  @param aExeName executable of the server
       
   114  @return true if connected
       
   115  */
       
   116 bool ServiceIPCClient::startServer(const QString& aServerName,
       
   117                                    const QString& aExeName)
       
   118 {
       
   119     return m_syncIPC->startServer(aServerName, aExeName);
       
   120 }
       
   121 
       
   122 /*!
       
   123  Send a request synchronously
       
   124  @param aRequestType name of the request
       
   125  @param aData data to send
       
   126  @return true if sent successful, otherwise false
       
   127  */
       
   128 bool ServiceIPCClient::sendSync(const QString& aRequestType,
       
   129                                 const QByteArray& aData)
       
   130 {
       
   131     return m_syncIPC->sendSync(aRequestType, aData);
       
   132 }
       
   133 
       
   134 /*!
       
   135  Send a request asynchronously
       
   136  @param aRequestType name of the request
       
   137  @param aData data to send
       
   138  @note Errors will be emitted via errors() signal
       
   139  @return void                                              
       
   140  */
       
   141 void ServiceIPCClient::sendAsync(const QString& aRequestType,
       
   142                                  const QByteArray& aData)
       
   143 {
       
   144     m_asyncIPC->sendAsync(aRequestType, aData);
       
   145 }
       
   146 
       
   147 /*!
       
   148  Reads all data pending in the buffer.\n
       
   149  For Sync version this will wait until all of the data is available.\n
       
   150  @return QByteArray of results
       
   151  */
       
   152 QByteArray ServiceIPCClient::readAll()
       
   153 {
       
   154     return m_syncIPC->readAll();
       
   155 }
       
   156 
       
   157 /*!
       
   158  Check if an async request is already pending
       
   159  @return true if an async request is pending
       
   160  false otherwise
       
   161  */
       
   162 /*bool ServiceIPCClient::requestPending()
       
   163 {
       
   164     return m_asyncIPC->requestPending();
       
   165 }*/
       
   166 
       
   167 /*!
       
   168  Reads all data pending in the buffer.\n
       
   169  For Async version it will return the currently read buffer.\n
       
   170  Use only after readyRead() signal is emitted. However partial data can be read
       
   171  */
       
   172 void ServiceIPCClient::handleAsyncMsg()
       
   173 {
       
   174     // Read the results
       
   175     QByteArray ipcResult(m_asyncIPC->readAll());
       
   176     
       
   177     if (!ipcResult.isEmpty()) {
       
   178         emit readAsync(ipcResult);  
       
   179     }
       
   180 }
       
   181 
       
   182 /*!
       
   183  Client side interface to subscribe broadcast message
       
   184  */
       
   185 void ServiceIPCClient::subscribeBroadcastMsg() 
       
   186 {
       
   187     QString null;
       
   188     m_broadcastIPC->sendAsync(SUBSCRIBEBROADCASTMSG, null.toAscii());
       
   189 }
       
   190 
       
   191 /*!
       
   192  Reads all data pending in the buffer.\n
       
   193  For broadcast version it will return the currently read buffer.\n
       
   194  Use only after readyRead() signal is emitted. However partial data can be read
       
   195  */
       
   196 void ServiceIPCClient::handleBroadcastMsg()
       
   197 {
       
   198     // Read the results
       
   199     QByteArray ipcResult(m_broadcastIPC->readAll());
       
   200     
       
   201     if (!ipcResult.isEmpty()) {
       
   202         emit handleMessage(ipcResult);  
       
   203         subscribeBroadcastMsg(); 
       
   204     }
       
   205 }
       
   206 
       
   207 }
       
   208 
       
   209 
       
   210 /*!
       
   211  \fn WRT::ServiceIPCClient::error( int aError )
       
   212  
       
   213  Signal emitted to handle any errors
       
   214  @param aError error code
       
   215  @note: For local socket implementation, the error can be interpreted 
       
   216  as QLocalSocket::LocalSocketError
       
   217  @return void
       
   218  */
       
   219 
       
   220 /*!
       
   221  \fn WRT::ServiceIPCClient::readyRead()
       
   222 
       
   223  Handle when a reply has been received for async requests\n
       
   224  Emitted when the entire data packet has been received
       
   225  @return void
       
   226  */
       
   227 // END OF FILE