From 874b8fef223d200133b54251e1ab37cdc0513236 Mon Sep 17 00:00:00 2001 From: Moritz Venn Date: Sat, 3 Oct 2009 21:52:19 +0000 Subject: [PATCH] stopListening does not stop listening immediately, handle wrong addresses --- growlee/src/plugin.py | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/growlee/src/plugin.py b/growlee/src/plugin.py index 1557e2a..2e43e1c 100644 --- a/growlee/src/plugin.py +++ b/growlee/src/plugin.py @@ -6,6 +6,7 @@ from netgrowl import GrowlRegistrationPacket, GrowlNotificationPacket, \ from twisted.internet.protocol import DatagramProtocol from twisted.internet import reactor from struct import unpack +from socket import gaierror from Screens.Setup import SetupSummary from Screens.Screen import Screen @@ -75,9 +76,17 @@ class GrowleeConfiguration(Screen, ConfigListScreen): if self["config"].isChanged(): global port if port: - port.stopListening() - - if config.plugins.growlee.enable_incoming.value or config.plugins.growlee.enable_outgoing.value: + def maybeConnect(*args, **kwargs): + if config.plugins.growlee.enable_incoming.value or config.plugins.growlee.enable_outgoing.value: + global port + port = reactor.listenUDP(GROWL_UDP_PORT, growlProtocolOneWrapper) + + d = port.stopListening() + if d is not None: + d.addCallback(maybeConnect).addErrback(emergencyDisable) + else: + maybeConnect() + elif config.plugins.growlee.enable_incoming.value or config.plugins.growlee.enable_outgoing.value: port = reactor.listenUDP(GROWL_UDP_PORT, growlProtocolOneWrapper) self.saveAll() @@ -86,13 +95,30 @@ class GrowleeConfiguration(Screen, ConfigListScreen): def configuration(session, **kwargs): session.open(GrowleeConfiguration) +def emergencyDisable(*args, **kwargs): + global port + if port: + port.stopListening() + port = None + + if gotNotification in Notifications.notificationAdded: + Notifications.notificationAdded.remove(gotNotification) + Notifications.AddPopup( + _("Network error.\nDisabling Growlee!"), + MessageBox.TYPE_ERROR, + 10 + ) + class GrowlProtocolOneWrapper(DatagramProtocol): def startProtocol(self): if config.plugins.growlee.enable_outgoing.value: addr = (config.plugins.growlee.address.value, GROWL_UDP_PORT) p = GrowlRegistrationPacket(password=config.plugins.growlee.password.value) p.addNotification() - self.transport.write(p.payload(), addr) + try: + self.transport.write(p.payload(), addr) + except gaierror: + emergencyDisable() def sendNotification(self, *args, **kwargs): if not self.transport or not config.plugins.growlee.enable_outgoing.value: @@ -100,7 +126,10 @@ class GrowlProtocolOneWrapper(DatagramProtocol): addr = (config.plugins.growlee.address.value, GROWL_UDP_PORT) p = GrowlNotificationPacket(*args, **kwargs) - self.transport.write(p.payload(), addr) + try: + self.transport.write(p.payload(), addr) + except gaierror: + emergencyDisable() def datagramReceived(self, data, addr): Len = len(data) -- 2.7.4