Source code: UDPTransport.java

index |  134 lines | javadoc ]

package nl.west.aaa;

import nl.west.aaa.*;
import java.net.*;
import java.io.*;

public class UDPTransport
    implements TransportProtocol
{

    /**
     * The maximum length of incoming packets.
     */
    static int MAXPACKETSIZE=4096;

    protected AAAUnit receiver;
    
    protected DatagramSocket sendSocket;
    
    protected int sendPort;

    /**
     * Initialize transport over UDP.
     * listenPort is the port that is listened on,
     * sendPort is the port UDP messages are sent to.
     */
    public UDPTransport(int listenPort,int sendPort)
        throws SocketException
    {
        this.sendPort=sendPort;
        sendSocket=new DatagramSocket(listenPort);
        addSocketListener(sendSocket);
    }
    
    /**
     * Provided for subclasses handling their own socket
     * management.
     */
    protected UDPTransport()
    {
    }
    
    /**
     * Handle an incoming packet and send it to the receiver (AAAUnit).
     */
    void handleIncomingPacket(DatagramPacket pack)
    {
        Identifier from=new Identifier(pack.getAddress());
        byte[] tmp=pack.getData();
        // copy the buffer so it can be reused and has the right length
        byte[] data=new byte[pack.getLength()];
        System.arraycopy(tmp,pack.getOffset(),data,0,pack.getLength());
        if (receiver!=null)
            receiver.handleIncoming(data,from,this);
    }
    
    /**
     * Start listening on the socket for new messages.
     * Any messages will be passed to the receiver (AAAUnit).
     */
    protected void addSocketListener(DatagramSocket socket)
    {
        (new Receiver(socket)).start();
    }

    /**
     * The thread that waits for incoming packtes on a socket.
     * The handleIncomingPacket() method is called.
     */
    class Receiver
        extends Thread
    {

        /**
         * The socket that is listened on.
         */
        private DatagramSocket sock;

        /**
         * Listen on the given socket.
         */
        Receiver(DatagramSocket sock)
        {
            this.sock=sock;
        }
    
        /**
         * Bind the given port and pass any packets
         * to handleIncomingPacket().
         */
        public void run()
        {
            try
            {
                // wait for packets
                DatagramPacket pack=new DatagramPacket(new byte[MAXPACKETSIZE],MAXPACKETSIZE);
                while(true)
                {
                    sock.receive(pack);
                    handleIncomingPacket(pack);
                }
            }
            catch (IOException e)
            {
            }
        }
    
    }

    /**
     * Send the given data to the destination.
     * The UDP packet is sent to the sendPort port
     * on the receiver.
     */    
    public void sendMessage(byte[] data,Identifier id)
        throws IOException
    {
        // get the address
        InetAddress addr=InetAddress.getByName(id.getHostname());
        // send it on the socket
        DatagramPacket pack=new DatagramPacket(data,data.length,addr,sendPort);
        sendSocket.send(pack);
    }
    
    /**
     * Listen for connections and pass them to the AAAUnit
     * useing the handleIncoming() method of the AAAUnit.
     */
    public void startListening(AAAUnit receiver)
    {
        this.receiver=receiver;
    }

}


Arthur <arthur@ch.twi.tudelft.nl> http://ch.twi.tudelft.nl/~arthur/
2002-05-27