I am working with a multiple port application, my issue is the about the decoder that I use, let me explain with the code
This is my tac file, where I read which port I will use from a config file, this config file have section for the port which one will listen the clients, also a decoder for the data received, for exmaple
[protocols]
keys: vt310,gt06,al900c
[al900c]
name:AL900C
port: 5010
[vt310]
name:VT310
port: 5020
[other]
name:OTHER
port: 5030
as you could see, I start telling with decoder will be searched on the config file
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)
My Server, each time start a connection, the server must use the decoder assigned at the factory creation,
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)
the problem is, at least I have noticed, always, each time one is started, the last one decoder is started, as you could see, on this case, always gt06 will be used, and I need to user the decoder according the port, this is my log, and you could notice that at 2013-05-04 11:29:56-0500, the class used for decoding change
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
You're using a global variable to specify which decoder a server on one of the ports uses:
factory = protocol.ServerFactory()
factory.protocol = TrackerServer
factory.protocol.decoder = deco
Since the global can only have one value, there can only be one decoder defined.
After this line:
factory.protocol = TrackerServer
factory.protocol
refers to the same object TrackerServer
refers to. So the program effectively does:
TrackerServer.decoder = deco
inside the loop. Pretty clear that that won't do what you want, right?
As an alternative, try factory.decoder = deco
in the loop and self.factory.decoder
in the protocol implementation (to replace self.decoder
).