Subversion Repositories programming

Rev

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

Rev 213 Rev 215
Line 26... Line 26...
26
    /**
26
    /**
27
     * Method: readPacket()
27
     * Method: readPacket()
28
     * Purpose: Read a packet on the currently opened DatagramSocket,
28
     * Purpose: Read a packet on the currently opened DatagramSocket,
29
     * and return it as a String.
29
     * and return it as a String.
30
     */
30
     */
31
    public static String readPacket (int maxlen)
31
    public static P2_Packet readPacket ()
32
    {
32
    {
33
        byte[] buf = new byte[maxlen];
33
        byte[] buf = new byte[256];
34
        packet = new DatagramPacket (buf, buf.length);
34
        packet = new DatagramPacket (buf, buf.length);
35
 
35
 
36
        try {
36
        try {
37
            socket.receive (packet);
37
            socket.receive (packet);
38
        } catch (SocketTimeoutException e) {
38
        } catch (SocketTimeoutException e) {
Line 46... Line 46...
46
            System.out.println ("Caught exception in readPacket()");
46
            System.out.println ("Caught exception in readPacket()");
47
            e.printStackTrace();
47
            e.printStackTrace();
48
            return null;
48
            return null;
49
        }
49
        }
50
 
50
 
51
        return new String (buf, 0, packet.getLength()/*- 1*/);
51
        return new P2_Packet (packet.getData());
52
    }
52
    }
53
 
53
 
54
    /**
54
    /**
55
     * Method: writePacket()
55
     * Method: writePacket()
56
     * Purpose: Send a packet to localhost:portNumber.
56
     * Purpose: Send a packet to localhost:portNumber.
57
     */
57
     */
58
    public static boolean writePacket (String msg) throws Exception
58
    public static boolean writePacket (byte type, byte[] data)
59
    {
59
    {
60
        InetAddress address = InetAddress.getLocalHost();
60
        InetAddress address = null;
61
        int port = portNumber;
61
        int port = portNumber;
62
 
62
 
-
 
63
        try {
-
 
64
            address = InetAddress.getLocalHost();
-
 
65
        } catch (UnknownHostException e) {
-
 
66
            System.err.println ("Caught UnknownHostException in writePacket(), " +
-
 
67
                                "are you sure the system is working?");
-
 
68
            System.exit(1);
-
 
69
        }
-
 
70
 
63
        byte[] buf = msg.getBytes();
71
        byte[] buf = P2_Packet.createPacket (type, data);
64
        packet = new DatagramPacket (buf, buf.length, address, port);
72
        packet = new DatagramPacket (buf, buf.length, address, port);
65
 
73
 
66
        try {
74
        try {
67
            socket.send (packet);
75
            socket.send (packet);
68
        } catch (PortUnreachableException e) {
76
        } catch (PortUnreachableException e) {
Line 76... Line 84...
76
        }
84
        }
77
 
85
 
78
        return true;
86
        return true;
79
    }
87
    }
80
 
88
 
-
 
89
    public static boolean writePacket (byte type)
-
 
90
    {
-
 
91
        byte[] pData = { 0 };
-
 
92
        return writePacket (type, pData);
-
 
93
    }
-
 
94
 
81
    /**
95
    /**
82
     * Method: guaranteedTransmit()
96
     * Method: guaranteedTransmit()
83
     * Purpose: A wrapper around readPacket() and writePacket() that implements
97
     * Purpose: A wrapper around readPacket() and writePacket() that implements
84
     * a reliable transmit mechanism. It allows you to specify a maximum number
98
     * a reliable transmit mechanism. It allows you to specify a maximum number
85
     * of times to try resending a packet.
99
     * of times to try resending a packet.
86
     */
100
     */
87
    public static String guaranteedTransmit (String msg, int maxlen, int maxRetries) throws Exception
101
    public static P2_Packet guaranteedTransmit (byte type, byte[] data)
88
    {
102
    {
89
        String reply = null;
103
        P2_Packet reply;
90
        int retries = 0;
104
        int retries = 0;
91
 
105
 
92
        writePacket (msg);
106
        writePacket (type, data);
93
        reply = readPacket (maxlen);
107
        reply = readPacket ();
94
 
108
 
95
        while (reply == null && retries < maxRetries)
109
        while (reply == null)
96
        {
110
        {
97
            System.err.println ("Packet lost, retransmitting...");
111
            System.err.println ("Packet lost, retransmitting...");
98
            writePacket (msg);
112
            writePacket (type, data);
99
            reply = readPacket (maxlen);
113
            reply = readPacket ();
100
            retries++;
114
            retries++;
101
        }
115
        }
102
 
116
 
103
        return reply;
117
        return reply;
104
    }
118
    }
105
 
119
 
-
 
120
    public static P2_Packet guaranteedTransmit (byte type)
-
 
121
    {
-
 
122
        byte[] pData = { 0 };
-
 
123
        
-
 
124
        return guaranteedTransmit (type, pData);
-
 
125
    }
-
 
126
    
106
    /**
127
    /**
107
     * Method: main()
128
     * Method: main()
108
     * Purpose: This will accept input from the user, and attempt to send it
129
     * Purpose: This will accept input from the user, and attempt to send it
109
     * to the corresponding server, which will convert the messages from 4B
130
     * to the corresponding server, which will convert the messages from 4B
110
     * to 5B encoding.
131
     * to 5B encoding.
Line 125... Line 146...
125
        } catch (ArrayIndexOutOfBoundsException e) {
146
        } catch (ArrayIndexOutOfBoundsException e) {
126
            System.err.println ("You need to call the program with \'java P2_Client <num>\'");
147
            System.err.println ("You need to call the program with \'java P2_Client <num>\'");
127
            System.exit(1);
148
            System.exit(1);
128
        }
149
        }
129
 
150
 
-
 
151
        String msg;
130
        String msg = new String("DROP" + dropHowMany);
152
        byte[] pData = { (byte)dropHowMany };
131
        String reply = guaranteedTransmit (msg, 6, 1);
153
        P2_Packet reply = guaranteedTransmit (P2_Packet.DROP, pData);
132
 
154
 
133
        if (!reply.equals(msg))
155
        if (reply.getPacketType() != P2_Packet.DROP)
134
        {
156
        {
135
            System.err.println ("Unable to inform the server how often to drop packets");
157
            System.err.println ("Unable to inform the server how often to drop packets");
136
            System.exit(1);
158
            System.exit(1);
137
        }
159
        }
138
 
160
 
Line 140... Line 162...
140
        msg = kbd.readLine();
162
        msg = kbd.readLine();
141
        boolean done = false;
163
        boolean done = false;
142
 
164
 
143
        while (!done)
165
        while (!done)
144
        {
166
        {
145
            reply = guaranteedTransmit (msg, 6, 1);
-
 
146
 
-
 
147
            if (reply.equals("QUIT"))
167
            if (msg.equals("QUIT"))
148
            {
168
            {
-
 
169
                reply = guaranteedTransmit (P2_Packet.QUIT);
149
                done = true;
170
                done = true;
150
                continue;
171
                continue;
151
            }
172
            }
-
 
173
            
-
 
174
            reply = guaranteedTransmit (P2_Packet.TRANSREQ, P2_EncDec.convertToByteArray(msg));
152
 
175
 
153
            System.out.println ("-> reply: " + reply);
176
            System.out.println ("-> reply: " + P2_EncDec.convertToString(reply.getPacketData()));
154
            System.out.print ("% decode: ");
177
            System.out.print ("% decode: ");
155
            msg = kbd.readLine();
178
            msg = kbd.readLine();
156
        }
179
        }
157
 
180
 
158
        // Close the socket
181
        // Close the socket