الملتوية المؤجلة غير قابلة للاستدعاء بعد فشل الاتصال

StackOverflow https://stackoverflow.com/questions/7368891

سؤال

وجود مصنع موكلي هنا:

import logging, traceback
from twisted.internet.protocol import ClientFactory
from twisted.internet import defer, reactor, ssl
from twisted.application import service
from protocols.smpp.protocol import SMPPClientProtocol

class SMPPClientFactory(ClientFactory):

    protocol = SMPPClientProtocol

    def __init__(self, config):
        self.config = config

    def getConfig(self):
        return self.config

    def clientConnectionFailed(self, connector, reason):
        print "clientConnectionFailed"

        self.connectDeferred.errback(reason)

    def clientConnectionLost(self, connector, reason):
        print "clientConnectionLost"

    def connect(self):
        self.connectDeferred = defer.Deferred()
        factory = SMPPClientFactory(self.config, self.msgHandler)

        self.log.warning('Establishing TCP connection to %s:%d' % (self.config.host, self.config.port))
        reactor.connectTCP(self.config.host, self.config.port, factory)

        return self.connectDeferred

وهو يطلق الكود هنا:

import logging, traceback
from twisted.internet import reactor, defer
from protocols.smpp.configs import SMPPClientConfig
from protocols.smpp.smpp_operations import SMPPOperationFactory
from testbed.client import SMPPClientFactory

class SMPP(object):

    def __init__(self, config=None):
        if config is None:
            config = SMPPClientConfig()

        self.config = config
        self.opFactory = SMPPOperationFactory(config)

    def run(self):
        try:
            #Bind
            SMPPClientFactory(self.config, self.handleMsg).connect().addErrback(self.connectFailed)
        except Exception, e:
            print "ERROR: %s" % str(e)

    def connectFailed(self, reason):
        print "Connection failed %s" % str(reason)

    def handleMsg(self, smpp, pdu):
        pass

if __name__ == '__main__':
    config = SMPPClientConfig(host='127.0.0.1', port=2775, username='smppclient1', password='password',
                              log_level=logging.DEBUG)

    logging.basicConfig(level=config.log_level, filename=config.log_file, format=config.log_format,datefmt=config.log_dateformat)
    SMPP(config).run()
    reactor.run()

عندما فشل الاتصال (الخادم البعيد هو أسفل) ، أحصل على كلينتكونكتيونفايلد المصنع يسمى ولكن من الغريب الحصول على "استثناءات.عزوخطأ:مثيل سمبكلينتفاكتوري ليس له سمة "كونكتديفيرد"".

أحتاج إلى استدعاء الخطأ عند فشل الاتصال ، يبدو أن هناك شيئا مفقودا عند التعامل مع التأجيلات ..

هل كانت مفيدة؟

المحلول

على رمز الإطلاق الخاص بك ، قمت بإنشاء مثيل سمبكلينتفاكتوري ودعا الاتصال() على ذلك.هذا المثال بالذات سيكون له سمة كونكتديفيرد.ومع ذلك ، ربط أيضا مثيل سمبكلينتفاكتوري آخر: factory = SMPPClientFactory(self.config, self.msgHandler) وهذا هو المثال الذي استخدمته لإنشاء الاتصال الفعلي.هذا لا يحتوي على السمة كونكتديفيرد لأنه مع هذا المثال لم يتم استدعاء الاتصال.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top