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

Subscribers

People subscribed via source and target branches