qthighway/xqserviceipc/xqserviceipc/xqserviceipc.cpp
changeset 1 2b40d63a9c3d
child 24 9d760f716ca8
equal deleted inserted replaced
0:cfcbf08528c4 1:2b40d63a9c3d
       
     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  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 aBackend IPC backend to use
       
    48  @param aParent Parent to this QObject
       
    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  @return void
       
    74  */
       
    75 void ServiceFwIPC::disconnect()
       
    76 {
       
    77     XQSERVICE_DEBUG_PRINT("ServiceFwIPC::disconnect");
       
    78     d->disconnect();
       
    79 }
       
    80 
       
    81 /*!
       
    82  Starts the service
       
    83  @param aServerName name of the server
       
    84  @param aExeName executable of the server
       
    85  @return true if connected
       
    86  */
       
    87 bool ServiceFwIPC::startServer(const QString& aServerName,
       
    88                                   const QString& aExeName,
       
    89                                   quint64& processId,
       
    90                                   ServiceIPCStartServerOptions options)
       
    91 {
       
    92     XQSERVICE_DEBUG_PRINT("ServiceFwIPC::startServer");
       
    93     return d->startServer(aServerName, aExeName, processId, options);
       
    94 }
       
    95 
       
    96 /*!
       
    97  Send a request synchronously
       
    98  @param aRequestType name of the request
       
    99  @param aData data to send
       
   100  @return true if sent successful, otherwise false
       
   101  */
       
   102 bool ServiceFwIPC::sendSync(const QString& aRequestType,
       
   103                             const QByteArray& aData)
       
   104 {
       
   105     XQSERVICE_DEBUG_PRINT("ServiceFwIPC::sendSync");
       
   106 #ifdef _DEBUG
       
   107     Q_ASSERT_X( aRequestType.contains(";") == false, "", "aRequestType cannot contain semicolons!" );
       
   108 #endif // _DEBUG
       
   109     bool sent = d->sendSync(aRequestType, aData);
       
   110     if (sent) {
       
   111         sent = waitForRead();
       
   112     }
       
   113     return sent;
       
   114 }
       
   115 
       
   116 
       
   117 /*!
       
   118  Send a request asynchronously
       
   119  @param aRequestType name of the request
       
   120  @param aData data to send
       
   121  @note Errors will be emitted via errors() signal
       
   122  @return void                                              
       
   123  */
       
   124 void ServiceFwIPC::sendAsync(const QString& aRequestType,
       
   125                              const QByteArray& aData)
       
   126 {
       
   127     XQSERVICE_DEBUG_PRINT("ServiceFwIPC::sendAsync");
       
   128 #ifdef _DEBUG
       
   129     Q_ASSERT_X( aRequestType.contains(";") == false, "", "aRequestType cannot contain semicolons!" );
       
   130 #endif // _DEBUG
       
   131     d->sendAsync(aRequestType, aData);
       
   132     iAsyncRequestPending = true;
       
   133 }
       
   134 
       
   135 /**
       
   136 * Async read
       
   137  */
       
   138 void ServiceFwIPC::readAll(QByteArray& aArray)
       
   139 {
       
   140     XQSERVICE_DEBUG_PRINT("ServiceFwIPC::readAll(1)");
       
   141     d->readAll(aArray);
       
   142 }
       
   143 
       
   144 /**
       
   145 * sync read
       
   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  @return void
       
   192  */
       
   193 
       
   194 /*!
       
   195  \fn QtService::ServiceFwIPC::readyRead()
       
   196 
       
   197  Handle when a reply has been received for async requests\n
       
   198  Emitted when the entire data packet has been received
       
   199  @return void
       
   200  */
       
   201 // END OF FILE