qthighway/xqserviceipc/xqserviceipc/xqserviceipc.cpp
branchRCL_3
changeset 10 cd2778e5acfe
parent 9 5d007b20cfd0
child 11 19a54be74e5e
equal deleted inserted replaced
9:5d007b20cfd0 10:cd2778e5acfe
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * This program is free software: you can redistribute it and/or modify
       
     6 * it under the terms of the GNU Lesser General Public License as published by
       
     7 * the Free Software Foundation, version 2.1 of the License.
       
     8 * 
       
     9 * This program is distributed in the hope that it will be useful,
       
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12 * GNU Lesser General Public License for more details.
       
    13 *
       
    14 * You should have received a copy of the GNU Lesser General Public License
       
    15 * along with this program.  If not, 
       
    16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
       
    17 *
       
    18 * Description:  IPC Client side implementation
       
    19 *
       
    20 */
       
    21 
       
    22 #include "xqservicelog.h"
       
    23 
       
    24 #include "xqserviceipc.h"
       
    25 #include "xqserviceipc_p.h"
       
    26 #include "xqserviceipcfactory.h"
       
    27 
       
    28 namespace QtService
       
    29 {
       
    30 
       
    31 /*!
       
    32     \class ServiceFwIPC
       
    33     \brief Public interface class for IPC operations
       
    34 */
       
    35 
       
    36 /*!
       
    37     Destructor
       
    38 */
       
    39 ServiceFwIPC::~ServiceFwIPC()
       
    40 {
       
    41     XQSERVICE_DEBUG_PRINT("ServiceFwIPC::~ServiceFwIPC");
       
    42     delete d;
       
    43 }
       
    44 
       
    45 /*!
       
    46     Constructor.
       
    47     \param aParent Parent to this QObject
       
    48     \param aBackend IPC backend to use
       
    49 */
       
    50 ServiceFwIPC::ServiceFwIPC(QObject* aParent, TServiceIPCBackends aBackend) :
       
    51                            QObject(aParent), iAsyncRequestPending(false)
       
    52 {
       
    53     XQSERVICE_DEBUG_PRINT("ServiceFwIPC::ServiceFwIPC");
       
    54     // Private implementation pattern
       
    55     //
       
    56     d = ServiceFwIPCFactory::createBackend(aBackend);
       
    57     d->q = this;
       
    58 }
       
    59 
       
    60 /*!
       
    61     Connect to the server.
       
    62     \param aServerName name of the server to connect to.
       
    63     \return true if connected, false if not.
       
    64 */
       
    65 bool ServiceFwIPC::connect(const QString& aServerName)
       
    66 {
       
    67     XQSERVICE_DEBUG_PRINT("ServiceFwIPC::connect");
       
    68     return d->connect(aServerName);
       
    69 }
       
    70 
       
    71 /*!
       
    72     Disconnect from the server.
       
    73 */
       
    74 void ServiceFwIPC::disconnect()
       
    75 {
       
    76     XQSERVICE_DEBUG_PRINT("ServiceFwIPC::disconnect");
       
    77     d->disconnect();
       
    78 }
       
    79 
       
    80 /*!
       
    81     Starts the service.
       
    82     \param aServerName Name of the server.
       
    83     \param aExeName Executable of the server.
       
    84     \return true if connected.
       
    85 */
       
    86 bool ServiceFwIPC::startServer(const QString& aServerName,
       
    87                                   const QString& aExeName,
       
    88                                   quint64& processId,
       
    89                                   ServiceIPCStartServerOptions options)
       
    90 {
       
    91     XQSERVICE_DEBUG_PRINT("ServiceFwIPC::startServer");
       
    92     return d->startServer(aServerName, aExeName, processId, options);
       
    93 }
       
    94 
       
    95 /*!
       
    96     Send a request synchronously.
       
    97     \param aRequestType Name of the request.
       
    98     \param aData Data to send.
       
    99     \return true if sent successful, otherwise false.
       
   100 */
       
   101 bool ServiceFwIPC::sendSync(const QString& aRequestType,
       
   102                             const QByteArray& aData)
       
   103 {
       
   104     XQSERVICE_DEBUG_PRINT("ServiceFwIPC::sendSync");
       
   105 #ifdef _DEBUG
       
   106     Q_ASSERT_X( aRequestType.contains(";") == false, "", "aRequestType cannot contain semicolons!" );
       
   107 #endif // _DEBUG
       
   108     bool sent = d->sendSync(aRequestType, aData);
       
   109     if (sent) {
       
   110         sent = waitForRead();
       
   111     }
       
   112     return sent;
       
   113 }
       
   114 
       
   115 
       
   116 /*!
       
   117     Send a request asynchronously.
       
   118     \param aRequestType Name of the request.
       
   119     \param aData Data to send.
       
   120     \note Errors will be emitted via error() signal.
       
   121 */
       
   122 void ServiceFwIPC::sendAsync(const QString& aRequestType,
       
   123                              const QByteArray& aData)
       
   124 {
       
   125     XQSERVICE_DEBUG_PRINT("ServiceFwIPC::sendAsync");
       
   126 #ifdef _DEBUG
       
   127     Q_ASSERT_X( aRequestType.contains(";") == false, "", "aRequestType cannot contain semicolons!" );
       
   128 #endif // _DEBUG
       
   129     d->sendAsync(aRequestType, aData);
       
   130     iAsyncRequestPending = true;
       
   131 }
       
   132 
       
   133 /*!
       
   134     Asynchronous read.
       
   135     \param aArray Array where read data will be put.
       
   136 */
       
   137 void ServiceFwIPC::readAll(QByteArray& aArray)
       
   138 {
       
   139     XQSERVICE_DEBUG_PRINT("ServiceFwIPC::readAll(1)");
       
   140     d->readAll(aArray);
       
   141 }
       
   142 
       
   143 /*!
       
   144     Synchronous read.
       
   145     \return Array which where read data is put.
       
   146 */
       
   147 QByteArray ServiceFwIPC::readAll()
       
   148 {
       
   149     XQSERVICE_DEBUG_PRINT("ServiceFwIPC::readAll(2)");
       
   150     return d->readAll();
       
   151 }
       
   152 
       
   153 /*!
       
   154     Waits until data is available for reading.
       
   155     \return true if data is available for reading.
       
   156 */
       
   157 bool ServiceFwIPC::waitForRead()
       
   158 {
       
   159     XQSERVICE_DEBUG_PRINT("ServiceFwIPC::waitForRead");
       
   160     return d->waitForRead();
       
   161 }
       
   162 
       
   163 /*!
       
   164     Check if an async request is already pending.
       
   165     \return true if an async request is pending,
       
   166             false otherwise.
       
   167 */
       
   168 bool ServiceFwIPC::requestPending()
       
   169 {
       
   170     XQSERVICE_DEBUG_PRINT("ServiceFwIPC::requestPending");
       
   171     return iAsyncRequestPending;
       
   172 }
       
   173 
       
   174 void ServiceFwIPC::setUserData(const void *data)
       
   175 {
       
   176     XQSERVICE_DEBUG_PRINT("ServiceFwIPC::setUserData: %x", (int)data);
       
   177     d->setUserData(data);
       
   178 }
       
   179 
       
   180 }
       
   181 
       
   182 
       
   183 
       
   184 /*!
       
   185     \fn QtService::ServiceFwIPC::error( int aError )
       
   186  
       
   187     Signal emitted to handle any errors.
       
   188     \param aError error code
       
   189     \note For local socket implementation, the error can be interpreted 
       
   190           as QLocalSocket::LocalSocketError
       
   191 */
       
   192 
       
   193 /*!
       
   194     \fn QtService::ServiceFwIPC::readyRead()
       
   195 
       
   196     Handle when a reply has been received for async requests.
       
   197     Emitted when the entire data packet has been received
       
   198 */
       
   199 
       
   200 /*!
       
   201     \fn QtService::ServiceFwIPC::ReadDone()
       
   202 
       
   203     Emitted when reading has completed.
       
   204 */
       
   205 // END OF FILE