August'24: Kamaelia is in maintenance mode and will recieve periodic updates, about twice a year, primarily targeted around Python 3 and ecosystem compatibility. PRs are always welcome. Latest Release: 1.14.32 (2024/3/24)

Kamaelia.Internet.UDP

Simple UDP components

These components provide simple support for sending and receiving UDP packets.

Note that this components are deemed somewhat experimental.

Example Usage

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()

Behaviour

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.

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).

None of these components terminate. They ignore any messages sent to their "control" inbox and do not send anything out of their "signal" outbox.

Implementation Details

SimplePeer, TargettedPeer and PostboxPeer are all derived from the base class BasicPeer. BasicPeer provides some basic code for 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.PostboxPeer

class 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:

  • localaddr -- Optional. The local addresss (interface) to bind to. (default="0.0.0.0")
  • localport -- Optional. The local port to bind to. (default=0)

Inboxes

  • control : Not listened to
  • inbox : Send (host,port,data) tuples here to send a UDP packet to (host,port) containing data

Outboxes

  • outbox : Data received on the socket is passed out here, form: ((host, port), data)
  • signal : No data sent to

Methods defined here

Warning!

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])

main(self)

Methods inherited from Kamaelia.Internet.UDP.BasicPeer :

Kamaelia.Internet.UDP.SimplePeer

class 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:

  • localaddr -- Optional. The local addresss (interface) to bind to. (default="0.0.0.0")
  • localport -- Optional. The local port to bind to. (default=0)
  • receiver_addr -- Optional. The address the receiver is bound to - to which packets will be sent. (default="0.0.0.0")
  • receiver_port -- Optional. The port the receiver is bound on - to which packets will be sent. (default=0)

Inboxes

  • control : NOT USED
  • inbox : Raw binary string data packets to be sent to the destination (receiver host,port)

Outboxes

  • outbox : (data,(host,port)) tuples for each packet received
  • signal : NOT USED

Methods defined here

Warning!

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])

main(self)

Methods inherited from Kamaelia.Internet.UDP.BasicPeer :

Kamaelia.Internet.UDP.TargettedPeer

class 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:

  • localaddr -- Optional. The local addresss (interface) to bind to. (default="0.0.0.0")
  • localport -- Optional. The local port to bind to. (default=0)
  • receiver_addr -- Optional. The address the receiver is bound to - to which packets will be sent. (default="0.0.0.0")
  • receiver_port -- Optional. The port the receiver is bound on - to which packets will be sent. (default=0)

Inboxes

  • control : Not listened to
  • inbox : Data recieved here is sent to the reciever addr/port
  • target : Data receieved here changes the receiver addr/port data is tuple form: (host, port)

Outboxes

  • outbox : Data received on the socket is passed out here, form: (data,(host, port))
  • signal : No data sent to

Methods defined here

Warning!

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])

main(self)

Methods inherited from Kamaelia.Internet.UDP.BasicPeer :

Feedback

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, 05 Jun 2009 at 03:01:38 UTC/GMT