utilities/serviceipcclient/serviceipc.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 "serviceipc.h"
       
    19 #include "serviceipc_p.h"
       
    20 #include "serviceipcfactory.h"
       
    21 
       
    22 namespace WRT
       
    23 {
       
    24 
       
    25 /*!
       
    26  \class ServiceFwIPC
       
    27  Public interface class for IPC operations
       
    28  */
       
    29 
       
    30 /*!
       
    31  Destructor
       
    32  */
       
    33 ServiceFwIPC::~ServiceFwIPC()
       
    34 {
       
    35     delete d;
       
    36 }
       
    37 
       
    38 /*!
       
    39  Constructor
       
    40  @param aBackend IPC backend to use
       
    41  @param aParent Parent to this QObject
       
    42  */
       
    43 ServiceFwIPC::ServiceFwIPC(QObject* aParent, TServiceIPCBackends aBackend)
       
    44     : QObject(aParent)
       
    45     , m_AsyncRequestPending(false)
       
    46 {
       
    47     // Private implementation pattern
       
    48     //
       
    49     d = ServiceFwIPCFactory::createBackend(aBackend);
       
    50     d->q = this;
       
    51 }
       
    52 
       
    53 /*!
       
    54  Connect to the server
       
    55  @param aServerName name of the server to connect to
       
    56  @return true if connected, false if not
       
    57  */
       
    58 bool ServiceFwIPC::connect(const QString& aServerName)
       
    59 {
       
    60     return d->connect(aServerName);
       
    61 }
       
    62 
       
    63 /*!
       
    64  Disconnect from the server
       
    65  @return void
       
    66  */
       
    67 void ServiceFwIPC::disconnect()
       
    68 {
       
    69     d->disconnect();
       
    70 }
       
    71 
       
    72 /*!
       
    73  Starts the service
       
    74  @param aServerName name of the server
       
    75  @param aExeName executable of the server
       
    76  @return true if connected
       
    77  */
       
    78 bool ServiceFwIPC::startServer(const QString& aServerName,
       
    79                                const QString& aExeName)
       
    80 {
       
    81     return d->startServer(aServerName, aExeName);
       
    82 }
       
    83 
       
    84 /*!
       
    85  Send a request synchronously
       
    86  @param aRequestType name of the request
       
    87  @param aData data to send
       
    88  @return true if sent successful, otherwise false
       
    89  */
       
    90 bool ServiceFwIPC::sendSync(const QString& aRequestType,
       
    91                             const QByteArray& aData)
       
    92 {
       
    93 #ifdef _DEBUG
       
    94     Q_ASSERT_X( aRequestType.contains(";") == false, "", "aRequestType cannot contain semicolons!" );
       
    95 #endif // _DEBUG
       
    96     bool sent = d->sendSync(aRequestType, aData);
       
    97     if (sent) {
       
    98         sent = waitForRead();
       
    99     }
       
   100     return sent;
       
   101 }
       
   102 
       
   103 /*!
       
   104  Send a request asynchronously
       
   105  @param aRequestType name of the request
       
   106  @param aData data to send
       
   107  @note Errors will be emitted via errors() signal
       
   108  @return void
       
   109  */
       
   110 void ServiceFwIPC::sendAsync(const QString& aRequestType,
       
   111                              const QByteArray& aData)
       
   112 {
       
   113 #ifdef _DEBUG
       
   114     Q_ASSERT_X( aRequestType.contains(";") == false, "", "aRequestType cannot contain semicolons!" );
       
   115 #endif // _DEBUG
       
   116     d->sendAsync(aRequestType, aData);
       
   117     m_AsyncRequestPending = true;
       
   118 }
       
   119 
       
   120 /*!
       
   121  Reads all data pending in the buffer.\n
       
   122  For Sync version this will wait until all of the data is available.\n
       
   123  For Async version it will return the currently read buffer.\n
       
   124  Use only after readyRead() signal is emitted. However partial data can be read
       
   125  @return QByteArray of results
       
   126  */
       
   127 QByteArray ServiceFwIPC::readAll()
       
   128 {
       
   129     return d->readAll();
       
   130 }
       
   131 
       
   132 /*!
       
   133  Waits until data is available for reading
       
   134  @return true if data is available for reading
       
   135  */
       
   136 bool ServiceFwIPC::waitForRead()
       
   137 {
       
   138     return d->waitForRead();
       
   139 }
       
   140 
       
   141 /*!
       
   142  Check if an async request is already pending
       
   143  @return true if an async request is pending
       
   144  false otherwise
       
   145  */
       
   146 bool ServiceFwIPC::requestPending()
       
   147 {
       
   148     return m_AsyncRequestPending;
       
   149 }
       
   150 
       
   151 /*!
       
   152  Retrieve the session id synchronously
       
   153  @param aSessionId name of the request
       
   154  @return true if retrieved successfully, otherwise false
       
   155  */
       
   156 bool ServiceFwIPC::getSessionId(int& aSessionId)
       
   157 {
       
   158     QByteArray data;
       
   159     bool ret(sendSync(GETSESSIONID, data));
       
   160     if (ret)
       
   161         data = d->readAll();
       
   162     aSessionId = data.toInt();
       
   163     return ret;
       
   164 }
       
   165 
       
   166 /*!
       
   167  Set the session id synchronously
       
   168  @param aSessionId name of the request
       
   169  @return true if set successfully, otherwise false
       
   170  */
       
   171 bool ServiceFwIPC::setSessionId(int aSessionId)
       
   172 {
       
   173     bool ret(sendSync(SETSESSIONINFO, QByteArray::number(aSessionId)));
       
   174     if (ret)
       
   175         d->readAll(); //clean buffer
       
   176     return ret;
       
   177 }
       
   178 
       
   179 } // end of namespace
       
   180 
       
   181 /*!
       
   182  \fn WRT::ServiceFwIPC::error( int aError )
       
   183 
       
   184  Signal emitted to handle any errors
       
   185  @param aError error code
       
   186  @note: For local socket implementation, the error can be interpreted
       
   187  as QLocalSocket::LocalSocketError
       
   188  @return void
       
   189  */
       
   190 
       
   191 /*!
       
   192  \fn WRT::ServiceFwIPC::readyRead()
       
   193 
       
   194  Handle when a reply has been received for async requests\n
       
   195  Emitted when the entire data packet has been received
       
   196  @return void
       
   197  */
       
   198 // END OF FILE