From 160430eaba2984673d56c07a0fb707252e53bc5a Mon Sep 17 00:00:00 2001
From: Daniel Drake <dsd@laptop.org>
Date: Mon, 4 Jan 2010 12:48:01 +0000
Subject: [PATCH] initial review
---
extensions/deviceicon/network.py | 11 +++++--
src/jarabe/desktop/meshbox.py | 22 ++++---------
src/jarabe/model/olpcmesh.py | 63 +++++++++++++++++++------------------
3 files changed, 47 insertions(+), 49 deletions(-)
diff --git a/extensions/deviceicon/network.py b/extensions/deviceicon/network.py
index 875b8e4..2dabf2f 100644
a
|
b
|
class OlpcMeshDeviceView(ToolButton): |
567 | 567 | |
568 | 568 | def _update_text(self): |
569 | 569 | state = self._device_state |
570 | | text = _("Mesh Network") |
571 | 570 | if state in (network.DEVICE_STATE_PREPARE, network.DEVICE_STATE_CONFIG, |
572 | 571 | network.DEVICE_STATE_NEED_AUTH, |
573 | 572 | network.DEVICE_STATE_IP_CONFIG, |
574 | 573 | network.DEVICE_STATE_ACTIVATED): |
575 | | text += " " + str(self._channel) |
| 574 | text = _("Mesh Network %d") % self._channel |
| 575 | else: |
| 576 | text = _("Mesh Network") |
576 | 577 | self._palette.props.primary_text = text |
577 | 578 | |
578 | 579 | def _update(self): |
… |
… |
class WirelessDeviceObserver(object): |
647 | 648 | self._device_view = None |
648 | 649 | self._tray = tray |
649 | 650 | |
650 | | self._device_view = view_class(self._device) |
| 651 | if view_class == WirelessDeviceView: |
| 652 | self._device_view = WirelessDeviceView(self._device) |
| 653 | elif view_class == OlpcMeshDeviceView: |
| 654 | self._device_view = OlpcMeshDeviceView(self._device) |
| 655 | |
651 | 656 | self._tray.add_device(self._device_view) |
652 | 657 | |
653 | 658 | def disconnect(self): |
diff --git a/src/jarabe/desktop/meshbox.py b/src/jarabe/desktop/meshbox.py
index 70c836a..e78b5e1 100644
a
|
b
|
class OlpcMeshView(CanvasPulsingIcon): |
459 | 459 | self._device_state = None |
460 | 460 | self._connection = None |
461 | 461 | self._active = False |
462 | | device = mesh_mgr.meshdev |
| 462 | device = mesh_mgr.mesh_device |
463 | 463 | |
464 | 464 | self.connect('button-release-event', self.__button_release_event_cb) |
465 | 465 | |
… |
… |
class OlpcMeshView(CanvasPulsingIcon): |
489 | 489 | self.set_palette(self._palette) |
490 | 490 | |
491 | 491 | def _create_palette(self): |
492 | | p = palette.Palette(_("Mesh Network") + " " + str(self._channel)) |
| 492 | _palette = palette.Palette(_("Mesh Network %d") % self._channel) |
493 | 493 | |
494 | 494 | self._connect_item = MenuItem(_('Connect'), 'dialog-ok') |
495 | 495 | self._connect_item.connect('activate', self.__connect_activate_cb) |
496 | | p.menu.append(self._connect_item) |
| 496 | _palette.menu.append(self._connect_item) |
497 | 497 | |
498 | | self._disconnect_item = MenuItem(_('Disconnect'), 'media-eject') |
499 | | self._disconnect_item.connect('activate', |
500 | | self._disconnect_activate_cb) |
501 | | p.menu.append(self._disconnect_item) |
502 | | |
503 | | return p |
| 498 | return _palette |
504 | 499 | |
505 | 500 | def __get_device_state_reply_cb(self, state): |
506 | 501 | self._device_state = state |
… |
… |
class OlpcMeshView(CanvasPulsingIcon): |
560 | 555 | else: |
561 | 556 | self.props.base_color = profile.get_color() |
562 | 557 | |
563 | | def _disconnect_activate_cb(self, item): |
564 | | pass |
565 | | |
566 | 558 | def __connect_activate_cb(self, icon): |
567 | 559 | self._connect() |
568 | 560 | |
… |
… |
class MeshBox(gtk.VBox): |
1123 | 1115 | self._layout.add(icon) |
1124 | 1116 | self._mesh.append(icon) |
1125 | 1117 | |
1126 | | def enable_olpc_mesh(self, meshdev): |
1127 | | mesh_mgr = OlpcMeshManager(meshdev) |
| 1118 | def enable_olpc_mesh(self, mesh_device): |
| 1119 | mesh_mgr = OlpcMeshManager(mesh_device) |
1128 | 1120 | self._add_olpc_mesh_icon(mesh_mgr, 1) |
1129 | 1121 | self._add_olpc_mesh_icon(mesh_mgr, 6) |
1130 | 1122 | self._add_olpc_mesh_icon(mesh_mgr, 11) |
… |
… |
class MeshBox(gtk.VBox): |
1141 | 1133 | self._layout.remove(net) |
1142 | 1134 | del self.wireless_networks[hash] |
1143 | 1135 | |
1144 | | def disable_olpc_mesh(self, meshdev): |
| 1136 | def disable_olpc_mesh(self, mesh_device): |
1145 | 1137 | for icon in self._mesh: |
1146 | 1138 | icon.disconnect() |
1147 | 1139 | self._layout.remove(icon) |
diff --git a/src/jarabe/model/olpcmesh.py b/src/jarabe/model/olpcmesh.py
index 26c8b53..0935de5 100644
a
|
b
|
DEVICE_STATE_ACTIVATED = 8 |
44 | 44 | DEVICE_STATE_FAILED = 9 |
45 | 45 | |
46 | 46 | class OlpcMeshManager(object): |
47 | | def __init__(self, meshdev): |
| 47 | def __init__(self, mesh_device): |
48 | 48 | self._bus = dbus.SystemBus() |
49 | | self.meshdev = meshdev |
| 49 | self.mesh_device = mesh_device |
50 | 50 | |
51 | 51 | # let's make sure we know the companion device early on |
52 | 52 | # (synchronous request) |
53 | | props = dbus.Interface(meshdev, 'org.freedesktop.DBus.Properties') |
54 | | ethdev_o = props.Get(_NM_OLPC_MESH_IFACE, 'Companion') |
55 | | self.ethdev = self._bus.get_object(_NM_SERVICE, ethdev_o) |
| 53 | props = dbus.Interface(mesh_device, 'org.freedesktop.DBus.Properties') |
| 54 | eth_device_o = props.Get(_NM_OLPC_MESH_IFACE, 'Companion') |
| 55 | self.eth_device = self._bus.get_object(_NM_SERVICE, eth_device_o) |
56 | 56 | |
57 | 57 | # asynchronously learn about mesh status |
58 | 58 | props.Get(_NM_DEVICE_IFACE, 'State', |
59 | 59 | reply_handler=self.__get_mesh_state_reply_cb, |
60 | 60 | error_handler=self.__get_state_error_cb) |
61 | 61 | # and companion |
62 | | props = dbus.Interface(self.ethdev, 'org.freedesktop.DBus.Properties') |
| 62 | props = dbus.Interface(self.eth_device, |
| 63 | 'org.freedesktop.DBus.Properties') |
63 | 64 | props.Get(_NM_DEVICE_IFACE, 'State', |
64 | 65 | reply_handler=self.__get_eth_state_reply_cb, |
65 | 66 | error_handler=self.__get_state_error_cb) |
… |
… |
class OlpcMeshManager(object): |
68 | 69 | # we find one that works |
69 | 70 | self._connection_queue = [] |
70 | 71 | |
71 | | self._bus.add_signal_receiver(self.__ethdev_state_changed_cb, |
| 72 | self._bus.add_signal_receiver(self.__eth_device_state_changed_cb, |
72 | 73 | signal_name='StateChanged', |
73 | | path=self.ethdev.object_path, |
| 74 | path=self.eth_device.object_path, |
74 | 75 | dbus_interface=_NM_DEVICE_IFACE) |
75 | 76 | |
76 | 77 | self._bus.add_signal_receiver(self.__mshdev_state_changed_cb, |
77 | 78 | signal_name='StateChanged', |
78 | | path=self.meshdev.object_path, |
| 79 | path=self.mesh_device.object_path, |
79 | 80 | dbus_interface=_NM_DEVICE_IFACE) |
80 | 81 | |
81 | 82 | self._idle_source = 0 |
82 | | self._meshdev_state = DEVICE_STATE_UNKNOWN |
83 | | self._ethdev_state = DEVICE_STATE_UNKNOWN |
| 83 | self._mesh_device_state = DEVICE_STATE_UNKNOWN |
| 84 | self._eth_device_state = DEVICE_STATE_UNKNOWN |
84 | 85 | |
85 | 86 | if len(network.get_settings().connections) == 0: |
86 | 87 | # if there are no configured APs, start meshing immediately |
… |
… |
class OlpcMeshManager(object): |
94 | 95 | logging.debug('Error getting the device state: %s', err) |
95 | 96 | |
96 | 97 | def __get_mesh_state_reply_cb(self, state): |
97 | | self._meshdev_state = state |
| 98 | self._mesh_device_state = state |
98 | 99 | self._maybe_schedule_idle_check() |
99 | 100 | |
100 | 101 | def __get_eth_state_reply_cb(self, state): |
101 | | self._ethdev_state = state |
| 102 | self._eth_device_state = state |
102 | 103 | self._maybe_schedule_idle_check() |
103 | 104 | |
104 | | def __ethdev_state_changed_cb(self, new_state, old_state, reason): |
105 | | self._ethdev_state = new_state |
| 105 | def __eth_device_state_changed_cb(self, new_state, old_state, reason): |
| 106 | """If a connection is activated on the eth device, stop trying our |
| 107 | automatic connections.""" |
| 108 | self._eth_device_state = new_state |
106 | 109 | self._maybe_schedule_idle_check() |
107 | 110 | |
108 | | # If a connection is activated on the eth device, stop trying our |
109 | | # automatic connections |
110 | 111 | if new_state >= DEVICE_STATE_PREPARE \ |
111 | 112 | and new_state <= DEVICE_STATE_ACTIVATED \ |
112 | 113 | and len(self._connection_queue) > 0: |
113 | 114 | self._connection_queue = [] |
114 | 115 | |
115 | 116 | def __mshdev_state_changed_cb(self, new_state, old_state, reason): |
116 | | self._meshdev_state = new_state |
| 117 | self._mesh_device_state = new_state |
117 | 118 | self._maybe_schedule_idle_check() |
118 | 119 | |
119 | 120 | if new_state == DEVICE_STATE_FAILED: |
… |
… |
class OlpcMeshManager(object): |
125 | 126 | self._connection_queue = [] |
126 | 127 | |
127 | 128 | def _maybe_schedule_idle_check(self): |
128 | | # if both devices are disconnected, wait 10 seconds for some activity |
129 | | # and if nothing happens then we can start automatic meshing |
130 | | if self._meshdev_state == DEVICE_STATE_DISCONNECTED \ |
131 | | and self._ethdev_state == DEVICE_STATE_DISCONNECTED: |
| 129 | """If both devices are disconnected, wait 10 seconds for some activity |
| 130 | and if nothing happens then we can start automatic meshing.""" |
| 131 | if self._mesh_device_state == DEVICE_STATE_DISCONNECTED \ |
| 132 | and self._eth_device_state == DEVICE_STATE_DISCONNECTED: |
132 | 133 | if self._idle_source != 0: |
133 | 134 | gobject.source_remove(self._idle_source) |
134 | 135 | self._idle_source = gobject.timeout_add_seconds(10, self._idle_check) |
135 | 136 | |
136 | 137 | def _idle_check(self): |
137 | | if self._meshdev_state == DEVICE_STATE_DISCONNECTED \ |
138 | | and self._ethdev_state == DEVICE_STATE_DISCONNECTED: |
| 138 | if self._mesh_device_state == DEVICE_STATE_DISCONNECTED \ |
| 139 | and self._eth_device_state == DEVICE_STATE_DISCONNECTED: |
139 | 140 | logging.debug("starting automesh due to inactivity") |
140 | 141 | self.start_automesh() |
141 | 142 | return False |
… |
… |
class OlpcMeshManager(object): |
164 | 165 | connection = self._make_connection(channel, anycast_addr) |
165 | 166 | |
166 | 167 | netmgr.ActivateConnection(network.SETTINGS_SERVICE, connection.path, |
167 | | self.meshdev.object_path, |
168 | | self.meshdev.object_path, |
| 168 | self.mesh_device.object_path, |
| 169 | self.mesh_device.object_path, |
169 | 170 | reply_handler=self.__activate_reply_cb, |
170 | 171 | error_handler=self.__activate_error_cb) |
171 | 172 | |
… |
… |
class OlpcMeshManager(object): |
176 | 177 | channel, anycast = self._connection_queue.pop() |
177 | 178 | self.activate_connection(channel, anycast) |
178 | 179 | |
179 | | # Activate a mesh connection on a user-specified channel |
180 | | # Looks for XS first, then resorts to simple mesh |
181 | 180 | def user_activate_channel(self, channel): |
| 181 | """Activate a mesh connection on a user-specified channel. |
| 182 | Looks for XS first, then resorts to simple mesh.""" |
182 | 183 | # Empties any existing queue in order to abort any connection attempt |
183 | 184 | # that was already happening |
184 | 185 | self._connection_queue = [ (channel, None), (channel, _XS_ANYCAST) ] |
185 | 186 | self._activate_from_queue() |
186 | 187 | |
187 | | # Start meshing, intended for when there are no better networks to |
188 | | # connect to. First looks for XS on all channels, then falls back to |
189 | | # simple mesh on channel 1. |
190 | 188 | def start_automesh(self): |
| 189 | """Start meshing automatically, intended when there are no better |
| 190 | networks to connect to. First looks for XS on all channels, then falls |
| 191 | back to simple mesh on channel 1.""" |
191 | 192 | # Empties any existing queue in order to abort any connection attempt |
192 | 193 | # that was already happening |
193 | 194 | self._connection_queue = [ |