Subversion Repositories programming

Rev

Rev 213 | Rev 216 | 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;
213 ira 26
    private static int sendCount = 0;
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
    {
215 ira 61
        if (sendCount >= dropEvery && type != P2_Packet.QUIT)
211 ira 62
        {
63
            System.out.println ("Dropped a packet");
64
            sendCount = 0; // reset the sendCount
215 ira 65
            return true;   // Fake send
211 ira 66
        }
67
        else { sendCount++; }
68
 
69
        InetAddress address = packet.getAddress();
70
        int port = packet.getPort();
71
 
215 ira 72
        byte[] buf = P2_Packet.createPacket (type, data);
211 ira 73
        packet = new DatagramPacket (buf, buf.length, address, port);
74
 
75
        try {
76
            socket.send (packet);
77
        } catch (IOException e) {
78
            System.out.println ("Caught exception in writePacket()");
79
            e.printStackTrace();
80
            return false;
81
        }
82
 
83
        return true;
84
    }
85
 
215 ira 86
    public static boolean writePacket (byte type)
87
    {
88
        byte[] pData = { 0 };
89
        return writePacket (type, pData);
90
    }
91
 
213 ira 92
    /**
93
     * Method: calcDrops()
94
     * Purpose: Parse a message sent by the client telling us to drop
95
     * every n-th message.
96
     */
215 ira 97
    public static int calcDrops (P2_Packet p)
211 ira 98
    {
215 ira 99
        if (p.getPacketType() != P2_Packet.DROP)
100
            throw new IllegalArgumentException(); // major problem
211 ira 101
 
215 ira 102
        byte[] num = p.getPacketData();
103
        return (int)num[0];
211 ira 104
    }
105
 
213 ira 106
    /**
107
     * Method: main()
108
     * Purpose: Open a listening UDP socket, and convert a client's
109
     * requests from 4B encoding to 5B encoding.
110
     */
211 ira 111
    public static void main (String[] args) throws Exception
112
    {
113
        boolean done = false;
114
        socket = new DatagramSocket(portNumber);
115
        packet = null;
116
        String s;
215 ira 117
        P2_Packet decodedPacket = null;
118
        byte[] pData;
211 ira 119
 
120
        // Try to get a "timeout info" packet
121
        while (!done)
122
        {
215 ira 123
            decodedPacket = readPacket ();
124
            dropEvery = calcDrops(decodedPacket);
211 ira 125
 
126
            if (dropEvery != -1)
127
            {
128
                done = true;
129
                System.out.println ("dropEvery: " + dropEvery);
130
            }
213 ira 131
 
215 ira 132
            writePacket (P2_Packet.DROP);
211 ira 133
        }
134
 
135
        done = false;
136
 
137
        // Keep reading until we get a quit packet
138
        while (!done)
139
        {
215 ira 140
            decodedPacket = readPacket ();
211 ira 141
 
142
            // Check and see if we got a quit message
215 ira 143
            if (decodedPacket.getPacketType() == P2_Packet.QUIT)
211 ira 144
            {
145
                done = true;
215 ira 146
                writePacket(P2_Packet.QUIT);
211 ira 147
                continue;
148
            }
149
 
215 ira 150
            if (decodedPacket.getPacketType() == P2_Packet.TRANSREQ)
151
            {
152
                try {
153
                    pData = P2_EncDec.convert4B (decodedPacket.getPacketData());
154
 
155
                    writePacket (P2_Packet.TRANSREPL, pData);
156
                } catch (IllegalArgumentException e) {
157
                    System.out.println ("Recieved a bad packet");
158
                    writePacket (P2_Packet.BADPACKET);
159
                    continue;
160
                }
211 ira 161
            }
162
 
215 ira 163
            System.out.println ("Got a strange packet type for this area");
211 ira 164
        }
213 ira 165
 
166
        // Close the socket
167
        socket.close();
211 ira 168
    }
169
}
213 ira 170