diff options
author | Louis-Francis Ratté-Boulianne <louis-francis.ratte-boulianne@collabora.co.uk> | 2010-12-03 13:27:57 -0500 |
---|---|---|
committer | Louis-Francis Ratté-Boulianne <louis-francis.ratte-boulianne@collabora.co.uk> | 2010-12-03 13:42:43 -0500 |
commit | 3c4a6774f7af3668d3506ddd998f878bff5f7630 (patch) | |
tree | e42f049d52b367deee2aaf4a56f8f6b8fd020b21 | |
parent | 28789e148f99edf24d6c9d620e5d620882486d3c (diff) |
msnp: make handling of errors consistent
-rw-r--r-- | papyon/client.py | 21 | ||||
-rw-r--r-- | papyon/event/client.py | 7 | ||||
-rw-r--r-- | papyon/msnp/__init__.py | 1 | ||||
-rw-r--r-- | papyon/msnp/base.py | 39 | ||||
-rw-r--r-- | papyon/msnp/constants.py | 21 | ||||
-rw-r--r-- | papyon/msnp/notification.py | 36 | ||||
-rw-r--r-- | papyon/msnp/switchboard.py | 12 |
7 files changed, 61 insertions, 76 deletions
diff --git a/papyon/client.py b/papyon/client.py index 5cd1484..1b77cf9 100644 --- a/papyon/client.py +++ b/papyon/client.py @@ -442,21 +442,8 @@ class Client(EventsDispatcher): for contact in im_contacts: self.__connect_contact_signals(contact) - def authentication_failed(proto): - self._dispatch("on_client_error", ClientErrorType.AUTHENTICATION, - AuthenticationError.INVALID_USERNAME_OR_PASSWORD) - self.__die = True - self._transport.lose_connection() - - def disconnected_by_other(proto): - self._dispatch("on_client_error", ClientErrorType.PROTOCOL, - ProtocolError.OTHER_CLIENT) - self.__die = True - self._transport.lose_connection() - - def server_down(proto): - self._dispatch("on_client_error", ClientErrorType.PROTOCOL, - ProtocolError.SERVER_DOWN) + def error(proto, error): + self._dispatch("on_client_error", ClientErrorType.PROTOCOL, error) self.__die = True self._transport.lose_connection() @@ -470,10 +457,8 @@ class Client(EventsDispatcher): logger.warning("No event handler attached for conversations") conversation._on_message_received(message) + self._protocol.connect("error", error) self._protocol.connect("notify::state", state_changed) - self._protocol.connect("authentication-failed", authentication_failed) - self._protocol.connect("disconnected-by-other", disconnected_by_other) - self._protocol.connect("server-down", server_down) self._protocol.connect("unmanaged-message-received", unmanaged_message_received) def __connect_switchboard_manager_signals(self): diff --git a/papyon/event/client.py b/papyon/event/client.py index 4d73128..6562a05 100644 --- a/papyon/event/client.py +++ b/papyon/event/client.py @@ -52,12 +52,6 @@ class ClientState(object): SYNCHRONIZED = 6 OPEN = 7 -class ProtocolError(object): - "Protocol related errors" - UNKNOWN = 0 - OTHER_CLIENT = 1 - SERVER_DOWN = 2 - #Backward compatibility declarations ClientErrorType = papyon.errors.ClientErrorType @@ -66,6 +60,7 @@ NetworkError = papyon.gnet.IoError AuthenticationError = papyon.service.SingleSignOn.AuthenticationError AddressBookError = papyon.service.AddressBook.constants.AddressBookError OfflineMessagesBoxError = papyon.service.OfflineIM.constants.OfflineMessagesBoxError +ProtocolError = papyon.msnp.constants.ProtocolError class ClientEventInterface(BaseEventInterface): diff --git a/papyon/msnp/__init__.py b/papyon/msnp/__init__.py index 439b397..103f063 100644 --- a/papyon/msnp/__init__.py +++ b/papyon/msnp/__init__.py @@ -27,4 +27,3 @@ from constants import * from notification import * from switchboard import * from mailbox import * -from base import ProtocolState diff --git a/papyon/msnp/base.py b/papyon/msnp/base.py index 5a4197a..1af12a7 100644 --- a/papyon/msnp/base.py +++ b/papyon/msnp/base.py @@ -21,24 +21,16 @@ """Base classes used by the specific classes of the Core Protocol""" +from constants import ProtocolState, ProtocolError + +import gobject import logging __all__ = ['BaseProtocol'] logger = logging.getLogger('papyon.protocol') -class ProtocolState(object): - CLOSED = 0 - OPENING = 1 - AUTHENTICATING = 2 - AUTHENTICATED = 3 - SYNCHRONIZING = 4 - SYNCHRONIZED = 5 - OPEN = 6 - CLOSING = 7 - - -class BaseProtocol(object): +class BaseProtocol(gobject.GObject): """Base class used to implement the Notification protocol as well as the Switchboard protocol @group Handlers: _handle_*, _default_handler, _error_handler @@ -53,6 +45,21 @@ class BaseProtocol(object): L{gnet.proxy.ProxyInfos} instance """ + __gsignals__ = { + "error" : (gobject.SIGNAL_RUN_FIRST, + gobject.TYPE_NONE, + (object,)), + } + + __gproperties__ = { + "state": (gobject.TYPE_INT, + "State", + "The state of the communication with the server.", + 0, 6, ProtocolState.CLOSED, + gobject.PARAM_READABLE) + } + + def __init__(self, client, transport, proxies={}): """Initializer @@ -66,6 +73,8 @@ class BaseProtocol(object): L{gnet.proxy.ProxyInfos} instance @type proxies: {type: string, proxy:L{gnet.proxy.ProxyInfos}} """ + gobject.GObject.__init__(self) + transport.connect("command-received", self._dispatch_command) transport.connect("connection-success", self._connect_cb) transport.connect("connection-failure", self._disconnect_cb) @@ -105,7 +114,11 @@ class BaseProtocol(object): handler = getattr(self, '_handle_' + command.name, self._default_handler) - handler(command) + try: + handler(command) + except Exception, err: + logger.exception(err) + logger.error('Ignoring invalid command :' + unicode(command)) else: self._error_handler(command) diff --git a/papyon/msnp/constants.py b/papyon/msnp/constants.py index df39952..7437127 100644 --- a/papyon/msnp/constants.py +++ b/papyon/msnp/constants.py @@ -17,7 +17,8 @@ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # -__all__ = ["ProtocolConstant", "ClientTypes", "UserNotificationTypes"] +__all__ = ["ProtocolConstant", "ProtocolError", "ProtocolState", + "ClientTypes", "UserNotificationTypes"] class ProtocolConstant(object): VER = (18, 16, 15) @@ -26,6 +27,24 @@ class ProtocolConstant(object): PRODUCT_KEY = "PK}_A_0N_K%O?A9S" CHL_MAGIC_NUM = 0x0E79A9C1 +class ProtocolError(object): + "Protocol related errors" + UNKNOWN = 0 + OTHER_CLIENT = 1 + SERVER_DOWN = 2 + INVALID_COMMAND = 3 + AUTHENTICATION_FAILED = 4 + +class ProtocolState(object): + CLOSED = 0 + OPENING = 1 + AUTHENTICATING = 2 + AUTHENTICATED = 3 + SYNCHRONIZING = 4 + SYNCHRONIZED = 5 + OPEN = 6 + CLOSING = 7 + class ClientTypes(object): COMPUTER = 1 WEBSITE = 2 diff --git a/papyon/msnp/notification.py b/papyon/msnp/notification.py index d5ef5be..ee47672 100644 --- a/papyon/msnp/notification.py +++ b/papyon/msnp/notification.py @@ -23,9 +23,9 @@ """Notification protocol Implementation Implements the protocol used to communicate with the Notification Server.""" -from base import BaseProtocol, ProtocolState +from base import BaseProtocol from message import Message -from constants import ProtocolConstant +from constants import ProtocolConstant, ProtocolError, ProtocolState from challenge import _msn_challenge import papyon @@ -55,7 +55,7 @@ __all__ = ['NotificationProtocol'] logger = logging.getLogger('papyon.protocol.notification') -class NotificationProtocol(BaseProtocol, gobject.GObject, Timer): +class NotificationProtocol(BaseProtocol, Timer): """Protocol used to communicate with the Notification Server @undocumented: do_get_property, do_set_property @@ -63,20 +63,8 @@ class NotificationProtocol(BaseProtocol, gobject.GObject, Timer): @ivar state: the current protocol state @type state: integer - @see L{base.ProtocolState}""" + @see L{constants.ProtocolState}""" __gsignals__ = { - "authentication-failed" : (gobject.SIGNAL_RUN_FIRST, - gobject.TYPE_NONE, - ()), - - "disconnected-by-other" : (gobject.SIGNAL_RUN_FIRST, - gobject.TYPE_NONE, - ()), - - "server-down" : (gobject.SIGNAL_RUN_FIRST, - gobject.TYPE_NONE, - ()), - "buddy-notification-received" : (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, (object, object, object, object)), @@ -94,14 +82,6 @@ class NotificationProtocol(BaseProtocol, gobject.GObject, Timer): (object, object)), } - __gproperties__ = { - "state": (gobject.TYPE_INT, - "State", - "The state of the communication with the server.", - 0, 6, ProtocolState.CLOSED, - gobject.PARAM_READABLE) - } - def __init__(self, client, transport, proxies={}, version=15): """Initializer @@ -116,7 +96,6 @@ class NotificationProtocol(BaseProtocol, gobject.GObject, Timer): @type proxies: {type: string, proxy:L{gnet.proxy.ProxyInfos}} """ BaseProtocol.__init__(self, client, transport, proxies) - gobject.GObject.__init__(self) Timer.__init__(self) self.__state = ProtocolState.CLOSED self._protocol_version = version @@ -382,7 +361,8 @@ class NotificationProtocol(BaseProtocol, gobject.GObject, Timer): if command.arguments[0] == "SSO": self._client._sso.RequestMultipleSecurityTokens( (self._sso_cb, command.arguments[3]), - ((lambda *args: self.emit("authentication-failed")),), + ((lambda error: self.emit("error", + ProtocolError.AUTHENTICATION_FAILED)),), SSO.LiveService.MESSENGER_CLEAR) self._client.address_book.connect("notify::state", @@ -421,9 +401,9 @@ class NotificationProtocol(BaseProtocol, gobject.GObject, Timer): reason = command.arguments[0] if reason == "OTH": - self.emit("disconnected-by-other") + self.emit("error", ProtocolError.OTHER_CLIENT) elif reason == "SSD": - self.emit("server-down") + self.emit("error", ProtocolError.SERVER_DOWN) else: self._transport.lose_connection() diff --git a/papyon/msnp/switchboard.py b/papyon/msnp/switchboard.py index 397575d..213b221 100644 --- a/papyon/msnp/switchboard.py +++ b/papyon/msnp/switchboard.py @@ -23,7 +23,8 @@ """Switchboard protocol Implementation Implements the protocol used to communicate with the Switchboard Server.""" -from base import BaseProtocol, ProtocolState +from base import BaseProtocol +from constants import ProtocolError, ProtocolState from message import Message import papyon.profile @@ -39,7 +40,7 @@ __all__ = ['SwitchboardProtocol'] logger = logging.getLogger('papyon.protocol.switchboard') -class SwitchboardProtocol(BaseProtocol, gobject.GObject): +class SwitchboardProtocol(BaseProtocol): """Protocol used to communicate with the Switchboard Server @undocumented: do_get_property, do_set_property @@ -77,12 +78,6 @@ class SwitchboardProtocol(BaseProtocol, gobject.GObject): (object,))} __gproperties__ = { - "state": (gobject.TYPE_INT, - "State", - "The state of the communication with the server.", - 0, 6, ProtocolState.CLOSED, - gobject.PARAM_READABLE), - "inviting": (gobject.TYPE_BOOLEAN, "Inviting", "True if an invite was sent, and the contact didn't join yet", @@ -109,7 +104,6 @@ class SwitchboardProtocol(BaseProtocol, gobject.GObject): @type proxies: {type: string, proxy:L{gnet.proxy.ProxyInfos}} """ BaseProtocol.__init__(self, client, transport, proxies) - gobject.GObject.__init__(self) self.participants = {} self.end_points = {} self.__session_id = session_id |