summaryrefslogtreecommitdiffstats
path: root/src/knemod/interfacemonitor.cpp
blob: fefa45fce964669ce41556db673511d80537ec82 (plain)
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
/* This file is part of KNemo
   Copyright (C) 2004, 2006 Percy Leonhardt <percy@eris23.de>

   KNemo is free software; you can redistribute it and/or modify
   it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of
   the License, or (at your option) any later version.

   KNemo 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 Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include <kdebug.h>

#include "interface.h"
#include "interfacemonitor.h"

InterfaceMonitor::InterfaceMonitor( TQObject* parent, const char* name )
    : TQObject( parent, name )
{
}

InterfaceMonitor::~InterfaceMonitor()
{
}

void InterfaceMonitor::checkStatus( Interface* interface )
{
    int currentState;
    int previousState = interface->getState();
    InterfaceData& data = interface->getData();
    int trafficThreshold = interface->getSettings().trafficThreshold;

    if ( !data.existing )
        // the interface does not exist
        currentState = Interface::NOT_EXISTING;
    else if ( !data.available )
        // the interface exists but is not connected
        currentState = Interface::NOT_AVAILABLE;
    else
    {
        // the interface is connected, look for traffic
        currentState = Interface::AVAILABLE;
        if ( ( data.rxPackets - data.prevRxPackets ) > (unsigned int) trafficThreshold )
            currentState |= Interface::RX_TRAFFIC;
        if ( ( data.txPackets - data.prevTxPackets ) > (unsigned int) trafficThreshold )
            currentState |= Interface::TX_TRAFFIC;
    }

    // update the statistics
    if ( data.incomingBytes > 0 )
    {
        emit incomingData( data.incomingBytes );
    }
    if ( data.outgoingBytes > 0 )
    {
        emit outgoingData( data.outgoingBytes );
    }

    data.prevRxPackets = data.rxPackets;
    data.prevTxPackets = data.txPackets;

    if ( ( previousState == Interface::NOT_EXISTING ||
           previousState == Interface::NOT_AVAILABLE ||
           previousState == Interface::UNKNOWN_STATE ) &&
         currentState & Interface::AVAILABLE )
    {
        emit available( previousState );
    }
    else if ( ( previousState == Interface::NOT_EXISTING ||
                previousState & Interface::AVAILABLE ||
                previousState == Interface::UNKNOWN_STATE ) &&
              currentState == Interface::NOT_AVAILABLE )
    {
        emit notAvailable( previousState );
    }
    else if ( ( previousState == Interface::NOT_AVAILABLE ||
                previousState & Interface::AVAILABLE ||
                previousState == Interface::UNKNOWN_STATE ) &&
              currentState == Interface::NOT_EXISTING )
    {
        emit notExisting( previousState );
    }

    // make sure the icon fits the current state
    if ( previousState != currentState )
    {
        emit statusChanged( currentState );
        interface->setState( currentState );
    }
}

#include "interfacemonitor.moc"