Merge lp:~mandel/ubuntuone-client/add_windows_network_manager into lp:ubuntuone-client

Proposed by Manuel de la Peña
Status: Merged
Approved by: Manuel de la Peña
Approved revision: 888
Merged at revision: 898
Proposed branch: lp:~mandel/ubuntuone-client/add_windows_network_manager
Merge into: lp:ubuntuone-client
Diff against target: 235 lines (+226/-0)
2 files modified
tests/platform/windows/test_network_manager.py (+67/-0)
ubuntuone/platform/windows/network_manager.py (+159/-0)
To merge this branch: bzr merge lp:~mandel/ubuntuone-client/add_windows_network_manager
Reviewer Review Type Date Requested Status
Facundo Batista (community) Approve
Roberto Alsina (community) Approve
Review via email: mp+50891@code.launchpad.net

Commit message

Implements the network manager that will execute callbacks when the status of the network changes.

Description of the change

Implements the network manager that will execute callbacks when the status of the network changes. To test the code on windows execute 'u1trial tests/platform/windows/test_network_manager.py'. On linux all tests should run correctly.

To post a comment you must log in.
Revision history for this message
Roberto Alsina (ralsina) wrote :

+1 pass on windows

review: Approve
Revision history for this message
Facundo Batista (facundo) wrote :

Like it!

review: Approve
888. By Manuel de la Peña

Removed unused import. Added clarifications to the code.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'tests/platform/windows/test_network_manager.py'
2--- tests/platform/windows/test_network_manager.py 1970-01-01 00:00:00 +0000
3+++ tests/platform/windows/test_network_manager.py 2011-02-28 14:34:34 +0000
4@@ -0,0 +1,67 @@
5+# -*- coding: utf-8 -*-
6+#
7+# Author: Manuel de la Pena<manuel@canonical.com>
8+#
9+# Copyright 2011 Canonical Ltd.
10+#
11+# This program is free software: you can redistribute it and/or modify it
12+# under the terms of the GNU General Public License version 3, as published
13+# by the Free Software Foundation.
14+#
15+# This program is distributed in the hope that it will be useful, but
16+# WITHOUT ANY WARRANTY; without even the implied warranties of
17+# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
18+# PURPOSE. See the GNU General Public License for more details.
19+#
20+# You should have received a copy of the GNU General Public License along
21+# with this program. If not, see <http://www.gnu.org/licenses/>.
22+"""Tests for the network manager."""
23+from mocker import MockerTestCase
24+from ubuntuone.platform.windows.network_manager import NetworkManager
25+
26+class TestNetworkManager(MockerTestCase):
27+ """Test he Network Manager."""
28+
29+ def setUp(self):
30+ super(TestNetworkManager, self).setUp()
31+ self.connection_info = self.mocker.mock()
32+ self.connection_no_info = self.mocker.mock()
33+ self.disconnected = self.mocker.mock()
34+ self.manager = NetworkManager(self.connection_no_info,
35+ self.connection_info, self.disconnected)
36+
37+ def test_connection_made(self):
38+ """Ensure db is called."""
39+ self.connection_info()
40+ self.mocker.replay()
41+ self.manager.ConnectionMade()
42+
43+ def test_connection_made_no_cb(self):
44+ """Ensure db is called."""
45+ self.manager.connected_cb_info = None
46+ self.mocker.replay()
47+ self.manager.ConnectionMade()
48+
49+ def test_connection_made_no_info(self):
50+ """Ensure db is called."""
51+ self.connection_no_info()
52+ self.mocker.replay()
53+ self.manager.ConnectionMadeNoQOCInfo()
54+
55+ def test_connection_made_no_info_no_cb(self):
56+ """Ensure db is called."""
57+ self.manager.connected_cb = None
58+ self.mocker.replay()
59+ self.manager.ConnectionMadeNoQOCInfo()
60+
61+ def test_disconnection(self):
62+ """Ensure db is called."""
63+ self.disconnected()
64+ self.mocker.replay()
65+ self.manager.ConnectionLost()
66+
67+ def test_disconnection_no_cb(self):
68+ """Ensure db is called."""
69+ self.manager.disconnected_cb = None
70+ self.mocker.replay()
71+ self.manager.ConnectionLost()
72
73=== added file 'ubuntuone/platform/windows/network_manager.py'
74--- ubuntuone/platform/windows/network_manager.py 1970-01-01 00:00:00 +0000
75+++ ubuntuone/platform/windows/network_manager.py 2011-02-28 14:34:34 +0000
76@@ -0,0 +1,159 @@
77+# -*- coding: utf-8 -*-
78+#
79+# Author: Manuel de la Pena<manuel@canonical.com>
80+#
81+# Copyright 2011 Canonical Ltd.
82+#
83+# This program is free software: you can redistribute it and/or modify it
84+# under the terms of the GNU General Public License version 3, as published
85+# by the Free Software Foundation.
86+#
87+# This program is distributed in the hope that it will be useful, but
88+# WITHOUT ANY WARRANTY; without even the implied warranties of
89+# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
90+# PURPOSE. See the GNU General Public License for more details.
91+#
92+# You should have received a copy of the GNU General Public License along
93+# with this program. If not, see <http://www.gnu.org/licenses/>.
94+"""Implementation of a Network Mnager using ISesNework in Python."""
95+import logging
96+
97+import pythoncom
98+
99+from win32com.server.policy import DesignatedWrapPolicy
100+from win32com.client import Dispatch
101+
102+# set te logging to store the data in the ubuntuone folder
103+logger = logging.getLogger('ubuntuone.platform.windows.NetworkManager')
104+
105+## from EventSys.h
106+PROGID_EventSystem = "EventSystem.EventSystem"
107+PROGID_EventSubscription = "EventSystem.EventSubscription"
108+
109+# SENS (System Event Notification Service) values for the events,
110+# this events contain the uuid of the event, the name of the event to be used
111+# as well as the method name of the method in the ISesNetwork interface that
112+# will be executed for the event.
113+# For more ingo look at:
114+# http://msdn.microsoft.com/en-us/library/aa377384(v=vs.85).aspx
115+
116+SUBSCRIPTION_NETALIVE = ('{cd1dcbd6-a14d-4823-a0d2-8473afde360f}',
117+ 'UbuntuOne Network Alive',
118+ 'ConnectionMade')
119+
120+SUBSCRIPTION_NETALIVE_NOQOC = ('{a82f0e80-1305-400c-ba56-375ae04264a1}',
121+ 'UbuntuOne Net Alive No Info',
122+ 'ConnectionMadeNoQOCInfo')
123+
124+SUBSCRIPTION_NETLOST = ('{45233130-b6c3-44fb-a6af-487c47cee611}',
125+ 'UbuntuOne Network Lost',
126+ 'ConnectionLost')
127+
128+SUBSCRIPTION_REACH = ('{4c6b2afa-3235-4185-8558-57a7a922ac7b}',
129+ 'UbuntuOne Network Reach',
130+ 'ConnectionMade')
131+
132+SUBSCRIPTION_REACH_NOQOC = ('{db62fa23-4c3e-47a3-aef2-b843016177cf}',
133+ 'UbuntuOne Network Reach No Info',
134+ 'ConnectionMadeNoQOCInfo')
135+
136+SUBSCRIPTION_REACH_NOQOC2 = ('{d4d8097a-60c6-440d-a6da-918b619ae4b7}',
137+ 'UbuntuOne Network Reach No Info 2',
138+ 'ConnectionMadeNoQOCInfo')
139+
140+SUBSCRIPTIONS = [SUBSCRIPTION_NETALIVE,
141+ SUBSCRIPTION_NETALIVE_NOQOC,
142+ SUBSCRIPTION_NETLOST,
143+ SUBSCRIPTION_REACH,
144+ SUBSCRIPTION_REACH_NOQOC,
145+ SUBSCRIPTION_REACH_NOQOC2 ]
146+
147+SENSGUID_EVENTCLASS_NETWORK = '{d5978620-5b9f-11d1-8dd2-00aa004abd5e}'
148+SENSGUID_PUBLISHER = "{5fee1bd6-5b9b-11d1-8dd2-00aa004abd5e}"
149+
150+# uuid of the implemented com interface
151+IID_ISesNetwork = '{d597bab1-5b9f-11d1-8dd2-00aa004abd5e}'
152+
153+class NetworkManager(DesignatedWrapPolicy):
154+ """Implement ISesNetwork to know about the network status."""
155+
156+ _com_interfaces_ = [IID_ISesNetwork]
157+ _public_methods_ = ['ConnectionMade',
158+ 'ConnectionMadeNoQOCInfo',
159+ 'ConnectionLost']
160+ _reg_clsid_ = '{41B032DA-86B5-4907-A7F7-958E59333010}'
161+ _reg_progid_ = "UbuntuOne.NetworkManager"
162+
163+ def __init__(self, connected_cb=None, connected_cb_info=None,
164+ disconnected_cb=None):
165+ self._wrap_(self)
166+ self.connected_cb = connected_cb
167+ self.connected_cb_info = connected_cb_info
168+ self.disconnected_cb = disconnected_cb
169+
170+ def ConnectionMade(self, *args):
171+ """Tell that the connection is up again."""
172+ logger.info('Connection was made.')
173+ if self.connected_cb_info:
174+ self.connected_cb_info()
175+
176+ def ConnectionMadeNoQOCInfo(self, *args):
177+ """Tell that the connection is up again."""
178+ logger.info('Connection was made no info.')
179+ if self.connected_cb:
180+ self.connected_cb()
181+
182+ def ConnectionLost(self, *args):
183+ """Tell the connection was lost."""
184+ logger.info('Connection was lost.')
185+ if self.disconnected_cb:
186+ self.disconnected_cb()
187+
188+ def register(self):
189+ """Register to listen to network events."""
190+ # call the CoInitialize to allow the registration to run in an other
191+ # thread
192+ pythoncom.CoInitialize()
193+ # interface to be used by com
194+ manager_interface = pythoncom.WrapObject(self)
195+ event_system = Dispatch(PROGID_EventSystem)
196+ # register to listent to each of the events to make sure that
197+ # the code will work on all platforms.
198+ for current_event in SUBSCRIPTIONS:
199+ # create an event subscription and add it to the event
200+ # service
201+ event_subscription = Dispatch(PROGID_EventSubscription)
202+ event_subscription.EventClassId = SENSGUID_EVENTCLASS_NETWORK
203+ event_subscription.PublisherID = SENSGUID_PUBLISHER
204+ event_subscription.SubscriptionID = current_event[0]
205+ event_subscription.SubscriptionName = current_event[1]
206+ event_subscription.MethodName = current_event[2]
207+ event_subscription.SubscriberInterface = manager_interface
208+ event_subscription.PerUser = True
209+ # store the event
210+ try:
211+ event_system.Store(PROGID_EventSubscription,
212+ event_subscription)
213+ except pythoncom.com_error as e:
214+ logger.error(
215+ 'Error registering %s to event %s', e, current_event[1])
216+
217+ pythoncom.PumpMessages()
218+
219+if __name__ == '__main__':
220+ # Run an expample of the code so that the user can test the code in
221+ # real life.
222+ from threading import Thread
223+ def connected():
224+ print 'Connected'
225+
226+ def connected_info():
227+ print 'Connected'
228+
229+ def disconnected():
230+ print 'Disconnected'
231+
232+ manager = NetworkManager(connected, connected_info, disconnected)
233+ p = Thread(target=manager.register)
234+ p.start()
235+

Subscribers

People subscribed via source and target branches