~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to libs/solid/control/networking.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  This file is part of the KDE project
 
2
    Copyright (C) 2006-2007 Will Stephenson <wstephenson@kde.org>
 
3
    Copyright (C) 2006-2007 Kevin Ottens <ervin@kde.org>
 
4
 
 
5
    This library is free software; you can redistribute it and/or
 
6
    modify it under the terms of the GNU Library General Public
 
7
    License version 2 as published by the Free Software Foundation.
 
8
 
 
9
    This library 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 GNU
 
12
    Library General Public License for more details.
 
13
 
 
14
    You should have received a copy of the GNU Library General Public License
 
15
    along with this library; see the file COPYING.LIB.  If not, write to
 
16
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
    Boston, MA 02110-1301, USA.
 
18
 
 
19
*/
 
20
 
 
21
#include <QtNetwork/QAbstractSocket>
 
22
#include <QtCore/QTimer>
 
23
 
 
24
#include <kglobal.h>
 
25
 
 
26
#include "networking_p.h"
 
27
#include "networking.h"
 
28
#include "org_kde_solid_networking.h"
 
29
 
 
30
K_GLOBAL_STATIC(Solid::Control::NetworkingPrivate, globalNetworkControl)
 
31
 
 
32
Solid::Control::NetworkingPrivate::NetworkingPrivate() : iface( 
 
33
        new OrgKdeSolidNetworkingInterface( "org.kde.Solid.Networking",
 
34
            "/status",
 
35
            QDBusConnection::sessionBus(),
 
36
            this ) )
 
37
{
 
38
}
 
39
 
 
40
Solid::Control::NetworkingPrivate::~NetworkingPrivate()
 
41
{
 
42
}
 
43
 
 
44
uint Solid::Control::NetworkingPrivate::requestConnection( QObject * receiver, const char * member )
 
45
{
 
46
    connect( this, SIGNAL( connectionResult( bool ) ), receiver, member );
 
47
    return iface->requestConnection();
 
48
}
 
49
 
 
50
void Solid::Control::NetworkingPrivate::releaseConnection()
 
51
{
 
52
    iface->releaseConnection();
 
53
}
 
54
 
 
55
Solid::Control::Networking::Result Solid::Control::NetworkingPrivate::beginManagingSocket( QAbstractSocket * socket, int autoDisconnectTimeout )
 
56
{
 
57
    mManagedSockets.insert( socket, new ManagedSocketContainer( socket, autoDisconnectTimeout ) );
 
58
    return Solid::Control::Networking::Accepted;
 
59
}
 
60
 
 
61
void Solid::Control::NetworkingPrivate::stopManagingSocket( QAbstractSocket * socket )
 
62
{
 
63
    ManagedSocketContainer * removed = mManagedSockets.take( socket );
 
64
    delete removed;
 
65
}
 
66
 
 
67
Solid::Control::Networking::Result Solid::Control::Networking::requestConnection( QObject * receiver, const char * member )
 
68
{
 
69
    return static_cast<Solid::Control::Networking::Result>( globalNetworkControl->requestConnection( receiver, member ) );
 
70
}
 
71
 
 
72
void Solid::Control::Networking::releaseConnection()
 
73
{
 
74
    globalNetworkControl->releaseConnection();
 
75
}
 
76
 
 
77
/*=========================================================================*/
 
78
 
 
79
 
 
80
Solid::Control::ManagedSocketContainer::ManagedSocketContainer( QAbstractSocket * socket, int autoDisconnectTimeout ) : mSocket( socket ), mAutoDisconnectTimer( 0 )
 
81
{
 
82
    if ( autoDisconnectTimeout >= 0 )
 
83
    {
 
84
        mAutoDisconnectTimer = new QTimer( this );
 
85
        mAutoDisconnectTimer->setSingleShot( true );
 
86
        mAutoDisconnectTimer->setInterval( autoDisconnectTimeout );
 
87
        connect( mAutoDisconnectTimer, SIGNAL( timeout() ), SLOT( autoDisconnect() ) );
 
88
    }
 
89
    // react to network management events
 
90
    connect( Solid::Networking::notifier(), SIGNAL( statusChanged( uint ) ), this, SLOT( networkStatusChanged( Networking::Status ) ) );
 
91
 
 
92
    if ( socket )
 
93
    {
 
94
        // react to socket events
 
95
        connect( socket, SIGNAL( destroyed() ), SLOT( socketDestroyed() ) );
 
96
        connect( socket, SIGNAL( error( QAbstractSocket::SocketError ) ), SLOT( socketError( QAbstractSocket::SocketError ) ) );
 
97
        connect( socket, SIGNAL( stateChanged( QAbstractSocket::SocketState ) ), SLOT( socketStateChanged( QAbstractSocket::SocketState ) ) );
 
98
        // initialise our state from that of the socket
 
99
        switch ( socket->state() )
 
100
        {
 
101
            case QAbstractSocket::UnconnectedState:
 
102
                mState = SocketUnconnected;
 
103
                break;
 
104
            case QAbstractSocket::HostLookupState:
 
105
            case QAbstractSocket::ConnectingState:
 
106
                mState = SocketConnecting;
 
107
                break;
 
108
            case QAbstractSocket::ConnectedState:
 
109
            case QAbstractSocket::ClosingState:
 
110
                mState = SocketConnected;
 
111
                break;
 
112
            default:
 
113
                mState = SocketUnconnected;
 
114
        }
 
115
    }
 
116
}
 
117
 
 
118
void Solid::Control::ManagedSocketContainer::networkStatusChanged( Solid::Networking::Status netStatus )
 
119
{
 
120
    switch ( mState )
 
121
    {
 
122
        case SocketUnconnected:
 
123
            break;
 
124
        case SocketConnecting:
 
125
            break;
 
126
        case AwaitingNetworkConnection:
 
127
            switch ( netStatus )
 
128
            {
 
129
                case Solid::Networking::Connected:
 
130
                    performConnectToHost();
 
131
                    break;
 
132
                default:
 
133
                    //do nothing
 
134
                    ;
 
135
            }
 
136
            break;
 
137
        case SocketConnected:
 
138
            switch ( netStatus )
 
139
            {
 
140
                case Solid::Networking::Unconnected:
 
141
                case Solid::Networking::Disconnecting:
 
142
                    mState = DisconnectWait;
 
143
                    if ( mAutoDisconnectTimer )
 
144
                    {
 
145
                        mAutoDisconnectTimer->start();
 
146
                    }
 
147
                    break;
 
148
                default:
 
149
                    // do nothing
 
150
                    ;
 
151
            }
 
152
            break;
 
153
        case DisconnectWait:
 
154
            switch ( netStatus )
 
155
            {
 
156
                case Solid::Networking::Connected:
 
157
                    // RECOVERED
 
158
                    mState = SocketConnected;
 
159
                    if ( mAutoDisconnectTimer )
 
160
                    {
 
161
                        mAutoDisconnectTimer->stop();
 
162
                    }
 
163
                    break;
 
164
                default:
 
165
                    // do nothing
 
166
                    ;
 
167
            }
 
168
            break;
 
169
    }
 
170
}
 
171
 
 
172
void Solid::Control::ManagedSocketContainer::socketError( QAbstractSocket::SocketError socketError )
 
173
{
 
174
    switch ( mState )
 
175
    {
 
176
        case SocketUnconnected:
 
177
            break;
 
178
        case SocketConnecting:
 
179
            switch ( socketError )
 
180
            {
 
181
                case QAbstractSocket::HostNotFoundError:
 
182
                case QAbstractSocket::NetworkError:
 
183
                    // socket tried to resolve and failed
 
184
                    // Either the host doesn't exist at all
 
185
                    // or the resolve failed because we're offline, so request that we go online
 
186
                    if ( Solid::Networking::status() != Solid::Networking::Connected )
 
187
                    {
 
188
                        mState = AwaitingNetworkConnection;
 
189
                        globalNetworkControl->requestConnection();
 
190
                    }
 
191
                    else
 
192
                    {
 
193
                        mState = SocketUnconnected;
 
194
                    }
 
195
                    break;
 
196
                default:
 
197
                    mState = SocketUnconnected;
 
198
            }
 
199
            break;
 
200
        case AwaitingNetworkConnection:
 
201
        case SocketConnected:
 
202
            // setup automatic reconnect now when/if we impl this
 
203
        case DisconnectWait:
 
204
            // maybe check the socket state that it thinks it is now unconnected too
 
205
            mState = SocketUnconnected;
 
206
            break;
 
207
    }
 
208
}
 
209
 
 
210
void Solid::Control::ManagedSocketContainer::socketStateChanged( QAbstractSocket::SocketState socketState )
 
211
{
 
212
    switch ( mState )
 
213
    {
 
214
        case SocketUnconnected:
 
215
            switch ( socketState )
 
216
            {
 
217
                case QAbstractSocket::HostLookupState:
 
218
                case QAbstractSocket::ConnectingState:
 
219
                    // the socket is trying to connect, cache its connection parameter in case it
 
220
                    // fails and we want to reconnect it when the network is available.
 
221
                    mState = SocketConnecting;
 
222
                    if ( mSocket )
 
223
                    {
 
224
                        mPeerName = mSocket->peerName();
 
225
                        mPeerPort = mSocket->peerPort();
 
226
                        mSocketOpenMode = mSocket->openMode();
 
227
                    }
 
228
                    break;
 
229
                default:
 
230
                    ;
 
231
            }
 
232
            break;
 
233
        case SocketConnecting:
 
234
            switch ( socketState )
 
235
            {
 
236
                case QAbstractSocket::HostLookupState:
 
237
                case QAbstractSocket::ConnectingState:
 
238
                    // still connecting, do nothing
 
239
                    break;
 
240
                case QAbstractSocket::BoundState:
 
241
                case QAbstractSocket::ConnectedState:
 
242
                case QAbstractSocket::ListeningState:
 
243
                    // socket connected unaided
 
244
                    mState = SocketConnected;
 
245
                    break;
 
246
                case QAbstractSocket::UnconnectedState:
 
247
                    // this state is preceded by ClosingState, so no action needed
 
248
                    break;
 
249
                case QAbstractSocket::ClosingState:
 
250
                    // it's unlikely that an unconnected socket can go to this state, but...
 
251
                    mState = SocketUnconnected;
 
252
                    break;
 
253
            }
 
254
            break;
 
255
        case AwaitingNetworkConnection:
 
256
            switch ( socketState )
 
257
            {
 
258
                case QAbstractSocket::ConnectedState:
 
259
                    // somehow the socket connected itself when it shouldn't have been able to.
 
260
                    mState = SocketConnected;
 
261
 
 
262
                    break;
 
263
                default:
 
264
                    //do nothing
 
265
                    ;
 
266
            }
 
267
            break;
 
268
        case SocketConnected:
 
269
            switch ( socketState )
 
270
            {
 
271
                case QAbstractSocket::UnconnectedState:
 
272
                case QAbstractSocket::ClosingState:
 
273
                    // socket disconnected
 
274
                    mState = SocketUnconnected;
 
275
                    break;
 
276
                case QAbstractSocket::ConnectingState:
 
277
                    mState = SocketConnected;
 
278
                    break;
 
279
                default:
 
280
                    ;
 
281
            }
 
282
            break;
 
283
        case DisconnectWait:
 
284
            switch ( socketState )
 
285
            {
 
286
                case QAbstractSocket::UnconnectedState:
 
287
                case QAbstractSocket::ClosingState:
 
288
                    // socket disconnected anyway
 
289
                    mState = SocketUnconnected;
 
290
                    if ( mAutoDisconnectTimer )
 
291
                    {
 
292
                        mAutoDisconnectTimer->stop();
 
293
                    }
 
294
                    break;
 
295
                default:
 
296
                    break;
 
297
            }
 
298
            break;
 
299
    }
 
300
}
 
301
 
 
302
void Solid::Control::ManagedSocketContainer::autoDisconnect()
 
303
{
 
304
    if ( mAutoDisconnectTimer && mSocket )
 
305
        mSocket->disconnectFromHost();
 
306
}
 
307
 
 
308
void Solid::Control::ManagedSocketContainer::socketDestroyed()
 
309
{
 
310
    mSocket = 0;
 
311
    delete mAutoDisconnectTimer;
 
312
    mAutoDisconnectTimer = 0;
 
313
    disconnect( globalNetworkControl );
 
314
}
 
315
 
 
316
void Solid::Control::ManagedSocketContainer::performConnectToHost()
 
317
{
 
318
    if ( mSocket )
 
319
    {
 
320
        mSocket->connectToHost( mPeerName, mPeerPort, mSocketOpenMode );
 
321
    }
 
322
}
 
323
 
 
324
#include "networking_p.moc"