Subversion Repositories programming

Rev

Rev 215 | Go to most recent revision | 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
8
 * Date Last Modified: 2006-02-05
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
 
40
        try {
41
            socket.receive (packet);
215 ira 42
        } catch (IllegalArgumentException e) {
43
            System.err.println ("Caught exception in readPacket()");
44
            e.printStackTrace ();
211 ira 45
        } catch (IOException e) {
215 ira 46
            System.err.println ("Caught exception in readPacket()");
47
            e.printStackTrace ();
211 ira 48
            return null;
49
        }
50
 
215 ira 51
        return new P2_Packet (packet.getData());
211 ira 52
    }
53
 
213 ira 54
    /**
55
     * Method: writePacket()
56
     * Purpose: write a message to the last client that sent us a message,
57
     * while dropping every n-th message, as specified by the client.
58
     */
215 ira 59
    public static boolean writePacket (byte type, byte[] data)
211 ira 60
    {
61
        InetAddress address = packet.getAddress();
62
        int port = packet.getPort();
63
 
215 ira 64
        byte[] buf = P2_Packet.createPacket (type, data);
211 ira 65
        packet = new DatagramPacket (buf, buf.length, address, port);
66
 
67
        try {
68
            socket.send (packet);
69
        } catch (IOException e) {
70
            System.out.println ("Caught exception in writePacket()");
71
            e.printStackTrace();
72
            return false;
73
        }
74
 
75
        return true;
76
    }
77
 
215 ira 78
    public static boolean writePacket (byte type)
79
    {
80
        byte[] pData = { 0 };
81
        return writePacket (type, pData);
82
    }
83
 
213 ira 84
    /**
85
     * Method: calcDrops()
86
     * Purpose: Parse a message sent by the client telling us to drop
87
     * every n-th message.
88
     */
215 ira 89
    public static int calcDrops (P2_Packet p)
211 ira 90
    {
215 ira 91
        if (p.getPacketType() != P2_Packet.DROP)
92
            throw new IllegalArgumentException(); // major problem
211 ira 93
 
215 ira 94
        byte[] num = p.getPacketData();
95
        return (int)num[0];
211 ira 96
    }
97
 
213 ira 98
    /**
99
     * Method: main()
100
     * Purpose: Open a listening UDP socket, and convert a client's
101
     * requests from 4B encoding to 5B encoding.
102
     */
211 ira 103
    public static void main (String[] args) throws Exception
104
    {
216 ira 105
        boolean done = false, droppedLast = false;
211 ira 106
        socket = new DatagramSocket(portNumber);
107
        packet = null;
108
        String s;
215 ira 109
        P2_Packet decodedPacket = null;
110
        byte[] pData;
211 ira 111
 
112
        // Try to get a "timeout info" packet
113
        while (!done)
114
        {
215 ira 115
            decodedPacket = readPacket ();
116
            dropEvery = calcDrops(decodedPacket);
211 ira 117
 
118
            if (dropEvery != -1)
119
            {
120
                done = true;
121
                System.out.println ("dropEvery: " + dropEvery);
122
            }
213 ira 123
 
215 ira 124
            writePacket (P2_Packet.DROP);
211 ira 125
        }
126
 
127
        done = false;
128
 
129
        // Keep reading until we get a quit packet
130
        while (!done)
131
        {
216 ira 132
 
215 ira 133
            decodedPacket = readPacket ();
211 ira 134
 
216 ira 135
            if ((decodedPacket.getPacketNumber() == dropEvery) && !droppedLast)
211 ira 136
            {
216 ira 137
                droppedLast = true;
211 ira 138
                continue;
139
            }
140
 
216 ira 141
            droppedLast = false;
142
 
143
            switch (decodedPacket.getPacketType())
215 ira 144
            {
216 ira 145
                case P2_Packet.TRANSREQ:
146
                    try {
147
                        pData = P2_EncDec.convert4B (decodedPacket.getPacketData());
148
                        writePacket (P2_Packet.TRANSREPL, pData);
149
                    } catch (IllegalArgumentException e) {
150
                        System.err.println ("Received a bad packet");
151
                        writePacket (P2_Packet.BADPACKET);
152
                    }
153
                    break;
154
                case P2_Packet.QUIT:
155
                    writePacket (P2_Packet.QUIT);
156
                    done = true;
157
                    break;
158
                default:
159
                    System.err.println ("Got a packetType that I was not "
160
                                      + "expecting here.");
215 ira 161
                    writePacket (P2_Packet.BADPACKET);
216 ira 162
                    break;
211 ira 163
            }
216 ira 164
 
211 ira 165
        }
213 ira 166
 
167
        // Close the socket
168
        socket.close();
211 ira 169
    }
170
}
213 ira 171