1

我正在使用多端口应用程序,我的问题是关于我使用的解码器,让我用代码解释一下

这是我的 tac 文件,我从配置文件中读取我将使用的端口,该配置文件包含用于侦听客户端的端口的部分,还有接收数据的解码器,例如

[protocols]
keys: vt310,gt06,al900c

[al900c]
name:AL900C
port: 5010

[vt310]
name:VT310
port: 5020

[other]
name:OTHER
port: 5030

如您所见,我开始告诉解码器将在配置文件中进行搜索

import os, sys
import ConfigParser
from twisted.application import internet, service
from twisted.internet import protocol
from listener.TrackerServer import TrackerServer

PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))
sys.path.append(PROJECT_DIR)

path = None
config = ConfigParser.ConfigParser()
config.read('protocols.cfg')

application = service.Application("tracker")
for device in config.get('protocols', 'keys').split(','):
    factory = protocol.ServerFactory()
    factory.clients = []
    factory.decoder = None
    factory.protocol = TrackerServer
    factory.devicer = config.get(device, 'name')
    internet.TCPServer(int(config.get(device, 'port')), factory).setServiceParent(application)

我的服务器,每次启动连接,服务器必须使用出厂时分配的解码器,

from twisted.internet import threads
from twisted.internet.protocol import Factory, Protocol

class TrackerServer(Protocol):
    """Tracker Twisted Protocol Class."""

    def connectionMade(self):
        """ConnectionMade Twisted event."""
        try:
            decoderModule = __import__('listener.protocols.%sDecoder' % (self.factory.devicer, ), fromlist=['%sDecoder' % (self.factory.devicer, )])
            decoderClass = getattr(decoderModule, '%sDecoder' % (self.factory.devicer, ))

            self.factory.decoder = decoderClass()
            print 'Connection made to', self.transport.getHost(), 'from', self.transport.getPeer()
            self.factory.clients.append(self)
        except ValueError:
            print "Oops!  Connection was not started"

    def connectionLost(self, reason):
        """ConnectionLost Twisted event."""
        self.factory.clients.remove(self)
        print "Connection lost ", reason

    def dataReceived(self, data):
        """DataReceived Twisted event."""
        #try:
        """ how to precess here a line for a specific client"""
        self.sendResponse(self.factory.decoder.processDatagram(data))
            #d = threads.deferToThread(self.factory.decoder.processDatagram(data ))
            #d.addCallback(self.sendResponse)
        #except ValueError:
        #    print "Oops!  That was no valid data.  Try again..."

    def sendResponse (self, response):
        self.transport.write(response)

问题是,至少我注意到,总是每次启动一个解码器时,都会启动最后一个解码器,如您所见,在这种情况下,总是使用 gt06,我需要根据端口使用解码器,这是我的日志,你可以注意到在 2013-05-04 11:29:56-0500,用于解码的类发生了变化

2013-05-08 09:09:05-0500 [TrackerServer,20,200.108.107.17] [03863077 @ GT06Decoder : Loging] 78780d01086471700386307700af40fe0d0a 
2013-05-08 09:09:07-0500 [TrackerServer,20,200.108.107.17] [03863077 @ GT06Decoder : Locating] lat: -16.4255433333, lng: -71.5245183333 
2013-05-08 09:09:09-0500 [TrackerServer,20,200.108.107.17] [03863077 @ GT06Decoder : Status] 78780a13440604000200b1085f0d0a 
2013-05-08 09:09:10-0500 [TrackerServer,20,200.108.107.17] [03863077 @ GT06Decoder : Locating] lat: -16.42506, lng: -71.5245766667 
2013-05-08 09:09:17-0500 [TrackerServer,20,200.108.107.17] [03863077 @ GT06Decoder : Locating] lat: -16.4262866667, lng: -71.5243683333 
2013-05-08 09:09:23-0500 [TrackerServer,20,200.108.107.17] [03863077 @ GT06Decoder : Locating] lat: -16.42436, lng: -71.5247866667 
2013-05-08 09:09:25-0500 [TrackerServer,20,200.108.107.17] [03863077 @ GT06Decoder : Locating] lat: -16.4249, lng: -71.5246266667 
2013-05-08 09:09:27-0500 [TrackerServer,20,200.108.107.17] [03863077 @ GT06Decoder : Locating] lat: -16.4271233333, lng: -71.524145 
2013-05-08 09:09:38-0500 [TrackerServer,20,200.108.107.17] [03863077 @ GT06Decoder : Locating] lat: -16.428455, lng: -71.5238933333 
2013-05-08 09:09:43-0500 [TrackerServer,1,190.113.208.135] [11742151 @ AL900CDecoder : Header is no valid] 2929b10007117421510caf0d 
2013-05-08 09:09:47-0500 [TrackerServer,20,200.108.107.17] [11742151 @ GT06Decoder : Locating] lat: -16.4291516667, lng: -71.5238566667 
2013-05-08 09:09:59-0500 [TrackerServer,20,200.108.107.17] [11742151 @ GT06Decoder : Locating] lat: -16.4293483333, lng: -71.5248666667 
2013-05-08 09:10:08-0500 [TrackerServer,20,200.108.107.17] [11742151 @ GT06Decoder : Locating] lat: -16.4294916667, lng: -71.525855 
2013-05-08 09:10:18-0500 [TrackerServer,20,200.108.107.17] [11742151 @ GT06Decoder : Locating] lat: -16.4296616667, lng: -71.526645 
2013-05-08 09:10:27-0500 [TrackerServer,20,200.108.107.17] [11742151 @ GT06Decoder : Locating] lat: -16.4297583333, lng: -71.5266416667 
2013-05-08 09:10:37-0500 [TrackerServer,20,200.108.107.17] [11742151 @ GT06Decoder : Locating] lat: -16.430435, lng: -71.5265033333 
2013-05-08 09:10:47-0500 [TrackerServer,20,200.108.107.17] [11742151 @ GT06Decoder : Locating] lat: -16.43122, lng: -71.5263833333 
2013-05-08 09:10:57-0500 [TrackerServer,20,200.108.107.17] [11742151 @ GT06Decoder : Locating] lat: -16.4317583333, lng: -71.52629 
2013-05-08 09:11:07-0500 [TrackerServer,20,200.108.107.17] [11742151 @ GT06Decoder : Locating] lat: -16.4323416667, lng: -71.5262116667 
2013-05-08 09:11:17-0500 [TrackerServer,20,200.108.107.17] [11742151 @ GT06Decoder : Locating] lat: -16.4328383333, lng: -71.5261283333 
2013-05-08 09:11:27-0500 [TrackerServer,20,200.108.107.17] [11742151 @ GT06Decoder : Locating] lat: -16.43324, lng: -71.526085 
2013-05-08 09:11:38-0500 [TrackerServer,20,200.108.107.17] [11742151 @ GT06Decoder : Locating] lat: -16.43384, lng: -71.5259933333 
2013-05-08 09:11:47-0500 [TrackerServer,20,200.108.107.17] [11742151 @ GT06Decoder : Locating] lat: -16.4342116667, lng: -71.5259316667 
2013-05-08 09:11:56-0500 [TrackerServer,21,200.108.107.17] [32045214 @ AL900CDecoder : Header is no valid] 2929b10007320452140cca0d 
2013-05-08 09:11:57-0500 [TrackerServer,20,200.108.107.17] [32045214 @ GT06Decoder : Locating] lat: -16.4349533333, lng: -71.52572 
2013-05-08 09:12:07-0500 [TrackerServer,20,200.108.107.17] [32045214 @ GT06Decoder : Locating] lat: -16.43544, lng: -71.5255333333 
2013-05-08 09:12:08-0500 [TrackerServer,20,200.108.107.17] [32045214 @ GT06Decoder : Status] 78780a13440604000200c71eee0d0a 
2013-05-08 09:12:17-0500 [TrackerServer,20,200.108.107.17] [32045214 @ GT06Decoder : Locating] lat: -16.43572, lng: -71.52553 
2013-05-08 09:12:18-0500 [TrackerServer,21,200.108.107.17] [32045214 @ AL900CDecoder : Header is no valid] 2929b10007320452140cca0d 
2013-05-08 09:12:27-0500 [TrackerServer,20,200.108.107.17] [32045214 @ GT06Decoder : Locating] lat: -16.4359733333, lng: -71.526235 
4

1 回答 1

3

您正在使用全局变量来指定其中一个端口上的服务器使用哪个解码器:

factory = protocol.ServerFactory()
factory.protocol = TrackerServer
factory.protocol.decoder = deco

由于全局只能有一个值,因此只能定义一个解码器。

在这一行之后:

factory.protocol = TrackerServer

factory.protocol指代同一个对象TrackerServer。所以程序有效地做了:

TrackerServer.decoder = deco

循环内。很明显,这不会做你想要的,对吧?

作为替代方案,尝试factory.decoder = deco在循环和self.factory.decoder协议实现中(替换self.decoder)。

于 2013-05-04T09:42:00.440 回答