Subversion Repositories programming

Rev

Rev 215 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*******************************************************************************
 * File: P2_Server.java
 * Author: Ira W. Snyder (devel@irasnyder.com)
 * License: GNU General Public License v2
 * Class: CS380 - Computer Networking
 *
 * Assignment: Project #2
 * Date Last Modified: 2006-02-05
 *
 * Purpose: Implement a simple server which will listen on UDP port 1337 for
 * incoming datagrams. It will first recieve a message telling it to drop
 * every n-th message. After that it will convert every message it recieves
 * from 4B to 5B encoding, and reply back to the client. It will only exit on
 * receiving the message "QUIT".
 ******************************************************************************/

import java.io.*;
import java.util.*;
import java.net.*;
import java.util.regex.*;

public class P2_Server
{
    private static int portNumber = 1337;
    private static int dropEvery = -1;
    private static boolean dropPacket = false;
    private static DatagramPacket packet;
    private static DatagramSocket socket;

    /**
     * Method: readPacket()
     * Purpose: Read a packet from the currently opened socket.
     * Note: Blocks until it recieves data.
     */
    public static P2_Packet readPacket ()
    {
        byte[] buf = new byte[256];
        packet = new DatagramPacket (buf, buf.length);

        try {
            socket.receive (packet);
        } catch (IllegalArgumentException e) {
            System.err.println ("Caught exception in readPacket()");
            e.printStackTrace ();
        } catch (IOException e) {
            System.err.println ("Caught exception in readPacket()");
            e.printStackTrace ();
            return null;
        }

        return new P2_Packet (packet.getData());
    }

    /**
     * Method: writePacket()
     * Purpose: write a message to the last client that sent us a message,
     * while dropping every n-th message, as specified by the client.
     */
    public static boolean writePacket (byte type, byte[] data)
    {
        InetAddress address = packet.getAddress();
        int port = packet.getPort();

        byte[] buf = P2_Packet.createPacket (type, data);
        packet = new DatagramPacket (buf, buf.length, address, port);

        try {
            socket.send (packet);
        } catch (IOException e) {
            System.out.println ("Caught exception in writePacket()");
            e.printStackTrace();
            return false;
        }

        return true;
    }

    public static boolean writePacket (byte type)
    {
        byte[] pData = { 0 };
        return writePacket (type, pData);
    }

    /**
     * Method: calcDrops()
     * Purpose: Parse a message sent by the client telling us to drop
     * every n-th message.
     */
    public static int calcDrops (P2_Packet p)
    {
        if (p.getPacketType() != P2_Packet.DROP)
            throw new IllegalArgumentException(); // major problem

        byte[] num = p.getPacketData();
        return (int)num[0];
    }

    /**
     * Method: main()
     * Purpose: Open a listening UDP socket, and convert a client's
     * requests from 4B encoding to 5B encoding.
     */
    public static void main (String[] args) throws Exception
    {
        boolean done = false, droppedLast = false;
        socket = new DatagramSocket(portNumber);
        packet = null;
        String s;
        P2_Packet decodedPacket = null;
        byte[] pData;

        // Try to get a "timeout info" packet
        while (!done)
        {
            decodedPacket = readPacket ();
            dropEvery = calcDrops(decodedPacket);

            if (dropEvery != -1)
            {
                done = true;
                System.out.println ("dropEvery: " + dropEvery);
            }

            writePacket (P2_Packet.DROP);
        }

        done = false;

        // Keep reading until we get a quit packet
        while (!done)
        {

            decodedPacket = readPacket ();

            if ((decodedPacket.getPacketNumber() == dropEvery) && !droppedLast)
            {
                droppedLast = true;
                continue;
            }

            droppedLast = false;

            switch (decodedPacket.getPacketType())
            {
                case P2_Packet.TRANSREQ:
                    try {
                        pData = P2_EncDec.convert4B (decodedPacket.getPacketData());
                        writePacket (P2_Packet.TRANSREPL, pData);
                    } catch (IllegalArgumentException e) {
                        System.err.println ("Received a bad packet");
                        writePacket (P2_Packet.BADPACKET);
                    }
                    break;
                case P2_Packet.QUIT:
                    writePacket (P2_Packet.QUIT);
                    done = true;
                    break;
                default:
                    System.err.println ("Got a packetType that I was not "
                                      + "expecting here.");
                    writePacket (P2_Packet.BADPACKET);
                    break;
            }
            
        }

        // Close the socket
        socket.close();
    }
}