Add helloworld example. (#59)

This will be used in the quickstart docs, similar to the other
languages.
This commit is contained in:
Jakob Andersen 2018-02-20 14:49:04 +01:00 committed by GitHub
parent 3f60746689
commit 582ca1b60d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 335 additions and 0 deletions

View File

@ -0,0 +1,50 @@
# Description
The hello world server and client demonstrate how to use Dart gRPC libraries to
perform unary RPCs.
See the definition of the hello world service in `protos/helloworld.proto`.
# Run the sample code
To compile and run the example, assuming you are in the root of the helloworld
folder, i.e., .../example/helloworld/, first get the dependencies by running:
```sh
$ pub get
```
Then, to run the server:
```sh
$ dart bin/server.dart
```
Likewise, to run the client:
```sh
$ dart bin/client.dart
```
# Regenerate the stubs
If you have made changes to the message or service definition in
`protos/helloworld.proto` and need to regenerate the corresponding Dart files,
you will need to have protoc version 3.0.0 or higher and the Dart protoc plugin
version 0.7.9 or higher on your PATH.
To install protoc, see the instructions on
[the Protocol Buffers website](https://developers.google.com/protocol-buffers/).
The easiest way to get the Dart protoc plugin is by running
```sh
$ pub global activate protoc_plugin
```
and follow the directions to add `~/.pub-cache/bin` to your PATH, if you haven't
already done so.
You can now regenerate the Dart files by running
```sh
$ protoc --dart_out=grpc:lib/src/generated -Iprotos protos/helloworld.proto
```

View File

@ -0,0 +1,38 @@
// Copyright (c) 2018, the gRPC project authors. Please see the AUTHORS file
// for details. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/// Dart implementation of the gRPC helloworld.Greeter client.
import 'dart:async';
import 'package:grpc/grpc.dart';
import 'package:helloworld/src/generated/helloworld.pb.dart';
import 'package:helloworld/src/generated/helloworld.pbgrpc.dart';
Future<Null> main(List<String> args) async {
final channel = new ClientChannel('localhost',
port: 50051, options: const ChannelOptions.insecure());
final stub = new GreeterClient(channel);
final name = args.isNotEmpty ? args[0] : 'world';
try {
final response = await stub.sayHello(new HelloRequest()..name = name);
print('Greeter client received: ${response.message}');
} catch (e) {
print('Caught error: $e');
}
await channel.shutdown();
}

View File

@ -0,0 +1,35 @@
// Copyright (c) 2018, the gRPC project authors. Please see the AUTHORS file
// for details. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/// Dart implementation of the gRPC helloworld.Greeter server.
import 'dart:async';
import 'package:grpc/grpc.dart';
import 'package:helloworld/src/generated/helloworld.pb.dart';
import 'package:helloworld/src/generated/helloworld.pbgrpc.dart';
class GreeterService extends GreeterServiceBase {
@override
Future<HelloReply> sayHello(ServiceCall call, HelloRequest request) async {
return new HelloReply()..message = 'Hello, ${request.name}!';
}
}
Future<Null> main(List<String> args) async {
final server = new Server.insecure([new GreeterService()], port: 50051);
await server.serve();
print('Server listening...');
}

View File

@ -0,0 +1,84 @@
///
// Generated code. Do not modify.
///
// ignore_for_file: non_constant_identifier_names,library_prefixes
library helloworld_helloworld;
// ignore: UNUSED_SHOWN_NAME
import 'dart:core' show int, bool, double, String, List, override;
import 'package:protobuf/protobuf.dart';
class HelloRequest extends GeneratedMessage {
static final BuilderInfo _i = new BuilderInfo('HelloRequest')
..aOS(1, 'name')
..hasRequiredFields = false;
HelloRequest() : super();
HelloRequest.fromBuffer(List<int> i,
[ExtensionRegistry r = ExtensionRegistry.EMPTY])
: super.fromBuffer(i, r);
HelloRequest.fromJson(String i,
[ExtensionRegistry r = ExtensionRegistry.EMPTY])
: super.fromJson(i, r);
HelloRequest clone() => new HelloRequest()..mergeFromMessage(this);
BuilderInfo get info_ => _i;
static HelloRequest create() => new HelloRequest();
static PbList<HelloRequest> createRepeated() => new PbList<HelloRequest>();
static HelloRequest getDefault() {
if (_defaultInstance == null)
_defaultInstance = new _ReadonlyHelloRequest();
return _defaultInstance;
}
static HelloRequest _defaultInstance;
static void $checkItem(HelloRequest v) {
if (v is! HelloRequest) checkItemFailed(v, 'HelloRequest');
}
String get name => $_getS(0, '');
set name(String v) {
$_setString(0, v);
}
bool hasName() => $_has(0);
void clearName() => clearField(1);
}
class _ReadonlyHelloRequest extends HelloRequest with ReadonlyMessageMixin {}
class HelloReply extends GeneratedMessage {
static final BuilderInfo _i = new BuilderInfo('HelloReply')
..aOS(1, 'message')
..hasRequiredFields = false;
HelloReply() : super();
HelloReply.fromBuffer(List<int> i,
[ExtensionRegistry r = ExtensionRegistry.EMPTY])
: super.fromBuffer(i, r);
HelloReply.fromJson(String i, [ExtensionRegistry r = ExtensionRegistry.EMPTY])
: super.fromJson(i, r);
HelloReply clone() => new HelloReply()..mergeFromMessage(this);
BuilderInfo get info_ => _i;
static HelloReply create() => new HelloReply();
static PbList<HelloReply> createRepeated() => new PbList<HelloReply>();
static HelloReply getDefault() {
if (_defaultInstance == null) _defaultInstance = new _ReadonlyHelloReply();
return _defaultInstance;
}
static HelloReply _defaultInstance;
static void $checkItem(HelloReply v) {
if (v is! HelloReply) checkItemFailed(v, 'HelloReply');
}
String get message => $_getS(0, '');
set message(String v) {
$_setString(0, v);
}
bool hasMessage() => $_has(0);
void clearMessage() => clearField(1);
}
class _ReadonlyHelloReply extends HelloReply with ReadonlyMessageMixin {}

View File

@ -0,0 +1,5 @@
///
// Generated code. Do not modify.
///
// ignore_for_file: non_constant_identifier_names,library_prefixes
library helloworld_helloworld_pbenum;

View File

@ -0,0 +1,50 @@
///
// Generated code. Do not modify.
///
// ignore_for_file: non_constant_identifier_names,library_prefixes
library helloworld_helloworld_pbgrpc;
import 'dart:async';
import 'package:grpc/grpc.dart';
import 'helloworld.pb.dart';
export 'helloworld.pb.dart';
class GreeterClient extends Client {
static final _$sayHello = new ClientMethod<HelloRequest, HelloReply>(
'/helloworld.Greeter/SayHello',
(HelloRequest value) => value.writeToBuffer(),
(List<int> value) => new HelloReply.fromBuffer(value));
GreeterClient(ClientChannel channel, {CallOptions options})
: super(channel, options: options);
ResponseFuture<HelloReply> sayHello(HelloRequest request,
{CallOptions options}) {
final call = $createCall(_$sayHello, new Stream.fromIterable([request]),
options: options);
return new ResponseFuture(call);
}
}
abstract class GreeterServiceBase extends Service {
String get $name => 'helloworld.Greeter';
GreeterServiceBase() {
$addMethod(new ServiceMethod<HelloRequest, HelloReply>(
'SayHello',
sayHello_Pre,
false,
false,
(List<int> value) => new HelloRequest.fromBuffer(value),
(HelloReply value) => value.writeToBuffer()));
}
Future<HelloReply> sayHello_Pre(
ServiceCall call, Future<HelloRequest> request) async {
return sayHello(call, await request);
}
Future<HelloReply> sayHello(ServiceCall call, HelloRequest request);
}

View File

@ -0,0 +1,19 @@
///
// Generated code. Do not modify.
///
// ignore_for_file: non_constant_identifier_names,library_prefixes
library helloworld_helloworld_pbjson;
const HelloRequest$json = const {
'1': 'HelloRequest',
'2': const [
const {'1': 'name', '3': 1, '4': 1, '5': 9, '10': 'name'},
],
};
const HelloReply$json = const {
'1': 'HelloReply',
'2': const [
const {'1': 'message', '3': 1, '4': 1, '5': 9, '10': 'message'},
],
};

View File

@ -0,0 +1,38 @@
// Copyright 2015 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
syntax = "proto3";
option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";
package helloworld;
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}

View File

@ -0,0 +1,16 @@
name: helloworld
description: Dart gRPC sample client and server.
version: 0.0.1
homepage: https://github.com/dart-lang/grpc-dart
environment:
sdk: '>=1.24.3 <2.0.0'
dependencies:
async: ^1.13.3
grpc:
path: ../../
protobuf: ^0.7.0
dev_dependencies:
test: ^0.12.0