[Twisted-Python] Need help pulling things together.

Greetings, I've struggled to learn Twisted for a couple of years, usually hitting the same wall I've hit yet again and giving up. I acknowledge the huge benefit to using Twisted for what I want to do, so I wanted to see if some nice folks out there can help me pull some things together. I've gone through the docs, previous posts, Twisted code and examples etc. quite extensively. Things just have not gelled for me. The project I'm working on is, at its core, a relatively basic chat server. There are "Areas" and "Users". What I do now with asynchat is create a new User() object for each UserSocket() object (each user who connects). The UserSocket() class parses the socket data and does a getattr(), i.e. if the client sends a "sendAreaMessage" command I getattr() User.do_sendAreaMessage(). The work is done in the User class, then an event is sent back to the user. This is the pattern my brain wants to use with Twisted, and I'm not sure whether or not I'm off to a bad start. I took this from the chat server example: class ChatProtocol(basic.LineReceiver): delimiter = '\n' def connectionMade(self): print "Got new client!" self.user = User(self) self.message("Welcome to chat.") def connectionLost(self, reason): print "Lost a client!" def lineReceived(self, line): data = line.split(" ") getattr(self.user,'do_'+data[0])(data[1:]) def message(self, message): self.transport.write(message + self.delimiter) class ChatFactory(Factory): protocol = ChatProtocol class Area: pass # Area stuff class User: def __init__(self,protocol): print "New user" self.protocol = protocol def do_foo(self,args): message = "do_foo with args: "+ ' '.join(args) self.protocol.transport.write(message + "\n") application = service.Application("sserverd") internet.TCPServer(4001, ChatFactory()).setServiceParent(application) With all the different examples I'm having a hard time finding the "right" way to write my server using Twisted. I thought I was on to something reading through the IRC server protocol, but without a full server example I got lost. Do I set up a generic handleCommand() like interface and define each command in the protocol class (i.e. do_sendMessage) and have callbacks that my User class redefines/implements? Or am I on the right path? I really appreciate any input on this! Brendon

Brendon Colby wrote:
With all the different examples I'm having a hard time finding the "right" way to write my server using Twisted. I thought I was on to something reading through the IRC server protocol, but without a full server example I got lost. Do I set up a generic handleCommand() like interface and define each command in the protocol class (i.e. do_sendMessage) and have callbacks that my User class redefines/implements? Or am I on the right path?
I really appreciate any input on this!
Hi Brendon, I'm having a hard time working out what you've got and where you're trying to go. I think you're asking where you should you place the abstraction between data from lineReceived and executing specific commands based on that data. I shall respond with the caveat that I'm not qualified to speak for the "right twisted way", but I hope I can be of some assistance. The first answer that springs to mind for most of your questions is "depends on what you're trying to achieve with your project". Where to place an abstraction layer is something that I often end up hitting my head against, and generally I've found this to be time-wasting vacillation. I'd honestly recommend you get on and code your management of users and of areas and worry about the abstraction layer later. Sitting around postulating doesn't get any code written and re-factoring small projects doesn't take that long. If it were me and I was writing a telnet-based chat server, I would probably separate chat messages from commands and make this distinction in the protocol. I'd handle command interpretation in the User class. class ChatProtocol...... def lineReceived(self,line): if line[0] == '/': self.user.cmdReceived(line[1:]) else: self.user.msgReceived(line) I hope this helps. Please point out if I've entirely misinterpreted your question. Cheers, Matt.

On 5/25/07, Matt Hubbard <m.hubbard@imperial.ac.uk> wrote:
Hi Brendon,
I'm having a hard time working out what you've got and where you're trying to go.
I think you're asking where you should you place the abstraction between data from lineReceived and executing specific commands based on that data. I shall respond with the caveat that I'm not qualified to speak for the "right twisted way", but I hope I can be of some assistance.
The first answer that springs to mind for most of your questions is "depends on what you're trying to achieve with your project".
Where to place an abstraction layer is something that I often end up hitting my head against, and generally I've found this to be time-wasting vacillation.
I'd honestly recommend you get on and code your management of users and of areas and worry about the abstraction layer later. Sitting around postulating doesn't get any code written and re-factoring small projects doesn't take that long.
If it were me and I was writing a telnet-based chat server, I would probably separate chat messages from commands and make this distinction in the protocol. I'd handle command interpretation in the User class.
class ChatProtocol...... def lineReceived(self,line): if line[0] == '/': self.user.cmdReceived(line[1:]) else: self.user.msgReceived(line)
I hope this helps. Please point out if I've entirely misinterpreted your question.
Cheers, Matt.
Hi Matt, You are exactly right. I've decided to just start coding and figure it out. I've been vacillating for two years! :) Plus I've found a protocol class that I can model (roughly). Thank you for your input! Brendon
participants (2)
-
Brendon Colby
-
Matt Hubbard