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
77d85ac5
Commit
77d85ac5
authored
Apr 04, 2019
by
Gabriele Civitarese
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor import
parent
bcf4ae00
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
27 additions
and
26 deletions
+27
-26
ChatClient.java
src/main/java/chat/ChatClient.java
+6
-5
ChatServiceImpl.java
src/main/java/chat/ChatServiceImpl.java
+8
-8
GreetingServiceClient.java
src/main/java/grpchelloserver/GreetingServiceClient.java
+6
-6
GreetingServiceImpl.java
src/main/java/grpchelloserver/GreetingServiceImpl.java
+7
-7
No files found.
src/main/java/chat/ChatClient.java
View file @
77d85ac5
package
chat
;
import
com.example.chat.ChatServiceGrpc.*
;
import
com.example.chat.ChatServiceGrpc
;
import
com.example.chat.ChatServiceOuterClass
;
import
com.example.chat.ChatServiceOuterClass
.*
;
import
io.grpc.ManagedChannel
;
import
io.grpc.ManagedChannelBuilder
;
import
io.grpc.stub.StreamObserver
;
...
...
@@ -40,17 +41,17 @@ public class ChatClient {
//creating the asynchronous stub
ChatService
Grpc
.
ChatService
Stub
stub
=
ChatServiceGrpc
.
newStub
(
channel
);
ChatServiceStub
stub
=
ChatServiceGrpc
.
newStub
(
channel
);
//the stub returns a stream (to communicate with the server, and thus with all the other clients).
//the argument is the stream of messages which are transmitted by the server.
StreamObserver
<
Chat
ServiceOuterClass
.
ChatMessage
>
serverStream
=
stub
.
chat
(
new
StreamObserver
<
ChatServiceOuterClass
.
ChatMessage
>()
{
StreamObserver
<
Chat
Message
>
serverStream
=
stub
.
chat
(
new
StreamObserver
<
ChatMessage
>()
{
//remember: all the methods here are CALLBACKS which are handled in an asynchronous manner.
//we define what to do when a message from the server arrives (just print the message)
public
void
onNext
(
Chat
ServiceOuterClass
.
Chat
Message
chatMessage
)
{
public
void
onNext
(
ChatMessage
chatMessage
)
{
String
from
=
chatMessage
.
getFrom
();
String
message
=
chatMessage
.
getMessage
();
...
...
@@ -86,7 +87,7 @@ public class ChatClient {
}
//we use the stream to communicate to the server our message.
serverStream
.
onNext
(
Chat
ServiceOuterClass
.
Chat
Message
.
newBuilder
().
setFrom
(
nickname
).
setMessage
(
message
).
build
());
serverStream
.
onNext
(
ChatMessage
.
newBuilder
().
setFrom
(
nickname
).
setMessage
(
message
).
build
());
}
...
...
src/main/java/chat/ChatServiceImpl.java
View file @
77d85ac5
package
chat
;
import
com.example.chat.ChatServiceGrpc
;
import
com.example.chat.ChatServiceOuterClass
;
import
com.example.chat.ChatServiceGrpc
.*
;
import
com.example.chat.ChatServiceOuterClass
.*
;
import
io.grpc.stub.StreamObserver
;
import
java.util.HashSet
;
import
java.util.LinkedHashSet
;
public
class
ChatServiceImpl
extends
ChatService
Grpc
.
ChatService
ImplBase
{
public
class
ChatServiceImpl
extends
ChatServiceImplBase
{
//an hashset to store all the streams which the server uses to communicate with each client
HashSet
<
StreamObserver
>
observers
=
new
LinkedHashSet
<
StreamObserver
>();
@Override
public
StreamObserver
<
Chat
ServiceOuterClass
.
ChatMessage
>
chat
(
final
StreamObserver
<
ChatServiceOuterClass
.
ChatMessage
>
responseObserver
){
@Override
public
StreamObserver
<
Chat
Message
>
chat
(
final
StreamObserver
<
ChatMessage
>
responseObserver
){
//the stream used to communicate with a specific client is stored in a hash set (avoiding duplicates)
synchronized
(
observers
)
{
...
...
@@ -22,10 +22,10 @@ public class ChatServiceImpl extends ChatServiceGrpc.ChatServiceImplBase {
}
//it returns the stream that will be used by the clients to send messages.
//the client will write on this stream
return
new
StreamObserver
<
Chat
ServiceOuterClass
.
Chat
Message
>()
{
return
new
StreamObserver
<
ChatMessage
>()
{
//receiving a message from a specific client
public
void
onNext
(
Chat
ServiceOuterClass
.
Chat
Message
chatMessage
)
{
public
void
onNext
(
ChatMessage
chatMessage
)
{
//unwrapping message
String
message
=
chatMessage
.
getMessage
();
...
...
@@ -43,12 +43,12 @@ public class ChatServiceImpl extends ChatServiceGrpc.ChatServiceImplBase {
}
//iterating on all the streams to communicate with all the clients
for
(
StreamObserver
<
Chat
ServiceOuterClass
.
Chat
Message
>
observer:
copy
){
for
(
StreamObserver
<
ChatMessage
>
observer:
copy
){
//we exclude the one which is sending the message
if
(!
observer
.
equals
(
responseObserver
))
//we simply forward the message
observer
.
onNext
(
Chat
ServiceOuterClass
.
Chat
Message
.
newBuilder
().
setMessage
(
message
).
setFrom
(
from
).
build
());
observer
.
onNext
(
ChatMessage
.
newBuilder
().
setMessage
(
message
).
setFrom
(
from
).
build
());
}
...
...
src/main/java/grpchelloserver/GreetingServiceClient.java
View file @
77d85ac5
package
grpchelloserver
;
import
com.example.grpc.GreetingServiceGrpc
;
import
com.example.grpc.GreetingServiceOuterClass
;
import
com.example.grpc.GreetingServiceOuterClass
.*
;
import
io.grpc.ManagedChannel
;
import
io.grpc.ManagedChannelBuilder
;
import
io.grpc.stub.StreamObserver
;
...
...
@@ -39,10 +39,10 @@ public class GreetingServiceClient {
GreetingServiceGrpc
.
GreetingServiceBlockingStub
stub
=
GreetingServiceGrpc
.
newBlockingStub
(
channel
);
//creating the HelloResponse object which will be provided as input to the RPC method
GreetingServiceOuterClass
.
HelloRequest
request
=
GreetingServiceOuterClass
.
HelloRequest
.
newBuilder
().
setName
(
"Civi
"
).
build
();
HelloRequest
request
=
HelloRequest
.
newBuilder
().
setName
(
"Pippo
"
).
build
();
//calling the method. it returns an instance of HelloResponse
GreetingServiceOuterClass
.
HelloResponse
response
=
stub
.
greeting
(
request
);
HelloResponse
response
=
stub
.
greeting
(
request
);
//printing the answer
System
.
out
.
println
(
response
.
getGreeting
());
...
...
@@ -62,13 +62,13 @@ public class GreetingServiceClient {
GreetingServiceGrpc
.
GreetingServiceStub
stub
=
GreetingServiceGrpc
.
newStub
(
channel
);
//creating the HelloResponse object which will be provided as input to the RPC method
GreetingServiceOuterClass
.
HelloRequest
request
=
GreetingServiceOuterClass
.
HelloRequest
.
newBuilder
().
setName
(
"Civi
"
).
build
();
HelloRequest
request
=
HelloRequest
.
newBuilder
().
setName
(
"Pippo
"
).
build
();
//calling the RPC method. since it is asynchronous, we need to define handlers
stub
.
streamGreeting
(
request
,
new
StreamObserver
<
GreetingServiceOuterClass
.
HelloResponse
>()
{
stub
.
streamGreeting
(
request
,
new
StreamObserver
<
HelloResponse
>()
{
//this hanlder takes care of each item received in the stream
public
void
onNext
(
GreetingServiceOuterClass
.
HelloResponse
helloResponse
)
{
public
void
onNext
(
HelloResponse
helloResponse
)
{
//each item is just printed
System
.
out
.
println
(
helloResponse
.
getGreeting
());
...
...
src/main/java/grpchelloserver/GreetingServiceImpl.java
View file @
77d85ac5
package
grpchelloserver
;
import
com.example.grpc.GreetingServiceGrpc
;
import
com.example.grpc.GreetingServiceOuterClass
;
import
com.example.grpc.GreetingServiceGrpc
.GreetingServiceImplBase
;
import
com.example.grpc.GreetingServiceOuterClass
.*
;
import
io.grpc.stub.StreamObserver
;
public
class
GreetingServiceImpl
extends
GreetingService
Grpc
.
GreetingService
ImplBase
{
public
class
GreetingServiceImpl
extends
GreetingServiceImplBase
{
@Override
public
void
greeting
(
GreetingServiceOuterClass
.
HelloRequest
request
,
StreamObserver
<
GreetingServiceOuterClass
.
HelloResponse
>
responseObserver
){
public
void
greeting
(
HelloRequest
request
,
StreamObserver
<
HelloResponse
>
responseObserver
){
//la richiesta è di tipo HelloRequest (definito in .proto)
System
.
out
.
println
(
request
);
//costruisco la richiesta di tipo HelloResponse (sempre definito in .proto)
GreetingServiceOuterClass
.
HelloResponse
response
=
GreetingServiceOuterClass
.
HelloResponse
.
newBuilder
().
setGreeting
(
"Hello there, "
+
request
.
getName
()).
build
();
HelloResponse
response
=
HelloResponse
.
newBuilder
().
setGreeting
(
"Hello there, "
+
request
.
getName
()).
build
();
//passo la risposta nello stream
responseObserver
.
onNext
(
response
);
...
...
@@ -24,7 +24,7 @@ public class GreetingServiceImpl extends GreetingServiceGrpc.GreetingServiceImpl
}
@Override
public
void
streamGreeting
(
GreetingServiceOuterClass
.
HelloRequest
request
,
StreamObserver
<
GreetingServiceOuterClass
.
HelloResponse
>
responseObserver
){
public
void
streamGreeting
(
HelloRequest
request
,
StreamObserver
<
HelloResponse
>
responseObserver
){
System
.
out
.
println
(
"Metodo stream chiamato!"
);
...
...
@@ -32,7 +32,7 @@ public class GreetingServiceImpl extends GreetingServiceGrpc.GreetingServiceImpl
System
.
out
.
println
(
request
);
//costruisco la richiesta di tipo HelloResponse (sempre definito in .proto)
GreetingServiceOuterClass
.
HelloResponse
response
=
GreetingServiceOuterClass
.
HelloResponse
.
newBuilder
().
setGreeting
(
"Hello there, "
+
request
.
getName
()).
build
();
HelloResponse
response
=
HelloResponse
.
newBuilder
().
setGreeting
(
"Hello there, "
+
request
.
getName
()).
build
();
//passo la risposta nello stream
responseObserver
.
onNext
(
response
);
...
...
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