Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
COM2022 Implementations
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
Ramkumar, Ragulan (UG - Comp Sci & Elec Eng)
COM2022 Implementations
Commits
6474bf19
Commit
6474bf19
authored
3 years ago
by
Ramkumar, Ragulan (UG - Comp Sci & Elec Eng)
Browse files
Options
Downloads
Patches
Plain Diff
Update Server/ClientThread.java
parent
b75d836b
Branches
main
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Server/ClientThread.java
+60
-44
60 additions, 44 deletions
Server/ClientThread.java
with
60 additions
and
44 deletions
Server/ClientThread.java
+
60
−
44
View file @
6474bf19
...
...
@@ -27,58 +27,69 @@ public class ClientThread implements Runnable {
while
(
true
)
{
String
message
=
this
.
clientInput
.
readLine
();
message
=
message
.
toUpperCase
();
if
(
message
.
equals
(
"HELLO"
))
{
this
.
UUID
=
java
.
util
.
UUID
.
randomUUID
();
Server
.
UUIDMap
.
put
(
this
.
UUID
.
toString
(),
this
);
send
(
"WELCOME"
);
send
(
"UUID "
+
this
.
UUID
);
Server
.
clientQueue
.
add
(
getClientObject
());
if
(
Server
.
clientQueue
.
size
()
==
1
)
{
// only person in queue
send
(
"NEXT"
);
// send client straight to resource
}
else
{
// send position and add to queue
send
(
"POSITION "
+
getPosition
());
}
send
(
""
);
}
else
if
(
message
.
equals
(
"POSITION"
))
{
send
(
"POSITION "
+
getPosition
());
send
(
""
);
}
else
if
(
message
.
startsWith
(
"UUID "
))
{
String
UUID
=
message
.
substring
(
5
,
message
.
length
()).
toLowerCase
();
if
(
Server
.
UUIDMap
.
containsKey
(
UUID
))
{
this
.
client
=
Server
.
UUIDMap
.
get
(
UUID
);
send
(
"WELCOME_BACK"
);
String
response_code
=
message
.
split
(
"\\s+"
)[
0
];
switch
(
response_code
)
{
case
"HELLO"
:
this
.
UUID
=
java
.
util
.
UUID
.
randomUUID
();
Server
.
UUIDMap
.
put
(
this
.
UUID
.
toString
(),
this
);
send
(
"WELCOME"
);
send
(
"UUID "
+
this
.
UUID
);
Server
.
clientQueue
.
add
(
getClientObject
());
if
(
Server
.
clientQueue
.
size
()
==
1
)
{
// only person in queue
send
(
"NEXT"
);
// send client straight to resource
}
else
{
// send position and add to queue
send
(
"POSITION "
+
getPosition
());
}
send
(
""
);
break
;
case
"POSITION"
:
send
(
"POSITION "
+
getPosition
());
}
else
{
send
(
"INCORRECT_UUID"
);
send
(
"Terminating connection"
);
send
(
""
);
serverOutput
.
close
();
}
send
(
""
);
}
else
if
(
message
.
equals
(
"GOODBYE"
))
{
send
(
"GOODBYE"
);
send
(
""
);
Server
.
clientQueue
.
remove
(
getClientObject
());
Server
.
UUIDMap
.
remove
(
getClientObject
().
getUUID
().
toString
());
serverOutput
.
close
();
}
else
if
(
message
.
equals
(
"DONE"
))
{
if
(
Server
.
clientQueue
.
indexOf
(
getClientObject
())
==
0
)
{
send
(
"Your connection is now being terminated."
);
break
;
case
"UUID"
:
String
UUID
=
message
.
substring
(
5
,
message
.
length
()).
toLowerCase
();
if
(
Server
.
UUIDMap
.
containsKey
(
UUID
))
{
this
.
client
=
Server
.
UUIDMap
.
get
(
UUID
);
send
(
"WELCOME_BACK"
);
send
(
"POSITION "
+
getPosition
());
}
else
{
send
(
"INCORRECT_UUID"
);
send
(
"Terminating connection"
);
send
(
""
);
serverOutput
.
close
();
}
send
(
""
);
break
;
case
"GOODBYE"
:
send
(
"GOODBYE"
);
send
(
""
);
removeClientFromServer
(
getClientObject
());
serverOutput
.
close
();
Server
.
clientQueue
.
remove
(
getClientObject
());
}
else
{
send
(
"It's not your turn. To exit the queue send GOODBYE."
);
break
;
case
"DONE"
:
if
(
Server
.
clientQueue
.
indexOf
(
getClientObject
())
==
0
)
{
send
(
"Your connection is now being terminated."
);
send
(
"GOODBYE"
);
send
(
""
);
removeClientFromServer
(
getClientObject
());
}
else
{
send
(
"It's not your turn. To exit the queue send GOODBYE."
);
send
(
""
);
}
break
;
default
:
send
(
"UNKNOWN_COMMAND"
);
send
(
""
);
}
}
else
{
send
(
"UNKNOWN_COMMAND"
);
send
(
""
);
}
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
finally
{
serverOutput
.
close
();
...
...
@@ -102,6 +113,11 @@ public class ClientThread implements Runnable {
serverOutput
.
println
(
s
);
}
private
void
removeClientFromServer
(
ClientThread
client
)
{
Server
.
clientQueue
.
remove
(
client
);
Server
.
UUIDMap
.
remove
(
client
.
getUUID
().
toString
());
}
public
String
getUUID
()
{
return
this
.
UUID
.
toString
();
}
...
...
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