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

class TftpServerWorker extends Thread
{
    private DatagramPacket req;
    private static final int RRQ     = 1;
    private static final int WRQ     = 2;
    private static final int DATA    = 3;
    private static final int ACK     = 4;
    private static final int ERROR   = 5;

    private static void store_short(byte[] array, int off, int val)
    {
	array[off + 0] = (byte)((val & 0xff00) >> 8);
	array[off + 1] = (byte)((val & 0x00ff));
	return;
    }

    private static int extract_short(byte[] array, int off)
    {
	int a = array[off+0] & 0xff;
	int b = array[off+1] & 0xff;
	return (a << 8 | b);
    }

    private void sendfile(String filename)
    {
        /*
         * open the file using a FileInputStream and send it, one block at
         * a time, to the receiver.
         */
	return;
    }

    public void run()
    {
        /*
         * parse the request packet, ensuring that it is a request and that
         * the mode is octet mode, and then call sendfile
         */
	return;
    }

    public TftpServerWorker(DatagramPacket req)
    {
	this.req = req;
    }
}

class TftpServer
{
    public void start_server()
    {
	try {
	    DatagramSocket ds = new DatagramSocket();
	    System.out.println("TftpServer on port " + ds.getLocalPort());

	    for(;;) {
		byte[] buf = new byte[576];
		DatagramPacket p = new DatagramPacket(buf, 576);
		ds.receive(p);
		
		TftpServerWorker worker = new TftpServerWorker(p);
		worker.start();
	    }
	}
	catch(Exception e) {
	    System.err.println("Exception: " + e);
	}

	return;
    }

    public static void main(String args[])
    {
	TftpServer d = new TftpServer();
	d.start_server();
    }
}
