Subversion Repositories programming

Rev

Rev 216 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
213 ira 1
/*******************************************************************************
2
 * File: P2_Server.java
3
 * Author: Ira W. Snyder (devel@irasnyder.com)
4
 * License: GNU General Public License v2
5
 * Class: CS380 - Computer Networking
6
 *
7
 * Assignment: Project #2
218 ira 8
 * Date Last Modified: 2006-02-08
213 ira 9
 *
10
 * Purpose: Implement a simple server which will listen on UDP port 1337 for
11
 * incoming datagrams. It will first recieve a message telling it to drop
12
 * every n-th message. After that it will convert every message it recieves
13
 * from 4B to 5B encoding, and reply back to the client. It will only exit on
14
 * receiving the message "QUIT".
15
 ******************************************************************************/
16
 
211 ira 17
import java.io.*;
18
import java.util.*;
19
import java.net.*;
20
import java.util.regex.*;
21
 
22
public class P2_Server
23
{
24
    private static int portNumber = 1337;
25
    private static int dropEvery = -1;
216 ira 26
    private static boolean dropPacket = false;
211 ira 27
    private static DatagramPacket packet;
28
    private static DatagramSocket socket;
29
 
213 ira 30
    /**
31
     * Method: readPacket()
32
     * Purpose: Read a packet from the currently opened socket.
33
     * Note: Blocks until it recieves data.
34
     */
215 ira 35
    public static P2_Packet readPacket ()
211 ira 36
    {
215 ira 37
        byte[] buf = new byte[256];
211 ira 38
        packet = new DatagramPacket (buf, buf.length);
39
 
218 ira 40
        // Try to recieve data from the client. Catch any
41
        // exceptions that may occur.
211 ira 42
        try {
43
            socket.receive (packet);
215 ira 44
        } catch (IllegalArgumentException e) {
45
            System.err.println ("Caught exception in readPacket()");
46
            e.printStackTrace ();
211 ira 47
        } catch (IOException e) {
215 ira 48
            System.err.println ("Caught exception in readPacket()");
49
            e.printStackTrace ();
211 ira 50
            return null;
51
        }
52
 
218 ira 53
        // Convert the packet, and print a status message.
54
        P2_Packet ret = new P2_Packet (packet.getData());
55
        System.out.printf ("Recieved a packet: %d bytes -- packetNum: %d\n",
56
                ret.getPacketDataSize(), ret.getPacketNumber());
57
 
58
        return ret;
211 ira 59
    }
60
 
213 ira 61
    /**
62
     * Method: writePacket()
218 ira 63
     * Purpose: Write a message to the last client that sent us a message.
213 ira 64
     */
215 ira 65
    public static boolean writePacket (byte type, byte[] data)
211 ira 66
    {
218 ira 67
        // Get the address and port of the last message sent to us.
211 ira 68
        InetAddress address = packet.getAddress();
69
        int port = packet.getPort();
70
 
218 ira 71
        // Set up our packet
215 ira 72
        byte[] buf = P2_Packet.createPacket (type, data);
211 ira 73
        packet = new DatagramPacket (buf, buf.length, address, port);
74
 
218 ira 75
        // Attempt to send the packet
211 ira 76
        try {
77
            socket.send (packet);
78
        } catch (IOException e) {
79
            System.out.println ("Caught exception in writePacket()");
80
            e.printStackTrace();
81
            return false;
82
        }
83
 
218 ira 84
        // It got sent ok
211 ira 85
        return true;
86
    }
87
 
218 ira 88
    /**
89
     * Method: writePacket()
90
     * Purpose: A simplified version of writePacket() above.
91
     */
215 ira 92
    public static boolean writePacket (byte type)
93
    {
94
        byte[] pData = { 0 };
95
        return writePacket (type, pData);
96
    }
97
 
213 ira 98
    /**
218 ira 99
     * Method: calcDropNum()
213 ira 100
     * Purpose: Parse a message sent by the client telling us to drop
101
     * every n-th message.
102
     */
218 ira 103
    public static int calcDropNum (P2_Packet p)
211 ira 104
    {
215 ira 105
        if (p.getPacketType() != P2_Packet.DROP)
106
            throw new IllegalArgumentException(); // major problem
211 ira 107
 
218 ira 108
        // The byte informing us how often to drop packets is in
109
        // the first byte only.
215 ira 110
        byte[] num = p.getPacketData();
111
        return (int)num[0];
211 ira 112
    }
113
 
213 ira 114
    /**
115
     * Method: main()
116
     * Purpose: Open a listening UDP socket, and convert a client's
117
     * requests from 4B encoding to 5B encoding.
118
     */
211 ira 119
    public static void main (String[] args) throws Exception
120
    {
216 ira 121
        boolean done = false, droppedLast = false;
211 ira 122
        socket = new DatagramSocket(portNumber);
123
        packet = null;
124
        String s;
215 ira 125
        P2_Packet decodedPacket = null;
126
        byte[] pData;
211 ira 127
 
128
        // Try to get a "timeout info" packet
129
        while (!done)
130
        {
215 ira 131
            decodedPacket = readPacket ();
218 ira 132
            dropEvery = calcDropNum (decodedPacket);
211 ira 133
 
134
            if (dropEvery != -1)
135
            {
136
                done = true;
137
                System.out.println ("dropEvery: " + dropEvery);
138
            }
213 ira 139
 
215 ira 140
            writePacket (P2_Packet.DROP);
211 ira 141
        }
142
 
143
        done = false;
144
 
145
        // Keep reading until we get a quit packet
146
        while (!done)
147
        {
215 ira 148
            decodedPacket = readPacket ();
211 ira 149
 
218 ira 150
            // Drop a packet if it is the correct time to do so
216 ira 151
            if ((decodedPacket.getPacketNumber() == dropEvery) && !droppedLast)
211 ira 152
            {
218 ira 153
                System.out.println ("Purposely dropped a packet");
216 ira 154
                droppedLast = true;
211 ira 155
                continue;
156
            }
157
 
216 ira 158
            droppedLast = false;
159
 
218 ira 160
            // Figure out what type of packet to send in reply
216 ira 161
            switch (decodedPacket.getPacketType())
215 ira 162
            {
216 ira 163
                case P2_Packet.TRANSREQ:
164
                    try {
165
                        pData = P2_EncDec.convert4B (decodedPacket.getPacketData());
166
                        writePacket (P2_Packet.TRANSREPL, pData);
167
                    } catch (IllegalArgumentException e) {
168
                        System.err.println ("Received a bad packet");
169
                        writePacket (P2_Packet.BADPACKET);
170
                    }
171
                    break;
172
                case P2_Packet.QUIT:
173
                    writePacket (P2_Packet.QUIT);
174
                    done = true;
175
                    break;
176
                default:
177
                    System.err.println ("Got a packetType that I was not "
178
                                      + "expecting here.");
215 ira 179
                    writePacket (P2_Packet.BADPACKET);
216 ira 180
                    break;
211 ira 181
            }
182
        }
213 ira 183
 
184
        // Close the socket
185
        socket.close();
211 ira 186
    }
187
}
213 ira 188