Subversion Repositories programming

Rev

Rev 215 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 215 Rev 216
Line 21... Line 21...
21
 
21
 
22
public class P2_Server
22
public class P2_Server
23
{
23
{
24
    private static int portNumber = 1337;
24
    private static int portNumber = 1337;
25
    private static int dropEvery = -1;
25
    private static int dropEvery = -1;
26
    private static int sendCount = 0;
26
    private static boolean dropPacket = false;
27
    private static DatagramPacket packet;
27
    private static DatagramPacket packet;
28
    private static DatagramSocket socket;
28
    private static DatagramSocket socket;
29
 
29
 
30
    /**
30
    /**
31
     * Method: readPacket()
31
     * Method: readPacket()
Line 56... Line 56...
56
     * Purpose: write a message to the last client that sent us a message,
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.
57
     * while dropping every n-th message, as specified by the client.
58
     */
58
     */
59
    public static boolean writePacket (byte type, byte[] data)
59
    public static boolean writePacket (byte type, byte[] data)
60
    {
60
    {
61
        if (sendCount >= dropEvery && type != P2_Packet.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();
61
        InetAddress address = packet.getAddress();
70
        int port = packet.getPort();
62
        int port = packet.getPort();
71
 
63
 
72
        byte[] buf = P2_Packet.createPacket (type, data);
64
        byte[] buf = P2_Packet.createPacket (type, data);
73
        packet = new DatagramPacket (buf, buf.length, address, port);
65
        packet = new DatagramPacket (buf, buf.length, address, port);
Line 108... Line 100...
108
     * Purpose: Open a listening UDP socket, and convert a client's
100
     * Purpose: Open a listening UDP socket, and convert a client's
109
     * requests from 4B encoding to 5B encoding.
101
     * requests from 4B encoding to 5B encoding.
110
     */
102
     */
111
    public static void main (String[] args) throws Exception
103
    public static void main (String[] args) throws Exception
112
    {
104
    {
113
        boolean done = false;
105
        boolean done = false, droppedLast = false;
114
        socket = new DatagramSocket(portNumber);
106
        socket = new DatagramSocket(portNumber);
115
        packet = null;
107
        packet = null;
116
        String s;
108
        String s;
117
        P2_Packet decodedPacket = null;
109
        P2_Packet decodedPacket = null;
118
        byte[] pData;
110
        byte[] pData;
Line 135... Line 127...
135
        done = false;
127
        done = false;
136
 
128
 
137
        // Keep reading until we get a quit packet
129
        // Keep reading until we get a quit packet
138
        while (!done)
130
        while (!done)
139
        {
131
        {
-
 
132
 
140
            decodedPacket = readPacket ();
133
            decodedPacket = readPacket ();
141
 
134
 
142
            // Check and see if we got a quit message
-
 
143
            if (decodedPacket.getPacketType() == P2_Packet.QUIT)
135
            if ((decodedPacket.getPacketNumber() == dropEvery) && !droppedLast)
144
            {
136
            {
145
                done = true;
137
                droppedLast = true;
146
                writePacket(P2_Packet.QUIT);
-
 
147
                continue;
138
                continue;
148
            }
139
            }
149
 
140
 
150
            if (decodedPacket.getPacketType() == P2_Packet.TRANSREQ)
-
 
151
            {
-
 
152
                try {
141
            droppedLast = false;
153
                    pData = P2_EncDec.convert4B (decodedPacket.getPacketData());
-
 
154
 
142
 
-
 
143
            switch (decodedPacket.getPacketType())
-
 
144
            {
-
 
145
                case P2_Packet.TRANSREQ:
-
 
146
                    try {
-
 
147
                        pData = P2_EncDec.convert4B (decodedPacket.getPacketData());
155
                    writePacket (P2_Packet.TRANSREPL, pData);
148
                        writePacket (P2_Packet.TRANSREPL, pData);
156
                } catch (IllegalArgumentException e) {
149
                    } catch (IllegalArgumentException e) {
157
                    System.out.println ("Recieved a bad packet");
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.");
158
                    writePacket (P2_Packet.BADPACKET);
161
                    writePacket (P2_Packet.BADPACKET);
159
                    continue;
162
                    break;
160
                }
-
 
161
            }
163
            }
162
 
-
 
163
            System.out.println ("Got a strange packet type for this area");
164
            
164
        }
165
        }
165
 
166
 
166
        // Close the socket
167
        // Close the socket
167
        socket.close();
168
        socket.close();
168
    }
169
    }