Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/services/console/interface/network/network.py
blob: ecd09aa9850aa07bac10f1db88d45e0a2720c64c (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
101
102
103
# Copyright (C) 2007, Eduardo Silva <edsiper@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import gobject
from net.device import Device
from gui.treeview import TreeView

class NetworkView(TreeView):
    def __init__(self):
        col_names = []
        col_names.append({'index': 0, 'name': 'Interface'})
        col_names.append({'index': 1, 'name': 'IP Address'})
        col_names.append({'index': 2, 'name': 'NetMask'})
        col_names.append({'index': 3, 'name': 'MAC Address'})
        col_names.append({'index': 4, 'name': 'Bytes Recv'})
        col_names.append({'index': 5, 'name': 'Bytes Sent'})
        col_names.append({'index': 6, 'name': 'Packets Recv'})
        col_names.append({'index': 7, 'name': 'Packets Sent'})

        self._iface_iter = []
        cols_type = [str, str, str, str, str, str, str, str]
        TreeView.__init__(self, cols_type, col_names)

        self._dev = Device()
        self.show_all()
        gobject.timeout_add(1500, self._update_data)

    def _update_data(self):
        interfaces = self._dev.get_interfaces()        
        for iface in interfaces:
            info = self._dev.get_iface_info(iface['interface'])
            row = []
            row.append({'index':0, 'info': iface['interface']})
            
            if info[0]:
                row.append({'index':1, 'info': info[0]})
            if info[1]:
                row.append({'index':2, 'info': info[1]})
            if info[2]:
                row.append({'index':3, 'info': info[2]})

            row.append({'index': 4, 'info': iface['bytes_sent']})
            row.append({'index': 5, 'info': iface['packets_sent']})
            row.append({'index': 6, 'info': iface['bytes_recv']})
            row.append({'index': 7, 'info': iface['packets_recv']})

            iter = self._get_iface_iter(iface['interface'])
            if not iter:
                iter = self.add_row(row)
                self._set_iface_iter(iter, iface['interface'])
            else:
                self.update_row(iter, row)

        self._clear_down_interfaces(interfaces)
        return True

    def _set_iface_iter(self, iter, iface):
        self._iface_iter.append([iter, iface])

    def _remove_iface_iter(self, search_iter):
        i = 0
        for [iter, interface] in self._iface_iter:
            if iter == search_iter:
                del self._iface_iter[i]
                return
            i+=1

    def _get_iface_iter(self, iface):
        for [iter, interface] in self._iface_iter:
            if iface == interface:
                return iter

        return None

    def _clear_down_interfaces(self, interfaces):
        for [iter, iface] in self._iface_iter:
            found = False
            for dev in interfaces:
                if dev['interface']==iface:
                    found = True
                    break

            if not found:
                self.remove_row(iter)
                self._remove_iface_iter(iter)

class Interface(object):
    def __init__(self):
        self.widget = NetworkView()