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

renamed for clarity

parent ae645a71
No related branches found
No related tags found
No related merge requests found
package TcpExamples;
import java.io.IOException;
import java.net.*;
/**
* This server program works a bit differently by creating and dispatching a
* new thread to handle multiple incoming socket connections, one after another, all running in parallel.
* This advanced technique is often used in high=performance high=capacity server programs.
*
* @see TcpExample4Client
* @see TcpExample4HandlerThread
*
* @see <a href="../../../src/TcpExamples/TcpExample4TerminalLog.txt" target="blank">TcpExample4TerminalLog.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 MV3500 class
*/
public class TcpExample4DispatchServer
{
/** Default constructor */
public TcpExample4DispatchServer()
{
// default constructor
}
/**
* Program invocation, execution starts here
* @param args command-line arguments
*/
public static void main(String[] args)
{
try {
ServerSocket serverSocket = new ServerSocket(2317);
Socket clientConnectionSocket;
TcpExample4HandlerThread handlerThread;
int connectionCount = 0; // state variable
System.out.println(TcpExample4DispatchServer.class.getName() + " ready to accept socket connections...");
while (true) // infinite loop
{
clientConnectionSocket = serverSocket.accept(); // block! until connected
connectionCount++; // unblocked, got another connection
// TODO option for the student, provide initial message *to the client*
// that we are handing off to a dispatch thread... because that is polite behavior.
// Plenty of code in Example3, we instead let our proxy thread
// TcpExample4HandlerThread introduce itself to the client.
System.out.println("=============================================================");
System.out.println(TcpExample4DispatchServer.class.getName() + ".handlerThread created for connection #" + connectionCount + "...");
// hand off this aready-created and connected socket to constructor
handlerThread = new TcpExample4HandlerThread(clientConnectionSocket);
handlerThread.start();// invokes the run() method in that object
System.out.println(TcpExample4DispatchServer.class.getName() + ".handlerThread is now dispatched and running, using most recent connection...");
// while(true) continue looping, serverSocket is still waiting for another customer client
}
}
catch (IOException e) {
System.out.println("Problem with " + TcpExample4DispatchServer.class.getName() + " 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) {
System.out.println("*** Be sure to stop any other running instances of programs using this port!");
}
}
System.out.println("============================================================="); // execution complete
}
}
package TcpExamples;
import static TcpExamples.TcpSentryScenarioHandlerThread.HALT_WHO_GOES_THERE;
import static TcpExamples.TcpSentryScenarioHandlerThread.HOLD_IT_RIGHT_THERE;
import static TcpExamples.TcpSentryScenarioHandlerThread.SENTRY_WATCH_PRESENT;
import static TcpExamples.TcpSentryScenarioHandlerThread.YOU_MAY_PASS;
import static TcpExamples.TcpSentryHandlerThread.HALT_WHO_GOES_THERE;
import static TcpExamples.TcpSentryHandlerThread.HOLD_IT_RIGHT_THERE;
import static TcpExamples.TcpSentryHandlerThread.SENTRY_WATCH_PRESENT;
import static TcpExamples.TcpSentryHandlerThread.YOU_MAY_PASS;
import java.io.*;
import java.net.*;
......@@ -13,7 +13,7 @@ import java.net.*;
* No fancy footwork here, it is pretty simple and similar to {@link TcpExample3Client}.
*
* @see TcpSentryScenarioDispatchServer
* @see TcpSentryScenarioHandlerThread
* @see TcpSentryHandlerThread
* @see TcpExample4Client
* @see TcpExample4DispatchServer
* @see TcpExample4HandlerThread
......@@ -26,12 +26,12 @@ import java.net.*;
* @author Don McGregor
* @author MV3500 class
*/
public class TcpSentryScenarioClient
public class TcpSentryClient
{
static String prefix = "[client] ";
/** Default constructor */
public TcpSentryScenarioClient()
public TcpSentryClient()
{
// no action needed here
}
......@@ -46,7 +46,7 @@ public class TcpSentryScenarioClient
try
{
System.out.println("===============================================================================");
System.out.println(prefix + "startup, menu 1h.TcpSentryClient, " + TcpSentryScenarioClient.class.getName());
System.out.println(prefix + "startup, menu 1h.TcpSentryClient, " + TcpSentryClient.class.getName());
System.out.println(prefix + "up to " + MAX_LOOP_COUNT + " visitors may approach.");
System.out.println("===============================================================================");
for (int loopCount = 1; loopCount <= MAX_LOOP_COUNT; loopCount++) // loop then exit
......@@ -117,7 +117,7 @@ public class TcpSentryScenarioClient
else System.out.println (prefix + "I'm not sure what that means, say again please...");
}
System.out.println("===============================================================================");
// To push this further, launch multiple copies of TcpSentryScenarioClient simultaneously
// To push this further, launch multiple copies of TcpSentryClient simultaneously
}
////////////////////////////////////////////////////////////////////////////////////////////
......
......@@ -9,7 +9,7 @@ import java.net.*;
* This advanced technique is often used in high=performance high=capacity server programs.
*
* @see TcpSentryScenarioClient
* @see TcpSentryScenarioHandlerThread
* @see TcpSentryHandlerThread
* @see TcpExample4Client
* @see TcpExample4DispatchServer
* @see TcpExample4HandlerThread
......@@ -22,12 +22,12 @@ import java.net.*;
* @author Don Brutzman
* @author MV3500 class
*/
public class TcpSentryScenarioDispatchServer
public class TcpSentryDispatchServer
{
static String prefix = "[dispatcher] ";
/** Default constructor */
public TcpSentryScenarioDispatchServer()
public TcpSentryDispatchServer()
{
// default constructor
}
......@@ -40,12 +40,12 @@ public class TcpSentryScenarioDispatchServer
try {
ServerSocket serverSocket = new ServerSocket(2317);
Socket serverSocketConnection;
TcpSentryScenarioHandlerThread sentryHandlerThread;
TcpSentryHandlerThread sentryHandlerThread;
int connectionCount = 0; // state variable
System.out.println("===============================================================================");
System.out.println(prefix + "startup, menu 1g.TcpSentryServer, " + TcpSentryScenarioDispatchServer.class.getName());
System.out.println(prefix + "startup, menu 1g.TcpSentryServer, " + TcpSentryDispatchServer.class.getName());
while (true) // infinite loop, handling client connections and dispatching another handler thread
{
......@@ -57,12 +57,12 @@ public class TcpSentryScenarioDispatchServer
// TODO option for the student, provide initial message *to the client*
// that we are handing off to a dispatch thread... because that is polite behavior.
// Plenty of code in Example3, we instead let our proxy thread
// TcpSentryScenarioHandlerThread introduce itself to the client.
// TcpSentryHandlerThread introduce itself to the client.
System.out.println(prefix + "received socket connection, creating handlerThread for visitor #" + connectionCount + "...");
// hand off this aready-created and connected socket to constructor
sentryHandlerThread = new TcpSentryScenarioHandlerThread(serverSocketConnection); // creation logs a message
sentryHandlerThread = new TcpSentryHandlerThread(serverSocketConnection); // creation logs a message
sentryHandlerThread.start();// invokes the run() method in that object, which sends initial reply on the socket
System.out.println(prefix + "a new sentry is now dispatched and running, using socket connection #" + connectionCount);
System.out.println("===============================================================================");
......
......@@ -31,7 +31,7 @@ import java.util.List;
* @author Don Brutzman
* @author MV3500 class
*/
public class TcpSentryScenarioHandlerThread extends Thread
public class TcpSentryHandlerThread extends Thread
{
/** Sentry Scenario access list, as written this list is case sensitive.
* See {@linktourl https://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line }
......@@ -55,10 +55,10 @@ public class TcpSentryScenarioHandlerThread extends Thread
*
* @param socket The socket connection handled by this thread
*/
TcpSentryScenarioHandlerThread(Socket socket)
TcpSentryHandlerThread(Socket socket)
{
this.socket = socket;
System.out.println(prefix + "1h.TcpSentryClient, " + TcpSentryScenarioHandlerThread.class.getName());
System.out.println(prefix + "1h.TcpSentryClient, " + TcpSentryHandlerThread.class.getName());
}
/**
* Program invocation and execution starts here - but is illegal and unwanted, so warn the unsuspecting user!
......@@ -80,7 +80,7 @@ public class TcpSentryScenarioHandlerThread extends Thread
{
try
{
System.out.println(prefix + "startup, menu 1g.TcpSentryServer, " + TcpSentryScenarioHandlerThread.class.getName());
System.out.println(prefix + "startup, menu 1g.TcpSentryServer, " + TcpSentryHandlerThread.class.getName());
// now starting to handle the thread
// setup input stream and output stream
......
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