Skip to content
This repository was archived by the owner on Sep 19, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/codeu/chat/RelayMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private static void loadTeamInfo(Server relay, String file) {
// this line that it is not worth trying to handle ahead of time.
// So instead just try to parse it and catch any exception.

final Uuid id = Uuid.fromString(tokens[0].trim());
final Uuid id = Uuid.parse(tokens[0].trim());
final byte[] secret = Secret.parse(tokens[1].trim());

relay.addTeam(id, secret);
Expand Down
10 changes: 8 additions & 2 deletions src/codeu/chat/ServerMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,16 @@ public static void main(String[] args) {

LOG.info("============================= START OF LOG =============================");

final Uuid id = Uuid.fromString(args[0]);
final int myPort = Integer.parseInt(args[2]);
final byte[] secret = Secret.parse(args[1]);

final int myPort = Integer.parseInt(args[2]);
Uuid id = null;
try {
id = Uuid.parse(args[0]);
} catch (IOException ex) {
System.out.println("Invalid id - shutting down server");
System.exit(1);
}

// This is the directory where it is safe to store data accross runs
// of the server.
Expand Down
20 changes: 13 additions & 7 deletions src/codeu/chat/util/Uuid.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,23 +179,29 @@ private static void buildString(Uuid current, StringBuilder build) {
}
}

// FROM STRING
// Parse
//
// Create a uuid from a sting.
public static Uuid fromString(String string) {
return fromString(null, string.split("\\."), 0);
public static Uuid parse(String string) throws IOException {
return parse(null, string.split("\\."), 0);
}

private static Uuid fromString(final Uuid root, String[] tokens, int index) {
private static Uuid parse(final Uuid root, String[] tokens, int index) throws IOException {

final int id = Integer.parseInt(tokens[index]);
final long id = Long.parseLong(tokens[index]);

final Uuid link = new Uuid(root, id);
if ((id >> 32) != 0) {
throw new IOException(String.format(
"ID value '%s' is too large to be an unsigned 32 bit integer",
tokens[index]));
}

final Uuid link = new Uuid(root, (int)(id & 0xFFFFFFFF));

final int nextIndex = index + 1;

return nextIndex < tokens.length ?
fromString(link, tokens, nextIndex) :
parse(link, tokens, nextIndex) :
link;
}
}
21 changes: 17 additions & 4 deletions test/codeu/chat/util/UuidTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package codeu.chat.util;

import java.io.IOException;
import static org.junit.Assert.*;
import org.junit.Test;

Expand Down Expand Up @@ -103,21 +104,21 @@ public void testRootEqualNot() {
}

@Test
public void testValidSingleLink() {
public void testValidSingleLink() throws IOException {

final String string = "100";
final Uuid id = Uuid.fromString(string);
final Uuid id = Uuid.parse(string);

assertNotNull(id);
assertNull(id.root());
assertEquals(id.id(), 100);
}

@Test
public void testValidMultiLink() {
public void testValidMultiLink() throws IOException {

final String string = "100.200";
final Uuid id = Uuid.fromString(string);
final Uuid id = Uuid.parse(string);

assertNotNull(id);
assertNotNull(id.root());
Expand All @@ -126,4 +127,16 @@ public void testValidMultiLink() {
assertEquals(id.id(), 200);
assertEquals(id.root().id(), 100);
}

@Test
public void testLargeId() throws IOException {

// Use a id value that would be too large for Integer.parseInt to handle
// but would still parse if we could use unsigned integers.
final String string = Long.toString(0xFFFFFFFFL);
final Uuid id = Uuid.parse(string);

assertNotNull(id);
assertEquals(id.id(), 0xFFFFFFFF);
}
}