Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
socket_chat
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
CSCE 361
starter code
socket_chat
Commits
c3a28e68
Commit
c3a28e68
authored
5 years ago
by
Christopher Bohn
Browse files
Options
Downloads
Patches
Plain Diff
Refactored for improved testability
parent
7c971ac1
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java
+45
-25
45 additions, 25 deletions
src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java
with
45 additions
and
25 deletions
src/main/java/edu/unl/cse/csce361/socket_chat/Chat.java
+
45
−
25
View file @
c3a28e68
...
...
@@ -273,13 +273,32 @@ public class Chat {
PrintStream
remoteOutput
)
{
// "Connection established. Host goes first."
System
.
out
.
println
(
bundle
.
getString
(
"connection.info.ready"
));
String
message
=
""
;
boolean
keepTalking
=
true
;
boolean
myTurnToTalk
=
isHost
;
try
{
while
(
keepTalking
)
{
keepTalking
=
communicateOneMessage
(
localInput
,
remoteInput
,
localOutput
,
remoteOutput
,
myTurnToTalk
);
myTurnToTalk
=
!
myTurnToTalk
;
}
}
/**
* Processes a single message from one chatter to the other.
*
* @param localInput a {@link java.io.BufferedReader} that gets user input from this end of the connection
* @param remoteInput a {@link java.io.BufferedReader} that gets user input from the other end of the connection
* @param localOutput a {@link java.io.PrintStream} that outputs to the user at this end of the connection
* @param remoteOutput a {@link java.io.PrintStream} that outputs to the user at the other end of the connection
* @param localMessage {@code true} if the message to be processed will originate locally, {@code false} if remotely
* @return {@code true} if the program should continue to execute after processing the message; {@code false}
* otherwise
*/
boolean
communicateOneMessage
(
BufferedReader
localInput
,
BufferedReader
remoteInput
,
PrintStream
localOutput
,
PrintStream
remoteOutput
,
boolean
localMessage
)
{
String
message
=
""
;
boolean
keepTalking
=
true
;
try
{
try
{
if
(
myTurnToTalk
)
{
if
(
localMessage
)
{
message
=
localInput
.
readLine
();
remoteOutput
.
println
(
encipher
(
message
));
}
else
{
...
...
@@ -299,14 +318,13 @@ public class Chat {
keepTalking
=
false
;
}
if
(
keepTalking
&&
keywords
.
contains
(
message
))
{
keepTalking
=
handleKeyword
(
message
,
myTurnToTalk
,
localInput
,
localOutput
);
}
myTurnToTalk
=
!
myTurnToTalk
;
keepTalking
=
handleKeyword
(
message
,
localMessage
,
localInput
,
localOutput
);
}
}
catch
(
IOException
ioException
)
{
System
.
err
.
println
(
"Connection dropped: "
+
ioException
);
System
.
exit
(
1
);
}
return
keepTalking
;
}
/**
...
...
@@ -319,8 +337,10 @@ public class Chat {
* @param output a {@link java.io.PrintStream} for output to the user
* @return {@code true} if the program should continue to execute after processing the keyword; {@code false}
* otherwise
* @throws IOException if an I/O error occurs while reading user responses to prompts
*/
private
boolean
handleKeyword
(
String
keyword
,
boolean
localMessage
,
BufferedReader
input
,
PrintStream
output
)
{
boolean
handleKeyword
(
String
keyword
,
boolean
localMessage
,
BufferedReader
input
,
PrintStream
output
)
throws
IOException
{
if
(
keyword
.
equals
(
bundle
.
getString
(
"communicate.keyword.exit"
)))
{
return
false
;
// Un-comment this next code block in step 3 of the assignment.
...
...
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