Subversion Repositories programming

Rev

Rev 211 | 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;
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
     */
211 ira 35
    public static String readPacket (int maxlen)
36
    {
37
        byte[] buf = new byte[maxlen];
38
        packet = new DatagramPacket (buf, buf.length);
39
 
40
        try {
41
            socket.receive (packet);
42
        } catch (IOException e) {
43
            System.out.println ("Caught exception in readPacket()");
44
            e.printStackTrace();
45
            return null;
46
        }
47
 
48
        System.out.println ("Recieved Packet. Len: " + packet.getLength());
49
 
50
        String s = new String (buf, 0, packet.getLength() /*- 1*/);
51
        return s;
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
     */
211 ira 59
    public static boolean writePacket (String msg)
60
    {
61
        if (sendCount == dropEvery && !msg.equals("QUIT"))
62
        {
63
            System.out.println ("Dropped a packet");
64
            sendCount = 0; // reset the sendCount
65
            return true;   // "Fake" send
66
        }
67
        else { sendCount++; }
68
 
69
        InetAddress address = packet.getAddress();
70
        int port = packet.getPort();
71
 
72
        byte[] buf = msg.getBytes();
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
 
213 ira 86
    /**
87
     * Method: calcDrops()
88
     * Purpose: Parse a message sent by the client telling us to drop
89
     * every n-th message.
90
     */
211 ira 91
    public static int calcDrops (String s)
92
    {
93
        String dropRegex = "^DROP(\\d*)(.*)$";
94
        Pattern p = Pattern.compile(dropRegex);
95
        Matcher m = p.matcher(s);
96
 
97
        if (m.matches())
98
            return Integer.parseInt(m.group(1));
99
 
100
        return -1;
101
    }
102
 
213 ira 103
    /**
104
     * Method: main()
105
     * Purpose: Open a listening UDP socket, and convert a client's
106
     * requests from 4B encoding to 5B encoding.
107
     */
211 ira 108
    public static void main (String[] args) throws Exception
109
    {
110
        boolean done = false;
111
        socket = new DatagramSocket(portNumber);
112
        packet = null;
113
        String s;
114
 
115
        // Try to get a "timeout info" packet
116
        while (!done)
117
        {
118
            s = readPacket (8);
119
            dropEvery = calcDrops(s);
120
 
121
            if (dropEvery != -1)
122
            {
123
                done = true;
124
                System.out.println ("dropEvery: " + dropEvery);
125
            }
213 ira 126
 
127
            writePacket (s);
211 ira 128
        }
129
 
130
        done = false;
131
 
132
        // Keep reading until we get a quit packet
133
        while (!done)
134
        {
135
            s = readPacket (5);
136
 
137
            // Check and see if we got a quit message
138
            if (s.equals("QUIT"))
139
            {
140
                done = true;
141
                writePacket(s);
142
                continue;
143
            }
144
 
145
            try {
146
                s = P2_EncDec.convert4B (s);
147
            } catch (IllegalArgumentException e) {
148
                System.out.println ("Recieved a bad packet");
149
                s = "BADVAL";
150
            }
151
 
152
            writePacket (s);
153
        }
213 ira 154
 
155
        // Close the socket
156
        socket.close();
211 ira 157
    }
158
}
213 ira 159