扭曲的多个端口

时间:2013-05-03 23:41:57

标签: python python-2.7 gps twisted twisted.application

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

这是我的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 

1 个答案:

答案 0 :(得分: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)。