[Twisted-Python] Help implement protocol
Hi all, I want to implement some handshake protocol, say.. 1. Client: WhatDate 2. Server: 5Aug 3. Client: WhatYear 4. Server: 2006 5. Client: WhatTime 6. 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?
Maybe you want to take a look at the FTPClientBasic class in http://twistedmatrix.com/trac/browser/trunk/twisted/protocols/ftp.py . There the author basically uses a queue of actions (=hand shakes) to be run, associating response lines to commands, and returning the results via deferreds.... On Sat, 2006-08-05 at 11:44 +0800, Keith Cheung (張國良) wrote:
Hi all,
I want to implement some handshake protocol, say..
1. Client: WhatDate 2. Server: 5Aug 3. Client: WhatYear 4. Server: 2006 5. Client: WhatTime 6. 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-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
On Sat, Aug 05, 2006 at 11:44:44AM +0800, Keith Cheung (張國良) wrote:
Hi all,
I want to implement some handshake protocol, say..
1. Client: WhatDate 2. Server: 5Aug 3. Client: WhatYear 4. Server: 2006 5. Client: WhatTime 6. 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' ... -Andrew.
On 8/4/06, Keith Cheung (張國良) <keith.cheung@lkmachinery.com.hk> wrote:
Hi all,
I want to implement some handshake protocol, say..
1. Client: WhatDate 2. Server: 5Aug 3. Client: WhatYear 4. Server: 2006 5. Client: WhatTime 6. 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?
I know this expands the scope of what you have to learn, but look at State Machine Compiler. You can create a single object, and then generate all the state management code from SMC. -- If you don't know what you want, you probably need a nap.
participants (4)
-
Andrew Bennetts
-
jarrod roberson
-
Keith Cheung (張國良)
-
Thomas Jacob