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

Chat can now recognize keywords other than EXIT

The program doesn't yet actually *do* anything in response to other
keywords, but the handleKeyword() method is ready for other keywords.

Closes #10
parent 9b4f0fdc
Branches
No related tags found
No related merge requests found
...@@ -3,10 +3,8 @@ package edu.unl.cse.csce361.socket_chat; ...@@ -3,10 +3,8 @@ package edu.unl.cse.csce361.socket_chat;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.InputMismatchException; import java.util.*;
import java.util.Locale; import java.util.stream.Collectors;
import java.util.ResourceBundle;
import java.util.Scanner;
import static java.lang.Thread.sleep; import static java.lang.Thread.sleep;
...@@ -16,12 +14,22 @@ public class Chat { ...@@ -16,12 +14,22 @@ public class Chat {
private Socket socket; private Socket socket;
private boolean isHost; private boolean isHost;
private ResourceBundle bundle; private ResourceBundle bundle;
private Set<String> keywords;
public Chat() { public Chat() {
bundle = ResourceBundle.getBundle("socketchat", Locale.US); setLocale(Locale.getDefault());
socket = connect(new Scanner(System.in)); 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 * THESE METHODS SET UP AND TEAR DOWN CONNECTION
*/ */
...@@ -212,29 +220,32 @@ public class Chat { ...@@ -212,29 +220,32 @@ public class Chat {
PrintStream remoteOutput) { PrintStream remoteOutput) {
// "Connection established. Host goes first." // "Connection established. Host goes first."
System.out.println(bundle.getString("connection.info.ready")); System.out.println(bundle.getString("connection.info.ready"));
String message = ""; String message;
boolean keepTalking = true;
boolean myTurnToTalk = isHost; boolean myTurnToTalk = isHost;
try { try {
while (!message.equals(bundle.getString("communicate.keyword.exit"))) { while (keepTalking) {
try { try {
if (myTurnToTalk) { if (myTurnToTalk) {
message = localInput.readLine(); message = localInput.readLine();
remoteOutput.println(encipher(message)); remoteOutput.println(encipher(message));
keepTalking = !keywords.contains(message) || handleKeyword(message, localInput, localOutput);
} else { } else {
String encipheredMessage = remoteInput.readLine(); String encipheredMessage = remoteInput.readLine();
if (encipheredMessage != null) { if (encipheredMessage != null) {
message = decipher(encipheredMessage); message = decipher(encipheredMessage);
localOutput.println(message); localOutput.println(message);
keepTalking = !keywords.contains(message) || handleKeyword(message, localInput, localOutput);
} else { } else {
// "Received null message: lost connection to remote chatter. Terminating." // "Received null message: lost connection to remote chatter. Terminating."
localOutput.println(bundle.getString("communicate.error.nullMessageFromRemote")); localOutput.println(bundle.getString("communicate.error.nullMessageFromRemote"));
message = bundle.getString("communicate.keyword.exit"); keepTalking = false;
} }
} }
} catch (SocketException ignored) { } catch (SocketException ignored) {
// "Unable to exchange message: lost connection to remote chatter. Terminating." // "Unable to exchange message: lost connection to remote chatter. Terminating."
localOutput.println(bundle.getString("communicate.error.cannotSendMessage")); localOutput.println(bundle.getString("communicate.error.cannotSendMessage"));
message = bundle.getString("communicate.keyword.exit"); keepTalking = false;
} }
myTurnToTalk = !myTurnToTalk; myTurnToTalk = !myTurnToTalk;
} }
...@@ -244,6 +255,26 @@ public class Chat { ...@@ -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) { private String encipher(String plaintext) {
// String ciphertext = ...; // String ciphertext = ...;
// return ciphertext; // return ciphertext;
......
...@@ -17,6 +17,7 @@ connection.error.portNumberTooHigh = Expected value less than 6 ...@@ -17,6 +17,7 @@ connection.error.portNumberTooHigh = Expected value less than 6
connection.error.portNumberTooLow = Expected positive value, 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.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.
communicate.error.unrecognizedKeyword = Unrecognized keyword
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.ready = Connection established. Host goes first.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment