Subversion Repositories programming

Rev

Rev 211 | 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 int sendCount = 0;
    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 String readPacket (int maxlen)
    {
        byte[] buf = new byte[maxlen];
        packet = new DatagramPacket (buf, buf.length);

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

        System.out.println ("Recieved Packet. Len: " + packet.getLength());

        String s = new String (buf, 0, packet.getLength() /*- 1*/);
        return s;
    }

    /**
     * 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 (String msg)
    {
        if (sendCount == dropEvery && !msg.equals("QUIT"))
        {
            System.out.println ("Dropped a packet");
            sendCount = 0; // reset the sendCount
            return true;   // "Fake" send
        }
        else { sendCount++; }

        InetAddress address = packet.getAddress();
        int port = packet.getPort();

        byte[] buf = msg.getBytes();
        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;
    }

    /**
     * Method: calcDrops()
     * Purpose: Parse a message sent by the client telling us to drop
     * every n-th message.
     */
    public static int calcDrops (String s)
    {
        String dropRegex = "^DROP(\\d*)(.*)$";
        Pattern p = Pattern.compile(dropRegex);
        Matcher m = p.matcher(s);

        if (m.matches())
            return Integer.parseInt(m.group(1));

        return -1;
    }

    /**
     * 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;
        socket = new DatagramSocket(portNumber);
        packet = null;
        String s;

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

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

            writePacket (s);
        }

        done = false;

        // Keep reading until we get a quit packet
        while (!done)
        {
            s = readPacket (5);

            // Check and see if we got a quit message
            if (s.equals("QUIT"))
            {
                done = true;
                writePacket(s);
                continue;
            }

            try {
                s = P2_EncDec.convert4B (s);
            } catch (IllegalArgumentException e) {
                System.out.println ("Recieved a bad packet");
                s = "BADVAL";
            }

            writePacket (s);
        }

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