File: NetworkConnectionMonitor.cpp

package info (click to toggle)
qtop 2.3.4-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,976 kB
  • sloc: cpp: 40,477; makefile: 7
file content (176 lines) | stat: -rw-r--r-- 5,351 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/******************************************************************************
*
* Copyright (C) 2002 Hugo PEREIRA <mailto: hugo.pereira@free.fr>
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* Any WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program.  If not, see <http://www.gnu.org/licenses/>.
*
*******************************************************************************/

#include "NetworkConnectionMonitor.h"
#include "NonCopyable.h"
#include "Debug.h"

#if !defined(Q_OS_WIN)
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#endif

#include <unistd.h>

//____________________________________________________________
namespace Network
{

    //_______________________________________________________________________
    class EncapsulatedSocket: private Base::NonCopyable<EncapsulatedSocket>
    {
        public:

        //* constructor
        explicit EncapsulatedSocket( int fileDescriptor ):
            fileDescriptor_( fileDescriptor )
        {}

        //* copy constructor
        explicit EncapsulatedSocket( const EncapsulatedSocket& other ) = delete;

        //* assignment operator
        EncapsulatedSocket& operator = ( const EncapsulatedSocket& other ) = delete;

        //* destructor
        ~EncapsulatedSocket()
        { if( fileDescriptor_ >= 0 ) ::close( fileDescriptor_ ); }

        int fileDescriptor() const
        { return fileDescriptor_; }

        bool isValid() const
        { return fileDescriptor_ >= 0; }

        private:

        int fileDescriptor_;

    };

    //________________________________________________
    ConnectionMonitor::ConnectionMonitor( QObject* parent ):
        QObject( parent ),
        Counter( "Network::ConnectionMonitor" )
    {}

    //______________________________________________________________________
    void ConnectionMonitor::setEnabled( bool enabled )
    {
        #if !defined(Q_OS_WIN)
        if( enabled == enabled_ ) return;
        enabled_ = enabled;

        if( deviceTimer_.isActive() ) deviceTimer_.stop();
        if( enabled_ && timeOut_ >= 0 ) deviceTimer_.start( 1000*timeOut_, this );
        #endif
    }

    //______________________________________________________________________
    void ConnectionMonitor::setTimeOut( int timeOut )
    {
        if( timeOut == timeOut_ ) return;

        timeOut_ = timeOut;
        if( !enabled_ ) return;

        if( deviceTimer_.isActive() ) deviceTimer_.stop();
        if( timeOut_ >= 0 ) deviceTimer_.start( 1000*timeOut_, this );
    }

    //________________________________________________
    ConnectionMonitor::DeviceSet ConnectionMonitor::devices( ConnectionMonitor::DeviceType type )
    {
        Debug::Throw( "Network::ConnectionMonitor::devices.\n" );
        #if defined(Q_OS_WIN)
        return DeviceSet();
        #else
        DeviceSet out;
        for( int index = 1;;++index )
        {

            EncapsulatedSocket socket( ::socket(AF_INET, SOCK_DGRAM, 0) );
            if( !socket.isValid() ) break;

            struct ifreq ifr;
            ifr.ifr_ifindex = index;
            if( ioctl( socket.fileDescriptor(), SIOCGIFNAME, &ifr, sizeof(ifr) ) ) break;
            if( ioctl( socket.fileDescriptor(), SIOCGIFFLAGS, &ifr, sizeof(ifr) ) ) break;

            // skip loopback
            if( ifr.ifr_flags & IFF_LOOPBACK ) continue;

            // check status
            if( type == DeviceType::Connected )
            {

                // check running status
                if( !( ifr.ifr_flags & IFF_RUNNING ) ) continue;

                // check bound address
                if( ioctl( socket.fileDescriptor(), SIOCGIFADDR, &ifr, sizeof(ifr) ) ) continue;

            }

            QString device( ifr.ifr_name );
            Debug::Throw() << "Network::ConnectionMonitor::devices - adding " << device << endl;
            out.insert( device );
        }

        return out;
        #endif
    }

    //________________________________________________
    void ConnectionMonitor::checkDevice()
    {

        Debug::Throw( "ConnectionMonitor::checkDevice.\n" );

        // get connected devices
        auto devices( connectedDevices() );

        // if no connected devices is found, clear current
        if( devices.isEmpty() )
        {
            device_ = QString();
            return;
        }

        // check if current device is in list found
        if( !( device_.isEmpty() || devices.find( device_ ) == devices.end() ) )
        { return; }

        // new device is found
        device_ = *devices.begin();
        emit deviceConnected( device_ );

    }

    //______________________________________________________________________
    void ConnectionMonitor::timerEvent( QTimerEvent* event )
    {

        if( event->timerId() == deviceTimer_.timerId() ) checkDevice();
        else return QObject::timerEvent( event );

    }

}