Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
grpc_examples
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Gabriele Civitarese
grpc_examples
Commits
a44aa90f
You need to sign in or sign up before continuing.
Commit
a44aa90f
authored
May 04, 2018
by
Gabriele Civitarese
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Aggiunta sincronizzazione lato server
parent
c7004263
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
4 deletions
+23
-4
ChatServiceImpl.java
src/main/java/chat/ChatServiceImpl.java
+23
-4
No files found.
src/main/java/chat/ChatServiceImpl.java
View file @
a44aa90f
...
@@ -15,8 +15,11 @@ public class ChatServiceImpl extends ChatServiceGrpc.ChatServiceImplBase {
...
@@ -15,8 +15,11 @@ public class ChatServiceImpl extends ChatServiceGrpc.ChatServiceImplBase {
@Override
public
StreamObserver
<
ChatServiceOuterClass
.
ChatMessage
>
chat
(
final
StreamObserver
<
ChatServiceOuterClass
.
ChatMessage
>
responseObserver
){
@Override
public
StreamObserver
<
ChatServiceOuterClass
.
ChatMessage
>
chat
(
final
StreamObserver
<
ChatServiceOuterClass
.
ChatMessage
>
responseObserver
){
//the stream used to communicate with a specific client is stored in a hash set (avoiding duplicates)
//the stream used to communicate with a specific client is stored in a hash set (avoiding duplicates)
observers
.
add
(
responseObserver
);
synchronized
(
observers
)
{
observers
.
add
(
responseObserver
);
}
//it returns the stream that will be used by the clients to send messages.
//it returns the stream that will be used by the clients to send messages.
//the client will write on this stream
//the client will write on this stream
return
new
StreamObserver
<
ChatServiceOuterClass
.
ChatMessage
>()
{
return
new
StreamObserver
<
ChatServiceOuterClass
.
ChatMessage
>()
{
...
@@ -30,8 +33,17 @@ public class ChatServiceImpl extends ChatServiceGrpc.ChatServiceImplBase {
...
@@ -30,8 +33,17 @@ public class ChatServiceImpl extends ChatServiceGrpc.ChatServiceImplBase {
System
.
out
.
println
(
"[MESSAGE RECEIVED] Received a message from "
+
from
+
": "
+
message
);
System
.
out
.
println
(
"[MESSAGE RECEIVED] Received a message from "
+
from
+
": "
+
message
);
HashSet
<
StreamObserver
>
copy
;
synchronized
(
observers
)
{
copy
=
new
HashSet
<>(
observers
);
}
//iterating on all the streams to communicate with all the clients
//iterating on all the streams to communicate with all the clients
for
(
StreamObserver
<
ChatServiceOuterClass
.
ChatMessage
>
observer:
observers
){
for
(
StreamObserver
<
ChatServiceOuterClass
.
ChatMessage
>
observer:
copy
){
//we exclude the one which is sending the message
//we exclude the one which is sending the message
if
(!
observer
.
equals
(
responseObserver
))
if
(!
observer
.
equals
(
responseObserver
))
...
@@ -45,13 +57,20 @@ public class ChatServiceImpl extends ChatServiceGrpc.ChatServiceImplBase {
...
@@ -45,13 +57,20 @@ public class ChatServiceImpl extends ChatServiceGrpc.ChatServiceImplBase {
//if there is an error (client abruptly disconnect) we remove the client.
//if there is an error (client abruptly disconnect) we remove the client.
public
void
onError
(
Throwable
throwable
)
{
public
void
onError
(
Throwable
throwable
)
{
observers
.
remove
(
responseObserver
);
synchronized
(
observers
)
{
observers
.
remove
(
responseObserver
);
}
}
}
//if the client explicitly terminated, we remove it from the hashset.
//if the client explicitly terminated, we remove it from the hashset.
public
void
onCompleted
()
{
public
void
onCompleted
()
{
observers
.
remove
(
responseObserver
);
synchronized
(
observers
)
{
observers
.
remove
(
responseObserver
);
}
}
}
};
};
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment