Skip to content
Snippets Groups Projects
Commit 9b4f0fdc authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Replaced all input/output strings with i18n resource bundle.

Closes #6
parent 33f767b8
No related branches found
No related tags found
No related merge requests found
...@@ -22,7 +22,9 @@ public class Chat { ...@@ -22,7 +22,9 @@ public class Chat {
socket = connect(new Scanner(System.in)); socket = connect(new Scanner(System.in));
} }
// THESE METHODS SET UP AND TEAR DOWN CONNECTION /*
* THESE METHODS SET UP AND TEAR DOWN CONNECTION
*/
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
public Socket connect(Scanner userInput) { public Socket connect(Scanner userInput) {
...@@ -95,7 +97,7 @@ public class Chat { ...@@ -95,7 +97,7 @@ public class Chat {
} catch (ConnectException ignored) { } catch (ConnectException ignored) {
// "Attempt ...: Chat server is not yet ready at ..." // "Attempt ...: Chat server is not yet ready at ..."
System.out.println(MessageFormat.format( System.out.println(MessageFormat.format(
bundle.getString("connection.error.hostNotReady.attempt.1.2.3.4.port"), bundle.getString("connection.error.hostNotReady.0.1.2.3.4.5"),
attemptCount, address[0], address[1], address[2], address[3], port)); attemptCount, address[0], address[1], address[2], address[3], port));
if (attemptCount < MAXIMUM_CONNECTION_ATTEMPTS) { if (attemptCount < MAXIMUM_CONNECTION_ATTEMPTS) {
// "Will attempt to connect again in ... seconds." // "Will attempt to connect again in ... seconds."
...@@ -117,8 +119,8 @@ public class Chat { ...@@ -117,8 +119,8 @@ public class Chat {
boolean haveGoodAddress = false; boolean haveGoodAddress = false;
byte[] address = new byte[4]; byte[] address = new byte[4];
while (!haveGoodAddress) { while (!haveGoodAddress) {
System.out.print("Enter IP address of host <##.##.##.##>: "); // "Enter IP address of host <##.##.##.##>:"
haveGoodAddress = true; System.out.print(bundle.getString("connection.prompt.getHostAddress") + " ");
try { try {
String addressString = userInput.nextLine(); String addressString = userInput.nextLine();
String[] tokens = addressString.split("\\."); String[] tokens = addressString.split("\\.");
...@@ -126,20 +128,25 @@ public class Chat { ...@@ -126,20 +128,25 @@ public class Chat {
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
address[i] = Byte.parseByte(tokens[i]); address[i] = Byte.parseByte(tokens[i]);
} }
haveGoodAddress = true;
} else { } else {
System.out.println("The IP address should be four dot-separated numbers; <" // "The IP address should be four dot-separated numbers; ... does not conform."
+ addressString + "> does not conform."); System.out.println(MessageFormat.format(bundle.getString("connection.error.malformedAddress"),
addressString));
haveGoodAddress = false; haveGoodAddress = false;
} }
} catch (NumberFormatException nfException) { } catch (NumberFormatException nfException) {
System.out.println("The IP address should be exactly as reported to the host user."); // "The IP address should be exactly as reported to the host user."
System.out.println(bundle.getString("connection.error.badNumberInAddress"));
String message = nfException.getMessage(); String message = nfException.getMessage();
if (message.startsWith("Value out of range. Value")) { // "Value out of range. Value"
if (message.startsWith(bundle.getString("exception.numberFormat.startOfValueOutOfRangeMessage"))) {
String[] messageTokens = message.split("\""); String[] messageTokens = message.split("\"");
long value = Long.parseLong(messageTokens[1]); // this may break if message format changes long value = Long.parseLong(messageTokens[1]); // this may break if message format changes
if ((127 < value) && (value < 256)) { if ((127 < value) && (value < 256)) {
System.out.println("Note that Java does not have unsigned integers, so subtract 256 from " + // "Note that Java does not have unsigned integers, so subtract 256 from values greater than..."
"values greater than 127. For example, " + value + " should be " + (value - 256) + "."); System.out.println(MessageFormat.format(bundle.getString(
"connection.error.userAttemptedUnsignedByte.0.1"), value, value - 256));
} }
} }
haveGoodAddress = false; haveGoodAddress = false;
...@@ -153,15 +160,22 @@ public class Chat { ...@@ -153,15 +160,22 @@ public class Chat {
int port = 0; int port = 0;
while (!haveGoodNumber) { while (!haveGoodNumber) {
System.out.print(prompt + ": "); System.out.print(prompt + ": ");
haveGoodNumber = true;
try { try {
port = userInput.nextInt(); port = userInput.nextInt();
if (port <= 0) throw new InputMismatchException("Expected positive value, got " + port); if (port <= 0) {
// "Expected positive value, got ..."
throw new InputMismatchException(MessageFormat.format(
bundle.getString("connection.error.portNumberTooLow"), port));
}
if (port >= 2 * (Short.MAX_VALUE + 1)) { if (port >= 2 * (Short.MAX_VALUE + 1)) {
throw new InputMismatchException("Expected value less than 65536, got " + port); // "Expected value less than 65536, got ..."
throw new InputMismatchException(MessageFormat.format(
bundle.getString("connection.error.portNumberTooHigh"), port));
} }
haveGoodNumber = true;
} catch (InputMismatchException ignored) { } catch (InputMismatchException ignored) {
System.out.println("The port number must be a positive integer strictly less than 65536."); // "The port number must be a positive integer strictly less than 65536."
System.out.println(bundle.getString("connection.error.badPortNumber"));
haveGoodNumber = false; haveGoodNumber = false;
} finally { } finally {
userInput.nextLine(); userInput.nextLine();
...@@ -170,7 +184,9 @@ public class Chat { ...@@ -170,7 +184,9 @@ public class Chat {
return port; return port;
} }
// THESE METHODS PERFORM CHAT AFTER CONNECTION IS SET UP /*
* THESE METHODS PERFORM CHAT AFTER CONNECTION IS SET UP
*/
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
public void communicate() { public void communicate() {
...@@ -181,8 +197,10 @@ public class Chat { ...@@ -181,8 +197,10 @@ public class Chat {
System.out, System.out,
new PrintStream(socket.getOutputStream())); new PrintStream(socket.getOutputStream()));
} catch (IOException ioException) { } catch (IOException ioException) {
System.err.println("Failed to set up input/output streams: " + ioException); // "Failed to set up input/output streams: ..."
System.err.println("Terminating."); // I'm pretty sure this is recoverable if the client is waiting on the server // "Terminating."
System.err.println(bundle.getString("communicate.error.cannotSetUpStreams") + ": " + ioException);
System.err.println(bundle.getString("communicate.info.terminating"));
System.exit(1); System.exit(1);
} }
} }
...@@ -192,11 +210,12 @@ public class Chat { ...@@ -192,11 +210,12 @@ public class Chat {
BufferedReader remoteInput, BufferedReader remoteInput,
PrintStream localOutput, PrintStream localOutput,
PrintStream remoteOutput) { PrintStream remoteOutput) {
System.out.println("Connection established. Host goes first."); // "Connection established. Host goes first."
System.out.println(bundle.getString("connection.info.ready"));
String message = ""; String message = "";
boolean myTurnToTalk = isHost; boolean myTurnToTalk = isHost;
try { try {
while (!message.equals("EXIT")) { while (!message.equals(bundle.getString("communicate.keyword.exit"))) {
try { try {
if (myTurnToTalk) { if (myTurnToTalk) {
message = localInput.readLine(); message = localInput.readLine();
...@@ -207,13 +226,15 @@ public class Chat { ...@@ -207,13 +226,15 @@ public class Chat {
message = decipher(encipheredMessage); message = decipher(encipheredMessage);
localOutput.println(message); localOutput.println(message);
} else { } else {
localOutput.println("Received null message: lost connection to remote chatter. Terminating."); // "Received null message: lost connection to remote chatter. Terminating."
message = "EXIT"; localOutput.println(bundle.getString("communicate.error.nullMessageFromRemote"));
message = bundle.getString("communicate.keyword.exit");
} }
} }
} catch (SocketException ignored) { } catch (SocketException ignored) {
localOutput.println("Unable to exchange message: lost connection to remote chatter. Terminating."); // "Unable to exchange message: lost connection to remote chatter. Terminating."
message = "EXIT"; localOutput.println(bundle.getString("communicate.error.cannotSendMessage"));
message = bundle.getString("communicate.keyword.exit");
} }
myTurnToTalk = !myTurnToTalk; myTurnToTalk = !myTurnToTalk;
} }
......
communicate.error.cannotSendMessage = Unable to exchange message: lost connection to remote chatter. Terminating.
communicate.error.cannotSetUpStreams = Failed to set up input/output streams
communicate.error.nullMessageFromRemote = Received null message: lost connection to remote chatter. Terminating.
communicate.info.terminating = Terminating.
communicate.keyword.exit = EXIT
connection.error.badNumberInAddress = The IP address should be exactly as reported to the host user.
connection.error.badPortNumber = The port number must be a positive integer strictly less than 65536.
connection.error.closingError = "Error while closing socket" connection.error.closingError = "Error while closing socket"
connection.error.generalConnectionFailure = Connection failed connection.error.generalConnectionFailure = Connection failed
connection.error.hostNotReady.attempt.1.2.3.4.port = Attempt {0}: Chat server is not yet ready at {1}.{2}.{3}.{4}:{5} connection.error.hostNotReady.0.1.2.3.4.5 = Attempt {0}: Chat server is not yet ready at {1}.{2}.{3}.{4}:{5}
connection.error.malformedAddress = The IP address should be four dot-separated numbers; <{0}> does not conform.
connection.error.portInUse.0 = Port {0} is already in use. connection.error.portInUse.0 = Port {0} is already in use.
connection.error.portNumberTooHigh = Expected value less than 65536, got {0}
connection.error.portNumberTooLow = Expected positive value, got {0}
connection.error.userAttemptedUnsignedByte.0.1 = Note that Java does not have unsigned integers, so subtract 256 from values greater than 127. For example, {0} should be {1}.
connection.error.tooManyAttempts = Exceeded maximum number of connection attempts. Terminating. connection.error.tooManyAttempts = Exceeded maximum number of connection attempts. Terminating.
connection.info.hostAddress.0.1.2.3 = Host address: {0}.{1}.{2}.{3} connection.info.hostAddress.0.1.2.3 = Host address: {0}.{1}.{2}.{3}
connection.info.ready = Connection established. Host goes first.
connection.info.reAttempt = Will attempt to connect again in {0} seconds. connection.info.reAttempt = Will attempt to connect again in {0} seconds.
connection.info.waiting = Waiting for client. connection.info.waiting = Waiting for client.
connection.prompt.getHostAddress = Enter IP address of host <##.##.##.##>:
connection.prompt.getHostPort.0.1.2.3 = Enter port host is opening at {0}.{1}.{2}.{3} connection.prompt.getHostPort.0.1.2.3 = Enter port host is opening at {0}.{1}.{2}.{3}
connection.prompt.isHost = Are you the chat host? connection.prompt.isHost = Are you the chat host?
connection.prompt.selectPortNumber = Select port number connection.prompt.selectPortNumber = Select port number
connection.response.yes = Y connection.response.yes = Y
connection.response.no = N connection.response.no = N
exception.numberFormat.startOfValueOutOfRangeMessage= Value out of range. Value
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment