Subversion Repositories programming

Rev

Rev 213 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
211 ira 1
import java.io.*;
2
import java.util.*;
3
import java.net.*;
4
import java.util.regex.*;
5
 
6
public class P2_Server
7
{
8
    private static int portNumber = 1337;
9
    private static int dropEvery = -1;
10
    private static int sendCount = 1;
11
    private static DatagramPacket packet;
12
    private static DatagramSocket socket;
13
 
14
    public static String readPacket (int maxlen)
15
    {
16
        byte[] buf = new byte[maxlen];
17
        packet = new DatagramPacket (buf, buf.length);
18
 
19
        try {
20
            socket.receive (packet);
21
        } catch (IOException e) {
22
            System.out.println ("Caught exception in readPacket()");
23
            e.printStackTrace();
24
            return null;
25
        }
26
 
27
        System.out.println ("Recieved Packet. Len: " + packet.getLength());
28
 
29
        String s = new String (buf, 0, packet.getLength() /*- 1*/);
30
        System.out.println ("RcvdPacket: |" + s + "|");
31
        return s;
32
    }
33
 
34
    public static boolean writePacket (String msg)
35
    {
36
        if (sendCount == dropEvery && !msg.equals("QUIT"))
37
        {
38
            System.out.println ("Dropped a packet");
39
            sendCount = 0; // reset the sendCount
40
            return true;   // "Fake" send
41
        }
42
        else { sendCount++; }
43
 
44
        System.out.println ("Num sent: " + sendCount);
45
 
46
        InetAddress address = packet.getAddress();
47
        int port = packet.getPort();
48
 
49
        byte[] buf = msg.getBytes();
50
        packet = new DatagramPacket (buf, buf.length, address, port);
51
 
52
        try {
53
            socket.send (packet);
54
        } catch (IOException e) {
55
            System.out.println ("Caught exception in writePacket()");
56
            e.printStackTrace();
57
            return false;
58
        }
59
 
60
        return true;
61
    }
62
 
63
    public static int calcDrops (String s)
64
    {
65
        String dropRegex = "^DROP(\\d*)(.*)$";
66
        Pattern p = Pattern.compile(dropRegex);
67
        Matcher m = p.matcher(s);
68
 
69
        if (m.matches())
70
            return Integer.parseInt(m.group(1));
71
 
72
        return -1;
73
    }
74
 
75
    public static void main (String[] args) throws Exception
76
    {
77
        boolean done = false;
78
        socket = new DatagramSocket(portNumber);
79
        packet = null;
80
        String s;
81
 
82
        // Try to get a "timeout info" packet
83
        while (!done)
84
        {
85
            s = readPacket (8);
86
            dropEvery = calcDrops(s);
87
 
88
            if (dropEvery != -1)
89
            {
90
                done = true;
91
                System.out.println ("dropEvery: " + dropEvery);
92
            }
93
        }
94
 
95
        done = false;
96
 
97
        // Keep reading until we get a quit packet
98
        while (!done)
99
        {
100
            s = readPacket (5);
101
 
102
            // Check and see if we got a quit message
103
            if (s.equals("QUIT"))
104
            {
105
                done = true;
106
                writePacket(s);
107
                continue;
108
            }
109
 
110
            try {
111
                s = P2_EncDec.convert4B (s);
112
            } catch (IllegalArgumentException e) {
113
                System.out.println ("Recieved a bad packet");
114
                s = "BADVAL";
115
            }
116
 
117
            writePacket (s);
118
        }
119
    }
120
}