r71466 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r71465‎ | r71466 | r71467 >
Date:08:43, 23 August 2010
Author:daniel
Status:deferred
Tags:
Comment:
fixed callback processing and ping
Modified paths:
  • /trunk/extensions/XMLRC/bridge/udp2xmpp.py (modified) (history)

Diff [purge]

Index: trunk/extensions/XMLRC/bridge/udp2xmpp.py
@@ -433,7 +433,7 @@
434434 pass
435435
436436 class XmppCallback (object):
437 - def __init__( self, stanza_name, timeout = 10, stanza_type = '', stanza_id = None, stanza_child_namespace = '', permanent = False, on_timeout = None, on_match = None ):
 437+ def __init__( self, stanza_name, timeout = 10, stanza_type = '', stanza_id = None, stanza_child_namespace = '', permanent = False, on_timeout = None, on_match = None, description = None ):
438438 self.stanza_name = stanza_name
439439 self.stanza_type = stanza_type
440440 self.stanza_id = stanza_id
@@ -444,17 +444,25 @@
445445
446446 self.connection = None
447447
 448+ if description is None:
 449+ self.description = "<%s type=%s id=%s ns=%s>" % ( stanza_name, stanza_type, stanza_id, stanza_child_namespace )
 450+ else:
 451+ self.description = description
 452+
448453 if on_timeout:
449454 self.on_timeout = on_timeout
450455
451456 if on_match:
452457 self.on_match = on_match
453458
 459+ def __str__(self):
 460+ return self.description
 461+
454462 def on_match(self, stanza):
455463 pass
456464
457465 def on_timeout(self):
458 - self.connection.warn("callback for %s timed out! %s" % (self.stanza_name, repr(self)))
 466+ self.connection.warn("callback timed out: %s" % (self.description))
459467
460468 def handle_tick(self):
461469 if self.timeout is None or self.permanent:
@@ -470,7 +478,7 @@
471479 def handle_stanza(self, conn, stanza):
472480 if self.matches_stanza( stanza ):
473481 try:
474 - self.on_match( stanza )
 482+ self.on_match( self, stanza )
475483 finally:
476484 if not self.permanent:
477485 self.unregister()
@@ -597,19 +605,19 @@
598606 return False
599607
600608
 609+ def process_other_stanza(self, conn, stanza):
 610+ self.debug("<< received <%s>: %s" % (stanza.getName(), stanza.__str__()) )
 611+ self.log_server_error( stanza )
 612+
601613 def process_message(self, conn, message):
602 - if (message.getError()):
603 - self.warn("received %s error from <%s>: %s" % (message.getType(), message.getError(), message.getFrom() ))
 614+ if self.log_server_error( message ):
 615+ return
604616 elif message.getBody():
605617 if message.getFrom().getResource() != self.jid.getNode(): #FIXME: this inly works if no different nick was specified when joining the channel
606618 self.debug("discarding %s message from <%s>: %s" % (message.getType(), message.getFrom(), message.getBody().strip() ))
607619
608 - def process_iq(self, conn, iq):
609 - self.debug("received iq: %s" % iq)
610 - self.log_server_error( iq )
611 -
612620 def process_presence(self, conn, presence):
613 - self.debug("received presence: %s" % presence)
 621+ self.debug("<< received presence: %s" % presence)
614622
615623 if self.log_server_error( presence ):
616624 return
@@ -662,8 +670,8 @@
663671 self.debug('authenticated using %s as %s' % ( auth, jid ) )
664672
665673 self.jabber.RegisterHandler( 'message', self.process_message )
666 - self.jabber.RegisterHandler( 'iq', self.process_iq )
667674 self.jabber.RegisterHandler( 'presence', self.process_presence )
 675+ self.jabber.RegisterHandler( 'iq', self.process_other_stanza )
668676
669677 self.jid = jid;
670678 self.info( 'connected as %s' % ( jid ) )
@@ -718,18 +726,19 @@
719727 def ping( self ):
720728 ping_id = "ping-%s" % random.randint(1000000, 9999999)
721729
722 - ping = xmpp.Iq( typ='get', attrs={ 'id': ping_id }, to= self.jid.getDomain(), frm= self.jid.getStripped() )
 730+ ping = xmpp.Iq( typ='get', attrs={ 'id': ping_id }, to= self.jid.getDomain(), frm= self.jid )
723731 ping.addChild( name= "ping", namespace = "urn:xmpp:ping" )
724 - self.jabber.send( ping )
725 - self.debug('XMPP ping sent')
726732
727 - callback = XmppCallback('iq', stanza_id = ping_id, stanza_child_namespace = 'urn:xmpp:ping',
 733+ callback = XmppCallback(stanza_name = 'iq', stanza_id = ping_id, stanza_type = 'result',
728734 timeout = 10,
729 - on_match = lambda cb, stanza: self.info('response received to ping!')
 735+ on_match = lambda cb, stanza: self.info('<< response received to ping!')
730736 )
731737
732 - self.add_callback( callback )
 738+ self.add_callback( callback ) #NOTE: register callback first, otherwise we might miss the response!
733739
 740+ self.jabber.send( ping )
 741+ self.debug('>> XMPP ping sent: %s' % ping.__str__() )
 742+
734743 def reconnect_backoff( self ):
735744 self.debug( "reconnect_backoff: tick = %d, tock = %d" % (self.backoff_tick, self.backoff_tock) )
736745
@@ -818,7 +827,7 @@
819828 body = packet[0]
820829 addr = packet[1] #TODO: optionally filter...
821830
822 - self.debug( "received packet from %s:%s" % addr )
 831+ self.debug( "<< received packet from %s:%s" % addr )
823832 self.process_rc_packet( body )
824833
825834 def process_rc_packet(self, data):

Status & tagging log