Skip to content

Commit a437699

Browse files
author
chenll
committed
add hello_kitty.proto
1 parent a2990e4 commit a437699

File tree

4 files changed

+183
-13
lines changed

4 files changed

+183
-13
lines changed

examples.iml

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package io.grpc.examples.hellokitty;
2+
3+
import io.grpc.ManagedChannel;
4+
import io.grpc.ManagedChannelBuilder;
5+
import io.grpc.StatusRuntimeException;
6+
7+
import java.util.concurrent.TimeUnit;
8+
import java.util.logging.Level;
9+
import java.util.logging.Logger;
10+
11+
public class HelloKittyClient {
12+
private static final Logger logger = Logger.getLogger(HelloKittyClient.class.getName());
13+
14+
private final ManagedChannel channel;
15+
private final HelloKittyGrpc.HelloKittyBlockingStub blockingStub;
16+
17+
/**
18+
* Construct client connecting to HelloWorld server at {@code host:port}.
19+
*/
20+
public HelloKittyClient(String host, int port) {
21+
this(ManagedChannelBuilder.forAddress(host, port)
22+
// Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
23+
// needing certificates.
24+
.usePlaintext(true)
25+
.build());
26+
}
27+
28+
/**
29+
* Construct client for accessing RouteGuide server using the existing channel.
30+
*/
31+
HelloKittyClient(ManagedChannel channel) {
32+
this.channel = channel;
33+
blockingStub = HelloKittyGrpc.newBlockingStub(channel);
34+
}
35+
36+
public void shutdown() throws InterruptedException {
37+
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
38+
}
39+
40+
/**
41+
* Say hello to server.
42+
*/
43+
public void greet(String name) {
44+
logger.info("Will try to greet " + name + " ...");
45+
HelloKittyRequest request = HelloKittyRequest.newBuilder().setName(name).build();
46+
HelloKittyReply response;
47+
try {
48+
response = blockingStub.sayHello(request);
49+
} catch (StatusRuntimeException e) {
50+
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
51+
return;
52+
}
53+
logger.info("Greeting: " + response.getMessage());
54+
}
55+
56+
/**
57+
* Greet server. If provided, the first element of {@code args} is the name to use in the
58+
* greeting.
59+
*/
60+
public static void main(String[] args) throws Exception {
61+
HelloKittyClient client = new HelloKittyClient("localhost", 50052);
62+
try {
63+
/* Access a service running on the local machine on port 50051 */
64+
String user = "kitty";
65+
if (args.length > 0) {
66+
user = args[0]; /* Use the arg as the name to greet if provided */
67+
}
68+
client.greet(user);
69+
} finally {
70+
client.shutdown();
71+
}
72+
}
73+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package io.grpc.examples.hellokitty;
2+
3+
import io.grpc.Server;
4+
import io.grpc.ServerBuilder;
5+
import io.grpc.examples.helloworld.HelloWorldServer;
6+
import io.grpc.stub.StreamObserver;
7+
8+
import java.io.IOException;
9+
import java.util.logging.Logger;
10+
11+
public class HelloKittyServer {
12+
13+
14+
private static final Logger logger = Logger.getLogger(HelloWorldServer.class.getName());
15+
16+
private Server server;
17+
18+
private void start() throws IOException {
19+
/* The port on which the server should run */
20+
int port = 50052;
21+
server = ServerBuilder.forPort(port)
22+
.addService(new HelloKittyServerImpl())
23+
.build()
24+
.start();
25+
logger.info("Server started, listening on " + port);
26+
Runtime.getRuntime().addShutdownHook(new Thread() {
27+
@Override
28+
public void run() {
29+
// Use stderr here since the logger may have been reset by its JVM shutdown hook.
30+
System.err.println("*** shutting down gRPC server since JVM is shutting down");
31+
HelloKittyServer.this.stop();
32+
System.err.println("*** server shut down");
33+
}
34+
});
35+
}
36+
37+
private void stop() {
38+
if (server != null) {
39+
server.shutdown();
40+
}
41+
}
42+
43+
/**
44+
* Await termination on the main thread since the grpc library uses daemon threads.
45+
*/
46+
private void blockUntilShutdown() throws InterruptedException {
47+
if (server != null) {
48+
server.awaitTermination();
49+
}
50+
}
51+
52+
/**
53+
* Main launches the server from the command line.
54+
*/
55+
public static void main(String[] args) throws IOException, InterruptedException {
56+
final HelloKittyServer server = new HelloKittyServer();
57+
server.start();
58+
server.blockUntilShutdown();
59+
}
60+
61+
static class HelloKittyServerImpl extends HelloKittyGrpc.HelloKittyImplBase {
62+
63+
@Override
64+
public void sayHello(HelloKittyRequest req, StreamObserver<HelloKittyReply> responseObserver) {
65+
HelloKittyReply reply = HelloKittyReply.newBuilder().setMessage("Hello " + req.getName()).build();
66+
responseObserver.onNext(reply);
67+
responseObserver.onCompleted();
68+
}
69+
}
70+
}

src/main/proto/hello_kitty.proto

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2015, gRPC Authors
2+
// All rights reserved.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
syntax = "proto3";
16+
17+
option java_multiple_files = true;
18+
option java_package = "io.grpc.examples.hellokitty";
19+
option java_outer_classname = "HelloKittyProto";
20+
option objc_class_prefix = "HLK";
21+
22+
package hellokitty;
23+
24+
// The greeting service definition.
25+
service HelloKitty {
26+
// Sends a greeting
27+
rpc SayHello (HelloKittyRequest) returns (HelloKittyReply) {
28+
}
29+
}
30+
31+
// The request message containing the user's name.
32+
message HelloKittyRequest {
33+
string name = 1;
34+
}
35+
36+
// The response message containing the greetings
37+
message HelloKittyReply {
38+
string message = 1;
39+
int32 code = 2;
40+
}

0 commit comments

Comments
 (0)