从AMP协议调用方法

时间:2015-10-27 19:55:13

标签: python python-2.7 twisted

我试图实现对我自己定义的属于AMP协议的方法的调用。

我按照接下来的步骤进行操作。 在客户端,我已经启动了一个反应堆,如下所示。

connector = reactor.connectSSL('localhost', 1234,\
 ClientReconnectFactory(self.CONNECTION_INFO, gsi),\
  ClientContextFactory())

self.CONNECTION_INFO为连接过程保留了一些有用的变量。

gsi是一个使用某些方法调用类的变量。

gsi = GroundStationInterface(self.CONNECTION_INFO, "Vigo", ClientProtocol)

ClientReconnectFactory是从ReconnectingClientFactory继承的对象,代码如下:

def __init__(self, CONNECTION_INFO, gsi):
    self.CONNECTION_INFO = CONNECTION_INFO
    self.gsi = gsi
    # self.continueTrying = 0

def startedConnecting(self, connector):
    log.msg('Starting connection...')

def buildProtocol(self, addr):
    log.msg('Building protocol...')
    self.resetDelay()
    return ClientProtocol(self.CONNECTION_INFO, self.gsi)

def clientConnectionLost(self, connector, reason):
    self.continueTrying = None

    log.msg('Lost connection. Reason: ', reason)
    ReconnectingClientFactory.clientConnectionLost(self,\
     connector, reason)

def clientConnectionFailed(self, connector, reason):
    self.continueTrying = None

    log.msg('Connection failed. Reason: ', reason)
    ReconnectingClientFactory.clientConnectionFailed(self,\
     connector, reason)

ClientProtocol是我的协议。

class ClientProtocol(AMP):

def __init__(self, CONNECTION_INFO, gsi):
    self.CONNECTION_INFO = CONNECTION_INFO
    self.gsi = gsi

    log.msg('he pasado por el __init_- de ClientProtocol')

def connectionMade(self):
    self.user_login()
    self.gsi.connectProtocol(self)

def connectionLost(self, reason):
    log.err("Connection lost")
    log.err(reason)
    self.gsi.disconnectProtocol()

@inlineCallbacks
def user_login(self):        
    try:
        res = yield self.callRemote(Login,\
         sUsername=self.CONNECTION_INFO['username'],\
          sPassword=self.CONNECTION_INFO['password'])
        log.msg(res)
        res = yield self.callRemote(StartRemote,\
         iSlotId=self.CONNECTION_INFO['slot_id'])
        log.msg(res)
    except Exception as e:
        log.err(e)
        reactor.stop()

def processFrame(self, frame):

    log.msg('Received frame: ' + frame)
    res = yield self.callRemote(SendMsg, sMsg=frame,\
     iTimestamp=misc.get_utc_timestamp())

    log.msg(res)

如何从对象gsi调用函数procressframe? 该对象属于另一个模块。

我尝试使用装饰器@staticmethod创建一个调用,但它没有认识到调用方法callRemote,而对象gsi是从ClientProtocol中获得的。

我将不胜感激。

0 个答案:

没有答案