From a8e3626943295e7a0ea6a4761ef4439b96be5c18 Mon Sep 17 00:00:00 2001 From: Christopher Bohn <bohn@unl.edu> Date: Tue, 5 Nov 2019 17:04:50 -0600 Subject: [PATCH] Cleaned up design - Replaced field-based streams/readers with injected dependencies - Replaced modulo-2 indexing of input/output arrays with remote/local input/output variables - Stubbed encipher and decipher methods Closes #2 & #5 --- .../edu/unl/cse/csce361/socket_chat/Chat.java | 58 +++++++++++++------ 1 file changed, 40 insertions(+), 18 deletions(-) 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 d5175db..b99ffa4 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 @@ -12,24 +12,11 @@ public class Chat { private Socket socket; private Scanner scanner; private boolean isHost; - private PrintStream[] output = new PrintStream[2]; - private BufferedReader[] input = new BufferedReader[2]; public Chat() { scanner = new Scanner(System.in); try { socket = connect(); - if (isHost) { - input[0] = new BufferedReader(new InputStreamReader(System.in)); - input[1] = new BufferedReader(new InputStreamReader(socket.getInputStream())); - output[0] = new PrintStream(socket.getOutputStream()); - output[1] = System.out; - } else { - input[0] = new BufferedReader(new InputStreamReader(socket.getInputStream())); - input[1] = new BufferedReader(new InputStreamReader(System.in)); - output[0] = System.out; - output[1] = new PrintStream(socket.getOutputStream()); - } } catch (IOException ioException) { System.err.println("Connection failed: " + ioException); System.exit(1); @@ -38,22 +25,56 @@ public class Chat { @SuppressWarnings("WeakerAccess") public void communicate() { + try { + communicate( + new BufferedReader(new InputStreamReader(System.in)), + new BufferedReader(new InputStreamReader(socket.getInputStream())), + System.out, + new PrintStream(socket.getOutputStream())); + socket.close(); + } catch (IOException ioException) { + System.err.println("Connection dropped: " + ioException); + System.exit(1); + } + } + + @SuppressWarnings("SameParameterValue") + private void communicate(BufferedReader localInput, + BufferedReader remoteInput, + PrintStream localOutput, + PrintStream remoteOutput) { System.out.println("Connection established. Host goes first."); String message = ""; - int turn = 0; + boolean myTurnToTalk = isHost; try { while (!message.equals("EXIT")) { - message = input[turn].readLine(); - output[turn].println(message); - turn = (turn + 1) % 2; + if (myTurnToTalk) { + message = localInput.readLine(); + remoteOutput.println(encipher(message)); + } else { + message = decipher(remoteInput.readLine()); + localOutput.println(message); + } + myTurnToTalk = !myTurnToTalk; } - socket.close(); } catch (IOException ioException) { System.err.println("Connection dropped: " + ioException); System.exit(1); } } + private String encipher(String plaintext) { +// String ciphertext = ...; +// return ciphertext; + return plaintext; + } + + private String decipher(String ciphertext) { +// String plaintext = ...; +// return plaintext; + return ciphertext; + } + private Socket connect() throws IOException { System.out.print("Are you the chat host? [Y] "); String answerString = scanner.nextLine().toUpperCase(); @@ -68,6 +89,7 @@ public class Chat { String prompt = "Select port number"; int port = getPort(prompt); ServerSocket serverSocket = new ServerSocket(port); + System.out.println("Waiting for client."); return serverSocket.accept(); } -- GitLab