Index: trunk/extensions/XMLRC/bridge/udp2xmpp.py |
— | — | @@ -433,7 +433,7 @@ |
434 | 434 | pass |
435 | 435 | |
436 | 436 | 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 ): |
438 | 438 | self.stanza_name = stanza_name |
439 | 439 | self.stanza_type = stanza_type |
440 | 440 | self.stanza_id = stanza_id |
— | — | @@ -444,17 +444,25 @@ |
445 | 445 | |
446 | 446 | self.connection = None |
447 | 447 | |
| 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 | + |
448 | 453 | if on_timeout: |
449 | 454 | self.on_timeout = on_timeout |
450 | 455 | |
451 | 456 | if on_match: |
452 | 457 | self.on_match = on_match |
453 | 458 | |
| 459 | + def __str__(self): |
| 460 | + return self.description |
| 461 | + |
454 | 462 | def on_match(self, stanza): |
455 | 463 | pass |
456 | 464 | |
457 | 465 | 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)) |
459 | 467 | |
460 | 468 | def handle_tick(self): |
461 | 469 | if self.timeout is None or self.permanent: |
— | — | @@ -470,7 +478,7 @@ |
471 | 479 | def handle_stanza(self, conn, stanza): |
472 | 480 | if self.matches_stanza( stanza ): |
473 | 481 | try: |
474 | | - self.on_match( stanza ) |
| 482 | + self.on_match( self, stanza ) |
475 | 483 | finally: |
476 | 484 | if not self.permanent: |
477 | 485 | self.unregister() |
— | — | @@ -597,19 +605,19 @@ |
598 | 606 | return False |
599 | 607 | |
600 | 608 | |
| 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 | + |
601 | 613 | 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 |
604 | 616 | elif message.getBody(): |
605 | 617 | if message.getFrom().getResource() != self.jid.getNode(): #FIXME: this inly works if no different nick was specified when joining the channel |
606 | 618 | self.debug("discarding %s message from <%s>: %s" % (message.getType(), message.getFrom(), message.getBody().strip() )) |
607 | 619 | |
608 | | - def process_iq(self, conn, iq): |
609 | | - self.debug("received iq: %s" % iq) |
610 | | - self.log_server_error( iq ) |
611 | | - |
612 | 620 | def process_presence(self, conn, presence): |
613 | | - self.debug("received presence: %s" % presence) |
| 621 | + self.debug("<< received presence: %s" % presence) |
614 | 622 | |
615 | 623 | if self.log_server_error( presence ): |
616 | 624 | return |
— | — | @@ -662,8 +670,8 @@ |
663 | 671 | self.debug('authenticated using %s as %s' % ( auth, jid ) ) |
664 | 672 | |
665 | 673 | self.jabber.RegisterHandler( 'message', self.process_message ) |
666 | | - self.jabber.RegisterHandler( 'iq', self.process_iq ) |
667 | 674 | self.jabber.RegisterHandler( 'presence', self.process_presence ) |
| 675 | + self.jabber.RegisterHandler( 'iq', self.process_other_stanza ) |
668 | 676 | |
669 | 677 | self.jid = jid; |
670 | 678 | self.info( 'connected as %s' % ( jid ) ) |
— | — | @@ -718,18 +726,19 @@ |
719 | 727 | def ping( self ): |
720 | 728 | ping_id = "ping-%s" % random.randint(1000000, 9999999) |
721 | 729 | |
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 ) |
723 | 731 | ping.addChild( name= "ping", namespace = "urn:xmpp:ping" ) |
724 | | - self.jabber.send( ping ) |
725 | | - self.debug('XMPP ping sent') |
726 | 732 | |
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', |
728 | 734 | 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!') |
730 | 736 | ) |
731 | 737 | |
732 | | - self.add_callback( callback ) |
| 738 | + self.add_callback( callback ) #NOTE: register callback first, otherwise we might miss the response! |
733 | 739 | |
| 740 | + self.jabber.send( ping ) |
| 741 | + self.debug('>> XMPP ping sent: %s' % ping.__str__() ) |
| 742 | + |
734 | 743 | def reconnect_backoff( self ): |
735 | 744 | self.debug( "reconnect_backoff: tick = %d, tock = %d" % (self.backoff_tick, self.backoff_tock) ) |
736 | 745 | |
— | — | @@ -818,7 +827,7 @@ |
819 | 828 | body = packet[0] |
820 | 829 | addr = packet[1] #TODO: optionally filter... |
821 | 830 | |
822 | | - self.debug( "received packet from %s:%s" % addr ) |
| 831 | + self.debug( "<< received packet from %s:%s" % addr ) |
823 | 832 | self.process_rc_packet( body ) |
824 | 833 | |
825 | 834 | def process_rc_packet(self, data): |