smf/smfservermodule/smfserver/server/smfserverqtsession.cpp
changeset 18 013a02bf2bb0
parent 13 b5d63d5fc252
equal deleted inserted replaced
17:106a4bfcb866 18:013a02bf2bb0
    10  * Chandradeep Gandhi, Sasken Communication Technologies Ltd - Initial contribution
    10  * Chandradeep Gandhi, Sasken Communication Technologies Ltd - Initial contribution
    11  *
    11  *
    12  * Contributors:
    12  * Contributors:
    13  * Manasij Roy, Nalina Hariharan
    13  * Manasij Roy, Nalina Hariharan
    14  *
    14  *
    15  *
       
    16  * Description: Session implementation for Qt desktop builds
    15  * Description: Session implementation for Qt desktop builds
    17  *
    16  *
    18  */
    17  */
       
    18 
       
    19 #include <qdebug.h>
    19 
    20 
    20 #include "smfserverqtsession.h"
    21 #include "smfserverqtsession.h"
    21 #include "smfserverqt_p.h"
    22 #include "smfserverqt_p.h"
    22 #include "smfserver.h"
    23 #include "smfserver.h"
    23 
    24 
    24 SmfServerQtSession::SmfServerQtSession(QLocalSocket *clientConnection, SmfServerQt *server)
    25 SmfServerQtSession::SmfServerQtSession(QLocalSocket *clientConnection, SmfServerQt *server)
    25     : m_opCode(-1), m_clientConnection(clientConnection), m_server(server)
    26     : m_opCode(-1), m_clientConnection(clientConnection), m_server(server)
    26 {
    27 	{
    27     connect(m_clientConnection, SIGNAL(readyRead()), this, SLOT(readDataFromClient()));
    28     connect(m_clientConnection, SIGNAL(readyRead()), this, SLOT(readDataFromClient()));
    28     connect(m_clientConnection, SIGNAL(error(QLocalSocket::LocalSocketError)),
    29     connect(m_clientConnection, SIGNAL(error(QLocalSocket::LocalSocketError)),
    29             this, SLOT(socketError(QLocalSocket::LocalSocketError)));
    30             this, SLOT(socketError(QLocalSocket::LocalSocketError)));
    30 }
    31 	}
    31 
    32 
    32 SmfServerQtSession::~SmfServerQtSession()
    33 SmfServerQtSession::~SmfServerQtSession()
    33 {
    34 	{
    34     // The socket has the QLocalServer as a parent, but it should be deleted to
    35     // The socket has the QLocalServer as a parent, but it should be deleted to
    35     // save unnecessary accumulation of these objects in memory. It won't be double deleted.
    36     // save unnecessary accumulation of these objects in memory. It won't be double deleted.
    36     delete m_clientConnection;
    37     delete m_clientConnection;
    37 }
    38 	}
    38 
    39 
    39 void SmfServerQtSession::readDataFromClient()
    40 void SmfServerQtSession::readDataFromClient()
    40 {
    41 	{
    41     // TODO: This needs to be much safer.
    42     // TODO: This needs to be much safer.
    42     if(m_clientConnection->bytesAvailable() < sizeof(typeof(SmfRequestTypeID)))
    43     if(m_clientConnection->bytesAvailable() < sizeof(typeof(SmfRequestTypeID)))
    43     {
    44     	{
    44         // Don't read yet, haven't received opcode
    45         // Don't read yet, haven't received opcode
    45         return;
    46         return;
    46     }
    47     	}
    47 
    48 
    48     if (m_opCode == -1)
    49     if (m_opCode == -1)
    49     {
    50     	{
    50         QDataStream in(m_clientConnection);
    51         QDataStream in(m_clientConnection);
    51         in >> m_opCode;
    52         in >> m_opCode;
    52     }
    53     	}
    53 
    54 
    54     // m_opCode set, so handle request
    55     // m_opCode set, so handle request
    55     handleRequest();
    56     handleRequest();
    56 }
    57 	}
    57 
    58 
    58 /**
    59 /**
    59  * Call the appropriate handler function
    60  * Call the appropriate handler function
    60  */
    61  */
    61 void SmfServerQtSession::handleRequest()
    62 void SmfServerQtSession::handleRequest()
    62 {
    63 	{
    63     switch (m_opCode)
    64     switch (m_opCode)
    64     {
    65 		{
    65     case SmfGetService:
    66 		case SmfGetService:
    66         handleGetService();
    67 			handleGetService();
    67         break;
    68 			break;
    68     default:
    69 		default:
    69         m_server->writeLog(QString("Bad Request received: ") + m_opCode);
    70 			qDebug()<<"Bad Request received =  "<<m_opCode);
    70     }
    71 		}
    71 }
    72 	}
    72 
    73 
    73 /**
    74 /**
    74  * Find available services for specified interface and return list to client.
    75  * Find available services for specified interface and return list to client.
    75  */
    76  */
    76 void SmfServerQtSession::handleGetService()
    77 void SmfServerQtSession::handleGetService()
    77 {
    78 	{
    78     m_server->writeLog("SmfServerQtSession::handleGetService()");
    79     qDebug()<<"SmfServerQtSession::handleGetService()";
    79 
    80 
    80     QDataStream in(m_clientConnection);
    81     QDataStream in(m_clientConnection);
    81 
    82 
    82     // Get interface type requested
    83     // Get interface type requested
    83     SmfInterfaceID ifName;
    84     SmfInterfaceID ifName;
    84     in >> ifName;
    85     in >> ifName;
    85     m_server->writeLog("Requested: " + ifName);
    86     qDebug()<<"Requested = "<<ifName;
    86 
    87 
    87     // Get the available services for this interface
    88     // Get the available services for this interface
    88     QMap<SmfPluginID, SmfProvider> services;
    89     QMap<SmfPluginID, SmfProvider> services;
    89     m_server->wrapper()->getPlugins(ifName, services);
    90     m_server->wrapper()->getPlugins(ifName, services);
    90 
    91 
    91     // write back the results
    92     // write back the results
    92     QDataStream out(m_clientConnection);
    93     QDataStream out(m_clientConnection);
    93     out << services;
    94     out << services;
    94 }
    95 	}
    95 
    96 
    96 void SmfServerQtSession::clientAuthorizationFinished(bool success)
    97 void SmfServerQtSession::clientAuthorizationFinished(bool success)
    97 {
    98 	{
    98     Q_UNUSED(success);
    99     Q_UNUSED(success);
    99 }
   100 	}
   100 
   101 
   101 void SmfServerQtSession::socketError(QLocalSocket::LocalSocketError error)
   102 void SmfServerQtSession::socketError(QLocalSocket::LocalSocketError error)
   102 {
   103 	{
   103     switch (error)
   104     switch (error)
   104     {
   105 		{
   105     case QLocalSocket::PeerClosedError:
   106 		case QLocalSocket::PeerClosedError:
   106         m_server->writeLog("Peer closed connection.");
   107 			qDebug()<<"Peer closed connection.";
   107         break;
   108 			break;
   108     case QLocalSocket::SocketTimeoutError:
   109 		case QLocalSocket::SocketTimeoutError:
   109         m_server->writeLog("error: connection timed out.");
   110 			qDebug()<<"error: connection timed out.";
   110         break;
   111 			break;
   111     default:
   112 		default:
   112         m_server->writeLog("error: unkown socket error: " + error);
   113 			qDebug()<<"error: unkown socket error: "<<error;
   113         break;
   114 			break;
   114     }
   115 		}
   115 }
   116 	}
   116