smf/smfservermodule/smfclient/client/smfclientqt.cpp
changeset 13 b5d63d5fc252
parent 10 77a56c951f86
equal deleted inserted replaced
12:5bed45b14781 13:b5d63d5fc252
    13  * Manasij Roy, Nalina Hariharan
    13  * Manasij Roy, Nalina Hariharan
    14  */
    14  */
    15 
    15 
    16 #include "smfclientqt.h"
    16 #include "smfclientqt.h"
    17 
    17 
    18 SmfClientQt::SmfClientQt(QObject *parent) :
    18 SmfClientQt::SmfClientQt(QObject *parent)
    19     QObject(parent)
    19     : QObject(parent)
    20 {
    20 {
       
    21     m_serverConnection = new QLocalSocket();
       
    22 
       
    23     connect(m_serverConnection, SIGNAL(connected()), this, SLOT(connectionEstablished()));
       
    24     connect(m_serverConnection, SIGNAL(readyRead()), this, SLOT(readIncomingData()));
       
    25     connect(m_serverConnection, SIGNAL(error(QLocalSocket::LocalSocketError)),
       
    26             this, SLOT(handleError(QLocalSocket::LocalSocketError)));
       
    27 
       
    28     m_serverConnection->connectToServer("SmfServerQt", QIODevice::ReadWrite);
       
    29 }
       
    30 
       
    31 SmfClientQt::~SmfClientQt()
       
    32 {
       
    33     m_serverConnection->close();
       
    34     delete m_serverConnection;
    21 }
    35 }
    22 
    36 
    23 /**
    37 /**
    24  * Send a request to the server.
    38  * Send a request to the server.
    25  * @param aSerializedData serialized by the caller.
    39  * @param aSerializedData serialized by the caller.
    26  * @param aInterfaceName Interface name
    40  * @param aInterfaceName Interface name
    27  * @param requestType Opcode
    41  * @param requestType Opcode
    28  */
    42  */
    29 int SmfClientQt::sendRequest(QByteArray& aSerializedData, QString aInterfaceName,
    43 int SmfClientQt::sendRequest(QByteArray& serializedData, QString interfaceName,
    30                              SmfRequestTypeID requestType)
    44                              SmfRequestTypeID requestType)
    31 {
    45 {
    32 
    46     QDataStream out(m_serverConnection);
       
    47     out << requestType;
       
    48     out << interfaceName;
       
    49     out << serializedData.size();
       
    50     out << serializedData;
    33 }
    51 }
    34 
    52 
    35 /**
    53 /**
    36  * This overloaded API is for ESmfGetServices, where data should be
    54  * This overloaded API is for ESmfGetServices, where data should be
    37  * fetched synchronously
    55  * fetched synchronously
    38  */
    56  */
    39 QByteArray SmfClientQt::sendRequest(QString aInterfaceName,
    57 QByteArray SmfClientQt::sendRequest(QString interfaceName,
    40             SmfRequestTypeID requestType)
    58             SmfRequestTypeID requestType)
    41 {
    59 {
       
    60     QDataStream out(m_serverConnection);
       
    61     out << requestType;
       
    62     out << interfaceName;
    42 
    63 
       
    64     // TODO: This needs to be asynchronous. Remove this wait when user API is updated.
       
    65     m_serverConnection->waitForBytesWritten(-1);
       
    66 
       
    67     QByteArray in;
       
    68     out >> in;
       
    69     return in;
    43 }
    70 }
    44 
    71 
    45 /**
    72 /**
    46  * For testing purpose only
    73  * For testing purpose only
    47  */
    74  */
    48 int SmfClientQt::sendDummyRequest(QByteArray* provider,QString aInterfaceName,
    75 int SmfClientQt::sendDummyRequest(QByteArray* provider,QString aInterfaceName,
    49             SmfRequestTypeID requestType)
    76             SmfRequestTypeID requestType)
    50 {
    77 {
    51 
    78     Q_UNUSED(provider);
       
    79     Q_UNUSED(aInterfaceName);
       
    80     Q_UNUSED(requestType);
    52 }
    81 }
    53 
    82 
    54 /**
    83 /**
    55 * CancelRequest.
    84 * CancelRequest.
    56 * Cancels an outstanding request.
    85 * Cancels an outstanding request.
    57 */
    86 */
    58 void SmfClientQt::CancelRequest()
    87 void SmfClientQt::CancelRequest()
    59 {
    88 {
    60 
    89 
    61 }
    90 }
       
    91 
       
    92 void SmfClientQt::connectionEstablished()
       
    93 {
       
    94     qDebug() << "Connected to server successfully.";
       
    95 }
       
    96 
       
    97 void SmfClientQt::readIncomingData()
       
    98 {
       
    99 }
       
   100 
       
   101 void SmfClientQt::handleError(QLocalSocket::LocalSocketError error)
       
   102 {
       
   103     switch(error)
       
   104     {
       
   105     case QLocalSocket::ServerNotFoundError:
       
   106         qDebug() << "Server not found.";
       
   107         break;
       
   108     default:
       
   109         qDebug() << "Unhandled socket error";
       
   110         break;
       
   111     }
       
   112 }