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
5f94437e
Commit
5f94437e
authored
Apr 04, 2019
by
Gabriele Civitarese
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of ewserver.di.unimi.it:civitarese/grpc_examples
parents
8b056be6
57a450fb
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
34 deletions
+24
-34
pom.xml
pom.xml
+1
-30
ChatServiceImpl.java
src/main/java/chat/ChatServiceImpl.java
+23
-4
No files found.
pom.xml
View file @
5f94437e
...
@@ -33,36 +33,7 @@
...
@@ -33,36 +33,7 @@
<version>
3.8.2
</version>
<version>
3.8.2
</version>
<scope>
test
</scope>
<scope>
test
</scope>
</dependency>
</dependency>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
3.8.2
</version>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
3.8.2
</version>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
3.8.2
</version>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
3.8.2
</version>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
junit
</groupId>
<artifactId>
junit
</artifactId>
<version>
3.8.2
</version>
<scope>
test
</scope>
</dependency>
</dependencies>
</dependencies>
...
...
src/main/java/chat/ChatServiceImpl.java
View file @
5f94437e
...
@@ -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