Skip to content
This repository was archived by the owner on Sep 19, 2021. It is now read-only.

Commit 1bbb5d9

Browse files
authored
Merge branch 'master' into relay-responding
2 parents aa911a7 + 043eb2c commit 1bbb5d9

3 files changed

Lines changed: 71 additions & 22 deletions

File tree

README.md

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,34 @@ JAVA 7 when working with this project.
3737
the following two commands in separate shells:
3838

3939
```
40-
$ sh run_server.sh
41-
$ sh run_client.sh
40+
$ sh run_server.sh <team_id> <team_secret> <port>
41+
$ sh run_client.sh <host> <port>
4242
```
4343

44-
The `run_server` and `run_client` scripts have hard-coded addresses for
45-
your local machine. If you are running the server on a different machine
46-
than the client, you will need to change the host portion of the address
47-
in `run_client.sh` to the name of the host where your server is running.
48-
Make sure the client and server are using the same port number.
44+
You must specify the following startup arguments for `run_server.sh:
45+
+ `<team_id>` and `<team_secret>`: a numeric id for your team, and a secret
46+
code, which are used to authenticate your server with the Relay server.
47+
You can specify any integer value for `<team_id>`, and a value expressed
48+
in hexadecimal format (using numbers `0-9` and letters in the range
49+
`A-F`) for `<team_secret>` when you launch the server in your local setup
50+
since it will not connect to the Relay server.
51+
+ `<port>`: the TCP port that your Server will listen on for connections
52+
from the Client. You can use any value between 1024 and 65535, as long as
53+
there is no other service currently listening on that port in your
54+
system. The server will return an error:
55+
56+
```
57+
java.net.BindException: Address already in use (Bind failed)
58+
```
59+
60+
if the port is already in use.
61+
62+
The startup arguments for `run_client.sh` are the following:
63+
+ `<host>`: the hostname or IP address of the computer on which the server
64+
is listening. If you are running server and client on the same computer,
65+
you can use `localhost` here.
66+
+ `<port>`: the port on which your server is listening. Must be the same
67+
port number you have specified when you launched `run_server.sh`.
4968
5069
All running images write informational and exceptional events to log files.
5170
The default setting for log messages is "INFO". You may change this to get
@@ -66,12 +85,13 @@ All the source files (except test-related source files) are in
6685
use the supplied scripts to build the project, the `.class` files will be placed
6786
in `./bin`. There is a `./third_party` directory that holds the jar files for
6887
JUnit (a Java testing framework). Your environment may or may not already have
69-
this installed. The supplied scripts use the version in ./third_party.
88+
this installed. The supplied scripts use the version in `./third_party`.
7089
7190
Finally, there are some high-level design documents in the project Wiki. Please
7291
review them as they can help you find your way around the sources.
7392
7493
94+
7595
## Source Directories
7696
7797
The major project components have been separated into their own packages. The

src/codeu/chat/common/Secret.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ public static byte[] parse(String stringSecret) {
3030
final int offset = stringSecret.length() % 2;
3131

3232
for (int i = 0; i < stringSecret.length(); i++) {
33-
expanded[offset + i] = (byte)toHex(stringSecret.charAt(i));
33+
expanded[offset + i] = (byte) toHex(stringSecret.charAt(i));
3434
}
3535

3636
final byte[] compressed = new byte[expanded.length / 2];
3737

3838
for (int i = 0; i < compressed.length; i++) {
39-
compressed[i] = (byte)((expanded[2 * i] << 4) | expanded[2 * i + 1]);
39+
compressed[i] = (byte) ((expanded[2 * i] << 4) | expanded[2 * i + 1]);
4040
}
4141

4242
return compressed;
@@ -46,7 +46,7 @@ private static final int toHex(char c) {
4646

4747
// If an invalid value was given, it will be treated as 0.
4848

49-
switch(c) {
49+
switch (c) {
5050
case '0':
5151
case '1':
5252
case '2':
@@ -59,13 +59,21 @@ private static final int toHex(char c) {
5959
case '9':
6060
return c - '0';
6161

62+
case 'a':
63+
case 'b':
64+
case 'c':
65+
case 'd':
66+
case 'e':
67+
case 'f':
68+
return c - 'a' + 10;
69+
6270
case 'A':
6371
case 'B':
6472
case 'C':
6573
case 'D':
6674
case 'E':
6775
case 'F':
68-
return c - 'A';
76+
return c - 'A' + 10;
6977

7078
default:
7179
return 0;

test/codeu/chat/common/SecretTest.java

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,54 @@
1414

1515
package codeu.chat.common;
1616

17-
import java.util.Arrays;
18-
1917
import static org.junit.Assert.*;
18+
19+
import java.util.Arrays;
2020
import org.junit.Test;
21-
import org.junit.BeforeClass;
22-
import org.junit.AfterClass;
2321

2422
public final class SecretTest {
2523

2624
@Test
2725
public void testParseEvenLength() {
2826

2927
final String input = "2345";
30-
final byte[] expected = { 0x23, 0x45 };
28+
final byte[] expected = {0x23, 0x45};
3129

3230
final byte[] actual = Secret.parse(input);
3331

3432
assertNotNull(actual);
3533
assertTrue(Arrays.equals(expected, actual));
34+
}
3635

36+
@Test
37+
public void testParseEvenLengthWithUppercaseLetters() {
38+
39+
final String input = "ABCDEF";
40+
final byte[] expected = {(byte) 0xAB, (byte) 0xCD, (byte) 0xEF};
41+
42+
final byte[] actual = Secret.parse(input);
43+
44+
assertNotNull(actual);
45+
assertTrue(Arrays.equals(expected, actual));
46+
}
47+
48+
@Test
49+
public void testParseEvenLengthWithLowercaseLetters() {
50+
51+
final String input = "abcdef";
52+
final byte[] expected = {(byte) 0xAB, (byte) 0xCD, (byte) 0xEF};
53+
54+
final byte[] actual = Secret.parse(input);
55+
56+
assertNotNull(actual);
57+
assertTrue(Arrays.equals(expected, actual));
3758
}
3859

3960
@Test
4061
public void testParseEvenLengthWithLeadingZero() {
4162

4263
final String input = "012345";
43-
final byte[] expected = { 0x01, 0x23, 0x45 };
64+
final byte[] expected = {0x01, 0x23, 0x45};
4465

4566
final byte[] actual = Secret.parse(input);
4667

@@ -52,7 +73,7 @@ public void testParseEvenLengthWithLeadingZero() {
5273
public void testParseEvenLengthWithLeadingDoubleZero() {
5374

5475
final String input = "00123456";
55-
final byte[] expected = { 0x00, 0x12, 0x34, 0x56 };
76+
final byte[] expected = {0x00, 0x12, 0x34, 0x56};
5677

5778
final byte[] actual = Secret.parse(input);
5879

@@ -64,7 +85,7 @@ public void testParseEvenLengthWithLeadingDoubleZero() {
6485
public void testParseOddLength() {
6586

6687
final String input = "12345";
67-
final byte[] expected = { 0x01, 0x23, 0x45 };
88+
final byte[] expected = {0x01, 0x23, 0x45};
6889

6990
final byte[] actual = Secret.parse(input);
7091

@@ -76,7 +97,7 @@ public void testParseOddLength() {
7697
public void testParseOddLengthWithLeadingZero() {
7798

7899
final String input = "0123456";
79-
final byte[] expected = { 0x00, 0x12, 0x34, 0x56 };
100+
final byte[] expected = {0x00, 0x12, 0x34, 0x56};
80101

81102
final byte[] actual = Secret.parse(input);
82103

@@ -88,7 +109,7 @@ public void testParseOddLengthWithLeadingZero() {
88109
public void testParseOddLengthWithLeadingDoubleZero() {
89110

90111
final String input = "001234567";
91-
final byte[] expected = { 0x00, 0x01, 0x23, 0x45, 0x67 };
112+
final byte[] expected = {0x00, 0x01, 0x23, 0x45, 0x67};
92113

93114
final byte[] actual = Secret.parse(input);
94115

0 commit comments

Comments
 (0)