These components provide simple support for sending and receiving UDP packets.
Send 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()
When 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.
All 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.
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:
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
Main loop
Safely send any data stored in the send buffer or waiting on the "inbox" inbox
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:
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
Main loop
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:
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
Main loop
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:
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
Main loop
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:
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
Main loop
Got 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, 19 Oct 2008 at 14:29:09 UTC/GMT