2011-01-31 7 views
4

ConnectionLost 예외를 처리 할 수 ​​없습니다. 내가 가지고있는 간단한 예. 우선 jabber 서버에 연결하여 ping을 수행합니다. 나는 그것을 위해 wokkel 라이브러리를 사용합니다. 그런 다음 ping을 보내는 메소드에 errback을 추가합니다. errback에서 ConnectionLost 오류를 처리합니다. 그 후, 나는 인터넷 연결을 닫는다. 하지만 ConnectionLost가 처리되는지는 알 수 없습니다. 내 응용 프로그램에서 연결을 닫고 모든 예외 처리기가 호출됩니다.ConnectionLost 예외 처리가 꼬인

핑이 잘됩니다.

[XmlStream,client] Ping to JID(u'jabber.ru') started at HivemindPingClientProtocol 
[-] SEND: «iq to='jabber.ru' type='get' id='H_3'>/>» 
[XmlStream,client] RECV: "/><feature-not-implemented xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" 

인터넷 연결이

[-] SEND: «iq to='jabber.ru' type='get' id='H_6'>/>» 
[-] SEND: «iq to='jabber.ru' type='get' id='H_7'>/>» 

ConnectionLost의 핸들러

가 호출되지 않습니다 닫힙니다.
[-] Protocol stopped 
[-] Protocol closed 
[-] Transport stopped 
[XmlStream,client] Stream closed at HivemindXMPPClient 

모든 예외

스트리밍을 닫은 후 처리 방법 _disconnected StreamManager 프린트되어 "스트림 HivemindXMPPClient 폐쇄".

[XmlStream,client] Failure [Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.ConnectionLost'>: Connection to the other side was lost in a non-clean fashion. 
[XmlStream,client] Failure [Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.ConnectionLost'>: Connection to the other side was lost in a non-clean fashion.] 
[XmlStream,client] Connection lost with [Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.ConnectionDone'>: Connection was closed cleanly.] 
[XmlStream,client] Stopping factory <hivemind.network.network_core.HivemindXmlStreamFactory object at 0xa2e904c>> 

왜 스트림을 닫은 후에 errbacks가 호출되는지 설명 할 수 있습니까? 사실 내가 reconnecting 기능을 구현하고 싶습니다 (이미 ReconnectingFactory를 사용하고 있지만 ConnectionLost에는 반응하지 않습니다). 다른 사람이 꼬인 상태로 구현을 다시 연결하는 몇 가지 예를 제공 할 수 있습니까?

스크립트 예제. 이 스크립트를 실행하십시오 (ping이 제대로 작동하는지 확인하십시오). 그런 다음 인터넷 연결을 종료하십시오. 여러 번 ping이 발생하면 스크립트를 종료해야합니다. 보시다시피 ConnectionLost 오류는 연결을 닫은 후에 처리됩니다.

import sys 
from twisted.python import log 
from twisted.words.protocols import jabber 
from twisted.internet.error import ConnectionLost 
from wokkel.client import XMPPClient 
from wokkel.ping import PingClientProtocol 
from twisted.internet.task import LoopingCall 

JID = unicode('[email protected]') 
PASSWORD = 'PASSWORD' 
INTERVAL = 3 

class SpecialPingClientProtocol(PingClientProtocol): 

    def __init__(self, entity, interval): 
     self.__entity = jabber.jid.internJID(entity) 
     self.__interval = interval 
     self.__pingLoop = None 

    def _onError(self, failure): 
     log.msg('Failure %s at %s' % (failure, self.__class__.__name__)) 
     error = failure.trap(jabber.error.StanzaError, ConnectionLost) 
     if error == jabber.error.StanzaError: 
      if failure.value.condition == 'feature-not-implemented': 
       return None 
     elif error == ConnectionLost: 
      # Do some beautiful things 
      log.msg('Connection is lost. I want to reconnect NOW') 
     return failure 

    def _sendPing(self): 
     defer = self.ping(self.__entity) 
     defer.addErrback(self._onError) 

    def stopPing(self): 
     log.msg('Ping to %s stopped at %s' % (self.__entity, self.__class__.__name__)) 
     if self.__pingLoop is not None and self.__pingLoop.running: 
      self.__pingLoop.stop() 
      self.__pingLoop = None 

    def startPing(self): 
     log.msg('Ping to %s started at %s ' % (self.__entity, self.__class__.__name__)) 
     self.__pingLoop = LoopingCall(self._sendPing) 
     self.__pingLoop.start(self.__interval, now = False) 

def main(): 
    log.startLogging(sys.stdout) 
    transport = XMPPClient(jabber.jid.internJID(JID), PASSWORD) 
    transport.logTraffic = True 
    pinger = SpecialPingClientProtocol(JID, INTERVAL) 
    pinger.setHandlerParent(transport) 
    transport.startService() 
    pinger.startPing() 
    reactor.run() 

if __name__ == '__main__': 
    from twisted.internet import reactor 
    main() 

답변

2

프로토콜이 있습니다

clientConnectionFailed(self, connector, reason) 
clientConnectionLost(self, connector, reason) 

가 실제로 모두를 무시하고 전화 PingClientProtocol.clientConnectionFailed 및 PingClientProtocol.clientConnectionLost

을 PingClientProtocol이 프로토콜에서 어떻게 든

+0

감사를 상속한다고 가정 할 수 있지만, PingClientProtocol은 프로토콜이 아니라 XMPPHandler를 상속합니다. 이 메소드는 ClientFactory에 속합니다. 나는 당신이하는 말을 실행하는 방법을 모른다. 간단한 예를 들어 줄 수 있습니까? 데프 connectionLost (이유) : 당신이 대체 할 수 있도록 –

+0

http://twistedmatrix.com/documents/current/api/twisted.words.protocols.jabber.ijabber.IXMPPHandler.html 은 유사한 방법을 가지고 #이 할 당신의 물건을 여기에 # 다음 슈퍼 메서드를 호출 XMPPHandler.connectionLost (self, reason) – sherpya