Simple UDP componentsThese components provide simple support for sending and receiving UDP packets. NOTE: This set of components really an evolution of those in UDP.py, and is likely to replace those in future. Example UsageSend console input to port 1500 of myserver.com and receive packets locally on port 1501 displaying their contents (and where they came from) on the console: from Kamaelia.Chassis.Pipeline import Pipeline from Kamaelia.Util.Console import ConsoleEchoer from Kamaelia.Util.Console import ConsoleReader from Kamaelia.Internet.UDP import SimplePeer Pipeline( ConsoleReader(), SimplePeer("127.0.0.1", 1501, "myserver.com", 1500), ConsoleEchoer(), ).run() Sends data from a data source as UDP packets, changing between 3 different destinations, once per second: class DestinationSelector(component): def main(self): while 1: for dest in [ ("server1.com",1500), ("server2.com",1500), ("server3.com",1500), ]: self.send(dest,"outbox") next=time.time()+1.0 while time.time() < next: yield 1 Graphline( SOURCE = MyDataSource(), SELECT = DestinationSelector(), UDP = TargettedPeer(), linkages = { ("SOURCE", "outbox") : ("UDP", "inbox"), ("SELECT", "outbox") : ("UDP", "target"), } ).run() Send UDP packets containing "hello" to several different servers, all on port 1500: from Kamaelia.Chassis.Pipeline import Pipeline from Kamaelia.Util.DataSource import DataSource from Kamaelia.Internet.UDP import PostboxPeer Pipeline( DataSource( [ ("myserver1.com",1500,"hello"), ("myserver2.com",1500,"hello"), ("myserver3.com",1500,"hello"), ] ), PostboxPeer(), ).run() BehaviourWhen any of these components receive a UDP packet on the local address and port they are bound to; they send out a tuple (data,(host,port)) out of their "outbox" outboxes. 'data' is a string containing the payload of the packet. (host,port) is the address of the sender/originator of the packet. SimplePeer is the simplest to use. Any data sent to its "inbox" inbox is sent as a UDP packet to the destination (receiver) specified at initialisation. UDPSender and UDPReceiver duplicate the sending and receiving functionality of SimplePeer in seperate components. TargettedPeer behaves identically to SimplePeer; however the destination (receiver) it sends UDP packets to can be changed by sending a new (host,port) tuple to its "target" inbox. PostboxPeer does not have a fixed destination (receiver) to which it sends UDP packets. Send (host,port,data) tuples to its "inbox" inbox to arrange for a UDP packet containing the specified data to be sent to the specified (host,port). All of the components shutdown upon receiving a ShutdownMicroprocess message on their "control" inbox. UDPSender also shuts down upon receiving a ProducerFinished message on its "control" inbox. In this case before it shuts down it sends any data in its send queue, and any data waiting on its inbox. Implementation DetailsAll of the UDP components are all derived from the base class BasicPeer. BasicPeer provides some basic code for sending and receiving from a socket. Although technically BasicPeer is a component, it is not a usable one as it does not implement a main() method. Kamaelia.Internet.UDP_ng.PostboxPeerclass PostboxPeer(BasicPeer)PostboxPeer([localaddr][,localport]) -> new PostboxPeer component. A simple component for receiving and transmitting UDP packets. It binds to the specified local address and port - from which it will receive packets. Sends packets to individually specified destinations Arguments:
Methods defined hereWarning! You should be using the inbox/outbox interface, not these methods (except construction). This documentation is designed as a roadmap as to their functionalilty for maintainers and new component developers. __init__(self[, localaddr][, localport])x.__init__(...) initializes x; see x.__class__.__doc__ for signature main(self)Main loop sendLoop(self)Safely send any data stored in the send buffer or waiting on the "inbox" inbox Methods inherited from Kamaelia.Internet.UDP_ng.BasicPeer :
Kamaelia.Internet.UDP_ng.SimplePeerclass SimplePeer(BasicPeer)SimplePeer([localaddr][,localport][,receiver_addr][,receiver_port]) -> new SimplePeer component. A simple component for receiving and transmitting UDP packets. It binds to the specified local address and port - from which it will receive packets and sends packets to a receiver on the specified address and port. Arguments:
Methods defined hereWarning! You should be using the inbox/outbox interface, not these methods (except construction). This documentation is designed as a roadmap as to their functionalilty for maintainers and new component developers. __init__(self[, localaddr][, localport][, receiver_addr][, receiver_port])x.__init__(...) initializes x; see x.__class__.__doc__ for signature main(self)Main loop Kamaelia.Internet.UDP_ng.TargettedPeerclass TargettedPeer(BasicPeer)TargettedPeer([localaddr][,localport][,receiver_addr][,receiver_port]) -> new TargettedPeer component. A simple component for receiving and transmitting UDP packets. It binds to the specified local address and port - from which it will receive packets and sends packets to a receiver on the specified address and port. Can change where it is sending to by sending the new (addr,port) receiver address to the "target" inbox. Arguments:
Inboxes
Outboxes
Methods defined hereWarning! You should be using the inbox/outbox interface, not these methods (except construction). This documentation is designed as a roadmap as to their functionalilty for maintainers and new component developers. __init__(self[, localaddr][, localport][, receiver_addr][, receiver_port])x.__init__(...) initializes x; see x.__class__.__doc__ for signature main(self)Main loop Kamaelia.Internet.UDP_ng.UDPReceiverclass UDPReceiver(BasicPeer)UDPReceiver([localaddr][,localport]) -> new UDPReceiver component. A simple component for receiving UDP packets. It binds to the specified local address and port - from which it will receive packets. Packets received are sent to it "outbox" outbox. Arguments:
Methods defined hereWarning! You should be using the inbox/outbox interface, not these methods (except construction). This documentation is designed as a roadmap as to their functionalilty for maintainers and new component developers. __init__(self[, localaddr][, localport])x.__init__(...) initializes x; see x.__class__.__doc__ for signature main(self)Main loop Kamaelia.Internet.UDP_ng.UDPSenderclass UDPSender(BasicPeer)UDPSender([receiver_addr][,receiver_port]) -> new UDPSender component. A simple component for transmitting UDP packets. It sends packets received from the "inbox" inbox to a receiver on the specified address and port. Arguments:
Methods defined hereWarning! You should be using the inbox/outbox interface, not these methods (except construction). This documentation is designed as a roadmap as to their functionalilty for maintainers and new component developers. __init__(self[, receiver_addr][, receiver_port])x.__init__(...) initializes x; see x.__class__.__doc__ for signature main(self)Main loop FeedbackGot a problem with the documentation? Something unclear that could be clearer? Want to help improve it? Constructive criticism is very welcome - especially if you can suggest a better rewording! Please leave you feedback here in reply to the documentation thread in the Kamaelia blog. -- Automatic documentation generator, 05 Jun 2009 at 03:01:38 UTC/GMT |
Kamaelia
is an open source project originated from and guided by BBC
Research. For more information browse the site or get in
contact.
This is an ongoing community based development site. As a result the contents of this page is the opinions of the contributors of the pages involved not the organisations involved. Specificially, this page may contain personal views which are not the views of the BBC. (the site is powered by a wiki engine)
(C) Copyright 2008 Kamaelia Contributors, including the British Broadcasting Corporation, All Rights Reserved.