Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
NetworkedGraphicsMV3500
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Savage
NetworkedGraphicsMV3500
Commits
1ce8fc35
Commit
1ce8fc35
authored
10 months ago
by
Brutzman, Don
Browse files
Options
Downloads
Patches
Plain Diff
work in progress, updates continuing
parent
adf33ded
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
examples/src/TcpExamples/TcpSentryScenarioDispatchServer.java
+18
-11
18 additions, 11 deletions
...ples/src/TcpExamples/TcpSentryScenarioDispatchServer.java
examples/src/TcpExamples/TcpSentryScenarioHandlerThread.java
+55
-17
55 additions, 17 deletions
examples/src/TcpExamples/TcpSentryScenarioHandlerThread.java
with
73 additions
and
28 deletions
examples/src/TcpExamples/TcpSentryScenarioDispatchServer.java
+
18
−
11
View file @
1ce8fc35
...
@@ -8,10 +8,13 @@ import java.net.*;
...
@@ -8,10 +8,13 @@ import java.net.*;
* new thread to handle multiple incoming socket connections, one after another, all running in parallel.
* 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.
* This advanced technique is often used in high=performance high=capacity server programs.
*
*
* @see TcpSentryScenarioClient
* @see TcpSentryScenarioHandlerThread
* @see TcpExample4Client
* @see TcpExample4Client
* @see TcpExample4DispatchServer
* @see TcpExample4HandlerThread
* @see TcpExample4HandlerThread
*
*
* @see <a href="../../../src/TcpExamples/Tcp
Example4
TerminalLog.txt" target="blank">Tcp
Example4
TerminalLog.txt</a>
* @see <a href="../../../src/TcpExamples/Tcp
SentryScenario
TerminalLog.txt" target="blank">Tcp
SentryScenario
TerminalLog.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>
*
*
...
@@ -21,6 +24,8 @@ import java.net.*;
...
@@ -21,6 +24,8 @@ import java.net.*;
*/
*/
public
class
TcpSentryScenarioDispatchServer
public
class
TcpSentryScenarioDispatchServer
{
{
static
String
prefix
=
"["
+
TcpSentryScenarioDispatchServer
.
class
.
getName
()
+
"] "
;
/** Default constructor */
/** Default constructor */
public
TcpSentryScenarioDispatchServer
()
public
TcpSentryScenarioDispatchServer
()
{
{
...
@@ -33,14 +38,15 @@ public class TcpSentryScenarioDispatchServer
...
@@ -33,14 +38,15 @@ public class TcpSentryScenarioDispatchServer
public
static
void
main
(
String
[]
args
)
public
static
void
main
(
String
[]
args
)
{
{
try
{
try
{
ServerSocket
serverSocket
=
new
ServerSocket
(
2317
);
ServerSocket
serverSocket
=
new
ServerSocket
(
2317
);
Socket
clientConnectionSocket
;
Socket
clientConnectionSocket
;
Tcp
Example4
HandlerThread
handlerThread
;
Tcp
SentryScenario
HandlerThread
handlerThread
;
int
connectionCount
=
0
;
// state variable
int
connectionCount
=
0
;
// state variable
System
.
out
.
println
(
TcpSentryScenarioDispatchServer
.
class
.
getName
()
+
" ready to accept socket connections..."
);
System
.
out
.
println
(
prefix
+
"waiting and ready to accept socket connection from client..."
);
while
(
true
)
// infinite loop
while
(
true
)
// infinite loop, handling client connections and dispatching another handler thread
{
{
clientConnectionSocket
=
serverSocket
.
accept
();
// block! until connected
clientConnectionSocket
=
serverSocket
.
accept
();
// block! until connected
...
@@ -49,26 +55,27 @@ public class TcpSentryScenarioDispatchServer
...
@@ -49,26 +55,27 @@ public class TcpSentryScenarioDispatchServer
// TODO option for the student, provide initial message *to the client*
// 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.
// 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
// Plenty of code in Example3, we instead let our proxy thread
// Tcp
Example4
HandlerThread introduce itself to the client.
// Tcp
SentryScenario
HandlerThread introduce itself to the client.
System
.
out
.
println
(
"============================================================="
);
System
.
out
.
println
(
"============================================================="
);
System
.
out
.
println
(
TcpSentryScenarioDispatchServer
.
class
.
getName
()
+
".handlerThread created for connection #"
+
connectionCount
+
"..."
);
System
.
out
.
println
(
prefix
+
".handlerThread created for connection #"
+
connectionCount
+
"..."
);
// hand off this aready-created and connected socket to constructor
// hand off this aready-created and connected socket to constructor
handlerThread
=
new
Tcp
Example4
HandlerThread
(
clientConnectionSocket
);
handlerThread
=
new
Tcp
SentryScenario
HandlerThread
(
clientConnectionSocket
);
handlerThread
.
start
();
// invokes the run() method in that object
handlerThread
.
start
();
// invokes the run() method in that object
System
.
out
.
println
(
TcpSentryScenarioDispatchServer
.
class
.
getName
()
+
".handlerThread is now dispatched and running, using most recent connection..."
);
System
.
out
.
println
(
prefix
+
".handlerThread is now dispatched and running, using most recent connection..."
);
// while(true) continue looping, serverSocket is still waiting for another customer client
// while(true) continue looping, serverSocket is still waiting for another customer client
}
}
}
}
catch
(
IOException
e
)
{
catch
(
IOException
e
)
{
System
.
out
.
println
(
"Problem with "
+
TcpSentryScenarioDispatchServer
.
class
.
getName
()
+
" networking:"
);
// describe what is happening
System
.
out
.
println
(
"Problem with "
+
prefix
+
" 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
)
{
System
.
out
.
println
(
"*** Be sure to stop any other running instances of programs using this port!"
);
System
.
out
.
println
(
"*** Be sure to stop any other running instances of programs using this port!"
);
}
}
System
.
exit
(-
1
);
}
}
System
.
out
.
println
(
"============================================================="
);
// execution complete
System
.
out
.
println
(
"============================================================="
);
// execution complete
}
}
...
...
This diff is collapsed.
Click to expand it.
examples/src/TcpExamples/TcpSentryScenarioHandlerThread.java
+
55
−
17
View file @
1ce8fc35
...
@@ -2,17 +2,19 @@ package TcpExamples;
...
@@ -2,17 +2,19 @@ package TcpExamples;
import
java.io.*
;
import
java.io.*
;
import
java.net.*
;
import
java.net.*
;
import
java.util.Arrays
;
import
java.util.List
;
/**
/**
* <p>
* <p>
* This utility class supports the {@link Tcp
Example4
DispatchServer} program,
* This utility class supports the {@link Tcp
SentryScenario
DispatchServer} program,
* handling all programming logic needed for a new socket connection
* handling all programming logic needed for a new socket connection
* to run in a thread of its own. This is the server
* to run in a thread of its own. This is the server
* portion as well, so we artificially invent what happens
* portion as well, so we artificially invent what happens
* if the server can't respond to a connection for several seconds.
* if the server can't respond to a connection for several seconds.
* </p>
* </p>
* <p>
* <p>
* Warning: do not run this class! It is created and used automatically by {@link Tcp
Example4
DispatchServer} at run time.
* Warning: do not run this class! It is created and used automatically by {@link Tcp
SentryScenario
DispatchServer} at run time.
* </p>
* </p>
*
*
* @see TcpSentryScenarioClient
* @see TcpSentryScenarioClient
...
@@ -21,7 +23,7 @@ import java.net.*;
...
@@ -21,7 +23,7 @@ import java.net.*;
* @see TcpExample4DispatchServer
* @see TcpExample4DispatchServer
* @see TcpExample4HandlerThread
* @see TcpExample4HandlerThread
*
*
* @see <a href="../../../src/TcpExamples/Tcp
Example4
TerminalLog.txt" target="blank">Tcp
Example4
TerminalLog.txt</a>
* @see <a href="../../../src/TcpExamples/Tcp
SentryScenario
TerminalLog.txt" target="blank">Tcp
SentryScenario
TerminalLog.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>
*
*
...
@@ -31,6 +33,18 @@ import java.net.*;
...
@@ -31,6 +33,18 @@ import java.net.*;
*/
*/
public
class
TcpSentryScenarioHandlerThread
extends
Thread
public
class
TcpSentryScenarioHandlerThread
extends
Thread
{
{
/** Sentry Scenario access list
* See {@linktourl https://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line }
*/
public
static
List
<
String
>
allowedNamesList
=
Arrays
.
asList
(
"Tim"
,
"Stephen"
,
"Mark"
,
"Rene"
,
"Simon"
,
"James"
,
"Ethan"
,
"Jin Hong"
);
public
static
final
String
HALT_WHO_GOES_THERE
=
"Halt who goes there?"
;
public
static
final
String
YOU_MAY_PASS
=
"You may pass, have a great NPS MOVES day!"
;
public
static
final
String
HOLD_IT_RIGHT_THERE
=
"You may not pass, leave immediately or else"
;
private
static
final
String
prefix
=
"["
+
TcpSentryScenarioHandlerThread
.
class
.
getName
()
+
"] "
;
/** The socket connection to a client */
/** The socket connection to a client */
Socket
socket
;
Socket
socket
;
...
@@ -50,8 +64,8 @@ public class TcpSentryScenarioHandlerThread extends Thread
...
@@ -50,8 +64,8 @@ public class TcpSentryScenarioHandlerThread extends Thread
*/
*/
public
static
void
main
(
String
[]
args
)
public
static
void
main
(
String
[]
args
)
{
{
System
.
out
.
println
(
"*** Tcp
Example4
HandlerThread is not a standalone executable progam."
);
System
.
out
.
println
(
"*** Tcp
SentryScenario
HandlerThread is not a standalone executable progam."
);
System
.
out
.
println
(
"*** Please run Tcp
Example4
DispatchServer instead... now exiting."
);
System
.
out
.
println
(
"*** Please run Tcp
SentryScenario
DispatchServer instead... now exiting."
);
}
}
/** Handles one connection. We add an artificial slowness
/** Handles one connection. We add an artificial slowness
...
@@ -64,26 +78,50 @@ public class TcpSentryScenarioHandlerThread extends Thread
...
@@ -64,26 +78,50 @@ public class TcpSentryScenarioHandlerThread extends Thread
{
{
try
try
{
{
System
.
out
.
println
(
TcpSentryScenarioHandlerThread
.
class
.
getName
()
+
"
starting to handle a thread..."
);
System
.
out
.
println
(
prefix
+
"starting to handle a thread..."
);
// get the connection output stream, then wait a period of time.
// input stream and output stream
OutputStream
os
=
socket
.
getOutputStream
();
InputStream
inputStream
=
socket
.
getInputStream
();
PrintStream
ps
=
new
PrintStream
(
os
);
Reader
inputStreamReader
=
new
InputStreamReader
(
inputStream
);
BufferedReader
bufferedReader
=
new
BufferedReader
(
inputStreamReader
);
final
long
TIMEOUT
=
2000
;
// 2000 milliseconds = 2 seconds, 10000 milliseconds = 10 seconds
OutputStream
outputStream
=
socket
.
getOutputStream
();
System
.
out
.
println
(
TcpSentryScenarioHandlerThread
.
class
.
getName
()
+
" pausing for TIMEOUT="
+
TIMEOUT
+
"ms"
+
PrintStream
printStream
=
new
PrintStream
(
outputStream
);
" to emulate computation and avoid server-side overload"
);
final
long
TIMEOUT
=
100
;
// 2000 milliseconds = 2 seconds, 10000 milliseconds = 10 seconds
// System.out.println(prefix + " pausing for TIMEOUT=" + TIMEOUT + "ms" +
// " to emulate computation and avoid server-side overload");
Thread
.
sleep
(
TIMEOUT
);
Thread
.
sleep
(
TIMEOUT
);
// ps is the PrintStream is the Java way to use System.print() to pass data along the socket.
// ps is the PrintStream is the Java way to use System.print() to pass data along the socket.
ps
.
println
(
"This message was written by the server "
+
TcpSentryScenarioHandlerThread
.
class
.
getName
());
// TODO insert socket count here!
printStream
.
println
(
prefix
+
"This message was written by the server "
);
// TODO insert socket count here!
ps
.
flush
();
// make sure that it indeed escapes current process and reaches the client
printStream
.
flush
();
// make sure that it indeed escapes current process and reaches the client
socket
.
close
();
// all clear, no longer need socket
System
.
out
.
println
(
TcpSentryScenarioHandlerThread
.
class
.
getName
()
+
" finished handling a thread, now exit."
);
////////////////////////////////////////////////////////////////////////////////////////////
// Assignment code
printStream
.
println
(
HALT_WHO_GOES_THERE
);
System
.
out
.
println
(
"socket send: "
+
HALT_WHO_GOES_THERE
);
String
clientResponse
=
bufferedReader
.
readLine
();
System
.
out
.
println
(
"clientResponse: "
+
clientResponse
);
if
(
clientResponse
.
equalsIgnoreCase
(
"quit"
)
||
clientResponse
.
equalsIgnoreCase
(
"exit"
))
{
printStream
.
flush
();
socket
.
close
();
}
System
.
out
.
println
(
"Hello, "
+
clientResponse
);
////////////////////////////////////////////////////////////////////////////////////////////
//// socket.close(); // all clear, no longer need socket
System
.
out
.
println
(
prefix
+
"finished handling a thread, now exit."
);
System
.
out
.
println
(
"============================================================="
);
// execution complete
}
}
catch
(
IOException
|
InterruptedException
e
)
// either a networking or a threading problem
catch
(
IOException
|
InterruptedException
e
)
// either a networking or a threading problem
{
{
System
.
out
.
println
(
"Problem with "
+
TcpSentryScenarioHandlerThread
.
class
.
getName
()
+
" networking:"
);
// describe what is happening
System
.
out
.
println
(
"Problem with "
+
prefix
+
" 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
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment