lightmelody

changeset 697:1bf63fd862b8 objectify

Added Connection.MessageHandlerEntry class Modified Connection.stanza_handlers to use MessageHandlerEntry Added Connection.unregister_message_handler method
author Arc Riley <arcriley@gmail.com>
date Thu, 27 Dec 2012 19:51:13 -0500
parents 5bbd20c70ada
children 6229f8be919d
files src/Connection.gs
diffstat 1 files changed, 49 insertions(+), 24 deletions(-) [+]
line diff
     1.1 --- a/src/Connection.gs	Thu Dec 27 16:38:56 2012 -0800
     1.2 +++ b/src/Connection.gs	Thu Dec 27 19:51:13 2012 -0500
     1.3 @@ -69,7 +69,7 @@
     1.4  
     1.5      // Registered Message Handlers
     1.6      id_handlers : dict of string, MessageHandler
     1.7 -    stanza_handlers : array of list of MessageHandler
     1.8 +    stanza_handlers : array of list of MessageHandlerEntry
     1.9  
    1.10      // Basic XMPP Message Handlers
    1.11      streamerror_handler : MessageHandler
    1.12 @@ -94,20 +94,20 @@
    1.13          // Create handler lists
    1.14          self.id_handlers = new dict of string, MessageHandler
    1.15          self.stanza_handlers = {
    1.16 -            new list of MessageHandler, // Lm.MessageType.MESSAGE
    1.17 -            new list of MessageHandler, // Lm.MessageType.PRESENCE
    1.18 -            new list of MessageHandler, // Lm.MessageType.IQ
    1.19 -            new list of MessageHandler, // Lm.MessageType.STREAM
    1.20 -            new list of MessageHandler, // Lm.MessageType.STREAM_ERROR
    1.21 -            new list of MessageHandler, // Lm.MessageType.STREAM_FEATURES
    1.22 -            new list of MessageHandler, // Lm.MessageType.AUTH
    1.23 -            new list of MessageHandler, // Lm.MessageType.CHALLENGE
    1.24 -            new list of MessageHandler, // Lm.MessageType.RESPONSE
    1.25 -            new list of MessageHandler, // Lm.MessageType.SUCCESS
    1.26 -            new list of MessageHandler, // Lm.MessageType.FAILURE
    1.27 -            new list of MessageHandler, // Lm.MessageType.PROCEED
    1.28 -            new list of MessageHandler, // Lm.MessageType.STARTTLS
    1.29 -            new list of MessageHandler  // Lm.MessageType.UNKNOWN
    1.30 +            new list of MessageHandlerEntry, // Lm.MessageType.MESSAGE
    1.31 +            new list of MessageHandlerEntry, // Lm.MessageType.PRESENCE
    1.32 +            new list of MessageHandlerEntry, // Lm.MessageType.IQ
    1.33 +            new list of MessageHandlerEntry, // Lm.MessageType.STREAM
    1.34 +            new list of MessageHandlerEntry, // Lm.MessageType.STREAM_ERROR
    1.35 +            new list of MessageHandlerEntry, // Lm.MessageType.STREAM_FEATURES
    1.36 +            new list of MessageHandlerEntry, // Lm.MessageType.AUTH
    1.37 +            new list of MessageHandlerEntry, // Lm.MessageType.CHALLENGE
    1.38 +            new list of MessageHandlerEntry, // Lm.MessageType.RESPONSE
    1.39 +            new list of MessageHandlerEntry, // Lm.MessageType.SUCCESS
    1.40 +            new list of MessageHandlerEntry, // Lm.MessageType.FAILURE
    1.41 +            new list of MessageHandlerEntry, // Lm.MessageType.PROCEED
    1.42 +            new list of MessageHandlerEntry, // Lm.MessageType.STARTTLS
    1.43 +            new list of MessageHandlerEntry  // Lm.MessageType.UNKNOWN
    1.44          }
    1.45          // TODO the above handlers don't support HandlerPriority
    1.46  
    1.47 @@ -141,8 +141,8 @@
    1.48                  return
    1.49  
    1.50          // Pass message to each handler until we're told to stop
    1.51 -        for handler in stanza_handlers[m.get_type()]
    1.52 -            if handler.handle_message(handler, self, m) == HandlerResult.REMOVE_MESSAGE
    1.53 +        for entry in stanza_handlers[m.get_type()]
    1.54 +            if entry.handler.handle_message(entry.handler, self, m) == HandlerResult.REMOVE_MESSAGE
    1.55                  break
    1.56  
    1.57      def inline _stream_error_cb(handler : MessageHandler,
    1.58 @@ -300,17 +300,24 @@
    1.59          return self.send(message)
    1.60  
    1.61  
    1.62 -    def register_message_handler (handler : MessageHandler, type : MessageType,
    1.63 +    def register_message_handler (handler : MessageHandler,
    1.64 +                                  type : MessageType,
    1.65                                    priority : HandlerPriority)
    1.66 -        //hd : HandlerData
    1.67 -
    1.68          // Bail if unknown message type is passed
    1.69 -        if type >= MessageType.UNKNOWN
    1.70 +        if type >= self.stanza_handlers.length
    1.71              return
    1.72  
    1.73 -        // TODO use HandlerData struct for sorting by priority
    1.74 -        stanza_handlers[type].add(handler)
    1.75 -        // stanza_handlers[type].sort_with_data(handler_compare_func)
    1.76 +        stanza_handlers[type].add(new MessageHandlerEntry(handler, priority))
    1.77 +        stanza_handlers[type].sort_with_data((GLib.CompareDataFunc)
    1.78 +                                             MessageHandlerEntry.compare)
    1.79 +
    1.80 +
    1.81 +    def unregister_message_handler (handler : MessageHandler,
    1.82 +                                    type : MessageType)
    1.83 +        for entry in stanza_handlers[type]
    1.84 +            if entry.handler == handler
    1.85 +                stanza_handlers[type].remove(entry)
    1.86 +                break
    1.87  
    1.88  
    1.89      //////////////////////////////////////////////////////////////////////////
    1.90 @@ -374,4 +381,22 @@
    1.91              return _proxy
    1.92      // TODO prop ssl
    1.93  
    1.94 +    //////////////////////////////////////////////////////////////////////////
    1.95 +    // MessageHandlerEntry
    1.96 +    //
    1.97 +    // This class is for a stanza_handlers to record a handler with a specific
    1.98 +    // priority and allow sorting by priorities.
    1.99  
   1.100 +    class MessageHandlerEntry : Object
   1.101 +        handler : MessageHandler
   1.102 +        priority : HandlerPriority
   1.103 +
   1.104 +
   1.105 +        construct (handler : MessageHandler, priority : HandlerPriority)
   1.106 +            self.handler = handler
   1.107 +            self.priority = priority
   1.108 +
   1.109 +
   1.110 +        def compare (other : MessageHandlerEntry, user_data : void*) : int
   1.111 +            return other.priority - self.priority
   1.112 +