|
1 /** |
|
2 * Copyright (c) 2010 Sasken Communication Technologies Ltd. |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html" |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Chandradeep Gandhi, Sasken Communication Technologies Ltd - Initial contribution |
|
11 * |
|
12 * Contributors: |
|
13 * Manasij Roy, Nalina Hariharan |
|
14 * |
|
15 * |
|
16 * Description: Session implementation for Qt desktop builds |
|
17 * |
|
18 */ |
|
19 |
|
20 #include "smfserverqtsession.h" |
|
21 #include "smfserverqt_p.h" |
|
22 #include "smfserver.h" |
|
23 |
|
24 SmfServerQtSession::SmfServerQtSession(QLocalSocket *clientConnection, SmfServerQt *server) |
|
25 : m_opCode(-1), m_clientConnection(clientConnection), m_server(server) |
|
26 { |
|
27 connect(m_clientConnection, SIGNAL(readyRead()), this, SLOT(readDataFromClient())); |
|
28 connect(m_clientConnection, SIGNAL(error(QLocalSocket::LocalSocketError)), |
|
29 this, SLOT(socketError(QLocalSocket::LocalSocketError))); |
|
30 } |
|
31 |
|
32 SmfServerQtSession::~SmfServerQtSession() |
|
33 { |
|
34 // 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 delete m_clientConnection; |
|
37 } |
|
38 |
|
39 void SmfServerQtSession::readDataFromClient() |
|
40 { |
|
41 // TODO: This needs to be much safer. |
|
42 if(m_clientConnection->bytesAvailable() < sizeof(typeof(SmfRequestTypeID))) |
|
43 { |
|
44 // Don't read yet, haven't received opcode |
|
45 return; |
|
46 } |
|
47 |
|
48 if (m_opCode == -1) |
|
49 { |
|
50 QDataStream in(m_clientConnection); |
|
51 in >> m_opCode; |
|
52 } |
|
53 |
|
54 // m_opCode set, so handle request |
|
55 handleRequest(); |
|
56 } |
|
57 |
|
58 /** |
|
59 * Call the appropriate handler function |
|
60 */ |
|
61 void SmfServerQtSession::handleRequest() |
|
62 { |
|
63 switch (m_opCode) |
|
64 { |
|
65 case SmfGetService: |
|
66 handleGetService(); |
|
67 break; |
|
68 default: |
|
69 m_server->writeLog(QString("Bad Request received: ") + m_opCode); |
|
70 } |
|
71 } |
|
72 |
|
73 /** |
|
74 * Find available services for specified interface and return list to client. |
|
75 */ |
|
76 void SmfServerQtSession::handleGetService() |
|
77 { |
|
78 m_server->writeLog("SmfServerQtSession::handleGetService()"); |
|
79 |
|
80 QDataStream in(m_clientConnection); |
|
81 |
|
82 // Get interface type requested |
|
83 SmfInterfaceID ifName; |
|
84 in >> ifName; |
|
85 m_server->writeLog("Requested: " + ifName); |
|
86 |
|
87 // Get the available services for this interface |
|
88 QMap<SmfPluginID, SmfProvider> services; |
|
89 m_server->wrapper()->getPlugins(ifName, services); |
|
90 |
|
91 // write back the results |
|
92 QDataStream out(m_clientConnection); |
|
93 out << services; |
|
94 } |
|
95 |
|
96 void SmfServerQtSession::clientAuthorizationFinished(bool success) |
|
97 { |
|
98 Q_UNUSED(success); |
|
99 } |
|
100 |
|
101 void SmfServerQtSession::socketError(QLocalSocket::LocalSocketError error) |
|
102 { |
|
103 switch (error) |
|
104 { |
|
105 case QLocalSocket::PeerClosedError: |
|
106 m_server->writeLog("Peer closed connection."); |
|
107 break; |
|
108 case QLocalSocket::SocketTimeoutError: |
|
109 m_server->writeLog("error: connection timed out."); |
|
110 break; |
|
111 default: |
|
112 m_server->writeLog("error: unkown socket error: " + error); |
|
113 break; |
|
114 } |
|
115 } |
|
116 |