Broadcast server

Matt Hammond matt.hammond at rd.bbc.co.uk
Thu Aug 31 07:33:56 EDT 2006


On Thu, 31 Aug 2006 10:14:15 +0100, <swell at netcourrier.com> wrote:

> I would like to write a server with the low level API of python (
> socket+select and/or socket+thread ) that allow me to register client
> and update them every X seconds ( could be the time, the temperature, a
> stock quote, a message , ... ).
>
> How to write the server that keep hot connections with clients and
> update them when events are trigerred. I don't know how to begin this ,
> i look at the python doc but the doc is more related to client updating
> the server and i am not sure of the right design that could be use
> here.

Kamaelia ( http://kamaelia.sf.net/ ) - the project I'm working on, feels  
like it may be well suited to this kind of task. Its based around having  
individual components that pass messages to each other. So you can  
envisage an event source propogating a message to all subscribed clients  
quite easily.

Your source's of events can be components, that publish their events to a  
'backplane' - a kind of distribution board that replicates messages sent  
to it to all subscribers.

     from Axon.Component import component
     from Kamaelia.Util.Backplane import Backplane, subscribeTo, publishTo
     from Kamaelia.Util.PipelineComponent import pipeline

     class EventSource(component):
         def main(self):
             while 1:
                 if event_happens:
                     self.send( "EVENT HAPPENED!\n", "outbox")
                 yield 1

     pipeline( EventSource(),
               publishTo("Events"),
             ).activate()

     Backplane("Events").activate()

You can have any number of event sources, all 'publishing' to the same  
backplane.

Then to handle clients connecting I'd create a server component, with a  
factory function that creates a component to handle each client  
connection. In this case, a component that 'subscribes' to messages coming  
 from the backplane:

     from Kamaelia.Chassis.ConnectedServer import SimpleServer

     def clientHandlerFactory():
         return subscribeTo("Events")

     SimpleServer(clientHandlerFactory).activate()

Then I'd start the system running:

     from Axon.Scheduler import scheduler

     scheduler.run.runThreads()

Things like the client handling could be extended relatively easily to  
marshall complex data types to strings, suitable for network transmission;  
or to manage clients choosing what data sources to subscribe to.

If you think this approach might work for you, I'm happy to give you a  
hand with getting Kamaelia up and running, and with learning how to write  
your own components to make the system more sophisticated.

We hang about on #kamaelia on freenode (irc) - please do drop in for a  
chat!

Hope this helps!


Matt
-- 
| Matt Hammond
| Research Engineer, Tech. Group, BBC, Kingswood Warren, Tadworth, Surrey,  
UK
| http://kamaelia.sf.net/
| http://www.bbc.co.uk/rd/



More information about the Python-list mailing list