[Twisted-Python] Multiple clients and ports

Carlos Eduardo Sotelo Pinto carlos.sotelo.pinto at gmail.com
Fri Apr 19 07:35:44 EDT 2013

Dear Phil

I am glad on your fast answer

2013/4/19 Phil Mayers <p.mayers at imperial.ac.uk>:
> On 04/19/2013 01:33 AM, Carlos Eduardo Sotelo Pinto wrote:
>> I am doing a gps tracker wich use multiple protocols and multiple
>> clients, my problen is when I have more than one client on more than
>> one protocol at the same time, data and validation start to crashing
>> one to other

I am receiving data from gps that must be validated and keep each
connectoin independent, for exmaple

I am working with different datagram protocolos form gps, a protocol
is a kind of parsing, for exmaple one gps send a login datagram, that
on hex  could be something like

datagram 70700112345678 as first for start a comunication where 7070
and 01 is the comman that says the datagram is trying to start a
comunicacion, 12345678 is the imei or device id
datagram 707002123456780f0f0f0f0f..... is the location datagram just
the comand changee that sais that it is a location datagram and then
the location info start, it is working on 5050 port

I have other device
datagram 20200187654321 as first for start a comunication where 2020
and 01 is the comman that says the datagram is trying to start a
comunicacion, 87654321 is the imei or device id
datagram 3030020f0f0f0f0f..... is the location datagram, the header
changed and no imei is provided, it is working on port 4040

I must do that the devies start comunication after login and the parse
info, also validate headers, command and others

as both are working at the same time, when arrive the first one with
header 7070, the class wich decode this device, keep on memory and
when the second one arrive, the validation will be wrong, cause will
try to validate the header 7070 with the values 2020, and on the
seconds one devie, could start checking datagram , s no imei, with the
first one ime

I have seen it on my program, however they are different parser clases
on diferent protocols, i need to keep each device complete independent

when I have more decvice the problem is bigger on the crashes, how
could I avoid that on keeping infor independent asyncronus is ok or
cred, I dont know

Best regards

> I don't understand what you mean. Can you be more specific?
>>      def __init__(self, decoder):
>>          """Class Constructor."""
>>          decoderModule = __import__('listener.protocols.%sDecoder' %
>> (decoder, ), fromlist=['%sDecoder' % (decoder, )])
>>          decoderClass = getattr(decoderModule, '%sDecoder' % (decoder, ))
> You might want to look at "twisted.python.reflect" which contains utils
> to do that kind of "give me the python object at this import"
>>      def dataReceived(self, data):
>>          Protocol.dataReceived(self, data)
>>          """DataReceived Twisted event."""
>>          try:
>>              self.sendResponse(self.decoder.processDatagram(data))
> Not sure if this is your problem - but stream protocols (TCP) don't
> guarantee that "message" boundaries will be preserved - that is, if the
> sender does:
> write(100 bytes)
> write(100 bytes)
> ...you might get
> dataReceived(10 bytes)
> dataReceived(39 bytes)
> dataReceived(27 bytes)
> ...or any other variation - the data can arrive in arbitrary chunks.
> Unless your processDatagram is doing the reassembly, you need to
> implement message framing in your dataReceived method.
> Note that the commented out versions of your base classes were all for
> protocols which do framing (e.g. line-based, etc.)
> Normally this looks something like:
>   def dataReceived(self, data):
>      self.buffer += data
>      if hasFullMessage(self.buffer):
>        msg, self.buffer = extractMessage(self.buffer)
>        processMsg(msg)
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

Carlos Eduardo Sotelo Pinto | http://carlossotelo.com | csotelo at twitter
    GNU Linux Admin | PHP Senior Web Developer
    Mobil: RPC (Claro)+51, 958194614 | Mov: +51, 959980794
    GTalk: carlos.sotelo.pinto at gmail.com | Skype: csotelop
    MSN: carlos.sotelo.pinto at gmail.com | Yahoo: csotelop
    GNULinux RU #379182 | GNULinux RM #277661
GPG FP:697E FAB8 8E83 1D60 BBFB 2264 9E3D 5761 F855 4F6B

More information about the Twisted-Python mailing list