Thanks Andrew and Thomas. Both are much better then my previous approach.
-----Original Message----- From: email@example.com [mailto:firstname.lastname@example.org] On Behalf Of Andrew Bennetts Sent: Monday, August 07, 2006 6:29 PM To: Twisted general discussion Subject: Re: [Twisted-Python] Help implement protocol
On Sat, Aug 05, 2006 at 11:44:44AM +0800, Keith Cheung (張國良) wrote:
I want to implement some handshake protocol, say..
- Client: WhatDate
- Server: 5Aug
- Client: WhatYear
- Server: 2006
- Client: WhatTime
- Server: 1005
So, for the client:- (pseudo code) def connectionMade(): send "WhatDate" state = askingForDate def lineReceived(data): switch state: askingForDate: date = data send "WhatYear" state = askingForYear break askingForYear: year = data send = "WhatTime" state = askingForTime break askingForTime: time = data state = initialState disconnect
The switch case becomes gigantic as the protocol grow. Instead of keeping states, are there better ways to implement?
Twisted code often uses dynamic method dispatch to methods with prefixed names like this:
def connectionMade(self): self.sendLine('WhatDate') self.state = 'askingForDate'
def lineReceived(self, line): handler = getattr(self, 'state_' + self.state) handler(line)
def state_askingForDate(self, line): self.date = line self.sendLine('WhatYear') self.state = 'askingForYear'
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python