From adf33ded834fb6151a01457f5ea4ba21dad4ab41 Mon Sep 17 00:00:00 2001 From: brutzman <brutzman@nps.edu> Date: Sat, 10 Aug 2024 22:19:58 -0700 Subject: [PATCH] work in progress, updates continuing --- .../TcpExamples/TcpSentryScenarioClient.java | 89 +++++++++++++------ 1 file changed, 64 insertions(+), 25 deletions(-) diff --git a/examples/src/TcpExamples/TcpSentryScenarioClient.java b/examples/src/TcpExamples/TcpSentryScenarioClient.java index ad86f9fcd8..71507b79d7 100644 --- a/examples/src/TcpExamples/TcpSentryScenarioClient.java +++ b/examples/src/TcpExamples/TcpSentryScenarioClient.java @@ -1,34 +1,41 @@ package TcpExamples; +import static TcpExamples.TcpSentryScenarioHandlerThread.HALT_WHO_GOES_THERE; +import static TcpExamples.TcpSentryScenarioHandlerThread.HOLD_IT_RIGHT_THERE; +import static TcpExamples.TcpSentryScenarioHandlerThread.YOU_MAY_PASS; import java.io.*; import java.net.*; -//import java.time.LocalTime; // conversion? /** - * This client program establishes a socket connection to the {@link TcpExample4DispatchServer}, + * This client program establishes a socket connection to the {@link TcpSentryScenarioDispatchServer}, * then checks how long it takes to read the single line it expects as a server response. * No fancy footwork here, it is pretty simple and similar to {@link TcpExample3Client}. * + * @see TcpSentryScenarioDispatchServer + * @see TcpSentryScenarioHandlerThread + * @see TcpExample4Client * @see TcpExample4DispatchServer * @see TcpExample4HandlerThread * - * @see <a href="../../../src/TcpExamples/TcpExample4TerminalLog.txt" target="blank">TcpExample4TerminalLog.txt</a> + * @see <a href="../../../src/TcpExamples/TcpSentryScenarioTerminalLog.txt" target="blank">TcpSentryScenarioTerminalLog.txt</a> * @see <a href="../../../src/TcpExamples/TcpExample4SequenceDiagram.png" target="blank">TcpExample4SequenceDiagram.png</a> * @see <a href="../../../src/TcpExamples/TcpExample4SequenceSketch.png" target="blank">TcpExample4SequenceSketch.png</a> * - * @author Don McGregor * @author Don Brutzman + * @author Don McGregor * @author MV3500 class */ public class TcpSentryScenarioClient { + static String prefix = "[" + TcpSentryScenarioClient.class.getName() + "] "; + /** Default constructor */ public TcpSentryScenarioClient() { // default constructor } static String DESTINATION_HOST = "localhost"; - static int MAX_LOOP_COUNT = 4; + static int MAX_LOOP_COUNT = 6; /** * Program invocation, execution starts here @@ -36,44 +43,76 @@ public class TcpSentryScenarioClient */ public static void main(String[] args) { try { - System.out.println(TcpSentryScenarioClient.class.getName() + " start, loop " + MAX_LOOP_COUNT + " times"); + System.out.println(prefix + " start, loop " + MAX_LOOP_COUNT + " times"); System.out.println("======================================================="); for (int loopCount = 1; loopCount <= MAX_LOOP_COUNT; loopCount++) // loop then exit { - System.out.println(TcpSentryScenarioClient.class.getName() + " creating new socket #" + loopCount + "..."); + System.out.println(prefix + " creating new socket #" + loopCount + "..."); // We request an IP to connect to ("localhost") and // port number at that IP (2317). This establishes // a connection to that IP in the form of the Socket // object; the server uses a ServerSocket to wait for - // connections.This particualar example is interacting - // with what it expects is a server that writes a single text - // line after 10 sec. - long startTime = System.currentTimeMillis(); + // connections. // open a socket for each loop Socket socket = new Socket(DESTINATION_HOST, 2317); + +//////////////////////////////////////////////////////////////////////////////////////////// +// Assignment code + String myName = new String(); + // input stream and output stream + InputStream inputStream = socket.getInputStream(); + Reader inputStreamReader = new InputStreamReader(inputStream); + BufferedReader bufferedReader = new BufferedReader(inputStreamReader); + + OutputStream outputStream = socket.getOutputStream(); + PrintStream printStream = new PrintStream(outputStream); - // Setup. Read the single line written by the server. - // We'd do things a bit differently if many lines to be read - // from the server, instead of one only. - InputStream is = socket.getInputStream(); - Reader isr = new InputStreamReader(is); - BufferedReader br = new BufferedReader(isr); + String serverMessage = bufferedReader.readLine(); // blocks + System.out.println(prefix + ": message received from server='" + serverMessage + "'"); + + String sentryResponse = bufferedReader.readLine(); + System.out.println ("Sentry: " + sentryResponse); + + if (sentryResponse.equals(HALT_WHO_GOES_THERE)) + { + // user provides name from console + myName = System.console().readLine(); + printStream.println(myName); // send it back to dispatch server +// System.out.println ("[trace] console return: " + myName); - String serverMessage = br.readLine(); // blocks - long readTime = System.currentTimeMillis(); - long timeLength = readTime - startTime; + sentryResponse = bufferedReader.readLine(); + System.out.println ("Sentry: " + sentryResponse); + + if (sentryResponse.equals(YOU_MAY_PASS)) + { + System.out.println ("Thank you officer."); + } + else if (sentryResponse.equals(HOLD_IT_RIGHT_THERE)) + { + System.out.println ("OK I am outta here!"); + } + // handling unexpected cases is important + else System.out.println ("I'm not sure what that means..."); + } - System.out.println(TcpSentryScenarioClient.class.getName() + ": message received from server='" + serverMessage + "'"); - System.out.println(TcpSentryScenarioClient.class.getName() + ": time msec required for read=" + timeLength); System.out.println("======================================================="); - // To push this further, launch multiple copies of TcpExample4Client simultaneously + // To push this further, launch multiple copies of TcpSentryScenarioClient simultaneously + + if (myName.equalsIgnoreCase("quit") || myName.equalsIgnoreCase("exit")) + { + printStream.flush(); + socket.close(); + } } - System.out.println(TcpSentryScenarioClient.class.getName() + " complete"); + +//////////////////////////////////////////////////////////////////////////////////////////// + + System.out.println(prefix + " complete"); // main method now exits } catch (IOException e) { - System.out.println("Problem with " + TcpSentryScenarioClient.class.getName() + " networking:networking:"); // describe what is happening + System.out.println("Problem with " + prefix + " networking:networking:"); // describe what is happening System.out.println("Error: " + e); // Provide more helpful information to user if exception occurs due to running twice at one time if (e instanceof java.net.BindException) { -- GitLab