[Twisted-Python] data update to multiple clients

Probably another newbie question but after much effort I am not progressing on the following. I am trying to design a system where I have a microprocessor periodically feeding data on a serial connection to a server script. I would then like the server script to notify one or more client scripts of the changed data across a local network. I have tried a few simple tcp server and client script examples which communicate well for a single call but do not seem to suit my application which is not client event driven. I considered having each client connect to the server every ?? seconds to query for new data but there may be a better way. Can anyone suggest a suitable design approach using twisted? Is there any example code available that may be similar. Chris

On 04/05/2010 12:39 PM, Chris Cusack wrote:
Perhaps I'm misunderstanding, but it's pretty simple. Just have the clients connect & stay connected. Keep a list of currently connected clients somewhere (see below) and every time you receive data on the serial port, write it out to all the clients. For example: # ack, global variable! client_list = [] class MyProtocol(Protocol): def connectionMade(self): client_list.append(self) def connectionLost(self, reason): if self in client_list: client_list.remove(self) def notifyClient(self, data): # you will probably want some framing here e.g. Netstring, len+data self.transport.write(data) class ServerProto(Protocol): def dataReceived(self, data): for cli in client_list: cli.notifyClient(data) ...or am I misunderstanding you?

On Apr 5, 2010, at 7:39 AM, Chris Cusack wrote:
Can anyone suggest a suitable design approach using twisted? Is there any example code available that may be similar.
This sounds like it may be a FAQ: <http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#HowdoImakeinputo...> Does that answer your question?

On 04/05/2010 12:39 PM, Chris Cusack wrote:
Perhaps I'm misunderstanding, but it's pretty simple. Just have the clients connect & stay connected. Keep a list of currently connected clients somewhere (see below) and every time you receive data on the serial port, write it out to all the clients. For example: # ack, global variable! client_list = [] class MyProtocol(Protocol): def connectionMade(self): client_list.append(self) def connectionLost(self, reason): if self in client_list: client_list.remove(self) def notifyClient(self, data): # you will probably want some framing here e.g. Netstring, len+data self.transport.write(data) class ServerProto(Protocol): def dataReceived(self, data): for cli in client_list: cli.notifyClient(data) ...or am I misunderstanding you?

On Apr 5, 2010, at 7:39 AM, Chris Cusack wrote:
Can anyone suggest a suitable design approach using twisted? Is there any example code available that may be similar.
This sounds like it may be a FAQ: <http://twistedmatrix.com/trac/wiki/FrequentlyAskedQuestions#HowdoImakeinputo...> Does that answer your question?
participants (3)
-
Chris Cusack
-
Glyph Lefkowitz
-
Phil Mayers