diff --git a/src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java b/src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java index 5668a7e2d70a3f9a618e13b635485f065fd27b8d..880ca66365fffdf0daf1f25bd13004fcdd32cb2f 100644 --- a/src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java +++ b/src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java @@ -3,10 +3,8 @@ package edu.unl.cse.csce361.socket_chat; import java.io.*; import java.net.*; import java.text.MessageFormat; -import java.util.InputMismatchException; -import java.util.Locale; -import java.util.ResourceBundle; -import java.util.Scanner; +import java.util.*; +import java.util.stream.Collectors; import static java.lang.Thread.sleep; @@ -16,12 +14,22 @@ public class Chat { private Socket socket; private boolean isHost; private ResourceBundle bundle; + private Set<String> keywords; public Chat() { - bundle = ResourceBundle.getBundle("socketchat", Locale.US); + setLocale(Locale.getDefault()); socket = connect(new Scanner(System.in)); } + private void setLocale(Locale locale) { + bundle = ResourceBundle.getBundle("socketchat", locale); + Set<String> culledKeySet = bundle.keySet().stream() + .filter(entry -> entry.startsWith("communicate.keyword.")) + .collect(Collectors.toSet()); + keywords = new HashSet<>(culledKeySet.size()); + culledKeySet.forEach(key -> keywords.add(bundle.getString(key))); + } + /* * THESE METHODS SET UP AND TEAR DOWN CONNECTION */ @@ -212,29 +220,32 @@ public class Chat { PrintStream remoteOutput) { // "Connection established. Host goes first." System.out.println(bundle.getString("connection.info.ready")); - String message = ""; + String message; + boolean keepTalking = true; boolean myTurnToTalk = isHost; try { - while (!message.equals(bundle.getString("communicate.keyword.exit"))) { + while (keepTalking) { try { if (myTurnToTalk) { message = localInput.readLine(); remoteOutput.println(encipher(message)); + keepTalking = !keywords.contains(message) || handleKeyword(message, localInput, localOutput); } else { String encipheredMessage = remoteInput.readLine(); if (encipheredMessage != null) { message = decipher(encipheredMessage); localOutput.println(message); + keepTalking = !keywords.contains(message) || handleKeyword(message, localInput, localOutput); } else { // "Received null message: lost connection to remote chatter. Terminating." localOutput.println(bundle.getString("communicate.error.nullMessageFromRemote")); - message = bundle.getString("communicate.keyword.exit"); + keepTalking = false; } } } catch (SocketException ignored) { // "Unable to exchange message: lost connection to remote chatter. Terminating." localOutput.println(bundle.getString("communicate.error.cannotSendMessage")); - message = bundle.getString("communicate.keyword.exit"); + keepTalking = false; } myTurnToTalk = !myTurnToTalk; } @@ -244,6 +255,26 @@ public class Chat { } } + private boolean handleKeyword(String keyword, BufferedReader input, PrintStream output) { + if (keyword.equals(bundle.getString("communicate.keyword.exit"))) { + return false; + /* + } else if (keyword.equals(bundle.getString("communicate.keyword.setLocale"))) { + if (isHost) { + Prompt user using output.println() (be sure to use i18n properties) + and get response using input.readLine(). Get the appropriate Locale and call + setLocale( ... ); + } + else { + output.println("Remote chatter is making updates; please be patient."); // replace with i18n property + } + */ + } else { + output.println(bundle.getString("communicate.error.unrecognizedKeyword") + ": " + keyword); + } + return true; + } + private String encipher(String plaintext) { // String ciphertext = ...; // return ciphertext; diff --git a/src/main/resources/socketchat_en.properties b/src/main/resources/socketchat_en.properties index bf6896eee0bd96583791c68bb5446843cc64e5cc..43e70873883da3ce0f6cc5f11b52de419ff6b27e 100644 --- a/src/main/resources/socketchat_en.properties +++ b/src/main/resources/socketchat_en.properties @@ -17,6 +17,7 @@ connection.error.portNumberTooHigh = Expected value less than 6 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. +communicate.error.unrecognizedKeyword = Unrecognized keyword connection.info.hostAddress.0.1.2.3 = Host address: {0}.{1}.{2}.{3} connection.info.ready = Connection established. Host goes first.