Skip to content
Snippets Groups Projects
Commit adf33ded authored by Brutzman, Don's avatar Brutzman, Don
Browse files

work in progress, updates continuing

parent 7b99d106
No related branches found
No related tags found
No related merge requests found
package TcpExamples; 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.io.*;
import java.net.*; 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. * 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}. * No fancy footwork here, it is pretty simple and similar to {@link TcpExample3Client}.
* *
* @see TcpSentryScenarioDispatchServer
* @see TcpSentryScenarioHandlerThread
* @see TcpExample4Client
* @see TcpExample4DispatchServer * @see TcpExample4DispatchServer
* @see TcpExample4HandlerThread * @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/TcpExample4SequenceDiagram.png" target="blank">TcpExample4SequenceDiagram.png</a>
* @see <a href="../../../src/TcpExamples/TcpExample4SequenceSketch.png" target="blank">TcpExample4SequenceSketch.png</a> * @see <a href="../../../src/TcpExamples/TcpExample4SequenceSketch.png" target="blank">TcpExample4SequenceSketch.png</a>
* *
* @author Don McGregor
* @author Don Brutzman * @author Don Brutzman
* @author Don McGregor
* @author MV3500 class * @author MV3500 class
*/ */
public class TcpSentryScenarioClient public class TcpSentryScenarioClient
{ {
static String prefix = "[" + TcpSentryScenarioClient.class.getName() + "] ";
/** Default constructor */ /** Default constructor */
public TcpSentryScenarioClient() public TcpSentryScenarioClient()
{ {
// default constructor // default constructor
} }
static String DESTINATION_HOST = "localhost"; static String DESTINATION_HOST = "localhost";
static int MAX_LOOP_COUNT = 4; static int MAX_LOOP_COUNT = 6;
/** /**
* Program invocation, execution starts here * Program invocation, execution starts here
...@@ -36,44 +43,76 @@ public class TcpSentryScenarioClient ...@@ -36,44 +43,76 @@ public class TcpSentryScenarioClient
*/ */
public static void main(String[] args) { public static void main(String[] args) {
try { 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("======================================================="); System.out.println("=======================================================");
for (int loopCount = 1; loopCount <= MAX_LOOP_COUNT; loopCount++) // loop then exit 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 // We request an IP to connect to ("localhost") and
// port number at that IP (2317). This establishes // port number at that IP (2317). This establishes
// a connection to that IP in the form of the Socket // a connection to that IP in the form of the Socket
// object; the server uses a ServerSocket to wait for // object; the server uses a ServerSocket to wait for
// connections.This particualar example is interacting // connections.
// with what it expects is a server that writes a single text
// line after 10 sec.
long startTime = System.currentTimeMillis();
// open a socket for each loop // open a socket for each loop
Socket socket = new Socket(DESTINATION_HOST, 2317); 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. String serverMessage = bufferedReader.readLine(); // blocks
// We'd do things a bit differently if many lines to be read System.out.println(prefix + ": message received from server='" + serverMessage + "'");
// from the server, instead of one only.
InputStream is = socket.getInputStream(); String sentryResponse = bufferedReader.readLine();
Reader isr = new InputStreamReader(is); System.out.println ("Sentry: " + sentryResponse);
BufferedReader br = new BufferedReader(isr);
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 sentryResponse = bufferedReader.readLine();
long readTime = System.currentTimeMillis(); System.out.println ("Sentry: " + sentryResponse);
long timeLength = readTime - startTime;
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("======================================================="); 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 // main method now exits
} catch (IOException e) { } 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); System.out.println("Error: " + e);
// Provide more helpful information to user if exception occurs due to running twice at one time // Provide more helpful information to user if exception occurs due to running twice at one time
if (e instanceof java.net.BindException) { if (e instanceof java.net.BindException) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment