mirror of https://github.com/grpc/grpc.git
commit
0d1625a6b4
226 changed files with 19858 additions and 1284 deletions
@ -0,0 +1,192 @@ |
||||
# gRPC over HTTP2 |
||||
|
||||
## Introduction |
||||
This document serves as a detailed description for an implementation of gRPC carried over HTTP2 draft 17 framing. It assumes familiarity with the HTTP2 specification. |
||||
|
||||
## Protocol |
||||
Production rules are using <a href="http://tools.ietf.org/html/rfc5234">ABNF syntax</a>. |
||||
|
||||
### Outline |
||||
|
||||
The following is the general sequence of message atoms in a GRPC request & response message stream |
||||
|
||||
* Request → Request-Headers *Delimited-Message EOS |
||||
* Response → (Response-Headers *Delimited-Message Trailers) / Trailers-Only |
||||
|
||||
|
||||
### Requests |
||||
|
||||
* Request → Request-Headers *Delimited-Message EOS |
||||
|
||||
Request-Headers are delivered as HTTP2 headers in HEADERS + CONTINUATION frames. |
||||
|
||||
* **Request-Headers** → Call-Definition *Custom-Metadata |
||||
* **Call-Definition** → Method Scheme Path TE [Authority] [Timeout] [Content-Type] [Message-Type] [Message-Encoding] [Message-Accept-Encoding] [User-Agent] |
||||
* **Method** → “:method POST” |
||||
* **Scheme** → “:scheme ” (“http” / “https”) |
||||
* **Path** → “:path” {_path identifying method within exposed API_} |
||||
* **Authority** → “:authority” {_virtual host name of authority_} |
||||
* **TE** → “te” “trailers” # Used to detect incompatible proxies |
||||
* **Timeout** → “grpc-timeout” TimeoutValue TimeoutUnit |
||||
* **TimeoutValue** → {_positive integer as ASCII string of at most 8 digits_} |
||||
* **TimeoutUnit** → Hour / Minute / Second / Millisecond / Microsecond / Nanosecond |
||||
* **Hour** → “H” |
||||
* **Minute** → “M” |
||||
* **Second** → “S” |
||||
* **Millisecond** → “m” |
||||
* **Microsecond** → “u” |
||||
* **Nanosecond** → “n” |
||||
* **Content-Type** → “content-type” “application/grpc” [(“+proto” / “+json” / {_custom_})] |
||||
* **Content-Coding** → “gzip” / “deflate” / “snappy” / {_custom_} |
||||
* **Message-Encoding** → “grpc-encoding” Content-Coding |
||||
* **Message-Accept-Encoding** → “grpc-accept-encoding” Content-Coding *("," Content-Coding) |
||||
* **User-Agent** → “user-agent” {_structured user-agent string_} |
||||
* **Message-Type** → “grpc-message-type” {_type name for message schema_} |
||||
* **Custom-Metadata** → Binary-Header / ASCII-Header |
||||
* **Binary-Header** → {lowercase ASCII header name ending in “-bin” } {_base64 encoded value_} |
||||
* **ASCII-Header** → {lowercase ASCII header name} {_value_} |
||||
|
||||
|
||||
HTTP2 requires that reserved headers, ones starting with “:” appear before all other headers. Additionally implementations should send **Timeout** immediately after the reserved headers and they should send the **Call-Definition** headers before sending **Custom-Metadata**. |
||||
|
||||
If **Timeout** is omitted a server should assume an infinite timeout. Client implementations are free to send a default minimum timeout based on their deployment requirements. |
||||
|
||||
**Custom-Metadata** is an arbitrary set of key-value pairs defined by the application layer. Aside from transport limits on the total length of HTTP2 HEADERS the only other constraint is that header names starting with “grpc-” are reserved for future use. |
||||
|
||||
Note that HTTP2 does not allow arbitrary octet sequences for header values so binary header values must be encoded using Base64 as per https://tools.ietf.org/html/rfc4648#section-4. Implementations MUST accept padded and un-padded values and should emit un-padded values. Applications define binary headers by having their names end with “-bin”. Runtime libraries use this suffix to detect binary headers and properly apply base64 encoding & decoding as headers are sent and received. |
||||
|
||||
The repeated sequence of **Delimited-Message** items is delivered in DATA frames |
||||
|
||||
* **Delimited-Message** → Compressed-Flag Message-Length Message |
||||
* **Compressed-Flag** → 0 / 1 # encoded as 1 byte unsigned integer |
||||
* **Message-Length** → {_length of Message_} # encoded as 4 byte unsigned integer |
||||
* **Message** → *{binary octet} |
||||
|
||||
A **Compressed-Flag** value of 1 indicates that the binary octet sequence of **Message** is compressed using the mechanism declared by the **Message-Encoding** header. A value of 0 indicates that no encoding of **Message** bytes has occurred. Compression contexts are NOT maintained over message boundaries, implementations must create a new context for each message in the stream. If the **Message-Encoding** header is omitted then the **Compressed-Flag** must be 0. |
||||
|
||||
For requests, **EOS** (end-of-stream) is indicated by the presence of the END_STREAM flag on the last received DATA frame. In scenarios where the **Request** stream needs to be closed but no data remains to be sent implementations MUST send an empty DATA frame with this flag set. |
||||
|
||||
###Responses |
||||
|
||||
* **Response** → (Response-Headers *Delimited-Message Trailers) / Trailers-Only |
||||
* **Response-Headers** → HTTP-Status [Message-Encoding] [Message-Accept-Encoding] Content-Type *Custom-Metadata |
||||
* **Trailers-Only** → HTTP-Status Content-Type Trailers |
||||
* **Trailers** → Status [Status-Message] *Custom-Metadata |
||||
* **HTTP-Status** → “:status 200” |
||||
* **Status** → “grpc-status” <status-code-as-ASCII-string> |
||||
* **Status-Message** → “grpc-message” <descriptive text for status as ASCII string> |
||||
|
||||
**Response-Headers** & **Trailers-Only** are each delivered in a single HTTP2 HEADERS frame block. Most responses are expected to have both headers and trailers but **Trailers-Only** is permitted for calls that produce an immediate error. Status must be sent in **Trailers** even if the status code is OK. |
||||
|
||||
For responses end-of-stream is indicated by the presence of the END_STREAM flag on the last received HEADERS frame that carries **Trailers**. |
||||
|
||||
Implementations should expect broken deployments to send non-200 HTTP status codes in responses as well as a variety of non-GRPC content-types and to omit **Status** & **Status-Message**. Implementations must synthesize a **Status** & **Status-Message** to propagate to the application layer when this occurs. |
||||
|
||||
####Example |
||||
|
||||
Sample unary-call showing HTTP2 framing sequence |
||||
|
||||
**Request** |
||||
|
||||
``` |
||||
HEADERS (flags = END_HEADERS) |
||||
:method = POST |
||||
:scheme = http |
||||
:path = /google.pubsub.v2.PublisherService/CreateTopic |
||||
:authority = pubsub.googleapis.com |
||||
grpc-timeout = 1S |
||||
content-type = application/grpc+proto |
||||
grpc-encoding = gzip |
||||
authorization = Bearer y235.wef315yfh138vh31hv93hv8h3v |
||||
|
||||
DATA (flags = END_STREAM) |
||||
<Delimited Message> |
||||
``` |
||||
**Response** |
||||
``` |
||||
HEADERS (flags = END_HEADERS) |
||||
:status = 200 |
||||
grpc-encoding = gzip |
||||
|
||||
DATA |
||||
<Delimited Message> |
||||
|
||||
HEADERS (flags = END_STREAM, END_HEADERS) |
||||
grpc-status = 0 # OK |
||||
trace-proto-bin = jher831yy13JHy3hc |
||||
``` |
||||
####User Agents |
||||
|
||||
While the protocol does not require a user-agent to function it is recommended that clients provide a structured user-agent string that provides a basic description of the calling library, version & platform to facilitate issue diagnosis in heterogeneous environments. The following structure is recommended to library developers |
||||
``` |
||||
User-Agent → “grpc-” Language ?(“-” Variant) “/” Version ?( “ (“ *(AdditionalProperty “;”) “)” ) |
||||
``` |
||||
E.g. |
||||
|
||||
``` |
||||
grpc-java/1.2.3 |
||||
grpc-ruby/1.2.3 |
||||
grpc-ruby-jruby/1.3.4 |
||||
grpc-java-android/0.9.1 (gingerbread/1.2.4; nexus5; tmobile) |
||||
``` |
||||
####HTTP2 Transport Mapping |
||||
|
||||
#####Stream Identification |
||||
All GRPC calls need to specify an internal ID. We will use HTTP2 stream-ids as call identifiers in this scheme. NOTE: These id’s are contextual to an open HTTP2 session and will not be unique within a given process that is handling more than one HTTP2 session nor can they be used as GUIDs. |
||||
|
||||
#####Data Frames |
||||
DATA frame boundaries have no relation to **Delimited-Message** boundaries and implementations should make no assumptions about their alignment. |
||||
|
||||
#####Errors |
||||
|
||||
When an application or runtime error occurs during an RPC a **Status** and **Status-Message** are delivered in **Trailers**. |
||||
|
||||
In some cases it is possible that the framing of the message stream has become corrupt and the RPC runtime will choose to use an **RST_STREAM** frame to indicate this state to its peer. RPC runtime implementations should interpret RST_STREAM as immediate full-closure of the stream and should propagate an error up to the calling application layer. |
||||
|
||||
The following mapping from RST_STREAM error codes to GRPC error codes is applied. |
||||
|
||||
HTTP2 Code|GRPC Code |
||||
----------|----------- |
||||
NO_ERROR(0)|INTERNAL - An explicit GRPC status of OK should have been sent but this might be used to aggressively lameduck in some scenarios. |
||||
PROTOCOL_ERROR(1)|INTERNAL |
||||
INTERNAL_ERROR(2)|INTERNAL |
||||
FLOW_CONTROL_ERROR(3)|INTERNAL |
||||
SETTINGS_TIMEOUT(4)|INTERNAL |
||||
STREAM_CLOSED|No mapping as there is no open stream to propagate to. Implementations should log. |
||||
FRAME_SIZE_ERROR|INTERNAL |
||||
REFUSED_STREAM|UNAVAILABLE - Indicates that no processing occurred and the request can be retried, possibly elsewhere. |
||||
CANCEL(8)|Mapped to call cancellation when sent by a client.Mapped to CANCELLED when sent by a server. Note that servers should only use this mechanism when they need to cancel a call but the payload byte sequence is incomplete. |
||||
COMPRESSION_ERROR|INTERNAL |
||||
CONNECT_ERROR|INTERNAL |
||||
ENHANCE_YOUR_CALM|RESOURCE_EXHAUSTED ...with additional error detail provided by runtime to indicate that the exhausted resource is bandwidth. |
||||
INADEQUATE_SECURITY| PERMISSION_DENIED … with additional detail indicating that permission was denied as protocol is not secure enough for call. |
||||
|
||||
|
||||
#####Security |
||||
|
||||
The HTTP2 specification mandates the use of TLS 1.2 or higher when TLS is used with HTTP2. It also places some additional constraints on the allowed ciphers in deployments to avoid known-problems as well as requiring SNI support. It is also expected that HTTP2 will be used in conjunction with proprietary transport security mechanisms about which the specification can make no meaningful recommendations. |
||||
|
||||
#####Connection Management |
||||
######GOAWAY Frame |
||||
Sent by servers to clients to indicate that they will no longer accept any new streams on the associated connections. This frame includes the id of the last successfully accepted stream by the server. Clients should consider any stream initiated after the last successfully accepted stream as UNAVAILABLE and retry the call elsewhere. Clients are free to continue working with the already accepted streams until they complete or the connection is terminated. |
||||
|
||||
Servers should send GOAWAY before terminating a connection to reliably inform clients which work has been accepted by the server and is being executed. |
||||
|
||||
######PING Frame |
||||
Both clients and servers can send a PING frame that the peer must respond to by precisely echoing what they received. This is used to assert that the connection is still live as well as providing a means to estimate end-to-end latency. If a server initiated PING does not receive a response within the deadline expected by the runtime all outstanding calls on the server will be closed with a CANCELLED status. An expired client initiated PING will cause all calls to be closed with an UNAVAILABLE status. Note that the frequency of PINGs is highly dependent on the network environment, implementations are free to adjust PING frequency based on network and application requirements. |
||||
|
||||
######Connection failure |
||||
If a detectable connection failure occurs on the client all calls will be closed with an UNAVAILABLE status. For servers open calls will be closed with a CANCELLED status. |
||||
|
||||
|
||||
### Appendix A - GRPC for Protobuf |
||||
|
||||
The service interfaces declared by protobuf are easily mapped onto GRPC by code generation extensions to protoc. The following defines the mapping to be used |
||||
|
||||
|
||||
* **Path** → / Service-Name / {_method name_} |
||||
* **Service-Name** → ?( {_proto package name_} "." ) {_service name_} |
||||
* **Message-Type** → {_fully qualified proto message name_} |
||||
* **Content-Type** → "application/grpc+proto" |
||||
|
||||
|
@ -0,0 +1,289 @@ |
||||
#gRPC Authentication support |
||||
|
||||
gRPC is designed to plug-in a number of authentication mechanisms. This document |
||||
provides a quick overview of the various auth mechanisms supported, discusses |
||||
the API with some examples, and concludes with a discussion of extensibility. |
||||
More documentation and examples are coming soon! |
||||
|
||||
## Supported auth mechanisms |
||||
|
||||
###SSL/TLS |
||||
gRPC has SSL/TLS integration and promotes the use of SSL/TLS to authenticate the |
||||
server, and encrypt all the data exchanged between the client and the server. |
||||
Optional mechanisms are available for clients to provide certificates to |
||||
accomplish mutual authentication. |
||||
|
||||
###OAuth 2.0 |
||||
gRPC provides a generic mechanism (described below) to attach metadata to |
||||
requests and responses. This mechanism can be used to attach OAuth 2.0 Access |
||||
Tokens to RPCs being made at a client. Additional support for acquiring Access |
||||
Tokens while accessing Google APIs through gRPC is provided for certain auth |
||||
flows, demonstrated through code examples below. |
||||
|
||||
## API |
||||
To reduce complexity and minimize API clutter, gRPC works with a unified concept |
||||
of a Credentials object. Users construct gRPC credentials using corresponding |
||||
bootstrap credentials (e.g., SSL client certs or Service Account Keys), and use |
||||
the credentials while creating a gRPC channel to any server. Depending on the |
||||
type of credential supplied, the channel uses the credentials during the initial |
||||
SSL/TLS handshake with the server, or uses the credential to generate and |
||||
attach Access Tokens to each request being made on the channel. |
||||
|
||||
###SSL/TLS for server authentication and encryption |
||||
This is the simplest authentication scenario, where a client just wants to |
||||
authenticate the server and encrypt all data. |
||||
|
||||
```cpp |
||||
SslCredentialsOptions ssl_opts; // Options to override SSL params, empty by default |
||||
// Create the credentials object by providing service account key in constructor |
||||
std::unique_ptr<Credentials> creds = CredentialsFactory::SslCredentials(ssl_opts); |
||||
// Create a channel using the credentials created in the previous step |
||||
std::shared_ptr<ChannelInterface> channel = CreateChannel(server_name, creds, channel_args); |
||||
// Create a stub on the channel |
||||
std::unique_ptr<Greeter::Stub> stub(Greeter::NewStub(channel)); |
||||
// Make actual RPC calls on the stub. |
||||
grpc::Status s = stub->sayHello(&context, *request, response); |
||||
``` |
||||
|
||||
For advanced use cases such as modifying the root CA or using client certs, |
||||
the corresponding options can be set in the SslCredentialsOptions parameter |
||||
passed to the factory method. |
||||
|
||||
|
||||
###Authenticating with Google |
||||
|
||||
gRPC applications can use a simple API to create a credential that works in various deployment scenarios. |
||||
|
||||
```cpp |
||||
std::unique_ptr<Credentials> creds = CredentialsFactory::GoogleDefaultCredentials(); |
||||
// Create a channel, stub and make RPC calls (same as in the previous example) |
||||
std::shared_ptr<ChannelInterface> channel = CreateChannel(server_name, creds, channel_args); |
||||
std::unique_ptr<Greeter::Stub> stub(Greeter::NewStub(channel)); |
||||
grpc::Status s = stub->sayHello(&context, *request, response); |
||||
``` |
||||
|
||||
This credential works for applications using Service Accounts as well as for |
||||
applications running in [Google Compute Engine (GCE)](https://cloud.google.com/compute/). In the former case, the |
||||
service account’s private keys are loaded from the file named in the environment |
||||
variable `GOOGLE_APPLICATION_CREDENTIALS`. The |
||||
keys are used to generate bearer tokens that are attached to each outgoing RPC |
||||
on the corresponding channel. |
||||
|
||||
For applications running in GCE, a default service account and corresponding |
||||
OAuth scopes can be configured during VM setup. At run-time, this credential |
||||
handles communication with the authentication systems to obtain OAuth2 access |
||||
tokens and attaches them to each outgoing RPC on the corresponding channel. |
||||
Extending gRPC to support other authentication mechanisms |
||||
The gRPC protocol is designed with a general mechanism for sending metadata |
||||
associated with RPC. Clients can send metadata at the beginning of an RPC and |
||||
servers can send back metadata at the beginning and end of the RPC. This |
||||
provides a natural mechanism to support OAuth2 and other authentication |
||||
mechanisms that need attach bearer tokens to individual request. |
||||
|
||||
In the simplest case, there is a single line of code required on the client |
||||
to add a specific token as metadata to an RPC and a corresponding access on |
||||
the server to retrieve this piece of metadata. The generation of the token |
||||
on the client side and its verification at the server can be done separately. |
||||
|
||||
A deeper integration can be achieved by plugging in a gRPC credentials implementation for any custom authentication mechanism that needs to attach per-request tokens. gRPC internals also allow switching out SSL/TLS with other encryption mechanisms. |
||||
|
||||
## Examples |
||||
|
||||
These authentication mechanisms will be available in all gRPC's supported languages. |
||||
The following sections demonstrate how authentication and authorization features described above appear in each language: more languages are coming soon. |
||||
|
||||
###SSL/TLS for server authentication and encryption (Ruby) |
||||
```ruby |
||||
# Base case - No encryption |
||||
stub = Helloworld::Greeter::Stub.new('localhost:50051') |
||||
... |
||||
|
||||
# With server authentication SSL/TLS |
||||
creds = GRPC::Core::Credentials.new(load_certs) # load_certs typically loads a CA roots file |
||||
stub = Helloworld::Greeter::Stub.new('localhost:50051', creds: creds) |
||||
``` |
||||
|
||||
###SSL/TLS for server authentication and encryption (C#) |
||||
```csharp |
||||
// Base case - No encryption |
||||
var channel = new Channel("localhost:50051"); |
||||
var client = new Greeter.GreeterClient(channel); |
||||
... |
||||
|
||||
// With server authentication SSL/TLS |
||||
var credentials = new SslCredentials(File.ReadAllText("ca.pem")); // Load a CA file |
||||
var channel = new Channel("localhost:50051", credentials); |
||||
var client = new Greeter.GreeterClient(channel); |
||||
``` |
||||
|
||||
###SSL/TLS for server authentication and encryption (Objective-C) |
||||
|
||||
The default for Objective-C is to use SSL/TLS, as that's the most common use case when accessing |
||||
remote APIs. |
||||
|
||||
```objective-c |
||||
// Base case - With server authentication SSL/TLS |
||||
HLWGreeter *client = [[HLWGreeter alloc] initWithHost:@"localhost:50051"]; |
||||
// Same as using @"https://localhost:50051". |
||||
... |
||||
|
||||
// No encryption |
||||
HLWGreeter *client = [[HLWGreeter alloc] initWithHost:@"http://localhost:50051"]; |
||||
// Specifying the HTTP scheme explicitly forces no encryption. |
||||
``` |
||||
|
||||
###SSL/TLS for server authentication and encryption (Python) |
||||
```python |
||||
# Base case - No encryption |
||||
stub = early_adopter_create_GreeterService_stub('localhost', 50051) |
||||
... |
||||
|
||||
# With server authentication SSL/TLS |
||||
stub = early_adopter_create_GreeterService_stub( |
||||
'localhost', 50051, secure=True, root_certificates=open('ca.pem').read()) |
||||
... |
||||
``` |
||||
n.b.: the beta API will look different |
||||
|
||||
###Authenticating with Google (Ruby) |
||||
```ruby |
||||
# Base case - No encryption/authorization |
||||
stub = Helloworld::Greeter::Stub.new('localhost:50051') |
||||
... |
||||
|
||||
# Authenticating with Google |
||||
require 'googleauth' # from http://www.rubydoc.info/gems/googleauth/0.1.0 |
||||
... |
||||
creds = GRPC::Core::Credentials.new(load_certs) # load_certs typically loads a CA roots file |
||||
scope = 'https://www.googleapis.com/auth/grpc-testing' |
||||
authorization = Google::Auth.get_application_default(scope) |
||||
stub = Helloworld::Greeter::Stub.new('localhost:50051', |
||||
creds: creds, |
||||
update_metadata: authorization.updater_proc) |
||||
``` |
||||
|
||||
###Authenticating with Google (Node.js) |
||||
|
||||
```node |
||||
// Base case - No encryption/authorization |
||||
var stub = new helloworld.Greeter('localhost:50051'); |
||||
... |
||||
// Authenticating with Google |
||||
var GoogleAuth = require('google-auth-library'); // from https://www.npmjs.com/package/google-auth-library |
||||
... |
||||
var creds = grpc.Credentials.createSsl(load_certs); // load_certs typically loads a CA roots file |
||||
var scope = 'https://www.googleapis.com/auth/grpc-testing'; |
||||
(new GoogleAuth()).getApplicationDefault(function(err, auth) { |
||||
if (auth.createScopeRequired()) { |
||||
auth = auth.createScoped(scope); |
||||
} |
||||
var stub = new helloworld.Greeter('localhost:50051', |
||||
{credentials: creds}, |
||||
grpc.getGoogleAuthDelegate(auth)); |
||||
}); |
||||
``` |
||||
|
||||
###Authenticating with Google (C#) |
||||
```csharp |
||||
// Base case - No encryption/authorization |
||||
var channel = new Channel("localhost:50051"); |
||||
var client = new Greeter.GreeterClient(channel); |
||||
... |
||||
|
||||
// Authenticating with Google |
||||
using Grpc.Auth; // from Grpc.Auth NuGet package |
||||
... |
||||
var credentials = new SslCredentials(File.ReadAllText("ca.pem")); // Load a CA file |
||||
var channel = new Channel("localhost:50051", credentials); |
||||
|
||||
string scope = "https://www.googleapis.com/auth/grpc-testing"; |
||||
var authorization = GoogleCredential.GetApplicationDefault(); |
||||
if (authorization.IsCreateScopedRequired) |
||||
{ |
||||
authorization = credential.CreateScoped(new[] { scope }); |
||||
} |
||||
var client = new Greeter.GreeterClient(channel, |
||||
new StubConfiguration(OAuth2InterceptorFactory.Create(credential))); |
||||
``` |
||||
|
||||
###Authenticating with Google (PHP) |
||||
```php |
||||
// Base case - No encryption/authorization |
||||
$client = new helloworld\GreeterClient( |
||||
new Grpc\BaseStub('localhost:50051', [])); |
||||
... |
||||
|
||||
// Authenticating with Google |
||||
// the environment variable "GOOGLE_APPLICATION_CREDENTIALS" needs to be set |
||||
$scope = "https://www.googleapis.com/auth/grpc-testing"; |
||||
$auth = Google\Auth\ApplicationDefaultCredentials::getCredentials($scope); |
||||
$opts = [ |
||||
'credentials' => Grpc\Credentials::createSsl(file_get_contents('ca.pem')); |
||||
'update_metadata' => $auth->getUpdateMetadataFunc(), |
||||
]; |
||||
|
||||
$client = new helloworld\GreeterClient( |
||||
new Grpc\BaseStub('localhost:50051', $opts)); |
||||
|
||||
``` |
||||
|
||||
###Authenticating with Google (Objective-C) |
||||
|
||||
This example uses the [Google iOS Sign-In library](https://developers.google.com/identity/sign-in/ios/), |
||||
but it's easily extrapolated to any other OAuth2 library. |
||||
|
||||
```objective-c |
||||
// Base case - No authentication |
||||
[client sayHelloWithRequest:request handler:^(HLWHelloReply *response, NSError *error) { |
||||
... |
||||
}]; |
||||
|
||||
... |
||||
|
||||
// Authenticating with Google |
||||
|
||||
// When signing the user in, ask her for the relevant scopes. |
||||
GIDSignIn.sharedInstance.scopes = @[@"https://www.googleapis.com/auth/grpc-testing"]; |
||||
|
||||
... |
||||
|
||||
#import <ProtoRPC/ProtoRPC.h> |
||||
|
||||
// Create a not-yet-started RPC. We want to set the request headers on this object before starting |
||||
// it. |
||||
ProtoRPC *call = |
||||
[client RPCToSayHelloWithRequest:request handler:^(HLWHelloReply *response, NSError *error) { |
||||
... |
||||
}]; |
||||
|
||||
// Set the access token to be used. |
||||
NSString *accessToken = GIDSignIn.sharedInstance.currentUser.authentication.accessToken; |
||||
call.requestMetadata[@"Authorization"] = [@"Bearer " stringByAppendingString:accessToken]}]; |
||||
|
||||
// Start the RPC. |
||||
[call start]; |
||||
``` |
||||
|
||||
You can see a working example app, with a more detailed explanation, [here](examples/objective-c/auth_sample). |
||||
|
||||
### Authenticating with Google (Python) |
||||
```python |
||||
# Base case - No encryption |
||||
stub = early_adopter_create_GreeterService_stub('localhost', 50051) |
||||
... |
||||
|
||||
# With server authentication SSL/TLS |
||||
import oauth2client.client |
||||
credentials = oauth2client.GoogleCredentials.get_application_default() |
||||
scope = 'https://www.googleapis.com/auth/grpc-testing' |
||||
scoped_credentials = credentials.create_scoped([scope]) |
||||
access_token = scoped_credentials.get_access_token().access_token |
||||
metadata_transformer = ( |
||||
lambda x: [('Authorization', 'Bearer {}'.format(access_token))]) |
||||
|
||||
stub = early_adopter_create_GreeterService_stub( |
||||
'localhost', 50051, secure=True, root_certificates=open('ca.pem').read(), |
||||
metadata_transformer=metadata_transformer) |
||||
... |
||||
``` |
||||
n.b.: the beta API will look different |
@ -0,0 +1,450 @@ |
||||
|
||||
# Getting started |
||||
|
||||
Welcome to the developer documentation for gRPC, a language-neutral, |
||||
platform-neutral remote procedure call (RPC) system developed at Google. |
||||
|
||||
This document introduces you to gRPC with a quick overview and a simple |
||||
Hello World example. You'll find more tutorials and reference docs in this repository - more documentation is coming soon! |
||||
|
||||
<a name="quickstart"></a> |
||||
## Quick start |
||||
You can find quick start guides for each language, including installation instructions, examples, and tutorials here: |
||||
* [C++](examples/cpp) |
||||
* [Java](https://github.com/grpc/grpc-java/tree/master/examples) |
||||
* [Go](https://github.com/grpc/grpc-go/tree/master/examples) |
||||
* [Ruby](examples/ruby) |
||||
* [Node.js](examples/node) |
||||
* [Android Java](examples/java/android) |
||||
* [Python](examples/python/helloworld) |
||||
* [C#](examples/csharp) |
||||
* [Objective-C](examples/objective-c/route_guide) |
||||
* [PHP](examples/php) |
||||
|
||||
## What's in this repository? |
||||
|
||||
The `examples` directory contains documentation, resources, and examples |
||||
for all gRPC users. You can find examples and instructions specific to your |
||||
favourite language in the relevant subdirectory. |
||||
|
||||
You can find out about the gRPC source code repositories in |
||||
[`grpc`](https://github.com/grpc/grpc). Each repository provides instructions |
||||
for building the appropriate libraries for your language. |
||||
|
||||
|
||||
## What is gRPC? |
||||
|
||||
In gRPC a *client* application can directly call |
||||
methods on a *server* application on a different machine as if it was a |
||||
local object, making it easier for you to create distributed applications and |
||||
services. As in many RPC systems, gRPC is based around the idea of defining |
||||
a *service*, specifying the methods that can be called remotely with their |
||||
parameters and return types. On the server side, the server implements this |
||||
interface and runs a gRPC server to handle client calls. On the client side, |
||||
the client has a *stub* that provides exactly the same methods as the server. |
||||
|
||||
<!--TODO: diagram--> |
||||
|
||||
gRPC clients and servers can run and talk to each other in a variety of |
||||
environments - from servers inside Google to your own desktop - and can |
||||
be written in any of gRPC's [supported languages](#quickstart). So, for |
||||
example, you can easily create a gRPC server in Java with clients in Go, |
||||
Python, or Ruby. In addition, the latest Google APIs will have gRPC versions |
||||
of their interfaces, letting you easily build Google functionality into |
||||
your applications. |
||||
|
||||
<a name="protocolbuffers"></a> |
||||
### Working with protocol buffers |
||||
|
||||
By default gRPC uses *protocol buffers*, Google’s |
||||
mature open source mechanism for serializing structured data (although it |
||||
can be used with other data formats such as JSON). As you'll |
||||
see in our example below, you define gRPC services using *proto files*, |
||||
with method parameters and return types specified as protocol buffer message |
||||
types. You |
||||
can find out lots more about protocol buffers in the [Protocol Buffers |
||||
documentation](https://developers.google.com/protocol-buffers/docs/overview). |
||||
|
||||
#### Protocol buffer versions |
||||
|
||||
While protocol buffers have been available for open source users for some |
||||
time, our examples use a new flavour of protocol buffers called proto3, |
||||
which has a slightly simplified syntax, some useful new features, and supports |
||||
lots more languages. This is currently available as an alpha release in |
||||
Java, C++, Java_nano (Android Java), Python, and Ruby from [the protocol buffers Github |
||||
repo](https://github.com/google/protobuf/releases), as well as a Go language |
||||
generator from [the golang/protobuf Github repo](https://github.com/golang/protobuf), with more languages in development. You can find out more in the [proto3 language guide](https://developers.google.com/protocol-buffers/docs/proto3), and see |
||||
the major differences from the current default version in the [release notes](https://github.com/google/protobuf/releases). More proto3 documentation is coming soon. |
||||
|
||||
In general, while you *can* use proto2 (the current default protocol buffers version), we recommend that you use proto3 with gRPC as it lets you use the full range of gRPC-supported languages, as well as avoiding compatibility |
||||
issues with proto2 clients talking to proto3 servers and vice versa. |
||||
|
||||
<a name="hello"></a> |
||||
## Hello gRPC! |
||||
|
||||
Now that you know a bit more about gRPC, the easiest way to see how it |
||||
works is to look at a simple example. Our Hello World walks you through the |
||||
construction of a simple gRPC client-server application, showing you how to: |
||||
|
||||
- Create a protocol buffers schema that defines a simple RPC service with |
||||
a single |
||||
Hello World method. |
||||
- Create a Java server that implements this interface. |
||||
- Create a Java client that accesses the Java server. |
||||
- Create a Go client that accesses |
||||
the same Java server. |
||||
|
||||
The complete code for the example is available in the `examples` |
||||
directory. We use the Git versioning system for source code management: |
||||
however, you don't need to know anything about Git to follow along other |
||||
than how to install and run a few git commands. |
||||
|
||||
This is an introductory example rather than a comprehensive tutorial, so |
||||
don't worry if you're not a Go or |
||||
Java developer - the concepts are similar for all languages, and you can |
||||
find more implementations of our Hello World example in other languages (and full tutorials where available) in |
||||
the [language-specific folders](#quickstart) in this repository. Complete tutorials and |
||||
reference documentation for all gRPC languages are coming soon. |
||||
|
||||
<a name="setup"></a> |
||||
### Setup |
||||
|
||||
This section explains how to set up your local machine to work with |
||||
the example code. If you just want to read the example, you can go straight |
||||
to the [next step](#servicedef). |
||||
|
||||
#### Install Git |
||||
|
||||
You can download and install Git from http://git-scm.com/download. Once |
||||
installed you should have access to the git command line tool. The main |
||||
commands that you will need to use are: |
||||
|
||||
- git clone ... : clone a remote repository onto your local machine |
||||
- git checkout ... : check out a particular branch or a tagged version of |
||||
the code to hack on |
||||
|
||||
#### Install gRPC |
||||
|
||||
To build and install gRPC plugins and related tools: |
||||
- For Java, see the [Java quick start](https://github.com/grpc/grpc-java). |
||||
- For Go, see the [Go quick start](https://github.com/grpc/grpc-go). |
||||
|
||||
#### Get the source code |
||||
|
||||
The example code for our Java example lives in the `grpc-java` |
||||
GitHub repository. Clone this repository to your local machine by running the |
||||
following command: |
||||
|
||||
|
||||
``` |
||||
git clone https://github.com/grpc/grpc-java.git |
||||
``` |
||||
|
||||
Change your current directory to grpc-java/examples |
||||
|
||||
``` |
||||
cd grpc-java/examples |
||||
``` |
||||
|
||||
|
||||
|
||||
<a name="servicedef"></a> |
||||
### Defining a service |
||||
|
||||
The first step in creating our example is to define a *service*: an RPC |
||||
service specifies the methods that can be called remotely with their parameters |
||||
and return types. As you saw in the |
||||
[overview](#protocolbuffers) above, gRPC does this using [protocol |
||||
buffers](https://developers.google.com/protocol-buffers/docs/overview). We |
||||
use the protocol buffers interface definition language (IDL) to define our |
||||
service methods, and define the parameters and return |
||||
types as protocol buffer message types. Both the client and the |
||||
server use interface code generated from the service definition. |
||||
|
||||
Here's our example service definition, defined using protocol buffers IDL in |
||||
[helloworld.proto](https://github.com/grpc/grpc-java/tree/master/examples/src/main/proto). The `Greeter` |
||||
service has one method, `SayHello`, that lets the server receive a single |
||||
`HelloRequest` |
||||
message from the remote client containing the user's name, then send back |
||||
a greeting in a single `HelloReply`. This is the simplest type of RPC you |
||||
can specify in gRPC - you can find out about other types in the tutorial for your chosen language. |
||||
|
||||
```proto |
||||
syntax = "proto3"; |
||||
|
||||
option java_package = "io.grpc.examples"; |
||||
|
||||
package helloworld; |
||||
|
||||
// The greeter 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; |
||||
} |
||||
|
||||
``` |
||||
|
||||
<a name="generating"></a> |
||||
### Generating gRPC code |
||||
|
||||
Once we've defined our service, we use the protocol buffer compiler |
||||
`protoc` to generate the special client and server code we need to create |
||||
our application - right now we're going to generate Java code, though you |
||||
can generate gRPC code in any gRPC-supported language (as you'll see later |
||||
in this example). The generated code contains both stub code for clients to |
||||
use and an abstract interface for servers to implement, both with the method |
||||
defined in our `Greeter` service. |
||||
|
||||
(If you didn't install the gRPC plugins and protoc on your system and are just reading along with |
||||
the example, you can skip this step and move |
||||
onto the next one where we examine the generated code.) |
||||
|
||||
For simplicity, we've provided a [Gradle build file](https://github.com/grpc/grpc-java/blob/master/examples/build.gradle) with our Java examples that runs `protoc` for you with the appropriate plugin, input, and output: |
||||
|
||||
```shell |
||||
../gradlew build |
||||
``` |
||||
|
||||
This generates the following classes from our .proto, which contain all the generated code |
||||
we need to create our example: |
||||
|
||||
- `Helloworld.java`, which |
||||
has all the protocol buffer code to populate, serialize, and retrieve our |
||||
`HelloRequest` and `HelloReply` message types |
||||
- `GreeterGrpc.java`, which contains (along with some other useful code): |
||||
- an interface for `Greeter` servers to implement |
||||
|
||||
```java |
||||
public static interface Greeter { |
||||
public void sayHello(io.grpc.examples.Helloworld.HelloRequest request, |
||||
io.grpc.stub.StreamObserver<io.grpc.examples.Helloworld.HelloReply> responseObserver); |
||||
} |
||||
``` |
||||
|
||||
- _stub_ classes that clients can use to talk to a `Greeter` server. As you can see, they also implement the `Greeter` interface. |
||||
|
||||
```java |
||||
public static class GreeterStub extends |
||||
io.grpc.stub.AbstractStub<GreeterStub, GreeterServiceDescriptor> |
||||
implements Greeter { |
||||
... |
||||
} |
||||
``` |
||||
|
||||
<a name="server"></a> |
||||
### Writing a server |
||||
|
||||
Now let's write some code! First we'll create a server application to implement |
||||
our service. Note that we're not going to go into a lot of detail about how |
||||
to create a server in this section. More detailed information will be in the |
||||
tutorial for your chosen language: check if there's one available yet in the relevant [quick start](#quickstart). |
||||
|
||||
Our server application has two classes: |
||||
|
||||
- a main server class that hosts the service implementation and allows access over the |
||||
network: [HelloWorldServer.java](https://github.com/grpc/grpc-java/blob/master/examples/src/main/java/io/grpc/examples/helloworld/HelloWorldServer.java). |
||||
|
||||
|
||||
- a simple service implementation class [GreeterImpl.java](https://github.com/grpc/grpc-java/blob/master/examples/src/main/java/io/grpc/examples/helloworld/HelloWorldServer.java#L51). |
||||
|
||||
|
||||
#### Service implementation |
||||
|
||||
[GreeterImpl.java](https://github.com/grpc/grpc-java/blob/master/examples/src/main/java/io/grpc/examples/helloworld/HelloWorldServer.java#L51) |
||||
actually implements our `Greeter` service's required behaviour. |
||||
|
||||
As you can see, the class `GreeterImpl` implements the interface |
||||
`GreeterGrpc.Greeter` that we [generated](#generating) from our proto |
||||
[IDL](https://github.com/grpc/grpc-java/tree/master/examples/src/main/proto) by implementing the method `sayHello`: |
||||
|
||||
```java |
||||
@Override |
||||
public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) { |
||||
HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + req.getName()).build(); |
||||
responseObserver.onValue(reply); |
||||
responseObserver.onCompleted(); |
||||
} |
||||
``` |
||||
- `sayHello` takes two parameters: |
||||
- `HelloRequest`: the request |
||||
- `StreamObserver<HelloReply>`: a response observer, which is |
||||
a special interface for the server to call with its response |
||||
|
||||
To return our response to the client and complete the call: |
||||
|
||||
1. We construct and populate a `HelloReply` response object with our exciting |
||||
message, as specified in our interface definition. |
||||
2. We return the `HelloReply` to the client and then specify that we've finished dealing with the RPC. |
||||
|
||||
|
||||
#### Server implementation |
||||
|
||||
[HelloWorldServer.java](https://github.com/grpc/grpc-java/blob/master/examples/src/main/java/io/grpc/examples/helloworld/HelloWorldServer.java) |
||||
shows the other main feature required to provide a gRPC service; making the service |
||||
implementation available from the network. |
||||
|
||||
```java |
||||
/* The port on which the server should run */ |
||||
private int port = 50051; |
||||
private ServerImpl server; |
||||
|
||||
private void start() throws Exception { |
||||
server = NettyServerBuilder.forPort(port) |
||||
.addService(GreeterGrpc.bindService(new GreeterImpl())) |
||||
.build().start(); |
||||
logger.info("Server started, listening on " + port); |
||||
Runtime.getRuntime().addShutdownHook(new Thread() { |
||||
@Override |
||||
public void run() { |
||||
// Use stderr here since the logger may have been reset by its JVM shutdown hook. |
||||
System.err.println("*** shutting down gRPC server since JVM is shutting down"); |
||||
HelloWorldServer.this.stop(); |
||||
System.err.println("*** server shut down"); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
``` |
||||
|
||||
Here we create an appropriate gRPC server, binding the `Greeter` service |
||||
implementation that we created to a port. Then we start the server running: the server is now ready to receive |
||||
requests from `Greeter` service clients on our specified port. We'll cover |
||||
how all this works in a bit more detail in our language-specific documentation. |
||||
|
||||
<a name="client"></a> |
||||
### Writing a client |
||||
|
||||
Client-side gRPC is pretty simple. In this step, we'll use the generated code |
||||
to write a simple client that can access the `Greeter` server we created |
||||
in the [previous section](#server). You can see the complete client code in |
||||
[HelloWorldClient.java](https://github.com/grpc/grpc-java/blob/master/examples/src/main/java/io/grpc/examples/helloworld/HelloWorldClient.java). |
||||
|
||||
Again, we're not going to go into much detail about how to implement a client; |
||||
we'll leave that for the tutorial. |
||||
|
||||
#### Connecting to the service |
||||
|
||||
First let's look at how we connect to the `Greeter` server. First we need |
||||
to create a gRPC channel, specifying the hostname and port of the server we |
||||
want to connect to. Then we use the channel to construct the stub instance. |
||||
|
||||
|
||||
```java |
||||
private final ChannelImpl channel; |
||||
private final GreeterGrpc.GreeterBlockingStub blockingStub; |
||||
|
||||
public HelloWorldClient(String host, int port) { |
||||
channel = |
||||
NettyChannelBuilder.forAddress(host, port).negotiationType(NegotiationType.PLAINTEXT) |
||||
.build(); |
||||
blockingStub = GreeterGrpc.newBlockingStub(channel); |
||||
} |
||||
|
||||
``` |
||||
|
||||
In this case, we create a blocking stub. This means that the RPC call waits |
||||
for the server to respond, and will either return a response or raise an |
||||
exception. gRPC Java has other kinds of stubs that make non-blocking calls |
||||
to the server, where the response is returned asynchronously. |
||||
|
||||
#### Calling an RPC |
||||
|
||||
Now we can contact the service and obtain a greeting: |
||||
|
||||
1. We construct and fill in a `HelloRequest` to send to the service. |
||||
2. We call the stub's `hello()` RPC with our request and get a `HelloReply` |
||||
back, from which we can get our greeting. |
||||
|
||||
|
||||
```java |
||||
HelloRequest req = HelloRequest.newBuilder().setName(name).build(); |
||||
HelloReply reply = blockingStub.sayHello(req); |
||||
|
||||
``` |
||||
|
||||
<a name="run"></a> |
||||
### Try it out! |
||||
|
||||
Our [Gradle build file](https://github.com/grpc/grpc-java/blob/master/examples/build.gradle) simplifies building and running the examples. |
||||
|
||||
You can build and run the server from the `grpc-java` root folder with: |
||||
|
||||
```sh |
||||
$ ./gradlew :grpc-examples:helloWorldServer |
||||
``` |
||||
|
||||
and in another terminal window confirm that it receives a message. |
||||
|
||||
```sh |
||||
$ ./gradlew :grpc-examples:helloWorldClient |
||||
``` |
||||
|
||||
### Adding another client |
||||
|
||||
Finally, let's look at one of gRPC's most useful features - interoperability |
||||
between code in different languages. So far, we've just looked at Java code |
||||
generated from and implementing our `Greeter` service definition. However, |
||||
as you'll see if you look at the language-specific subdirectories |
||||
in this repository, we've also generated and implemented `Greeter` |
||||
in some of gRPC's other supported languages. Each service |
||||
and client uses interface code generated from the same proto |
||||
that we used for the Java example. |
||||
|
||||
So, for example, if we visit the [`go` example |
||||
directory](https://github.com/grpc/grpc-go/tree/master/examples) and look at the |
||||
[`greeter_client`](https://github.com/grpc/grpc-go/blob/master/examples/greeter_client/main.go), |
||||
we can see that like the Java client, it connects to a `Greeter` service |
||||
at `localhost:50051` and uses a stub to call the `SayHello` method with a |
||||
`HelloRequest`: |
||||
|
||||
```go |
||||
const ( |
||||
address = "localhost:50051" |
||||
defaultName = "world" |
||||
) |
||||
|
||||
func main() { |
||||
// Set up a connection to the server. |
||||
conn, err := grpc.Dial(address) |
||||
if err != nil { |
||||
log.Fatalf("did not connect: %v", err) |
||||
} |
||||
defer conn.Close() |
||||
c := pb.NewGreeterClient(conn) |
||||
|
||||
// Contact the server and print out its response. |
||||
name := defaultName |
||||
if len(os.Args) > 1 { |
||||
name = os.Args[1] |
||||
} |
||||
r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: |
||||
name}) |
||||
if err != nil { |
||||
log.Fatalf("could not greet: %v", err) |
||||
} |
||||
log.Printf("Greeting: %s", r.Message) |
||||
} |
||||
``` |
||||
|
||||
|
||||
If we run the Java server from earlier in another terminal window, we can |
||||
run the Go client and connect to it just like the Java client, even though |
||||
it's written in a different language. |
||||
|
||||
``` |
||||
$ greeter_client |
||||
``` |
||||
## Read more! |
||||
|
||||
- You can find links to language-specific tutorials, examples, and other docs in each language's [quick start](#quickstart). |
||||
- [gRPC Authentication Support](doc/grpc-auth-support.md) introduces authentication support in gRPC with supported mechanisms and examples. |
@ -0,0 +1,65 @@ |
||||
#gRPC in 3 minutes (C++) |
||||
|
||||
## Installation |
||||
|
||||
To install gRPC on your system, follow the instructions here: |
||||
[https://github.com/grpc/grpc/blob/master/INSTALL](https://github.com/grpc/grpc/blob/master/INSTALL). |
||||
|
||||
## Hello C++ gRPC! |
||||
|
||||
Here's how to build and run the C++ implementation of the [Hello World](examples/protos/helloworld.proto) example used in [Getting started](https://github.com/grpc/grpc/tree/master/examples). |
||||
|
||||
The example code for this and our other examples lives in the `examples` |
||||
directory. Clone this repository to your local machine by running the |
||||
following command: |
||||
|
||||
|
||||
```sh |
||||
$ git clone https://github.com/grpc/grpc.git |
||||
``` |
||||
|
||||
Change your current directory to examples/cpp/helloworld |
||||
|
||||
```sh |
||||
$ cd examples/cpp/helloworld/ |
||||
``` |
||||
|
||||
|
||||
### Generating gRPC code |
||||
|
||||
To generate the client and server side interfaces: |
||||
|
||||
```sh |
||||
$ make helloworld.grpc.pb.cc helloworld.pb.cc |
||||
``` |
||||
Which internally invokes the proto-compiler as: |
||||
|
||||
```sh |
||||
$ protoc -I ../../protos/ --grpc_out=. --plugin=protoc-gen-grpc=grpc_cpp_plugin ../../protos/helloworld.proto |
||||
$ protoc -I ../../protos/ --cpp_out=. ../../protos/helloworld.proto |
||||
``` |
||||
|
||||
### Client and server implementations |
||||
|
||||
The client implementation is at [greeter_client.cc](examples/cpp/helloworld/greeter_client.cc). |
||||
|
||||
The server implementation is at [greeter_server.cc](examples/cpp/helloworld/greeter_server.cc). |
||||
|
||||
### Try it! |
||||
Build client and server: |
||||
```sh |
||||
$ make |
||||
``` |
||||
Run the server, which will listen on port 50051: |
||||
```sh |
||||
$ ./greeter_server |
||||
``` |
||||
Run the client (in a different terminal): |
||||
```sh |
||||
$ ./greeter_client |
||||
``` |
||||
If things go smoothly, you will see the "Greeter received: Hello world" in the client side output. |
||||
|
||||
## Tutorial |
||||
|
||||
You can find a more detailed tutorial in [gRPC Basics: C++](examples/cpp/cpptutorial.md) |
@ -0,0 +1,365 @@ |
||||
#gRPC Basics: C++ |
||||
|
||||
This tutorial provides a basic C++ programmer's introduction to working with gRPC. By walking through this example you'll learn how to: |
||||
|
||||
- Define a service in a .proto file. |
||||
- Generate server and client code using the protocol buffer compiler. |
||||
- Use the C++ gRPC API to write a simple client and server for your service. |
||||
|
||||
It assumes that you have read the [Getting started](https://github.com/grpc/grpc/tree/master/examples) guide and are familiar with [protocol buffers] (https://developers.google.com/protocol-buffers/docs/overview). Note that the example in this tutorial uses the proto3 version of the protocol buffers language, which is currently in alpha release: you can find out more in the [proto3 language guide](https://developers.google.com/protocol-buffers/docs/proto3) and see the [release notes](https://github.com/google/protobuf/releases) for the new version in the protocol buffers Github repository. |
||||
|
||||
This isn't a comprehensive guide to using gRPC in C++: more reference documentation is coming soon. |
||||
|
||||
## Why use gRPC? |
||||
|
||||
Our example is a simple route mapping application that lets clients get information about features on their route, create a summary of their route, and exchange route information such as traffic updates with the server and other clients. |
||||
|
||||
With gRPC we can define our service once in a .proto file and implement clients and servers in any of gRPC's supported languages, which in turn can be run in environments ranging from servers inside Google to your own tablet - all the complexity of communication between different languages and environments is handled for you by gRPC. We also get all the advantages of working with protocol buffers, including efficient serialization, a simple IDL, and easy interface updating. |
||||
|
||||
## Example code and setup |
||||
|
||||
The example code for our tutorial is in [examples/cpp/route_guide](examples/cpp/route_guide). To download the example, clone this repository by running the following command: |
||||
```shell |
||||
$ git clone https://github.com/grpc/grpc.git |
||||
``` |
||||
|
||||
Then change your current directory to `examples/cpp/route_guide`: |
||||
```shell |
||||
$ cd examples/cpp/route_guide |
||||
``` |
||||
|
||||
You also should have the relevant tools installed to generate the server and client interface code - if you don't already, follow the setup instructions in [the C++ quick start guide](examples/cpp). |
||||
|
||||
|
||||
## Defining the service |
||||
|
||||
Our first step (as you'll know from [Getting started](examples/) is to define the gRPC *service* and the method *request* and *response* types using [protocol buffers] (https://developers.google.com/protocol-buffers/docs/overview). You can see the complete .proto file in [`examples/protos/route_guide.proto`](examples/protos/route_guide.proto). |
||||
|
||||
To define a service, you specify a named `service` in your .proto file: |
||||
|
||||
``` |
||||
service RouteGuide { |
||||
... |
||||
} |
||||
``` |
||||
|
||||
Then you define `rpc` methods inside your service definition, specifying their request and response types. gRPC lets you define four kinds of service method, all of which are used in the `RouteGuide` service: |
||||
|
||||
- A *simple RPC* where the client sends a request to the server using the stub and waits for a response to come back, just like a normal function call. |
||||
``` |
||||
// Obtains the feature at a given position. |
||||
rpc GetFeature(Point) returns (Feature) {} |
||||
``` |
||||
|
||||
- A *server-side streaming RPC* where the client sends a request to the server and gets a stream to read a sequence of messages back. The client reads from the returned stream until there are no more messages. As you can see in our example, you specify a server-side streaming method by placing the `stream` keyword before the *response* type. |
||||
``` |
||||
// Obtains the Features available within the given Rectangle. Results are |
||||
// streamed rather than returned at once (e.g. in a response message with a |
||||
// repeated field), as the rectangle may cover a large area and contain a |
||||
// huge number of features. |
||||
rpc ListFeatures(Rectangle) returns (stream Feature) {} |
||||
``` |
||||
|
||||
- A *client-side streaming RPC* where the client writes a sequence of messages and sends them to the server, again using a provided stream. Once the client has finished writing the messages, it waits for the server to read them all and return its response. You specify a client-side streaming method by placing the `stream` keyword before the *request* type. |
||||
``` |
||||
// Accepts a stream of Points on a route being traversed, returning a |
||||
// RouteSummary when traversal is completed. |
||||
rpc RecordRoute(stream Point) returns (RouteSummary) {} |
||||
``` |
||||
|
||||
- A *bidirectional streaming RPC* where both sides send a sequence of messages using a read-write stream. The two streams operate independently, so clients and servers can read and write in whatever order they like: for example, the server could wait to receive all the client messages before writing its responses, or it could alternately read a message then write a message, or some other combination of reads and writes. The order of messages in each stream is preserved. You specify this type of method by placing the `stream` keyword before both the request and the response. |
||||
``` |
||||
// Accepts a stream of RouteNotes sent while a route is being traversed, |
||||
// while receiving other RouteNotes (e.g. from other users). |
||||
rpc RouteChat(stream RouteNote) returns (stream RouteNote) {} |
||||
``` |
||||
|
||||
Our .proto file also contains protocol buffer message type definitions for all the request and response types used in our service methods - for example, here's the `Point` message type: |
||||
``` |
||||
// Points are represented as latitude-longitude pairs in the E7 representation |
||||
// (degrees multiplied by 10**7 and rounded to the nearest integer). |
||||
// Latitudes should be in the range +/- 90 degrees and longitude should be in |
||||
// the range +/- 180 degrees (inclusive). |
||||
message Point { |
||||
int32 latitude = 1; |
||||
int32 longitude = 2; |
||||
} |
||||
``` |
||||
|
||||
|
||||
## Generating client and server code |
||||
|
||||
Next we need to generate the gRPC client and server interfaces from our .proto service definition. We do this using the protocol buffer compiler `protoc` with a special gRPC C++ plugin. |
||||
|
||||
For simplicity, we've provided a [makefile](examples/cpp/route_guide/Makefile) that runs `protoc` for you with the appropriate plugin, input, and output (if you want to run this yourself, make sure you've installed protoc and followed the gRPC code [installation instructions](https://github.com/grpc/grpc/blob/master/INSTALL) first): |
||||
|
||||
```shell |
||||
$ make route_guide.grpc.pb.cc route_guide.pb.cc |
||||
``` |
||||
|
||||
which actually runs: |
||||
|
||||
```shell |
||||
$ protoc -I ../../protos --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` ../../protos/route_guide.proto |
||||
$ protoc -I ../../protos --cpp_out=. ../../protos/route_guide.proto |
||||
``` |
||||
|
||||
Running this command generates the following files in your current directory: |
||||
- `route_guide.pb.h`, the header which declares your generated message classes |
||||
- `route_guide.pb.cc`, which contains the implementation of your message classes |
||||
- `route_guide.grpc.pb.h`, the header which declares your generated service classes |
||||
- `route_guide.grpc.pb.cc`, which contains the implementation of your service classes |
||||
|
||||
These contain: |
||||
- All the protocol buffer code to populate, serialize, and retrieve our request and response message types |
||||
- A class called `RouteGuide` that contains |
||||
- a remote interface type (or *stub*) for clients to call with the methods defined in the `RouteGuide` service. |
||||
- two abstract interfaces for servers to implement, also with the methods defined in the `RouteGuide` service. |
||||
|
||||
|
||||
<a name="server"></a> |
||||
## Creating the server |
||||
|
||||
First let's look at how we create a `RouteGuide` server. If you're only interested in creating gRPC clients, you can skip this section and go straight to [Creating the client](#client) (though you might find it interesting anyway!). |
||||
|
||||
There are two parts to making our `RouteGuide` service do its job: |
||||
- Implementing the service interface generated from our service definition: doing the actual "work" of our service. |
||||
- Running a gRPC server to listen for requests from clients and return the service responses. |
||||
|
||||
You can find our example `RouteGuide` server in [examples/cpp/route_guide/route_guide_server.cc](examples/cpp/route_guide/route_guide_server.cc). Let's take a closer look at how it works. |
||||
|
||||
### Implementing RouteGuide |
||||
|
||||
As you can see, our server has a `RouteGuideImpl` class that implements the generated `RouteGuide::Service` interface: |
||||
|
||||
```cpp |
||||
class RouteGuideImpl final : public RouteGuide::Service { |
||||
... |
||||
} |
||||
``` |
||||
In this case we're implementing the *synchronous* version of `RouteGuide`, which provides our default gRPC server behaviour. It's also possible to implement an asynchronous interface, `RouteGuide::AsyncService`, which allows you to further customize your server's threading behaviour, though we won't look at this in this tutorial. |
||||
|
||||
`RouteGuideImpl` implements all our service methods. Let's look at the simplest type first, `GetFeature`, which just gets a `Point` from the client and returns the corresponding feature information from its database in a `Feature`. |
||||
|
||||
```cpp |
||||
Status GetFeature(ServerContext* context, const Point* point, |
||||
Feature* feature) override { |
||||
feature->set_name(GetFeatureName(*point, feature_list_)); |
||||
feature->mutable_location()->CopyFrom(*point); |
||||
return Status::OK; |
||||
} |
||||
``` |
||||
|
||||
The method is passed a context object for the RPC, the client's `Point` protocol buffer request, and a `Feature` protocol buffer to fill in with the response information. In the method we populate the `Feature` with the appropriate information, and then `return` with an `OK` status to tell gRPC that we've finished dealing with the RPC and that the `Feature` can be returned to the client. |
||||
|
||||
Now let's look at something a bit more complicated - a streaming RPC. `ListFeatures` is a server-side streaming RPC, so we need to send back multiple `Feature`s to our client. |
||||
|
||||
```cpp |
||||
Status ListFeatures(ServerContext* context, const Rectangle* rectangle, |
||||
ServerWriter<Feature>* writer) override { |
||||
auto lo = rectangle->lo(); |
||||
auto hi = rectangle->hi(); |
||||
long left = std::min(lo.longitude(), hi.longitude()); |
||||
long right = std::max(lo.longitude(), hi.longitude()); |
||||
long top = std::max(lo.latitude(), hi.latitude()); |
||||
long bottom = std::min(lo.latitude(), hi.latitude()); |
||||
for (const Feature& f : feature_list_) { |
||||
if (f.location().longitude() >= left && |
||||
f.location().longitude() <= right && |
||||
f.location().latitude() >= bottom && |
||||
f.location().latitude() <= top) { |
||||
writer->Write(f); |
||||
} |
||||
} |
||||
return Status::OK; |
||||
} |
||||
``` |
||||
|
||||
As you can see, instead of getting simple request and response objects in our method parameters, this time we get a request object (the `Rectangle` in which our client wants to find `Feature`s) and a special `ServerWriter` object. In the method, we populate as many `Feature` objects as we need to return, writing them to the `ServerWriter` using its `Write()` method. Finally, as in our simple RPC, we `return Status::OK` to tell gRPC that we've finished writing responses. |
||||
|
||||
If you look at the client-side streaming method `RecordRoute` you'll see it's quite similar, except this time we get a `ServerReader` instead of a request object and a single response. We use the `ServerReader`s `Read()` method to repeatedly read in our client's requests to a request object (in this case a `Point`) until there are no more messages: the server needs to check the return value of `Read()` after each call. If `true`, the stream is still good and it can continue reading; if `false` the message stream has ended. |
||||
|
||||
```cpp |
||||
while (stream->Read(&point)) { |
||||
...//process client input |
||||
} |
||||
``` |
||||
Finally, let's look at our bidirectional streaming RPC `RouteChat()`. |
||||
|
||||
```cpp |
||||
Status RouteChat(ServerContext* context, |
||||
ServerReaderWriter<RouteNote, RouteNote>* stream) override { |
||||
std::vector<RouteNote> received_notes; |
||||
RouteNote note; |
||||
while (stream->Read(¬e)) { |
||||
for (const RouteNote& n : received_notes) { |
||||
if (n.location().latitude() == note.location().latitude() && |
||||
n.location().longitude() == note.location().longitude()) { |
||||
stream->Write(n); |
||||
} |
||||
} |
||||
received_notes.push_back(note); |
||||
} |
||||
|
||||
return Status::OK; |
||||
} |
||||
``` |
||||
|
||||
This time we get a `ServerReaderWriter` that can be used to read *and* write messages. The syntax for reading and writing here is exactly the same as for our client-streaming and server-streaming methods. Although each side will always get the other's messages in the order they were written, both the client and server can read and write in any order — the streams operate completely independently. |
||||
|
||||
### Starting the server |
||||
|
||||
Once we've implemented all our methods, we also need to start up a gRPC server so that clients can actually use our service. The following snippet shows how we do this for our `RouteGuide` service: |
||||
|
||||
```cpp |
||||
void RunServer(const std::string& db_path) { |
||||
std::string server_address("0.0.0.0:50051"); |
||||
RouteGuideImpl service(db_path); |
||||
|
||||
ServerBuilder builder; |
||||
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); |
||||
builder.RegisterService(&service); |
||||
std::unique_ptr<Server> server(builder.BuildAndStart()); |
||||
std::cout << "Server listening on " << server_address << std::endl; |
||||
server->Wait(); |
||||
} |
||||
``` |
||||
As you can see, we build and start our server using a `ServerBuilder`. To do this, we: |
||||
|
||||
1. Create an instance of our service implementation class `RouteGuideImpl`. |
||||
2. Create an instance of the factory `ServerBuilder` class. |
||||
3. Specify the address and port we want to use to listen for client requests using the builder's `AddListeningPort()` method. |
||||
4. Register our service implementation with the builder. |
||||
5. Call `BuildAndStart()` on the builder to create and start an RPC server for our service. |
||||
5. Call `Wait()` on the server to do a blocking wait until process is killed or `Shutdown()` is called. |
||||
|
||||
<a name="client"></a> |
||||
## Creating the client |
||||
|
||||
In this section, we'll look at creating a C++ client for our `RouteGuide` service. You can see our complete example client code in [examples/cpp/route_guide/route_guide_client.cc](examples/cpp/route_guide/route_guide_client.cc). |
||||
|
||||
### Creating a stub |
||||
|
||||
To call service methods, we first need to create a *stub*. |
||||
|
||||
First we need to create a gRPC *channel* for our stub, specifying the server address and port we want to connect to and any special channel arguments - in our case we'll use the default `ChannelArguments` and no SSL: |
||||
|
||||
```cpp |
||||
grpc::CreateChannel("localhost:50051", grpc::InsecureCredentials(), ChannelArguments()); |
||||
``` |
||||
|
||||
Now we can use the channel to create our stub using the `NewStub` method provided in the `RouteGuide` class we generated from our .proto. |
||||
|
||||
```cpp |
||||
public: |
||||
RouteGuideClient(std::shared_ptr<Channel> channel, const std::string& db) |
||||
: stub_(RouteGuide::NewStub(channel)) { |
||||
... |
||||
} |
||||
``` |
||||
|
||||
### Calling service methods |
||||
|
||||
Now let's look at how we call our service methods. Note that in this tutorial we're calling the *blocking/synchronous* versions of each method: this means that the RPC call waits for the server to respond, and will either return a response or raise an exception. |
||||
|
||||
#### Simple RPC |
||||
|
||||
Calling the simple RPC `GetFeature` is nearly as straightforward as calling a local method. |
||||
|
||||
```cpp |
||||
Point point; |
||||
Feature feature; |
||||
point = MakePoint(409146138, -746188906); |
||||
GetOneFeature(point, &feature); |
||||
|
||||
... |
||||
|
||||
bool GetOneFeature(const Point& point, Feature* feature) { |
||||
ClientContext context; |
||||
Status status = stub_->GetFeature(&context, point, feature); |
||||
... |
||||
} |
||||
``` |
||||
|
||||
As you can see, we create and populate a request protocol buffer object (in our case `Point`), and create a response protocol buffer object for the server to fill in. We also create a `ClientContext` object for our call - you can optionally set RPC configuration values on this object, such as deadlines, though for now we'll use the default settings. Note that you cannot reuse this object between calls. Finally, we call the method on the stub, passing it the context, request, and response. If the method returns `OK`, then we can read the response information from the server from our response object. |
||||
|
||||
```cpp |
||||
std::cout << "Found feature called " << feature->name() << " at " |
||||
<< feature->location().latitude()/kCoordFactor_ << ", " |
||||
<< feature->location().longitude()/kCoordFactor_ << std::endl; |
||||
``` |
||||
|
||||
#### Streaming RPCs |
||||
|
||||
Now let's look at our streaming methods. If you've already read [Creating the server](#server) some of this may look very familiar - streaming RPCs are implemented in a similar way on both sides. Here's where we call the server-side streaming method `ListFeatures`, which returns a stream of geographical `Feature`s: |
||||
|
||||
```cpp |
||||
std::unique_ptr<ClientReader<Feature> > reader( |
||||
stub_->ListFeatures(&context, rect)); |
||||
while (reader->Read(&feature)) { |
||||
std::cout << "Found feature called " |
||||
<< feature.name() << " at " |
||||
<< feature.location().latitude()/kCoordFactor_ << ", " |
||||
<< feature.location().longitude()/kCoordFactor_ << std::endl; |
||||
} |
||||
Status status = reader->Finish(); |
||||
``` |
||||
|
||||
Instead of passing the method a context, request, and response, we pass it a context and request and get a `ClientReader` object back. The client can use the `ClientReader` to read the server's responses. We use the `ClientReader`s `Read()` method to repeatedly read in the server's responses to a response protocol buffer object (in this case a `Feature`) until there are no more messages: the client needs to check the return value of `Read()` after each call. If `true`, the stream is still good and it can continue reading; if `false` the message stream has ended. Finally, we call `Finish()` on the stream to complete the call and get our RPC status. |
||||
|
||||
The client-side streaming method `RecordRoute` is similar, except there we pass the method a context and response object and get back a `ClientWriter`. |
||||
|
||||
```cpp |
||||
std::unique_ptr<ClientWriter<Point> > writer( |
||||
stub_->RecordRoute(&context, &stats)); |
||||
for (int i = 0; i < kPoints; i++) { |
||||
const Feature& f = feature_list_[feature_distribution(generator)]; |
||||
std::cout << "Visiting point " |
||||
<< f.location().latitude()/kCoordFactor_ << ", " |
||||
<< f.location().longitude()/kCoordFactor_ << std::endl; |
||||
if (!writer->Write(f.location())) { |
||||
// Broken stream. |
||||
break; |
||||
} |
||||
std::this_thread::sleep_for(std::chrono::milliseconds( |
||||
delay_distribution(generator))); |
||||
} |
||||
writer->WritesDone(); |
||||
Status status = writer->Finish(); |
||||
if (status.IsOk()) { |
||||
std::cout << "Finished trip with " << stats.point_count() << " points\n" |
||||
<< "Passed " << stats.feature_count() << " features\n" |
||||
<< "Travelled " << stats.distance() << " meters\n" |
||||
<< "It took " << stats.elapsed_time() << " seconds" |
||||
<< std::endl; |
||||
} else { |
||||
std::cout << "RecordRoute rpc failed." << std::endl; |
||||
} |
||||
``` |
||||
|
||||
Once we've finished writing our client's requests to the stream using `Write()`, we need to call `WritesDone()` on the stream to let gRPC know that we've finished writing, then `Finish()` to complete the call and get our RPC status. If the status is `OK`, our response object that we initially passed to `RecordRoute()` will be populated with the server's response. |
||||
|
||||
Finally, let's look at our bidirectional streaming RPC `RouteChat()`. In this case, we just pass a context to the method and get back a `ClientReaderWriter`, which we can use to both write and read messages. |
||||
|
||||
```cpp |
||||
std::shared_ptr<ClientReaderWriter<RouteNote, RouteNote> > stream( |
||||
stub_->RouteChat(&context)); |
||||
``` |
||||
|
||||
The syntax for reading and writing here is exactly the same as for our client-streaming and server-streaming methods. Although each side will always get the other's messages in the order they were written, both the client and server can read and write in any order — the streams operate completely independently. |
||||
|
||||
## Try it out! |
||||
|
||||
Build client and server: |
||||
```shell |
||||
$ make |
||||
``` |
||||
Run the server, which will listen on port 50051: |
||||
```shell |
||||
$ ./route_guide_server |
||||
``` |
||||
Run the client (in a different terminal): |
||||
```shell |
||||
$ ./route_guide_client |
||||
``` |
||||
|
@ -0,0 +1,119 @@ |
||||
#
|
||||
# Copyright 2015, Google Inc.
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are
|
||||
# met:
|
||||
#
|
||||
# * Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
# * Redistributions in binary form must reproduce the above
|
||||
# copyright notice, this list of conditions and the following disclaimer
|
||||
# in the documentation and/or other materials provided with the
|
||||
# distribution.
|
||||
# * Neither the name of Google Inc. nor the names of its
|
||||
# contributors may be used to endorse or promote products derived from
|
||||
# this software without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
#
|
||||
|
||||
CXX = g++
|
||||
CPPFLAGS += -I/usr/local/include -pthread
|
||||
CXXFLAGS += -std=c++11
|
||||
LDFLAGS += -L/usr/local/lib -lgrpc++_unsecure -lgrpc -lgpr -lprotobuf -lpthread -ldl
|
||||
PROTOC = protoc
|
||||
GRPC_CPP_PLUGIN = grpc_cpp_plugin
|
||||
GRPC_CPP_PLUGIN_PATH ?= `which $(GRPC_CPP_PLUGIN)`
|
||||
|
||||
PROTOS_PATH = ../../protos
|
||||
|
||||
vpath %.proto $(PROTOS_PATH) |
||||
|
||||
all: system-check greeter_client greeter_server greeter_async_client greeter_async_server |
||||
|
||||
greeter_client: helloworld.pb.o helloworld.grpc.pb.o greeter_client.o |
||||
$(CXX) $^ $(LDFLAGS) -o $@
|
||||
|
||||
greeter_server: helloworld.pb.o helloworld.grpc.pb.o greeter_server.o |
||||
$(CXX) $^ $(LDFLAGS) -o $@
|
||||
|
||||
greeter_async_client: helloworld.pb.o helloworld.grpc.pb.o greeter_async_client.o |
||||
$(CXX) $^ $(LDFLAGS) -o $@
|
||||
|
||||
greeter_async_server: helloworld.pb.o helloworld.grpc.pb.o greeter_async_server.o |
||||
$(CXX) $^ $(LDFLAGS) -o $@
|
||||
|
||||
%.grpc.pb.cc: %.proto |
||||
$(PROTOC) -I $(PROTOS_PATH) --grpc_out=. --plugin=protoc-gen-grpc=$(GRPC_CPP_PLUGIN_PATH) $<
|
||||
|
||||
%.pb.cc: %.proto |
||||
$(PROTOC) -I $(PROTOS_PATH) --cpp_out=. $<
|
||||
|
||||
clean: |
||||
rm -f *.o *.pb.cc *.pb.h greeter_client greeter_server greeter_async_client greeter_async_server
|
||||
|
||||
|
||||
# The following is to test your system and ensure a smoother experience.
|
||||
# They are by no means necessary to actually compile a grpc-enabled software.
|
||||
|
||||
PROTOC_CMD = which $(PROTOC)
|
||||
PROTOC_CHECK_CMD = $(PROTOC) --version | grep -q libprotoc.3
|
||||
PLUGIN_CHECK_CMD = which $(GRPC_CPP_PLUGIN)
|
||||
HAS_PROTOC = $(shell $(PROTOC_CMD) > /dev/null && echo true || echo false)
|
||||
ifeq ($(HAS_PROTOC),true) |
||||
HAS_VALID_PROTOC = $(shell $(PROTOC_CHECK_CMD) 2> /dev/null && echo true || echo false)
|
||||
endif |
||||
HAS_PLUGIN = $(shell $(PLUGIN_CHECK_CMD) > /dev/null && echo true || echo false)
|
||||
|
||||
SYSTEM_OK = false
|
||||
ifeq ($(HAS_VALID_PROTOC),true) |
||||
ifeq ($(HAS_PLUGIN),true) |
||||
SYSTEM_OK = true
|
||||
endif |
||||
endif |
||||
|
||||
system-check: |
||||
ifneq ($(HAS_VALID_PROTOC),true) |
||||
@echo " DEPENDENCY ERROR"
|
||||
@echo
|
||||
@echo "You don't have protoc 3.0.0 installed in your path."
|
||||
@echo "Please install Google protocol buffers 3.0.0 and its compiler."
|
||||
@echo "You can find it here:"
|
||||
@echo
|
||||
@echo " https://github.com/google/protobuf/releases/tag/v3.0.0-alpha-1"
|
||||
@echo
|
||||
@echo "Here is what I get when trying to evaluate your version of protoc:"
|
||||
@echo
|
||||
-$(PROTOC) --version
|
||||
@echo
|
||||
@echo
|
||||
endif |
||||
ifneq ($(HAS_PLUGIN),true) |
||||
@echo " DEPENDENCY ERROR"
|
||||
@echo
|
||||
@echo "You don't have the grpc c++ protobuf plugin installed in your path."
|
||||
@echo "Please install grpc. You can find it here:"
|
||||
@echo
|
||||
@echo " https://github.com/grpc/grpc"
|
||||
@echo
|
||||
@echo "Here is what I get when trying to detect if you have the plugin:"
|
||||
@echo
|
||||
-which $(GRPC_CPP_PLUGIN)
|
||||
@echo
|
||||
@echo
|
||||
endif |
||||
ifneq ($(SYSTEM_OK),true) |
||||
@false
|
||||
endif |
@ -0,0 +1,260 @@ |
||||
# gRPC C++ Hello World Tutorial |
||||
|
||||
### Install gRPC |
||||
Make sure you have installed gRPC on your system. Follow the instructions here: |
||||
[https://github.com/grpc/grpc/blob/master/INSTALL](https://github.com/grpc/grpc/blob/master/INSTALL). |
||||
|
||||
### Get the tutorial source code |
||||
|
||||
The example code for this and our other examples lives in the `examples` |
||||
directory. Clone this repository to your local machine by running the |
||||
following command: |
||||
|
||||
|
||||
```sh |
||||
$ git clone https://github.com/grpc/grpc.git |
||||
``` |
||||
|
||||
Change your current directory to examples/cpp/helloworld |
||||
|
||||
```sh |
||||
$ cd examples/cpp/helloworld/ |
||||
``` |
||||
|
||||
### Defining a service |
||||
|
||||
The first step in creating our example is to define a *service*: an RPC |
||||
service specifies the methods that can be called remotely with their parameters |
||||
and return types. As you saw in the |
||||
[overview](#protocolbuffers) above, gRPC does this using [protocol |
||||
buffers](https://developers.google.com/protocol-buffers/docs/overview). We |
||||
use the protocol buffers interface definition language (IDL) to define our |
||||
service methods, and define the parameters and return |
||||
types as protocol buffer message types. Both the client and the |
||||
server use interface code generated from the service definition. |
||||
|
||||
Here's our example service definition, defined using protocol buffers IDL in |
||||
[helloworld.proto](examples/protos/helloworld.proto). The `Greeting` |
||||
service has one method, `hello`, that lets the server receive a single |
||||
`HelloRequest` |
||||
message from the remote client containing the user's name, then send back |
||||
a greeting in a single `HelloReply`. This is the simplest type of RPC you |
||||
can specify in gRPC - we'll look at some other types later in this document. |
||||
|
||||
``` |
||||
syntax = "proto3"; |
||||
|
||||
option java_package = "ex.grpc"; |
||||
|
||||
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; |
||||
} |
||||
|
||||
``` |
||||
|
||||
<a name="generating"></a> |
||||
### Generating gRPC code |
||||
|
||||
Once we've defined our service, we use the protocol buffer compiler |
||||
`protoc` to generate the special client and server code we need to create |
||||
our application. The generated code contains both stub code for clients to |
||||
use and an abstract interface for servers to implement, both with the method |
||||
defined in our `Greeting` service. |
||||
|
||||
To generate the client and server side interfaces: |
||||
|
||||
```sh |
||||
$ make helloworld.grpc.pb.cc helloworld.pb.cc |
||||
``` |
||||
Which internally invokes the proto-compiler as: |
||||
|
||||
```sh |
||||
$ protoc -I ../../protos/ --grpc_out=. --plugin=protoc-gen-grpc=grpc_cpp_plugin ../../protos/helloworld.proto |
||||
$ protoc -I ../../protos/ --cpp_out=. ../../protos/helloworld.proto |
||||
``` |
||||
|
||||
### Writing a client |
||||
|
||||
- Create a channel. A channel is a logical connection to an endpoint. A gRPC |
||||
channel can be created with the target address, credentials to use and |
||||
arguments as follows |
||||
|
||||
``` |
||||
auto channel = CreateChannel("localhost:50051", InsecureCredentials(), ChannelArguments()); |
||||
``` |
||||
|
||||
- Create a stub. A stub implements the rpc methods of a service and in the |
||||
generated code, a method is provided to created a stub with a channel: |
||||
|
||||
``` |
||||
auto stub = helloworld::Greeter::NewStub(channel); |
||||
``` |
||||
|
||||
- Make a unary rpc, with `ClientContext` and request/response proto messages. |
||||
|
||||
``` |
||||
ClientContext context; |
||||
HelloRequest request; |
||||
request.set_name("hello"); |
||||
HelloReply reply; |
||||
Status status = stub->SayHello(&context, request, &reply); |
||||
``` |
||||
|
||||
- Check returned status and response. |
||||
|
||||
``` |
||||
if (status.ok()) { |
||||
// check reply.message() |
||||
} else { |
||||
// rpc failed. |
||||
} |
||||
``` |
||||
|
||||
For a working example, refer to [greeter_client.cc](examples/cpp/helloworld/greeter_client.cc). |
||||
|
||||
### Writing a server |
||||
|
||||
- Implement the service interface |
||||
|
||||
``` |
||||
class GreeterServiceImpl final : public Greeter::Service { |
||||
Status SayHello(ServerContext* context, const HelloRequest* request, |
||||
HelloReply* reply) override { |
||||
std::string prefix("Hello "); |
||||
reply->set_message(prefix + request->name()); |
||||
return Status::OK; |
||||
} |
||||
}; |
||||
|
||||
``` |
||||
|
||||
- Build a server exporting the service |
||||
|
||||
``` |
||||
GreeterServiceImpl service; |
||||
ServerBuilder builder; |
||||
builder.AddListeningPort("0.0.0.0:50051", grpc::InsecureServerCredentials()); |
||||
builder.RegisterService(&service); |
||||
std::unique_ptr<Server> server(builder.BuildAndStart()); |
||||
``` |
||||
|
||||
For a working example, refer to [greeter_server.cc](examples/cpp/helloworld/greeter_server.cc). |
||||
|
||||
### Writing asynchronous client and server |
||||
|
||||
gRPC uses `CompletionQueue` API for asynchronous operations. The basic work flow |
||||
is |
||||
- bind a `CompletionQueue` to a rpc call |
||||
- do something like a read or write, present with a unique `void*` tag |
||||
- call `CompletionQueue::Next` to wait for operations to complete. If a tag |
||||
appears, it indicates that the corresponding operation is complete. |
||||
|
||||
#### Async client |
||||
|
||||
The channel and stub creation code is the same as the sync client. |
||||
|
||||
- Initiate the rpc and create a handle for the rpc. Bind the rpc to a |
||||
`CompletionQueue`. |
||||
|
||||
``` |
||||
CompletionQueue cq; |
||||
auto rpc = stub->AsyncSayHello(&context, request, &cq); |
||||
``` |
||||
|
||||
- Ask for reply and final status, with a unique tag |
||||
|
||||
``` |
||||
Status status; |
||||
rpc->Finish(&reply, &status, (void*)1); |
||||
``` |
||||
|
||||
- Wait for the completion queue to return the next tag. The reply and status are |
||||
ready once the tag passed into the corresponding `Finish()` call is returned. |
||||
|
||||
``` |
||||
void* got_tag; |
||||
bool ok = false; |
||||
cq.Next(&got_tag, &ok); |
||||
if (ok && got_tag == (void*)1) { |
||||
// check reply and status |
||||
} |
||||
``` |
||||
|
||||
For a working example, refer to [greeter_async_client.cc](examples/cpp/helloworld/greeter_async_client.cc). |
||||
|
||||
#### Async server |
||||
|
||||
The server implementation requests a rpc call with a tag and then wait for the |
||||
completion queue to return the tag. The basic flow is |
||||
|
||||
- Build a server exporting the async service |
||||
|
||||
``` |
||||
helloworld::Greeter::AsyncService service; |
||||
ServerBuilder builder; |
||||
builder.AddListeningPort("0.0.0.0:50051", InsecureServerCredentials()); |
||||
builder.RegisterAsyncService(&service); |
||||
auto cq = builder.AddCompletionQueue(); |
||||
auto server = builder.BuildAndStart(); |
||||
``` |
||||
|
||||
- Request one rpc |
||||
|
||||
``` |
||||
ServerContext context; |
||||
HelloRequest request; |
||||
ServerAsyncResponseWriter<HelloReply> responder; |
||||
service.RequestSayHello(&context, &request, &responder, &cq, &cq, (void*)1); |
||||
``` |
||||
|
||||
- Wait for the completion queue to return the tag. The context, request and |
||||
responder are ready once the tag is retrieved. |
||||
|
||||
``` |
||||
HelloReply reply; |
||||
Status status; |
||||
void* got_tag; |
||||
bool ok = false; |
||||
cq.Next(&got_tag, &ok); |
||||
if (ok && got_tag == (void*)1) { |
||||
// set reply and status |
||||
responder.Finish(reply, status, (void*)2); |
||||
} |
||||
``` |
||||
|
||||
- Wait for the completion queue to return the tag. The rpc is finished when the |
||||
tag is back. |
||||
|
||||
``` |
||||
void* got_tag; |
||||
bool ok = false; |
||||
cq.Next(&got_tag, &ok); |
||||
if (ok && got_tag == (void*)2) { |
||||
// clean up |
||||
} |
||||
``` |
||||
|
||||
To handle multiple rpcs, the async server creates an object `CallData` to |
||||
maintain the state of each rpc and use the address of it as the unique tag. For |
||||
simplicity the server only uses one completion queue for all events, and runs a |
||||
main loop in `HandleRpcs` to query the queue. |
||||
|
||||
For a working example, refer to [greeter_async_server.cc](examples/cpp/helloworld/greeter_async_server.cc). |
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,98 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#include <iostream> |
||||
#include <memory> |
||||
#include <string> |
||||
|
||||
#include <grpc/grpc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc++/channel.h> |
||||
#include <grpc++/client_context.h> |
||||
#include <grpc++/completion_queue.h> |
||||
#include <grpc++/create_channel.h> |
||||
#include <grpc++/credentials.h> |
||||
#include "helloworld.grpc.pb.h" |
||||
|
||||
using grpc::Channel; |
||||
using grpc::ChannelArguments; |
||||
using grpc::ClientAsyncResponseReader; |
||||
using grpc::ClientContext; |
||||
using grpc::CompletionQueue; |
||||
using grpc::Status; |
||||
using helloworld::HelloRequest; |
||||
using helloworld::HelloReply; |
||||
using helloworld::Greeter; |
||||
|
||||
class GreeterClient { |
||||
public: |
||||
explicit GreeterClient(std::shared_ptr<Channel> channel) |
||||
: stub_(Greeter::NewStub(channel)) {} |
||||
|
||||
std::string SayHello(const std::string& user) { |
||||
HelloRequest request; |
||||
request.set_name(user); |
||||
HelloReply reply; |
||||
ClientContext context; |
||||
CompletionQueue cq; |
||||
Status status; |
||||
|
||||
std::unique_ptr<ClientAsyncResponseReader<HelloReply> > rpc( |
||||
stub_->AsyncSayHello(&context, request, &cq)); |
||||
rpc->Finish(&reply, &status, (void*)1); |
||||
void* got_tag; |
||||
bool ok = false; |
||||
cq.Next(&got_tag, &ok); |
||||
GPR_ASSERT(ok); |
||||
GPR_ASSERT(got_tag == (void*)1); |
||||
|
||||
if (status.ok()) { |
||||
return reply.message(); |
||||
} else { |
||||
return "Rpc failed"; |
||||
} |
||||
} |
||||
|
||||
private: |
||||
std::unique_ptr<Greeter::Stub> stub_; |
||||
}; |
||||
|
||||
int main(int argc, char** argv) { |
||||
GreeterClient greeter(grpc::CreateChannel( |
||||
"localhost:50051", grpc::InsecureCredentials(), ChannelArguments())); |
||||
std::string user("world"); |
||||
std::string reply = greeter.SayHello(user); |
||||
std::cout << "Greeter received: " << reply << std::endl; |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,136 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#include <memory> |
||||
#include <iostream> |
||||
#include <string> |
||||
#include <thread> |
||||
|
||||
#include <grpc/grpc.h> |
||||
#include <grpc/support/log.h> |
||||
#include <grpc++/completion_queue.h> |
||||
#include <grpc++/server.h> |
||||
#include <grpc++/server_builder.h> |
||||
#include <grpc++/server_context.h> |
||||
#include <grpc++/server_credentials.h> |
||||
#include "helloworld.grpc.pb.h" |
||||
|
||||
using grpc::Server; |
||||
using grpc::ServerAsyncResponseWriter; |
||||
using grpc::ServerBuilder; |
||||
using grpc::ServerContext; |
||||
using grpc::ServerCompletionQueue; |
||||
using grpc::Status; |
||||
using helloworld::HelloRequest; |
||||
using helloworld::HelloReply; |
||||
using helloworld::Greeter; |
||||
|
||||
class ServerImpl final { |
||||
public: |
||||
~ServerImpl() { |
||||
server_->Shutdown(); |
||||
cq_->Shutdown(); |
||||
} |
||||
|
||||
// There is no shutdown handling in this code.
|
||||
void Run() { |
||||
std::string server_address("0.0.0.0:50051"); |
||||
|
||||
ServerBuilder builder; |
||||
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); |
||||
builder.RegisterAsyncService(&service_); |
||||
cq_ = builder.AddCompletionQueue(); |
||||
server_ = builder.BuildAndStart(); |
||||
std::cout << "Server listening on " << server_address << std::endl; |
||||
|
||||
HandleRpcs(); |
||||
} |
||||
|
||||
private: |
||||
class CallData { |
||||
public: |
||||
CallData(Greeter::AsyncService* service, ServerCompletionQueue* cq) |
||||
: service_(service), cq_(cq), responder_(&ctx_), status_(CREATE) { |
||||
Proceed(); |
||||
} |
||||
|
||||
void Proceed() { |
||||
if (status_ == CREATE) { |
||||
service_->RequestSayHello(&ctx_, &request_, &responder_, cq_, cq_, |
||||
this); |
||||
status_ = PROCESS; |
||||
} else if (status_ == PROCESS) { |
||||
new CallData(service_, cq_); |
||||
std::string prefix("Hello "); |
||||
reply_.set_message(prefix + request_.name()); |
||||
responder_.Finish(reply_, Status::OK, this); |
||||
status_ = FINISH; |
||||
} else { |
||||
delete this; |
||||
} |
||||
} |
||||
|
||||
private: |
||||
Greeter::AsyncService* service_; |
||||
ServerCompletionQueue* cq_; |
||||
ServerContext ctx_; |
||||
HelloRequest request_; |
||||
HelloReply reply_; |
||||
ServerAsyncResponseWriter<HelloReply> responder_; |
||||
enum CallStatus { CREATE, PROCESS, FINISH }; |
||||
CallStatus status_; |
||||
}; |
||||
|
||||
// This can be run in multiple threads if needed.
|
||||
void HandleRpcs() { |
||||
new CallData(&service_, cq_.get()); |
||||
void* tag; |
||||
bool ok; |
||||
while (true) { |
||||
cq_->Next(&tag, &ok); |
||||
GPR_ASSERT(ok); |
||||
static_cast<CallData*>(tag)->Proceed(); |
||||
} |
||||
} |
||||
|
||||
std::unique_ptr<ServerCompletionQueue> cq_; |
||||
Greeter::AsyncService service_; |
||||
std::unique_ptr<Server> server_; |
||||
}; |
||||
|
||||
int main(int argc, char** argv) { |
||||
ServerImpl server; |
||||
server.Run(); |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,85 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#include <iostream> |
||||
#include <memory> |
||||
#include <string> |
||||
|
||||
#include <grpc/grpc.h> |
||||
#include <grpc++/channel.h> |
||||
#include <grpc++/client_context.h> |
||||
#include <grpc++/create_channel.h> |
||||
#include <grpc++/credentials.h> |
||||
#include "helloworld.grpc.pb.h" |
||||
|
||||
using grpc::Channel; |
||||
using grpc::ChannelArguments; |
||||
using grpc::ClientContext; |
||||
using grpc::Status; |
||||
using helloworld::HelloRequest; |
||||
using helloworld::HelloReply; |
||||
using helloworld::Greeter; |
||||
|
||||
class GreeterClient { |
||||
public: |
||||
GreeterClient(std::shared_ptr<Channel> channel) |
||||
: stub_(Greeter::NewStub(channel)) {} |
||||
|
||||
std::string SayHello(const std::string& user) { |
||||
HelloRequest request; |
||||
request.set_name(user); |
||||
HelloReply reply; |
||||
ClientContext context; |
||||
|
||||
Status status = stub_->SayHello(&context, request, &reply); |
||||
if (status.ok()) { |
||||
return reply.message(); |
||||
} else { |
||||
return "Rpc failed"; |
||||
} |
||||
} |
||||
|
||||
private: |
||||
std::unique_ptr<Greeter::Stub> stub_; |
||||
}; |
||||
|
||||
int main(int argc, char** argv) { |
||||
GreeterClient greeter( |
||||
grpc::CreateChannel("localhost:50051", grpc::InsecureCredentials(), |
||||
ChannelArguments())); |
||||
std::string user("world"); |
||||
std::string reply = greeter.SayHello(user); |
||||
std::cout << "Greeter received: " << reply << std::endl; |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,78 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#include <iostream> |
||||
#include <memory> |
||||
#include <string> |
||||
|
||||
#include <grpc/grpc.h> |
||||
#include <grpc++/server.h> |
||||
#include <grpc++/server_builder.h> |
||||
#include <grpc++/server_context.h> |
||||
#include <grpc++/server_credentials.h> |
||||
#include "helloworld.grpc.pb.h" |
||||
|
||||
using grpc::Server; |
||||
using grpc::ServerBuilder; |
||||
using grpc::ServerContext; |
||||
using grpc::Status; |
||||
using helloworld::HelloRequest; |
||||
using helloworld::HelloReply; |
||||
using helloworld::Greeter; |
||||
|
||||
class GreeterServiceImpl final : public Greeter::Service { |
||||
Status SayHello(ServerContext* context, const HelloRequest* request, |
||||
HelloReply* reply) override { |
||||
std::string prefix("Hello "); |
||||
reply->set_message(prefix + request->name()); |
||||
return Status::OK; |
||||
} |
||||
}; |
||||
|
||||
void RunServer() { |
||||
std::string server_address("0.0.0.0:50051"); |
||||
GreeterServiceImpl service; |
||||
|
||||
ServerBuilder builder; |
||||
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); |
||||
builder.RegisterService(&service); |
||||
std::unique_ptr<Server> server(builder.BuildAndStart()); |
||||
std::cout << "Server listening on " << server_address << std::endl; |
||||
server->Wait(); |
||||
} |
||||
|
||||
int main(int argc, char** argv) { |
||||
RunServer(); |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,113 @@ |
||||
#
|
||||
# Copyright 2015, Google Inc.
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
# modification, are permitted provided that the following conditions are
|
||||
# met:
|
||||
#
|
||||
# * Redistributions of source code must retain the above copyright
|
||||
# notice, this list of conditions and the following disclaimer.
|
||||
# * Redistributions in binary form must reproduce the above
|
||||
# copyright notice, this list of conditions and the following disclaimer
|
||||
# in the documentation and/or other materials provided with the
|
||||
# distribution.
|
||||
# * Neither the name of Google Inc. nor the names of its
|
||||
# contributors may be used to endorse or promote products derived from
|
||||
# this software without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
#
|
||||
|
||||
CXX = g++
|
||||
CPPFLAGS += -I/usr/local/include -pthread
|
||||
CXXFLAGS += -std=c++11
|
||||
LDFLAGS += -L/usr/local/lib -lgrpc++_unsecure -lgrpc -lgpr -lprotobuf -lpthread -ldl
|
||||
PROTOC = protoc
|
||||
GRPC_CPP_PLUGIN = grpc_cpp_plugin
|
||||
GRPC_CPP_PLUGIN_PATH ?= `which $(GRPC_CPP_PLUGIN)`
|
||||
|
||||
PROTOS_PATH = ../../protos
|
||||
|
||||
vpath %.proto $(PROTOS_PATH) |
||||
|
||||
all: system-check route_guide_client route_guide_server |
||||
|
||||
route_guide_client: route_guide.pb.o route_guide.grpc.pb.o route_guide_client.o helper.o |
||||
$(CXX) $^ $(LDFLAGS) -o $@
|
||||
|
||||
route_guide_server: route_guide.pb.o route_guide.grpc.pb.o route_guide_server.o helper.o |
||||
$(CXX) $^ $(LDFLAGS) -o $@
|
||||
|
||||
%.grpc.pb.cc: %.proto |
||||
$(PROTOC) -I $(PROTOS_PATH) --grpc_out=. --plugin=protoc-gen-grpc=$(GRPC_CPP_PLUGIN_PATH) $<
|
||||
|
||||
%.pb.cc: %.proto |
||||
$(PROTOC) -I $(PROTOS_PATH) --cpp_out=. $<
|
||||
|
||||
clean: |
||||
rm -f *.o *.pb.cc *.pb.h route_guide_client route_guide_server
|
||||
|
||||
|
||||
# The following is to test your system and ensure a smoother experience.
|
||||
# They are by no means necessary to actually compile a grpc-enabled software.
|
||||
|
||||
PROTOC_CMD = which $(PROTOC)
|
||||
PROTOC_CHECK_CMD = $(PROTOC) --version | grep -q libprotoc.3
|
||||
PLUGIN_CHECK_CMD = which $(GRPC_CPP_PLUGIN)
|
||||
HAS_PROTOC = $(shell $(PROTOC_CMD) > /dev/null && echo true || echo false)
|
||||
ifeq ($(HAS_PROTOC),true) |
||||
HAS_VALID_PROTOC = $(shell $(PROTOC_CHECK_CMD) 2> /dev/null && echo true || echo false)
|
||||
endif |
||||
HAS_PLUGIN = $(shell $(PLUGIN_CHECK_CMD) > /dev/null && echo true || echo false)
|
||||
|
||||
SYSTEM_OK = false
|
||||
ifeq ($(HAS_VALID_PROTOC),true) |
||||
ifeq ($(HAS_PLUGIN),true) |
||||
SYSTEM_OK = true
|
||||
endif |
||||
endif |
||||
|
||||
system-check: |
||||
ifneq ($(HAS_VALID_PROTOC),true) |
||||
@echo " DEPENDENCY ERROR"
|
||||
@echo
|
||||
@echo "You don't have protoc 3.0.0 installed in your path."
|
||||
@echo "Please install Google protocol buffers 3.0.0 and its compiler."
|
||||
@echo "You can find it here:"
|
||||
@echo
|
||||
@echo " https://github.com/google/protobuf/releases/tag/v3.0.0-alpha-1"
|
||||
@echo
|
||||
@echo "Here is what I get when trying to evaluate your version of protoc:"
|
||||
@echo
|
||||
-$(PROTOC) --version
|
||||
@echo
|
||||
@echo
|
||||
endif |
||||
ifneq ($(HAS_PLUGIN),true) |
||||
@echo " DEPENDENCY ERROR"
|
||||
@echo
|
||||
@echo "You don't have the grpc c++ protobuf plugin installed in your path."
|
||||
@echo "Please install grpc. You can find it here:"
|
||||
@echo
|
||||
@echo " https://github.com/grpc/grpc"
|
||||
@echo
|
||||
@echo "Here is what I get when trying to detect if you have the plugin:"
|
||||
@echo
|
||||
-which $(GRPC_CPP_PLUGIN)
|
||||
@echo
|
||||
@echo
|
||||
endif |
||||
ifneq ($(SYSTEM_OK),true) |
||||
@false
|
||||
endif |
@ -0,0 +1,178 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#include <algorithm> |
||||
#include <cctype> |
||||
#include <fstream> |
||||
#include <iostream> |
||||
#include <sstream> |
||||
#include <string> |
||||
#include <vector> |
||||
#include "route_guide.grpc.pb.h" |
||||
|
||||
namespace examples { |
||||
|
||||
std::string GetDbFileContent(int argc, char** argv) { |
||||
std::string db_path; |
||||
std::string arg_str("--db_path"); |
||||
if (argc > 1) { |
||||
std::string argv_1 = argv[1]; |
||||
size_t start_position = argv_1.find(arg_str); |
||||
if (start_position != std::string::npos) { |
||||
start_position += arg_str.size(); |
||||
if (argv_1[start_position] == ' ' || |
||||
argv_1[start_position] == '=') { |
||||
db_path = argv_1.substr(start_position + 1); |
||||
} |
||||
} |
||||
} else { |
||||
db_path = "route_guide_db.json"; |
||||
} |
||||
std::ifstream db_file(db_path); |
||||
if (!db_file.is_open()) { |
||||
std::cout << "Failed to open " << db_path << std::endl; |
||||
return ""; |
||||
} |
||||
std::stringstream db; |
||||
db << db_file.rdbuf(); |
||||
return db.str(); |
||||
} |
||||
|
||||
// A simple parser for the json db file. It requires the db file to have the
|
||||
// exact form of [{"location": { "latitude": 123, "longitude": 456}, "name":
|
||||
// "the name can be empty" }, { ... } ... The spaces will be stripped.
|
||||
class Parser { |
||||
public: |
||||
explicit Parser(const std::string& db) : db_(db) { |
||||
// Remove all spaces.
|
||||
db_.erase( |
||||
std::remove_if(db_.begin(), db_.end(), isspace), |
||||
db_.end()); |
||||
if (!Match("[")) { |
||||
SetFailedAndReturnFalse(); |
||||
} |
||||
} |
||||
|
||||
bool Finished() { |
||||
return current_ >= db_.size(); |
||||
} |
||||
|
||||
bool TryParseOne(Feature* feature) { |
||||
if (failed_ || Finished() || !Match("{")) { |
||||
return SetFailedAndReturnFalse(); |
||||
} |
||||
if (!Match(location_) || !Match("{") || !Match(latitude_)) { |
||||
return SetFailedAndReturnFalse(); |
||||
} |
||||
long temp = 0; |
||||
ReadLong(&temp); |
||||
feature->mutable_location()->set_latitude(temp); |
||||
if (!Match(",") || !Match(longitude_)) { |
||||
return SetFailedAndReturnFalse(); |
||||
} |
||||
ReadLong(&temp); |
||||
feature->mutable_location()->set_longitude(temp); |
||||
if (!Match("},") || !Match(name_) || !Match("\"")) { |
||||
return SetFailedAndReturnFalse(); |
||||
} |
||||
size_t name_start = current_; |
||||
while (current_ != db_.size() && db_[current_++] != '"') { |
||||
} |
||||
if (current_ == db_.size()) { |
||||
return SetFailedAndReturnFalse(); |
||||
} |
||||
feature->set_name(db_.substr(name_start, current_-name_start-1)); |
||||
if (!Match("},")) { |
||||
if (db_[current_ - 1] == ']' && current_ == db_.size()) { |
||||
return true; |
||||
} |
||||
return SetFailedAndReturnFalse(); |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
private: |
||||
|
||||
bool SetFailedAndReturnFalse() { |
||||
failed_ = true; |
||||
return false; |
||||
} |
||||
|
||||
bool Match(const std::string& prefix) { |
||||
bool eq = db_.substr(current_, prefix.size()) == prefix; |
||||
current_ += prefix.size(); |
||||
return eq; |
||||
} |
||||
|
||||
void ReadLong(long* l) { |
||||
size_t start = current_; |
||||
while (current_ != db_.size() && db_[current_] != ',' && db_[current_] != '}') { |
||||
current_++; |
||||
} |
||||
// It will throw an exception if fails.
|
||||
*l = std::stol(db_.substr(start, current_ - start)); |
||||
} |
||||
|
||||
bool failed_ = false; |
||||
std::string db_; |
||||
size_t current_ = 0; |
||||
const std::string location_ = "\"location\":"; |
||||
const std::string latitude_ = "\"latitude\":"; |
||||
const std::string longitude_ = "\"longitude\":"; |
||||
const std::string name_ = "\"name\":"; |
||||
}; |
||||
|
||||
void ParseDb(const std::string& db, std::vector<Feature>* feature_list) { |
||||
feature_list->clear(); |
||||
std::string db_content(db); |
||||
db_content.erase( |
||||
std::remove_if(db_content.begin(), db_content.end(), isspace), |
||||
db_content.end()); |
||||
|
||||
Parser parser(db_content); |
||||
Feature feature; |
||||
while (!parser.Finished()) { |
||||
feature_list->push_back(Feature()); |
||||
if (!parser.TryParseOne(&feature_list->back())) { |
||||
std::cout << "Error parsing the db file"; |
||||
feature_list->clear(); |
||||
break; |
||||
} |
||||
} |
||||
std::cout << "DB parsed, loaded " << feature_list->size() |
||||
<< " features." << std::endl; |
||||
} |
||||
|
||||
|
||||
} // namespace examples
|
||||
|
@ -0,0 +1,252 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#include <chrono> |
||||
#include <iostream> |
||||
#include <memory> |
||||
#include <random> |
||||
#include <string> |
||||
#include <thread> |
||||
|
||||
#include <grpc/grpc.h> |
||||
#include <grpc++/channel.h> |
||||
#include <grpc++/client_context.h> |
||||
#include <grpc++/create_channel.h> |
||||
#include <grpc++/credentials.h> |
||||
#include "helper.h" |
||||
#include "route_guide.grpc.pb.h" |
||||
|
||||
using grpc::Channel; |
||||
using grpc::ChannelArguments; |
||||
using grpc::ClientContext; |
||||
using grpc::ClientReader; |
||||
using grpc::ClientReaderWriter; |
||||
using grpc::ClientWriter; |
||||
using grpc::Status; |
||||
using examples::Point; |
||||
using examples::Feature; |
||||
using examples::Rectangle; |
||||
using examples::RouteSummary; |
||||
using examples::RouteNote; |
||||
using examples::RouteGuide; |
||||
|
||||
Point MakePoint(long latitude, long longitude) { |
||||
Point p; |
||||
p.set_latitude(latitude); |
||||
p.set_longitude(longitude); |
||||
return p; |
||||
} |
||||
|
||||
Feature MakeFeature(const std::string& name, |
||||
long latitude, long longitude) { |
||||
Feature f; |
||||
f.set_name(name); |
||||
f.mutable_location()->CopyFrom(MakePoint(latitude, longitude)); |
||||
return f; |
||||
} |
||||
|
||||
RouteNote MakeRouteNote(const std::string& message, |
||||
long latitude, long longitude) { |
||||
RouteNote n; |
||||
n.set_message(message); |
||||
n.mutable_location()->CopyFrom(MakePoint(latitude, longitude)); |
||||
return n; |
||||
} |
||||
|
||||
class RouteGuideClient { |
||||
public: |
||||
RouteGuideClient(std::shared_ptr<Channel> channel, const std::string& db) |
||||
: stub_(RouteGuide::NewStub(channel)) { |
||||
examples::ParseDb(db, &feature_list_); |
||||
} |
||||
|
||||
void GetFeature() { |
||||
Point point; |
||||
Feature feature; |
||||
point = MakePoint(409146138, -746188906); |
||||
GetOneFeature(point, &feature); |
||||
point = MakePoint(0, 0); |
||||
GetOneFeature(point, &feature); |
||||
} |
||||
|
||||
void ListFeatures() { |
||||
examples::Rectangle rect; |
||||
Feature feature; |
||||
ClientContext context; |
||||
|
||||
rect.mutable_lo()->set_latitude(400000000); |
||||
rect.mutable_lo()->set_longitude(-750000000); |
||||
rect.mutable_hi()->set_latitude(420000000); |
||||
rect.mutable_hi()->set_longitude(-730000000); |
||||
std::cout << "Looking for features between 40, -75 and 42, -73" |
||||
<< std::endl; |
||||
|
||||
std::unique_ptr<ClientReader<Feature> > reader( |
||||
stub_->ListFeatures(&context, rect)); |
||||
while (reader->Read(&feature)) { |
||||
std::cout << "Found feature called " |
||||
<< feature.name() << " at " |
||||
<< feature.location().latitude()/kCoordFactor_ << ", " |
||||
<< feature.location().longitude()/kCoordFactor_ << std::endl; |
||||
} |
||||
Status status = reader->Finish(); |
||||
if (status.ok()) { |
||||
std::cout << "ListFeatures rpc succeeded." << std::endl; |
||||
} else { |
||||
std::cout << "ListFeatures rpc failed." << std::endl; |
||||
} |
||||
} |
||||
|
||||
void RecordRoute() { |
||||
Point point; |
||||
RouteSummary stats; |
||||
ClientContext context; |
||||
const int kPoints = 10; |
||||
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); |
||||
|
||||
std::default_random_engine generator(seed); |
||||
std::uniform_int_distribution<int> feature_distribution( |
||||
0, feature_list_.size() - 1); |
||||
std::uniform_int_distribution<int> delay_distribution( |
||||
500, 1500); |
||||
|
||||
std::unique_ptr<ClientWriter<Point> > writer( |
||||
stub_->RecordRoute(&context, &stats)); |
||||
for (int i = 0; i < kPoints; i++) { |
||||
const Feature& f = feature_list_[feature_distribution(generator)]; |
||||
std::cout << "Visiting point " |
||||
<< f.location().latitude()/kCoordFactor_ << ", " |
||||
<< f.location().longitude()/kCoordFactor_ << std::endl; |
||||
if (!writer->Write(f.location())) { |
||||
// Broken stream.
|
||||
break; |
||||
} |
||||
std::this_thread::sleep_for(std::chrono::milliseconds( |
||||
delay_distribution(generator))); |
||||
} |
||||
writer->WritesDone(); |
||||
Status status = writer->Finish(); |
||||
if (status.ok()) { |
||||
std::cout << "Finished trip with " << stats.point_count() << " points\n" |
||||
<< "Passed " << stats.feature_count() << " features\n" |
||||
<< "Travelled " << stats.distance() << " meters\n" |
||||
<< "It took " << stats.elapsed_time() << " seconds" |
||||
<< std::endl; |
||||
} else { |
||||
std::cout << "RecordRoute rpc failed." << std::endl; |
||||
} |
||||
} |
||||
|
||||
void RouteChat() { |
||||
ClientContext context; |
||||
|
||||
std::shared_ptr<ClientReaderWriter<RouteNote, RouteNote> > stream( |
||||
stub_->RouteChat(&context)); |
||||
|
||||
std::thread writer([stream]() { |
||||
std::vector<RouteNote> notes{ |
||||
MakeRouteNote("First message", 0, 0), |
||||
MakeRouteNote("Second message", 0, 1), |
||||
MakeRouteNote("Third message", 1, 0), |
||||
MakeRouteNote("Fourth message", 0, 0)}; |
||||
for (const RouteNote& note : notes) { |
||||
std::cout << "Sending message " << note.message() |
||||
<< " at " << note.location().latitude() << ", " |
||||
<< note.location().longitude() << std::endl; |
||||
stream->Write(note); |
||||
} |
||||
stream->WritesDone(); |
||||
}); |
||||
|
||||
RouteNote server_note; |
||||
while (stream->Read(&server_note)) { |
||||
std::cout << "Got message " << server_note.message() |
||||
<< " at " << server_note.location().latitude() << ", " |
||||
<< server_note.location().longitude() << std::endl; |
||||
} |
||||
writer.join(); |
||||
Status status = stream->Finish(); |
||||
if (!status.ok()) { |
||||
std::cout << "RouteChat rpc failed." << std::endl; |
||||
} |
||||
} |
||||
|
||||
private: |
||||
|
||||
bool GetOneFeature(const Point& point, Feature* feature) { |
||||
ClientContext context; |
||||
Status status = stub_->GetFeature(&context, point, feature); |
||||
if (!status.ok()) { |
||||
std::cout << "GetFeature rpc failed." << std::endl; |
||||
return false; |
||||
} |
||||
if (!feature->has_location()) { |
||||
std::cout << "Server returns incomplete feature." << std::endl; |
||||
return false; |
||||
} |
||||
if (feature->name().empty()) { |
||||
std::cout << "Found no feature at " |
||||
<< feature->location().latitude()/kCoordFactor_ << ", " |
||||
<< feature->location().longitude()/kCoordFactor_ << std::endl; |
||||
} else { |
||||
std::cout << "Found feature called " << feature->name() << " at " |
||||
<< feature->location().latitude()/kCoordFactor_ << ", " |
||||
<< feature->location().longitude()/kCoordFactor_ << std::endl; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
const float kCoordFactor_ = 10000000.0; |
||||
std::unique_ptr<RouteGuide::Stub> stub_; |
||||
std::vector<Feature> feature_list_; |
||||
}; |
||||
|
||||
int main(int argc, char** argv) { |
||||
// Expect only arg: --db_path=path/to/route_guide_db.json.
|
||||
std::string db = examples::GetDbFileContent(argc, argv); |
||||
RouteGuideClient guide( |
||||
grpc::CreateChannel("localhost:50051", grpc::InsecureCredentials(), |
||||
ChannelArguments()), |
||||
db); |
||||
|
||||
std::cout << "-------------- GetFeature --------------" << std::endl; |
||||
guide.GetFeature(); |
||||
std::cout << "-------------- ListFeatures --------------" << std::endl; |
||||
guide.ListFeatures(); |
||||
std::cout << "-------------- RecordRoute --------------" << std::endl; |
||||
guide.RecordRoute(); |
||||
std::cout << "-------------- RouteChat --------------" << std::endl; |
||||
guide.RouteChat(); |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,601 @@ |
||||
[{ |
||||
"location": { |
||||
"latitude": 407838351, |
||||
"longitude": -746143763 |
||||
}, |
||||
"name": "Patriots Path, Mendham, NJ 07945, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 408122808, |
||||
"longitude": -743999179 |
||||
}, |
||||
"name": "101 New Jersey 10, Whippany, NJ 07981, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 413628156, |
||||
"longitude": -749015468 |
||||
}, |
||||
"name": "U.S. 6, Shohola, PA 18458, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 419999544, |
||||
"longitude": -740371136 |
||||
}, |
||||
"name": "5 Conners Road, Kingston, NY 12401, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 414008389, |
||||
"longitude": -743951297 |
||||
}, |
||||
"name": "Mid Hudson Psychiatric Center, New Hampton, NY 10958, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 419611318, |
||||
"longitude": -746524769 |
||||
}, |
||||
"name": "287 Flugertown Road, Livingston Manor, NY 12758, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 406109563, |
||||
"longitude": -742186778 |
||||
}, |
||||
"name": "4001 Tremley Point Road, Linden, NJ 07036, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 416802456, |
||||
"longitude": -742370183 |
||||
}, |
||||
"name": "352 South Mountain Road, Wallkill, NY 12589, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 412950425, |
||||
"longitude": -741077389 |
||||
}, |
||||
"name": "Bailey Turn Road, Harriman, NY 10926, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 412144655, |
||||
"longitude": -743949739 |
||||
}, |
||||
"name": "193-199 Wawayanda Road, Hewitt, NJ 07421, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 415736605, |
||||
"longitude": -742847522 |
||||
}, |
||||
"name": "406-496 Ward Avenue, Pine Bush, NY 12566, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 413843930, |
||||
"longitude": -740501726 |
||||
}, |
||||
"name": "162 Merrill Road, Highland Mills, NY 10930, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 410873075, |
||||
"longitude": -744459023 |
||||
}, |
||||
"name": "Clinton Road, West Milford, NJ 07480, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 412346009, |
||||
"longitude": -744026814 |
||||
}, |
||||
"name": "16 Old Brook Lane, Warwick, NY 10990, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 402948455, |
||||
"longitude": -747903913 |
||||
}, |
||||
"name": "3 Drake Lane, Pennington, NJ 08534, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 406337092, |
||||
"longitude": -740122226 |
||||
}, |
||||
"name": "6324 8th Avenue, Brooklyn, NY 11220, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 406421967, |
||||
"longitude": -747727624 |
||||
}, |
||||
"name": "1 Merck Access Road, Whitehouse Station, NJ 08889, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 416318082, |
||||
"longitude": -749677716 |
||||
}, |
||||
"name": "78-98 Schalck Road, Narrowsburg, NY 12764, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 415301720, |
||||
"longitude": -748416257 |
||||
}, |
||||
"name": "282 Lakeview Drive Road, Highland Lake, NY 12743, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 402647019, |
||||
"longitude": -747071791 |
||||
}, |
||||
"name": "330 Evelyn Avenue, Hamilton Township, NJ 08619, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 412567807, |
||||
"longitude": -741058078 |
||||
}, |
||||
"name": "New York State Reference Route 987E, Southfields, NY 10975, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 416855156, |
||||
"longitude": -744420597 |
||||
}, |
||||
"name": "103-271 Tempaloni Road, Ellenville, NY 12428, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 404663628, |
||||
"longitude": -744820157 |
||||
}, |
||||
"name": "1300 Airport Road, North Brunswick Township, NJ 08902, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 407113723, |
||||
"longitude": -749746483 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 402133926, |
||||
"longitude": -743613249 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 400273442, |
||||
"longitude": -741220915 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 411236786, |
||||
"longitude": -744070769 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 411633782, |
||||
"longitude": -746784970 |
||||
}, |
||||
"name": "211-225 Plains Road, Augusta, NJ 07822, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 415830701, |
||||
"longitude": -742952812 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 413447164, |
||||
"longitude": -748712898 |
||||
}, |
||||
"name": "165 Pedersen Ridge Road, Milford, PA 18337, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 405047245, |
||||
"longitude": -749800722 |
||||
}, |
||||
"name": "100-122 Locktown Road, Frenchtown, NJ 08825, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 418858923, |
||||
"longitude": -746156790 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 417951888, |
||||
"longitude": -748484944 |
||||
}, |
||||
"name": "650-652 Willi Hill Road, Swan Lake, NY 12783, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 407033786, |
||||
"longitude": -743977337 |
||||
}, |
||||
"name": "26 East 3rd Street, New Providence, NJ 07974, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 417548014, |
||||
"longitude": -740075041 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 410395868, |
||||
"longitude": -744972325 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 404615353, |
||||
"longitude": -745129803 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 406589790, |
||||
"longitude": -743560121 |
||||
}, |
||||
"name": "611 Lawrence Avenue, Westfield, NJ 07090, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 414653148, |
||||
"longitude": -740477477 |
||||
}, |
||||
"name": "18 Lannis Avenue, New Windsor, NY 12553, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 405957808, |
||||
"longitude": -743255336 |
||||
}, |
||||
"name": "82-104 Amherst Avenue, Colonia, NJ 07067, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 411733589, |
||||
"longitude": -741648093 |
||||
}, |
||||
"name": "170 Seven Lakes Drive, Sloatsburg, NY 10974, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 412676291, |
||||
"longitude": -742606606 |
||||
}, |
||||
"name": "1270 Lakes Road, Monroe, NY 10950, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 409224445, |
||||
"longitude": -748286738 |
||||
}, |
||||
"name": "509-535 Alphano Road, Great Meadows, NJ 07838, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 406523420, |
||||
"longitude": -742135517 |
||||
}, |
||||
"name": "652 Garden Street, Elizabeth, NJ 07202, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 401827388, |
||||
"longitude": -740294537 |
||||
}, |
||||
"name": "349 Sea Spray Court, Neptune City, NJ 07753, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 410564152, |
||||
"longitude": -743685054 |
||||
}, |
||||
"name": "13-17 Stanley Street, West Milford, NJ 07480, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 408472324, |
||||
"longitude": -740726046 |
||||
}, |
||||
"name": "47 Industrial Avenue, Teterboro, NJ 07608, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 412452168, |
||||
"longitude": -740214052 |
||||
}, |
||||
"name": "5 White Oak Lane, Stony Point, NY 10980, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 409146138, |
||||
"longitude": -746188906 |
||||
}, |
||||
"name": "Berkshire Valley Management Area Trail, Jefferson, NJ, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 404701380, |
||||
"longitude": -744781745 |
||||
}, |
||||
"name": "1007 Jersey Avenue, New Brunswick, NJ 08901, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 409642566, |
||||
"longitude": -746017679 |
||||
}, |
||||
"name": "6 East Emerald Isle Drive, Lake Hopatcong, NJ 07849, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 408031728, |
||||
"longitude": -748645385 |
||||
}, |
||||
"name": "1358-1474 New Jersey 57, Port Murray, NJ 07865, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 413700272, |
||||
"longitude": -742135189 |
||||
}, |
||||
"name": "367 Prospect Road, Chester, NY 10918, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 404310607, |
||||
"longitude": -740282632 |
||||
}, |
||||
"name": "10 Simon Lake Drive, Atlantic Highlands, NJ 07716, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 409319800, |
||||
"longitude": -746201391 |
||||
}, |
||||
"name": "11 Ward Street, Mount Arlington, NJ 07856, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 406685311, |
||||
"longitude": -742108603 |
||||
}, |
||||
"name": "300-398 Jefferson Avenue, Elizabeth, NJ 07201, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 419018117, |
||||
"longitude": -749142781 |
||||
}, |
||||
"name": "43 Dreher Road, Roscoe, NY 12776, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 412856162, |
||||
"longitude": -745148837 |
||||
}, |
||||
"name": "Swan Street, Pine Island, NY 10969, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 416560744, |
||||
"longitude": -746721964 |
||||
}, |
||||
"name": "66 Pleasantview Avenue, Monticello, NY 12701, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 405314270, |
||||
"longitude": -749836354 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 414219548, |
||||
"longitude": -743327440 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 415534177, |
||||
"longitude": -742900616 |
||||
}, |
||||
"name": "565 Winding Hills Road, Montgomery, NY 12549, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 406898530, |
||||
"longitude": -749127080 |
||||
}, |
||||
"name": "231 Rocky Run Road, Glen Gardner, NJ 08826, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 407586880, |
||||
"longitude": -741670168 |
||||
}, |
||||
"name": "100 Mount Pleasant Avenue, Newark, NJ 07104, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 400106455, |
||||
"longitude": -742870190 |
||||
}, |
||||
"name": "517-521 Huntington Drive, Manchester Township, NJ 08759, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 400066188, |
||||
"longitude": -746793294 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 418803880, |
||||
"longitude": -744102673 |
||||
}, |
||||
"name": "40 Mountain Road, Napanoch, NY 12458, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 414204288, |
||||
"longitude": -747895140 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 414777405, |
||||
"longitude": -740615601 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 415464475, |
||||
"longitude": -747175374 |
||||
}, |
||||
"name": "48 North Road, Forestburgh, NY 12777, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 404062378, |
||||
"longitude": -746376177 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 405688272, |
||||
"longitude": -749285130 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 400342070, |
||||
"longitude": -748788996 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 401809022, |
||||
"longitude": -744157964 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 404226644, |
||||
"longitude": -740517141 |
||||
}, |
||||
"name": "9 Thompson Avenue, Leonardo, NJ 07737, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 410322033, |
||||
"longitude": -747871659 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 407100674, |
||||
"longitude": -747742727 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 418811433, |
||||
"longitude": -741718005 |
||||
}, |
||||
"name": "213 Bush Road, Stone Ridge, NY 12484, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 415034302, |
||||
"longitude": -743850945 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 411349992, |
||||
"longitude": -743694161 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 404839914, |
||||
"longitude": -744759616 |
||||
}, |
||||
"name": "1-17 Bergen Court, New Brunswick, NJ 08901, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 414638017, |
||||
"longitude": -745957854 |
||||
}, |
||||
"name": "35 Oakland Valley Road, Cuddebackville, NY 12729, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 412127800, |
||||
"longitude": -740173578 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 401263460, |
||||
"longitude": -747964303 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 412843391, |
||||
"longitude": -749086026 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 418512773, |
||||
"longitude": -743067823 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 404318328, |
||||
"longitude": -740835638 |
||||
}, |
||||
"name": "42-102 Main Street, Belford, NJ 07718, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 419020746, |
||||
"longitude": -741172328 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 404080723, |
||||
"longitude": -746119569 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 401012643, |
||||
"longitude": -744035134 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 404306372, |
||||
"longitude": -741079661 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 403966326, |
||||
"longitude": -748519297 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 405002031, |
||||
"longitude": -748407866 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 409532885, |
||||
"longitude": -742200683 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 416851321, |
||||
"longitude": -742674555 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 406411633, |
||||
"longitude": -741722051 |
||||
}, |
||||
"name": "3387 Richmond Terrace, Staten Island, NY 10303, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 413069058, |
||||
"longitude": -744597778 |
||||
}, |
||||
"name": "261 Van Sickle Road, Goshen, NY 10924, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 418465462, |
||||
"longitude": -746859398 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 411733222, |
||||
"longitude": -744228360 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 410248224, |
||||
"longitude": -747127767 |
||||
}, |
||||
"name": "3 Hasta Way, Newton, NJ 07860, USA" |
||||
}] |
@ -0,0 +1,202 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#include <algorithm> |
||||
#include <chrono> |
||||
#include <cmath> |
||||
#include <iostream> |
||||
#include <memory> |
||||
#include <string> |
||||
|
||||
#include <grpc/grpc.h> |
||||
#include <grpc++/server.h> |
||||
#include <grpc++/server_builder.h> |
||||
#include <grpc++/server_context.h> |
||||
#include <grpc++/server_credentials.h> |
||||
#include "helper.h" |
||||
#include "route_guide.grpc.pb.h" |
||||
|
||||
using grpc::Server; |
||||
using grpc::ServerBuilder; |
||||
using grpc::ServerContext; |
||||
using grpc::ServerReader; |
||||
using grpc::ServerReaderWriter; |
||||
using grpc::ServerWriter; |
||||
using grpc::Status; |
||||
using examples::Point; |
||||
using examples::Feature; |
||||
using examples::Rectangle; |
||||
using examples::RouteSummary; |
||||
using examples::RouteNote; |
||||
using examples::RouteGuide; |
||||
using std::chrono::system_clock; |
||||
|
||||
|
||||
float ConvertToRadians(float num) { |
||||
return num * 3.1415926 /180; |
||||
} |
||||
|
||||
float GetDistance(const Point& start, const Point& end) { |
||||
const float kCoordFactor = 10000000.0; |
||||
float lat_1 = start.latitude() / kCoordFactor; |
||||
float lat_2 = end.latitude() / kCoordFactor; |
||||
float lon_1 = start.longitude() / kCoordFactor; |
||||
float lon_2 = end.longitude() / kCoordFactor; |
||||
float lat_rad_1 = ConvertToRadians(lat_1); |
||||
float lat_rad_2 = ConvertToRadians(lat_2); |
||||
float delta_lat_rad = ConvertToRadians(lat_2-lat_1); |
||||
float delta_lon_rad = ConvertToRadians(lon_2-lon_1); |
||||
|
||||
float a = pow(sin(delta_lat_rad/2), 2) + cos(lat_rad_1) * cos(lat_rad_2) * |
||||
pow(sin(delta_lon_rad/2), 2); |
||||
float c = 2 * atan2(sqrt(a), sqrt(1-a)); |
||||
int R = 6371000; // metres
|
||||
|
||||
return R * c; |
||||
} |
||||
|
||||
std::string GetFeatureName(const Point& point, |
||||
const std::vector<Feature>& feature_list) { |
||||
for (const Feature& f : feature_list) { |
||||
if (f.location().latitude() == point.latitude() && |
||||
f.location().longitude() == point.longitude()) { |
||||
return f.name(); |
||||
} |
||||
} |
||||
return ""; |
||||
} |
||||
|
||||
class RouteGuideImpl final : public RouteGuide::Service { |
||||
public: |
||||
explicit RouteGuideImpl(const std::string& db) { |
||||
examples::ParseDb(db, &feature_list_); |
||||
} |
||||
|
||||
Status GetFeature(ServerContext* context, const Point* point, |
||||
Feature* feature) override { |
||||
feature->set_name(GetFeatureName(*point, feature_list_)); |
||||
feature->mutable_location()->CopyFrom(*point); |
||||
return Status::OK; |
||||
} |
||||
|
||||
Status ListFeatures(ServerContext* context, |
||||
const examples::Rectangle* rectangle, |
||||
ServerWriter<Feature>* writer) override { |
||||
auto lo = rectangle->lo(); |
||||
auto hi = rectangle->hi(); |
||||
long left = (std::min)(lo.longitude(), hi.longitude()); |
||||
long right = (std::max)(lo.longitude(), hi.longitude()); |
||||
long top = (std::max)(lo.latitude(), hi.latitude()); |
||||
long bottom = (std::min)(lo.latitude(), hi.latitude()); |
||||
for (const Feature& f : feature_list_) { |
||||
if (f.location().longitude() >= left && |
||||
f.location().longitude() <= right && |
||||
f.location().latitude() >= bottom && |
||||
f.location().latitude() <= top) { |
||||
writer->Write(f); |
||||
} |
||||
} |
||||
return Status::OK; |
||||
} |
||||
|
||||
Status RecordRoute(ServerContext* context, ServerReader<Point>* reader, |
||||
RouteSummary* summary) override { |
||||
Point point; |
||||
int point_count = 0; |
||||
int feature_count = 0; |
||||
float distance = 0.0; |
||||
Point previous; |
||||
|
||||
system_clock::time_point start_time = system_clock::now(); |
||||
while (reader->Read(&point)) { |
||||
point_count++; |
||||
if (!GetFeatureName(point, feature_list_).empty()) { |
||||
feature_count++; |
||||
} |
||||
if (point_count != 1) { |
||||
distance += GetDistance(previous, point); |
||||
} |
||||
previous = point; |
||||
} |
||||
system_clock::time_point end_time = system_clock::now(); |
||||
summary->set_point_count(point_count); |
||||
summary->set_feature_count(feature_count); |
||||
summary->set_distance(static_cast<long>(distance)); |
||||
auto secs = std::chrono::duration_cast<std::chrono::seconds>( |
||||
end_time - start_time); |
||||
summary->set_elapsed_time(secs.count()); |
||||
|
||||
return Status::OK; |
||||
} |
||||
|
||||
Status RouteChat(ServerContext* context, |
||||
ServerReaderWriter<RouteNote, RouteNote>* stream) override { |
||||
std::vector<RouteNote> received_notes; |
||||
RouteNote note; |
||||
while (stream->Read(¬e)) { |
||||
for (const RouteNote& n : received_notes) { |
||||
if (n.location().latitude() == note.location().latitude() && |
||||
n.location().longitude() == note.location().longitude()) { |
||||
stream->Write(n); |
||||
} |
||||
} |
||||
received_notes.push_back(note); |
||||
} |
||||
|
||||
return Status::OK; |
||||
} |
||||
|
||||
private: |
||||
|
||||
std::vector<Feature> feature_list_; |
||||
}; |
||||
|
||||
void RunServer(const std::string& db_path) { |
||||
std::string server_address("0.0.0.0:50051"); |
||||
RouteGuideImpl service(db_path); |
||||
|
||||
ServerBuilder builder; |
||||
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); |
||||
builder.RegisterService(&service); |
||||
std::unique_ptr<Server> server(builder.BuildAndStart()); |
||||
std::cout << "Server listening on " << server_address << std::endl; |
||||
server->Wait(); |
||||
} |
||||
|
||||
int main(int argc, char** argv) { |
||||
// Expect only arg: --db_path=path/to/route_guide_db.json.
|
||||
std::string db = examples::GetDbFileContent(argc, argv); |
||||
RunServer(db); |
||||
|
||||
return 0; |
||||
} |
@ -0,0 +1,5 @@ |
||||
bin/ |
||||
obj/ |
||||
packages/ |
||||
*.suo |
||||
*.userprefs |
@ -0,0 +1,2 @@ |
||||
bin |
||||
obj |
@ -0,0 +1,617 @@ |
||||
// Generated by the protocol buffer compiler. DO NOT EDIT! |
||||
// source: helloworld.proto |
||||
#pragma warning disable 1591, 0612, 3021 |
||||
#region Designer generated code |
||||
|
||||
using pb = global::Google.ProtocolBuffers; |
||||
using pbc = global::Google.ProtocolBuffers.Collections; |
||||
using pbd = global::Google.ProtocolBuffers.Descriptors; |
||||
using scg = global::System.Collections.Generic; |
||||
namespace helloworld { |
||||
|
||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] |
||||
public static partial class Helloworld { |
||||
|
||||
#region Extension registration |
||||
public static void RegisterAllExtensions(pb::ExtensionRegistry registry) { |
||||
} |
||||
#endregion |
||||
#region Static variables |
||||
internal static pbd::MessageDescriptor internal__static_helloworld_HelloRequest__Descriptor; |
||||
internal static pb::FieldAccess.FieldAccessorTable<global::helloworld.HelloRequest, global::helloworld.HelloRequest.Builder> internal__static_helloworld_HelloRequest__FieldAccessorTable; |
||||
internal static pbd::MessageDescriptor internal__static_helloworld_HelloReply__Descriptor; |
||||
internal static pb::FieldAccess.FieldAccessorTable<global::helloworld.HelloReply, global::helloworld.HelloReply.Builder> internal__static_helloworld_HelloReply__FieldAccessorTable; |
||||
#endregion |
||||
#region Descriptor |
||||
public static pbd::FileDescriptor Descriptor { |
||||
get { return descriptor; } |
||||
} |
||||
private static pbd::FileDescriptor descriptor; |
||||
|
||||
static Helloworld() { |
||||
byte[] descriptorData = global::System.Convert.FromBase64String( |
||||
string.Concat( |
||||
"ChBoZWxsb3dvcmxkLnByb3RvEgpoZWxsb3dvcmxkIhwKDEhlbGxvUmVxdWVz", |
||||
"dBIMCgRuYW1lGAEgASgJIh0KCkhlbGxvUmVwbHkSDwoHbWVzc2FnZRgBIAEo", |
||||
"CTJJCgdHcmVldGVyEj4KCFNheUhlbGxvEhguaGVsbG93b3JsZC5IZWxsb1Jl", |
||||
"cXVlc3QaFi5oZWxsb3dvcmxkLkhlbGxvUmVwbHkiAEISChBpby5ncnBjLmV4", |
||||
"YW1wbGVz")); |
||||
pbd::FileDescriptor.InternalDescriptorAssigner assigner = delegate(pbd::FileDescriptor root) { |
||||
descriptor = root; |
||||
internal__static_helloworld_HelloRequest__Descriptor = Descriptor.MessageTypes[0]; |
||||
internal__static_helloworld_HelloRequest__FieldAccessorTable = |
||||
new pb::FieldAccess.FieldAccessorTable<global::helloworld.HelloRequest, global::helloworld.HelloRequest.Builder>(internal__static_helloworld_HelloRequest__Descriptor, |
||||
new string[] { "Name", }); |
||||
internal__static_helloworld_HelloReply__Descriptor = Descriptor.MessageTypes[1]; |
||||
internal__static_helloworld_HelloReply__FieldAccessorTable = |
||||
new pb::FieldAccess.FieldAccessorTable<global::helloworld.HelloReply, global::helloworld.HelloReply.Builder>(internal__static_helloworld_HelloReply__Descriptor, |
||||
new string[] { "Message", }); |
||||
pb::ExtensionRegistry registry = pb::ExtensionRegistry.CreateInstance(); |
||||
RegisterAllExtensions(registry); |
||||
return registry; |
||||
}; |
||||
pbd::FileDescriptor.InternalBuildGeneratedFileFrom(descriptorData, |
||||
new pbd::FileDescriptor[] { |
||||
}, assigner); |
||||
} |
||||
#endregion |
||||
|
||||
} |
||||
#region Messages |
||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] |
||||
public sealed partial class HelloRequest : pb::GeneratedMessage<HelloRequest, HelloRequest.Builder> { |
||||
private HelloRequest() { } |
||||
private static readonly HelloRequest defaultInstance = new HelloRequest().MakeReadOnly(); |
||||
private static readonly string[] _helloRequestFieldNames = new string[] { "name" }; |
||||
private static readonly uint[] _helloRequestFieldTags = new uint[] { 10 }; |
||||
public static HelloRequest DefaultInstance { |
||||
get { return defaultInstance; } |
||||
} |
||||
|
||||
public override HelloRequest DefaultInstanceForType { |
||||
get { return DefaultInstance; } |
||||
} |
||||
|
||||
protected override HelloRequest ThisMessage { |
||||
get { return this; } |
||||
} |
||||
|
||||
public static pbd::MessageDescriptor Descriptor { |
||||
get { return global::helloworld.Helloworld.internal__static_helloworld_HelloRequest__Descriptor; } |
||||
} |
||||
|
||||
protected override pb::FieldAccess.FieldAccessorTable<HelloRequest, HelloRequest.Builder> InternalFieldAccessors { |
||||
get { return global::helloworld.Helloworld.internal__static_helloworld_HelloRequest__FieldAccessorTable; } |
||||
} |
||||
|
||||
public const int NameFieldNumber = 1; |
||||
private bool hasName; |
||||
private string name_ = ""; |
||||
public bool HasName { |
||||
get { return hasName; } |
||||
} |
||||
public string Name { |
||||
get { return name_; } |
||||
} |
||||
|
||||
public override bool IsInitialized { |
||||
get { |
||||
return true; |
||||
} |
||||
} |
||||
|
||||
public override void WriteTo(pb::ICodedOutputStream output) { |
||||
CalcSerializedSize(); |
||||
string[] field_names = _helloRequestFieldNames; |
||||
if (hasName) { |
||||
output.WriteString(1, field_names[0], Name); |
||||
} |
||||
UnknownFields.WriteTo(output); |
||||
} |
||||
|
||||
private int memoizedSerializedSize = -1; |
||||
public override int SerializedSize { |
||||
get { |
||||
int size = memoizedSerializedSize; |
||||
if (size != -1) return size; |
||||
return CalcSerializedSize(); |
||||
} |
||||
} |
||||
|
||||
private int CalcSerializedSize() { |
||||
int size = memoizedSerializedSize; |
||||
if (size != -1) return size; |
||||
|
||||
size = 0; |
||||
if (hasName) { |
||||
size += pb::CodedOutputStream.ComputeStringSize(1, Name); |
||||
} |
||||
size += UnknownFields.SerializedSize; |
||||
memoizedSerializedSize = size; |
||||
return size; |
||||
} |
||||
public static HelloRequest ParseFrom(pb::ByteString data) { |
||||
return ((Builder) CreateBuilder().MergeFrom(data)).BuildParsed(); |
||||
} |
||||
public static HelloRequest ParseFrom(pb::ByteString data, pb::ExtensionRegistry extensionRegistry) { |
||||
return ((Builder) CreateBuilder().MergeFrom(data, extensionRegistry)).BuildParsed(); |
||||
} |
||||
public static HelloRequest ParseFrom(byte[] data) { |
||||
return ((Builder) CreateBuilder().MergeFrom(data)).BuildParsed(); |
||||
} |
||||
public static HelloRequest ParseFrom(byte[] data, pb::ExtensionRegistry extensionRegistry) { |
||||
return ((Builder) CreateBuilder().MergeFrom(data, extensionRegistry)).BuildParsed(); |
||||
} |
||||
public static HelloRequest ParseFrom(global::System.IO.Stream input) { |
||||
return ((Builder) CreateBuilder().MergeFrom(input)).BuildParsed(); |
||||
} |
||||
public static HelloRequest ParseFrom(global::System.IO.Stream input, pb::ExtensionRegistry extensionRegistry) { |
||||
return ((Builder) CreateBuilder().MergeFrom(input, extensionRegistry)).BuildParsed(); |
||||
} |
||||
public static HelloRequest ParseDelimitedFrom(global::System.IO.Stream input) { |
||||
return CreateBuilder().MergeDelimitedFrom(input).BuildParsed(); |
||||
} |
||||
public static HelloRequest ParseDelimitedFrom(global::System.IO.Stream input, pb::ExtensionRegistry extensionRegistry) { |
||||
return CreateBuilder().MergeDelimitedFrom(input, extensionRegistry).BuildParsed(); |
||||
} |
||||
public static HelloRequest ParseFrom(pb::ICodedInputStream input) { |
||||
return ((Builder) CreateBuilder().MergeFrom(input)).BuildParsed(); |
||||
} |
||||
public static HelloRequest ParseFrom(pb::ICodedInputStream input, pb::ExtensionRegistry extensionRegistry) { |
||||
return ((Builder) CreateBuilder().MergeFrom(input, extensionRegistry)).BuildParsed(); |
||||
} |
||||
private HelloRequest MakeReadOnly() { |
||||
return this; |
||||
} |
||||
|
||||
public static Builder CreateBuilder() { return new Builder(); } |
||||
public override Builder ToBuilder() { return CreateBuilder(this); } |
||||
public override Builder CreateBuilderForType() { return new Builder(); } |
||||
public static Builder CreateBuilder(HelloRequest prototype) { |
||||
return new Builder(prototype); |
||||
} |
||||
|
||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] |
||||
public sealed partial class Builder : pb::GeneratedBuilder<HelloRequest, Builder> { |
||||
protected override Builder ThisBuilder { |
||||
get { return this; } |
||||
} |
||||
public Builder() { |
||||
result = DefaultInstance; |
||||
resultIsReadOnly = true; |
||||
} |
||||
internal Builder(HelloRequest cloneFrom) { |
||||
result = cloneFrom; |
||||
resultIsReadOnly = true; |
||||
} |
||||
|
||||
private bool resultIsReadOnly; |
||||
private HelloRequest result; |
||||
|
||||
private HelloRequest PrepareBuilder() { |
||||
if (resultIsReadOnly) { |
||||
HelloRequest original = result; |
||||
result = new HelloRequest(); |
||||
resultIsReadOnly = false; |
||||
MergeFrom(original); |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
public override bool IsInitialized { |
||||
get { return result.IsInitialized; } |
||||
} |
||||
|
||||
protected override HelloRequest MessageBeingBuilt { |
||||
get { return PrepareBuilder(); } |
||||
} |
||||
|
||||
public override Builder Clear() { |
||||
result = DefaultInstance; |
||||
resultIsReadOnly = true; |
||||
return this; |
||||
} |
||||
|
||||
public override Builder Clone() { |
||||
if (resultIsReadOnly) { |
||||
return new Builder(result); |
||||
} else { |
||||
return new Builder().MergeFrom(result); |
||||
} |
||||
} |
||||
|
||||
public override pbd::MessageDescriptor DescriptorForType { |
||||
get { return global::helloworld.HelloRequest.Descriptor; } |
||||
} |
||||
|
||||
public override HelloRequest DefaultInstanceForType { |
||||
get { return global::helloworld.HelloRequest.DefaultInstance; } |
||||
} |
||||
|
||||
public override HelloRequest BuildPartial() { |
||||
if (resultIsReadOnly) { |
||||
return result; |
||||
} |
||||
resultIsReadOnly = true; |
||||
return result.MakeReadOnly(); |
||||
} |
||||
|
||||
public override Builder MergeFrom(pb::IMessage other) { |
||||
if (other is HelloRequest) { |
||||
return MergeFrom((HelloRequest) other); |
||||
} else { |
||||
base.MergeFrom(other); |
||||
return this; |
||||
} |
||||
} |
||||
|
||||
public override Builder MergeFrom(HelloRequest other) { |
||||
if (other == global::helloworld.HelloRequest.DefaultInstance) return this; |
||||
PrepareBuilder(); |
||||
if (other.HasName) { |
||||
Name = other.Name; |
||||
} |
||||
this.MergeUnknownFields(other.UnknownFields); |
||||
return this; |
||||
} |
||||
|
||||
public override Builder MergeFrom(pb::ICodedInputStream input) { |
||||
return MergeFrom(input, pb::ExtensionRegistry.Empty); |
||||
} |
||||
|
||||
public override Builder MergeFrom(pb::ICodedInputStream input, pb::ExtensionRegistry extensionRegistry) { |
||||
PrepareBuilder(); |
||||
pb::UnknownFieldSet.Builder unknownFields = null; |
||||
uint tag; |
||||
string field_name; |
||||
while (input.ReadTag(out tag, out field_name)) { |
||||
if(tag == 0 && field_name != null) { |
||||
int field_ordinal = global::System.Array.BinarySearch(_helloRequestFieldNames, field_name, global::System.StringComparer.Ordinal); |
||||
if(field_ordinal >= 0) |
||||
tag = _helloRequestFieldTags[field_ordinal]; |
||||
else { |
||||
if (unknownFields == null) { |
||||
unknownFields = pb::UnknownFieldSet.CreateBuilder(this.UnknownFields); |
||||
} |
||||
ParseUnknownField(input, unknownFields, extensionRegistry, tag, field_name); |
||||
continue; |
||||
} |
||||
} |
||||
switch (tag) { |
||||
case 0: { |
||||
throw pb::InvalidProtocolBufferException.InvalidTag(); |
||||
} |
||||
default: { |
||||
if (pb::WireFormat.IsEndGroupTag(tag)) { |
||||
if (unknownFields != null) { |
||||
this.UnknownFields = unknownFields.Build(); |
||||
} |
||||
return this; |
||||
} |
||||
if (unknownFields == null) { |
||||
unknownFields = pb::UnknownFieldSet.CreateBuilder(this.UnknownFields); |
||||
} |
||||
ParseUnknownField(input, unknownFields, extensionRegistry, tag, field_name); |
||||
break; |
||||
} |
||||
case 10: { |
||||
result.hasName = input.ReadString(ref result.name_); |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
|
||||
if (unknownFields != null) { |
||||
this.UnknownFields = unknownFields.Build(); |
||||
} |
||||
return this; |
||||
} |
||||
|
||||
|
||||
public bool HasName { |
||||
get { return result.hasName; } |
||||
} |
||||
public string Name { |
||||
get { return result.Name; } |
||||
set { SetName(value); } |
||||
} |
||||
public Builder SetName(string value) { |
||||
pb::ThrowHelper.ThrowIfNull(value, "value"); |
||||
PrepareBuilder(); |
||||
result.hasName = true; |
||||
result.name_ = value; |
||||
return this; |
||||
} |
||||
public Builder ClearName() { |
||||
PrepareBuilder(); |
||||
result.hasName = false; |
||||
result.name_ = ""; |
||||
return this; |
||||
} |
||||
} |
||||
static HelloRequest() { |
||||
object.ReferenceEquals(global::helloworld.Helloworld.Descriptor, null); |
||||
} |
||||
} |
||||
|
||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] |
||||
public sealed partial class HelloReply : pb::GeneratedMessage<HelloReply, HelloReply.Builder> { |
||||
private HelloReply() { } |
||||
private static readonly HelloReply defaultInstance = new HelloReply().MakeReadOnly(); |
||||
private static readonly string[] _helloReplyFieldNames = new string[] { "message" }; |
||||
private static readonly uint[] _helloReplyFieldTags = new uint[] { 10 }; |
||||
public static HelloReply DefaultInstance { |
||||
get { return defaultInstance; } |
||||
} |
||||
|
||||
public override HelloReply DefaultInstanceForType { |
||||
get { return DefaultInstance; } |
||||
} |
||||
|
||||
protected override HelloReply ThisMessage { |
||||
get { return this; } |
||||
} |
||||
|
||||
public static pbd::MessageDescriptor Descriptor { |
||||
get { return global::helloworld.Helloworld.internal__static_helloworld_HelloReply__Descriptor; } |
||||
} |
||||
|
||||
protected override pb::FieldAccess.FieldAccessorTable<HelloReply, HelloReply.Builder> InternalFieldAccessors { |
||||
get { return global::helloworld.Helloworld.internal__static_helloworld_HelloReply__FieldAccessorTable; } |
||||
} |
||||
|
||||
public const int MessageFieldNumber = 1; |
||||
private bool hasMessage; |
||||
private string message_ = ""; |
||||
public bool HasMessage { |
||||
get { return hasMessage; } |
||||
} |
||||
public string Message { |
||||
get { return message_; } |
||||
} |
||||
|
||||
public override bool IsInitialized { |
||||
get { |
||||
return true; |
||||
} |
||||
} |
||||
|
||||
public override void WriteTo(pb::ICodedOutputStream output) { |
||||
CalcSerializedSize(); |
||||
string[] field_names = _helloReplyFieldNames; |
||||
if (hasMessage) { |
||||
output.WriteString(1, field_names[0], Message); |
||||
} |
||||
UnknownFields.WriteTo(output); |
||||
} |
||||
|
||||
private int memoizedSerializedSize = -1; |
||||
public override int SerializedSize { |
||||
get { |
||||
int size = memoizedSerializedSize; |
||||
if (size != -1) return size; |
||||
return CalcSerializedSize(); |
||||
} |
||||
} |
||||
|
||||
private int CalcSerializedSize() { |
||||
int size = memoizedSerializedSize; |
||||
if (size != -1) return size; |
||||
|
||||
size = 0; |
||||
if (hasMessage) { |
||||
size += pb::CodedOutputStream.ComputeStringSize(1, Message); |
||||
} |
||||
size += UnknownFields.SerializedSize; |
||||
memoizedSerializedSize = size; |
||||
return size; |
||||
} |
||||
public static HelloReply ParseFrom(pb::ByteString data) { |
||||
return ((Builder) CreateBuilder().MergeFrom(data)).BuildParsed(); |
||||
} |
||||
public static HelloReply ParseFrom(pb::ByteString data, pb::ExtensionRegistry extensionRegistry) { |
||||
return ((Builder) CreateBuilder().MergeFrom(data, extensionRegistry)).BuildParsed(); |
||||
} |
||||
public static HelloReply ParseFrom(byte[] data) { |
||||
return ((Builder) CreateBuilder().MergeFrom(data)).BuildParsed(); |
||||
} |
||||
public static HelloReply ParseFrom(byte[] data, pb::ExtensionRegistry extensionRegistry) { |
||||
return ((Builder) CreateBuilder().MergeFrom(data, extensionRegistry)).BuildParsed(); |
||||
} |
||||
public static HelloReply ParseFrom(global::System.IO.Stream input) { |
||||
return ((Builder) CreateBuilder().MergeFrom(input)).BuildParsed(); |
||||
} |
||||
public static HelloReply ParseFrom(global::System.IO.Stream input, pb::ExtensionRegistry extensionRegistry) { |
||||
return ((Builder) CreateBuilder().MergeFrom(input, extensionRegistry)).BuildParsed(); |
||||
} |
||||
public static HelloReply ParseDelimitedFrom(global::System.IO.Stream input) { |
||||
return CreateBuilder().MergeDelimitedFrom(input).BuildParsed(); |
||||
} |
||||
public static HelloReply ParseDelimitedFrom(global::System.IO.Stream input, pb::ExtensionRegistry extensionRegistry) { |
||||
return CreateBuilder().MergeDelimitedFrom(input, extensionRegistry).BuildParsed(); |
||||
} |
||||
public static HelloReply ParseFrom(pb::ICodedInputStream input) { |
||||
return ((Builder) CreateBuilder().MergeFrom(input)).BuildParsed(); |
||||
} |
||||
public static HelloReply ParseFrom(pb::ICodedInputStream input, pb::ExtensionRegistry extensionRegistry) { |
||||
return ((Builder) CreateBuilder().MergeFrom(input, extensionRegistry)).BuildParsed(); |
||||
} |
||||
private HelloReply MakeReadOnly() { |
||||
return this; |
||||
} |
||||
|
||||
public static Builder CreateBuilder() { return new Builder(); } |
||||
public override Builder ToBuilder() { return CreateBuilder(this); } |
||||
public override Builder CreateBuilderForType() { return new Builder(); } |
||||
public static Builder CreateBuilder(HelloReply prototype) { |
||||
return new Builder(prototype); |
||||
} |
||||
|
||||
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] |
||||
public sealed partial class Builder : pb::GeneratedBuilder<HelloReply, Builder> { |
||||
protected override Builder ThisBuilder { |
||||
get { return this; } |
||||
} |
||||
public Builder() { |
||||
result = DefaultInstance; |
||||
resultIsReadOnly = true; |
||||
} |
||||
internal Builder(HelloReply cloneFrom) { |
||||
result = cloneFrom; |
||||
resultIsReadOnly = true; |
||||
} |
||||
|
||||
private bool resultIsReadOnly; |
||||
private HelloReply result; |
||||
|
||||
private HelloReply PrepareBuilder() { |
||||
if (resultIsReadOnly) { |
||||
HelloReply original = result; |
||||
result = new HelloReply(); |
||||
resultIsReadOnly = false; |
||||
MergeFrom(original); |
||||
} |
||||
return result; |
||||
} |
||||
|
||||
public override bool IsInitialized { |
||||
get { return result.IsInitialized; } |
||||
} |
||||
|
||||
protected override HelloReply MessageBeingBuilt { |
||||
get { return PrepareBuilder(); } |
||||
} |
||||
|
||||
public override Builder Clear() { |
||||
result = DefaultInstance; |
||||
resultIsReadOnly = true; |
||||
return this; |
||||
} |
||||
|
||||
public override Builder Clone() { |
||||
if (resultIsReadOnly) { |
||||
return new Builder(result); |
||||
} else { |
||||
return new Builder().MergeFrom(result); |
||||
} |
||||
} |
||||
|
||||
public override pbd::MessageDescriptor DescriptorForType { |
||||
get { return global::helloworld.HelloReply.Descriptor; } |
||||
} |
||||
|
||||
public override HelloReply DefaultInstanceForType { |
||||
get { return global::helloworld.HelloReply.DefaultInstance; } |
||||
} |
||||
|
||||
public override HelloReply BuildPartial() { |
||||
if (resultIsReadOnly) { |
||||
return result; |
||||
} |
||||
resultIsReadOnly = true; |
||||
return result.MakeReadOnly(); |
||||
} |
||||
|
||||
public override Builder MergeFrom(pb::IMessage other) { |
||||
if (other is HelloReply) { |
||||
return MergeFrom((HelloReply) other); |
||||
} else { |
||||
base.MergeFrom(other); |
||||
return this; |
||||
} |
||||
} |
||||
|
||||
public override Builder MergeFrom(HelloReply other) { |
||||
if (other == global::helloworld.HelloReply.DefaultInstance) return this; |
||||
PrepareBuilder(); |
||||
if (other.HasMessage) { |
||||
Message = other.Message; |
||||
} |
||||
this.MergeUnknownFields(other.UnknownFields); |
||||
return this; |
||||
} |
||||
|
||||
public override Builder MergeFrom(pb::ICodedInputStream input) { |
||||
return MergeFrom(input, pb::ExtensionRegistry.Empty); |
||||
} |
||||
|
||||
public override Builder MergeFrom(pb::ICodedInputStream input, pb::ExtensionRegistry extensionRegistry) { |
||||
PrepareBuilder(); |
||||
pb::UnknownFieldSet.Builder unknownFields = null; |
||||
uint tag; |
||||
string field_name; |
||||
while (input.ReadTag(out tag, out field_name)) { |
||||
if(tag == 0 && field_name != null) { |
||||
int field_ordinal = global::System.Array.BinarySearch(_helloReplyFieldNames, field_name, global::System.StringComparer.Ordinal); |
||||
if(field_ordinal >= 0) |
||||
tag = _helloReplyFieldTags[field_ordinal]; |
||||
else { |
||||
if (unknownFields == null) { |
||||
unknownFields = pb::UnknownFieldSet.CreateBuilder(this.UnknownFields); |
||||
} |
||||
ParseUnknownField(input, unknownFields, extensionRegistry, tag, field_name); |
||||
continue; |
||||
} |
||||
} |
||||
switch (tag) { |
||||
case 0: { |
||||
throw pb::InvalidProtocolBufferException.InvalidTag(); |
||||
} |
||||
default: { |
||||
if (pb::WireFormat.IsEndGroupTag(tag)) { |
||||
if (unknownFields != null) { |
||||
this.UnknownFields = unknownFields.Build(); |
||||
} |
||||
return this; |
||||
} |
||||
if (unknownFields == null) { |
||||
unknownFields = pb::UnknownFieldSet.CreateBuilder(this.UnknownFields); |
||||
} |
||||
ParseUnknownField(input, unknownFields, extensionRegistry, tag, field_name); |
||||
break; |
||||
} |
||||
case 10: { |
||||
result.hasMessage = input.ReadString(ref result.message_); |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
|
||||
if (unknownFields != null) { |
||||
this.UnknownFields = unknownFields.Build(); |
||||
} |
||||
return this; |
||||
} |
||||
|
||||
|
||||
public bool HasMessage { |
||||
get { return result.hasMessage; } |
||||
} |
||||
public string Message { |
||||
get { return result.Message; } |
||||
set { SetMessage(value); } |
||||
} |
||||
public Builder SetMessage(string value) { |
||||
pb::ThrowHelper.ThrowIfNull(value, "value"); |
||||
PrepareBuilder(); |
||||
result.hasMessage = true; |
||||
result.message_ = value; |
||||
return this; |
||||
} |
||||
public Builder ClearMessage() { |
||||
PrepareBuilder(); |
||||
result.hasMessage = false; |
||||
result.message_ = ""; |
||||
return this; |
||||
} |
||||
} |
||||
static HelloReply() { |
||||
object.ReferenceEquals(global::helloworld.Helloworld.Descriptor, null); |
||||
} |
||||
} |
||||
|
||||
#endregion |
||||
|
||||
} |
||||
|
||||
#endregion Designer generated code |
@ -0,0 +1,78 @@ |
||||
// Generated by the protocol buffer compiler. DO NOT EDIT! |
||||
// source: helloworld.proto |
||||
#region Designer generated code |
||||
|
||||
using System; |
||||
using System.Threading; |
||||
using System.Threading.Tasks; |
||||
using Grpc.Core; |
||||
|
||||
namespace helloworld { |
||||
public static class Greeter |
||||
{ |
||||
static readonly string __ServiceName = "helloworld.Greeter"; |
||||
|
||||
static readonly Marshaller<global::helloworld.HelloRequest> __Marshaller_HelloRequest = Marshallers.Create((arg) => arg.ToByteArray(), global::helloworld.HelloRequest.ParseFrom); |
||||
static readonly Marshaller<global::helloworld.HelloReply> __Marshaller_HelloReply = Marshallers.Create((arg) => arg.ToByteArray(), global::helloworld.HelloReply.ParseFrom); |
||||
|
||||
static readonly Method<global::helloworld.HelloRequest, global::helloworld.HelloReply> __Method_SayHello = new Method<global::helloworld.HelloRequest, global::helloworld.HelloReply>( |
||||
MethodType.Unary, |
||||
"SayHello", |
||||
__Marshaller_HelloRequest, |
||||
__Marshaller_HelloReply); |
||||
|
||||
// client-side stub interface |
||||
public interface IGreeterClient |
||||
{ |
||||
global::helloworld.HelloReply SayHello(global::helloworld.HelloRequest request, CancellationToken token = default(CancellationToken)); |
||||
Task<global::helloworld.HelloReply> SayHelloAsync(global::helloworld.HelloRequest request, CancellationToken token = default(CancellationToken)); |
||||
} |
||||
|
||||
// server-side interface |
||||
public interface IGreeter |
||||
{ |
||||
Task<global::helloworld.HelloReply> SayHello(ServerCallContext context, global::helloworld.HelloRequest request); |
||||
} |
||||
|
||||
// client stub |
||||
public class GreeterClient : AbstractStub<GreeterClient, StubConfiguration>, IGreeterClient |
||||
{ |
||||
public GreeterClient(Channel channel) : this(channel, StubConfiguration.Default) |
||||
{ |
||||
} |
||||
public GreeterClient(Channel channel, StubConfiguration config) : base(channel, config) |
||||
{ |
||||
} |
||||
public global::helloworld.HelloReply SayHello(global::helloworld.HelloRequest request, CancellationToken token = default(CancellationToken)) |
||||
{ |
||||
var call = CreateCall(__ServiceName, __Method_SayHello); |
||||
return Calls.BlockingUnaryCall(call, request, token); |
||||
} |
||||
public Task<global::helloworld.HelloReply> SayHelloAsync(global::helloworld.HelloRequest request, CancellationToken token = default(CancellationToken)) |
||||
{ |
||||
var call = CreateCall(__ServiceName, __Method_SayHello); |
||||
return Calls.AsyncUnaryCall(call, request, token); |
||||
} |
||||
} |
||||
|
||||
// creates service definition that can be registered with a server |
||||
public static ServerServiceDefinition BindService(IGreeter serviceImpl) |
||||
{ |
||||
return ServerServiceDefinition.CreateBuilder(__ServiceName) |
||||
.AddMethod(__Method_SayHello, serviceImpl.SayHello).Build(); |
||||
} |
||||
|
||||
// creates a new client stub |
||||
public static IGreeterClient NewStub(Channel channel) |
||||
{ |
||||
return new GreeterClient(channel); |
||||
} |
||||
|
||||
// creates a new client stub |
||||
public static IGreeterClient NewStub(Channel channel, StubConfiguration config) |
||||
{ |
||||
return new GreeterClient(channel, config); |
||||
} |
||||
} |
||||
} |
||||
#endregion |
@ -0,0 +1,22 @@ |
||||
using System.Reflection; |
||||
using System.Runtime.CompilerServices; |
||||
|
||||
// Information about this assembly is defined by the following attributes. |
||||
// Change them to the values specific to your project. |
||||
[assembly: AssemblyTitle("Greeter")] |
||||
[assembly: AssemblyDescription("")] |
||||
[assembly: AssemblyConfiguration("")] |
||||
[assembly: AssemblyCompany("")] |
||||
[assembly: AssemblyProduct("")] |
||||
[assembly: AssemblyCopyright("jtattermusch")] |
||||
[assembly: AssemblyTrademark("")] |
||||
[assembly: AssemblyCulture("")] |
||||
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". |
||||
// The form "{Major}.{Minor}.*" will automatically update the build and revision, |
||||
// and "{Major}.{Minor}.{Build}.*" will update just the revision. |
||||
[assembly: AssemblyVersion("1.0.*")] |
||||
// The following attributes are used to specify the signing key for the assembly, |
||||
// if desired. See the Mono documentation for more information about signing. |
||||
//[assembly: AssemblyDelaySign(false)] |
||||
//[assembly: AssemblyKeyFile("")] |
||||
|
@ -0,0 +1,52 @@ |
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
// TODO(jtattermusch): as of now, C# protobufs don't officially support |
||||
// proto3. |
||||
syntax = "proto2"; |
||||
|
||||
option java_package = "io.grpc.examples"; |
||||
|
||||
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 { |
||||
optional string name = 1; |
||||
} |
||||
|
||||
// The response message containing the greetings |
||||
message HelloReply { |
||||
optional string message = 1; |
||||
} |
@ -0,0 +1,2 @@ |
||||
bin |
||||
obj |
@ -0,0 +1,25 @@ |
||||
using System; |
||||
using Grpc.Core; |
||||
using helloworld; |
||||
|
||||
namespace GreeterClient |
||||
{ |
||||
class ClientMainClass |
||||
{ |
||||
public static void Main(string[] args) |
||||
{ |
||||
GrpcEnvironment.Initialize(); |
||||
|
||||
using (Channel channel = new Channel("127.0.0.1:50051")) |
||||
{ |
||||
var client = Greeter.NewStub(channel); |
||||
String user = "you"; |
||||
|
||||
var reply = client.SayHello(new HelloRequest.Builder { Name = user }.Build()); |
||||
Console.WriteLine("Greeting: " + reply.Message); |
||||
} |
||||
|
||||
GrpcEnvironment.Shutdown(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,22 @@ |
||||
using System.Reflection; |
||||
using System.Runtime.CompilerServices; |
||||
|
||||
// Information about this assembly is defined by the following attributes. |
||||
// Change them to the values specific to your project. |
||||
[assembly: AssemblyTitle("GreeterClient")] |
||||
[assembly: AssemblyDescription("")] |
||||
[assembly: AssemblyConfiguration("")] |
||||
[assembly: AssemblyCompany("")] |
||||
[assembly: AssemblyProduct("")] |
||||
[assembly: AssemblyCopyright("jtattermusch")] |
||||
[assembly: AssemblyTrademark("")] |
||||
[assembly: AssemblyCulture("")] |
||||
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". |
||||
// The form "{Major}.{Minor}.*" will automatically update the build and revision, |
||||
// and "{Major}.{Minor}.{Build}.*" will update just the revision. |
||||
[assembly: AssemblyVersion("1.0.*")] |
||||
// The following attributes are used to specify the signing key for the assembly, |
||||
// if desired. See the Mono documentation for more information about signing. |
||||
//[assembly: AssemblyDelaySign(false)] |
||||
//[assembly: AssemblyKeyFile("")] |
||||
|
@ -0,0 +1,2 @@ |
||||
bin |
||||
obj |
@ -0,0 +1,37 @@ |
||||
using System; |
||||
using System.Threading.Tasks; |
||||
using Grpc.Core; |
||||
using helloworld; |
||||
|
||||
namespace GreeterServer |
||||
{ |
||||
class GreeterImpl : Greeter.IGreeter |
||||
{ |
||||
// Server side handler of the SayHello RPC |
||||
public Task<HelloReply> SayHello(ServerCallContext context, HelloRequest request) |
||||
{ |
||||
var reply = new HelloReply.Builder { Message = "Hello " + request.Name }.Build(); |
||||
return Task.FromResult(reply); |
||||
} |
||||
} |
||||
|
||||
class ServerMainClass |
||||
{ |
||||
public static void Main(string[] args) |
||||
{ |
||||
GrpcEnvironment.Initialize(); |
||||
|
||||
Server server = new Server(); |
||||
server.AddServiceDefinition(Greeter.BindService(new GreeterImpl())); |
||||
int port = server.AddListeningPort("localhost", 50051); |
||||
server.Start(); |
||||
|
||||
Console.WriteLine("Greeter server listening on port " + port); |
||||
Console.WriteLine("Press any key to stop the server..."); |
||||
Console.ReadKey(); |
||||
|
||||
server.ShutdownAsync().Wait(); |
||||
GrpcEnvironment.Shutdown(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,22 @@ |
||||
using System.Reflection; |
||||
using System.Runtime.CompilerServices; |
||||
|
||||
// Information about this assembly is defined by the following attributes. |
||||
// Change them to the values specific to your project. |
||||
[assembly: AssemblyTitle("GreeterServer")] |
||||
[assembly: AssemblyDescription("")] |
||||
[assembly: AssemblyConfiguration("")] |
||||
[assembly: AssemblyCompany("")] |
||||
[assembly: AssemblyProduct("")] |
||||
[assembly: AssemblyCopyright("jtattermusch")] |
||||
[assembly: AssemblyTrademark("")] |
||||
[assembly: AssemblyCulture("")] |
||||
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". |
||||
// The form "{Major}.{Minor}.*" will automatically update the build and revision, |
||||
// and "{Major}.{Minor}.{Build}.*" will update just the revision. |
||||
[assembly: AssemblyVersion("1.0.*")] |
||||
// The following attributes are used to specify the signing key for the assembly, |
||||
// if desired. See the Mono documentation for more information about signing. |
||||
//[assembly: AssemblyDelaySign(false)] |
||||
//[assembly: AssemblyKeyFile("")] |
||||
|
@ -0,0 +1,72 @@ |
||||
gRPC in 3 minutes (C#) |
||||
======================== |
||||
|
||||
BACKGROUND |
||||
------------- |
||||
For this sample, we've already generated the server and client stubs from `helloworld.proto`. |
||||
Example projects depend on NuGet packages `Grpc` and `Google.ProtocolBuffers` which have been already added to the project for you. |
||||
|
||||
PREREQUISITES |
||||
------------- |
||||
**Windows** |
||||
- .NET 4.5+ |
||||
- VS 2013 (with NuGet plugin installed) |
||||
|
||||
**Linux (Mono)** |
||||
- Mono |
||||
- Monodevelop 5.9 with NuGet Add-in installed (older versions might work) |
||||
|
||||
**MacOS (Mono)** |
||||
- Xamarin Studio (with NuGet plugin installed) |
||||
|
||||
BUILD |
||||
------- |
||||
|
||||
**Windows** |
||||
- Clone this repository. |
||||
|
||||
- Open solution `Greeter.sln` with Visual Studio |
||||
|
||||
- Build the solution (this will automatically download NuGet dependencies) |
||||
|
||||
**Linux (Mono)** |
||||
- Clone this repository. |
||||
|
||||
- Install gRPC C Core using instructions in https://github.com/grpc/homebrew-grpc |
||||
|
||||
- gRPC C# depends on native shared library `libgrpc_csharp_ext.so`. To make it visible |
||||
to Mono runtime, follow instructions in [Using gRPC C# on Linux](https://github.com/grpc/grpc/tree/master/src/csharp#usage-linux-mono) |
||||
|
||||
- Open solution `Greeter.sln` in MonoDevelop (you need to manually restore dependencies by using `mono nuget.exe restore` if you don't have NuGet add-in) |
||||
|
||||
- Build the solution. |
||||
|
||||
**MacOS (Mono)** |
||||
- See [Using gRPC C# on MacOS](https://github.com/grpc/grpc/tree/master/src/csharp#usage-macos-mono) for more info |
||||
on MacOS support. |
||||
|
||||
Try it! |
||||
------- |
||||
|
||||
- Run the server |
||||
|
||||
``` |
||||
> cd GreeterServer/bin/Debug |
||||
> GreeterServer.exe |
||||
``` |
||||
|
||||
- Run the client |
||||
|
||||
``` |
||||
> cd GreeterClient/bin/Debug |
||||
> GreeterClient.exe |
||||
``` |
||||
|
||||
You can also run the server and client directly from Visual Studio. |
||||
|
||||
On Linux or Mac, use `mono GreeterServer.exe` and `mono GreeterClient.exe` to run the server and client. |
||||
|
||||
Tutorial |
||||
-------- |
||||
|
||||
You can find a more detailed tutorial in [gRPC Basics: C#](examples/csharp/route_guide/README.md) |
@ -0,0 +1,5 @@ |
||||
bin/ |
||||
obj/ |
||||
packages/ |
||||
*.suo |
||||
*.userprefs |
@ -0,0 +1,409 @@ |
||||
#gRPC Basics: C# # |
||||
|
||||
This tutorial provides a basic C# programmer's introduction to working with gRPC. By walking through this example you'll learn how to: |
||||
|
||||
- Define a service in a .proto file. |
||||
- Generate server and client code using the protocol buffer compiler. |
||||
- Use the C# gRPC API to write a simple client and server for your service. |
||||
|
||||
It assumes that you have read the [Getting started](https://github.com/grpc/grpc/tree/master/examples) guide and are familiar with [protocol buffers] (https://developers.google.com/protocol-buffers/docs/overview). Note that the example in this tutorial only uses the proto2 version of the protocol buffers language, as proto3 support for C# is not ready yet (see [protobuf C# README](https://github.com/google/protobuf/tree/master/csharp#proto2--proto3)). |
||||
|
||||
This isn't a comprehensive guide to using gRPC in C#: more reference documentation is coming soon. |
||||
|
||||
## Why use gRPC? |
||||
|
||||
Our example is a simple route mapping application that lets clients get information about features on their route, create a summary of their route, and exchange route information such as traffic updates with the server and other clients. |
||||
|
||||
With gRPC we can define our service once in a .proto file and implement clients and servers in any of gRPC's supported languages, which in turn can be run in environments ranging from servers inside Google to your own tablet - all the complexity of communication between different languages and environments is handled for you by gRPC. We also get all the advantages of working with protocol buffers, including efficient serialization, a simple IDL, and easy interface updating. |
||||
|
||||
## Example code and setup |
||||
|
||||
The example code for our tutorial is in [examples/csharp/route_guide](examples/csharp/route_guide). To download the example, clone this repository by running the following command: |
||||
```shell |
||||
$ git clone https://github.com/grpc/grpc.git |
||||
``` |
||||
|
||||
All the files for this tutorial are in the directory `examples/csharp/route_guide`. |
||||
Open the solution `examples/csharp/route_guide/RouteGuide.sln` from Visual Studio (or Monodevelop on Linux). |
||||
|
||||
On Windows, you should not need to do anything besides opening the solution. All the needed dependencies will be restored |
||||
for you automatically by the `Grpc` NuGet package upon building the solution. |
||||
|
||||
On Linux (or MacOS), you will first need to install protobuf and gRPC C Core using Linuxbrew (or Homebrew) tool in order to be |
||||
able to generate the server and client interface code and run the examples. Follow the instructions for [Linux](https://github.com/grpc/grpc/tree/master/src/csharp#usage-linux-mono) or [MacOS](https://github.com/grpc/grpc/tree/master/src/csharp#usage-macos-mono). |
||||
|
||||
## Defining the service |
||||
|
||||
Our first step (as you'll know from [Getting started](https://github.com/grpc/grpc/tree/master/examples)) is to define the gRPC *service* and the method *request* and *response* types using [protocol buffers] (https://developers.google.com/protocol-buffers/docs/overview). You can see the complete .proto file in [`examples/csharp/route_guide/RouteGuide/protos/route_guide.proto`](examples/csharp/route_guide/RouteGuide/protos/route_guide.proto). |
||||
|
||||
To define a service, you specify a named `service` in your .proto file: |
||||
|
||||
```protobuf |
||||
service RouteGuide { |
||||
... |
||||
} |
||||
``` |
||||
|
||||
Then you define `rpc` methods inside your service definition, specifying their request and response types. gRPC lets you define four kinds of service method, all of which are used in the `RouteGuide` service: |
||||
|
||||
- A *simple RPC* where the client sends a request to the server using the stub and waits for a response to come back, just like a normal function call. |
||||
```protobuf |
||||
// Obtains the feature at a given position. |
||||
rpc GetFeature(Point) returns (Feature) {} |
||||
``` |
||||
|
||||
- A *server-side streaming RPC* where the client sends a request to the server and gets a stream to read a sequence of messages back. The client reads from the returned stream until there are no more messages. As you can see in our example, you specify a server-side streaming method by placing the `stream` keyword before the *response* type. |
||||
```protobuf |
||||
// Obtains the Features available within the given Rectangle. Results are |
||||
// streamed rather than returned at once (e.g. in a response message with a |
||||
// repeated field), as the rectangle may cover a large area and contain a |
||||
// huge number of features. |
||||
rpc ListFeatures(Rectangle) returns (stream Feature) {} |
||||
``` |
||||
|
||||
- A *client-side streaming RPC* where the client writes a sequence of messages and sends them to the server, again using a provided stream. Once the client has finished writing the messages, it waits for the server to read them all and return its response. You specify a server-side streaming method by placing the `stream` keyword before the *request* type. |
||||
```protobuf |
||||
// Accepts a stream of Points on a route being traversed, returning a |
||||
// RouteSummary when traversal is completed. |
||||
rpc RecordRoute(stream Point) returns (RouteSummary) {} |
||||
``` |
||||
|
||||
- A *bidirectional streaming RPC* where both sides send a sequence of messages using a read-write stream. The two streams operate independently, so clients and servers can read and write in whatever order they like: for example, the server could wait to receive all the client messages before writing its responses, or it could alternately read a message then write a message, or some other combination of reads and writes. The order of messages in each stream is preserved. You specify this type of method by placing the `stream` keyword before both the request and the response. |
||||
```protobuf |
||||
// Accepts a stream of RouteNotes sent while a route is being traversed, |
||||
// while receiving other RouteNotes (e.g. from other users). |
||||
rpc RouteChat(stream RouteNote) returns (stream RouteNote) {} |
||||
``` |
||||
|
||||
Our .proto file also contains protocol buffer message type definitions for all the request and response types used in our service methods - for example, here's the `Point` message type: |
||||
```protobuf |
||||
// Points are represented as latitude-longitude pairs in the E7 representation |
||||
// (degrees multiplied by 10**7 and rounded to the nearest integer). |
||||
// Latitudes should be in the range +/- 90 degrees and longitude should be in |
||||
// the range +/- 180 degrees (inclusive). |
||||
message Point { |
||||
int32 latitude = 1; |
||||
int32 longitude = 2; |
||||
} |
||||
``` |
||||
|
||||
|
||||
## Generating client and server code |
||||
|
||||
Next we need to generate the gRPC client and server interfaces from our .proto service definition. We do this using the protocol buffer compiler `protoc` with a special gRPC C# plugin. |
||||
|
||||
If you want to run this yourself, make sure you've installed protoc and gRPC C# plugin. The instructions vary based on your OS: |
||||
- For Windows, the `Grpc.Tools` NuGet package contains the binaries you will need to generate the code. |
||||
- For Linux, make sure you've [installed gRPC C Core using Linuxbrew](https://github.com/grpc/grpc/tree/master/src/csharp#usage-linux-mono) |
||||
- For MacOS, make sure you've [installed gRPC C Core using Homebrew](https://github.com/grpc/grpc/tree/master/src/csharp#usage-macos-mono) |
||||
|
||||
Once that's done, the following command can be used to generate the C# code. |
||||
|
||||
To generate the code on Windows, we use `protoc.exe` and `grpc_csharp_plugin.exe` binaries that are shipped with the `Grpc.Tools` NuGet package under the `tools` directory. |
||||
Normally you would need to add the `Grpc.Tools` package to the solution yourself, but in this tutorial it has been already done for you. Following command should be run from the `csharp/route_guide` directory: |
||||
``` |
||||
> packages\Grpc.Tools.0.5.1\tools\protoc -I RouteGuide/protos --csharp_out=RouteGuide --grpc_out=RouteGuide --plugin=protoc-gen-grpc=packages\Grpc.Tools.0.5.1\tools\grpc_csharp_plugin.exe RouteGuide/protos/route_guide.proto |
||||
``` |
||||
|
||||
On Linux/MacOS, we rely on `protoc` and `grpc_csharp_plugin` being installed by Linuxbrew/Homebrew. Run this command from the route_guide directory: |
||||
```shell |
||||
$ protoc -I RouteGuide/protos --csharp_out=RouteGuide --grpc_out=RouteGuide --plugin=protoc-gen-grpc=`which grpc_csharp_plugin` RouteGuide/protos/route_guide.proto |
||||
``` |
||||
|
||||
Running one of the previous commands regenerates the following files in the RouteGuide directory: |
||||
- `RouteGuide/RouteGuide.cs` defines a namespace `examples` |
||||
- This contains all the protocol buffer code to populate, serialize, and retrieve our request and response message types |
||||
- `RouteGuide/RouteGuideGrpc.cs`, provides stub and service classes |
||||
- an interface `RouteGuide.IRouteGuide` to inherit from when defining RouteGuide service implementations |
||||
- a class `RouteGuide.RouteGuideClient` that can be used to access remote RouteGuide instances |
||||
|
||||
|
||||
<a name="server"></a> |
||||
## Creating the server |
||||
|
||||
First let's look at how we create a `RouteGuide` server. If you're only interested in creating gRPC clients, you can skip this section and go straight to [Creating the client](#client) (though you might find it interesting anyway!). |
||||
|
||||
There are two parts to making our `RouteGuide` service do its job: |
||||
- Implementing the service interface generated from our service definition: doing the actual "work" of our service. |
||||
- Running a gRPC server to listen for requests from clients and return the service responses. |
||||
|
||||
You can find our example `RouteGuide` server in [examples/csharp/route_guide/RouteGuideServer/RouteGuideImpl.cs](examples/csharp/route_guide/RouteGuideServer/RouteGuideServerImpl.cs). Let's take a closer look at how it works. |
||||
|
||||
### Implementing RouteGuide |
||||
|
||||
As you can see, our server has a `RouteGuideImpl` class that implements the generated `RouteGuide.IRouteGuide`: |
||||
|
||||
```csharp |
||||
// RouteGuideImpl provides an implementation of the RouteGuide service. |
||||
public class RouteGuideImpl : RouteGuide.IRouteGuide |
||||
``` |
||||
|
||||
#### Simple RPC |
||||
|
||||
`RouteGuideImpl` implements all our service methods. Let's look at the simplest type first, `GetFeature`, which just gets a `Point` from the client and returns the corresponding feature information from its database in a `Feature`. |
||||
|
||||
```csharp |
||||
public Task<Feature> GetFeature(Grpc.Core.ServerCallContext context, Point request) |
||||
{ |
||||
return Task.FromResult(CheckFeature(request)); |
||||
} |
||||
``` |
||||
|
||||
The method is passed a context for the RPC (which is empty in the alpha release), the client's `Point` protocol buffer request, and returns a `Feature` protocol buffer. In the method we create the `Feature` with the appropriate information, and then return it. To allow asynchronous |
||||
implementation, the method returns `Task<Feature>` rather than just `Feature`. You are free to perform your computations synchronously and return |
||||
the result once you've finished, just as we do in the example. |
||||
|
||||
#### Server-side streaming RPC |
||||
|
||||
Now let's look at something a bit more complicated - a streaming RPC. `ListFeatures` is a server-side streaming RPC, so we need to send back multiple `Feature` protocol buffers to our client. |
||||
|
||||
```csharp |
||||
// in RouteGuideImpl |
||||
public async Task ListFeatures(Grpc.Core.ServerCallContext context, Rectangle request, |
||||
Grpc.Core.IServerStreamWriter<Feature> responseStream) |
||||
{ |
||||
int left = Math.Min(request.Lo.Longitude, request.Hi.Longitude); |
||||
int right = Math.Max(request.Lo.Longitude, request.Hi.Longitude); |
||||
int top = Math.Max(request.Lo.Latitude, request.Hi.Latitude); |
||||
int bottom = Math.Min(request.Lo.Latitude, request.Hi.Latitude); |
||||
|
||||
foreach (var feature in features) |
||||
{ |
||||
if (!RouteGuideUtil.Exists(feature)) |
||||
{ |
||||
continue; |
||||
} |
||||
|
||||
int lat = feature.Location.Latitude; |
||||
int lon = feature.Location.Longitude; |
||||
if (lon >= left && lon <= right && lat >= bottom && lat <= top) |
||||
{ |
||||
await responseStream.WriteAsync(feature); |
||||
} |
||||
} |
||||
} |
||||
``` |
||||
|
||||
As you can see, here the request object is a `Rectangle` in which our client wants to find `Feature`s, but instead of returning a simple response we need to write responses to an asynchronous stream `IServerStreamWriter` using async method `WriteAsync`. |
||||
|
||||
#### Client-side streaming RPC |
||||
|
||||
Similarly, the client-side streaming method `RecordRoute` uses an [IAsyncEnumerator](https://github.com/Reactive-Extensions/Rx.NET/blob/master/Ix.NET/Source/System.Interactive.Async/IAsyncEnumerator.cs), to read the stream of requests using the async method `MoveNext` and the `Current` property. |
||||
|
||||
```csharp |
||||
public async Task<RouteSummary> RecordRoute(Grpc.Core.ServerCallContext context, |
||||
Grpc.Core.IAsyncStreamReader<Point> requestStream) |
||||
{ |
||||
int pointCount = 0; |
||||
int featureCount = 0; |
||||
int distance = 0; |
||||
Point previous = null; |
||||
var stopwatch = new Stopwatch(); |
||||
stopwatch.Start(); |
||||
|
||||
while (await requestStream.MoveNext()) |
||||
{ |
||||
var point = requestStream.Current; |
||||
pointCount++; |
||||
if (RouteGuideUtil.Exists(CheckFeature(point))) |
||||
{ |
||||
featureCount++; |
||||
} |
||||
if (previous != null) |
||||
{ |
||||
distance += (int) CalcDistance(previous, point); |
||||
} |
||||
previous = point; |
||||
} |
||||
|
||||
stopwatch.Stop(); |
||||
return RouteSummary.CreateBuilder().SetPointCount(pointCount) |
||||
.SetFeatureCount(featureCount).SetDistance(distance) |
||||
.SetElapsedTime((int) (stopwatch.ElapsedMilliseconds / 1000)).Build(); |
||||
} |
||||
``` |
||||
|
||||
#### Bidirectional streaming RPC |
||||
|
||||
Finally, let's look at our bidirectional streaming RPC `RouteChat`. |
||||
|
||||
```csharp |
||||
public async Task RouteChat(Grpc.Core.ServerCallContext context, |
||||
Grpc.Core.IAsyncStreamReader<RouteNote> requestStream, Grpc.Core.IServerStreamWriter<RouteNote> responseStream) |
||||
{ |
||||
while (await requestStream.MoveNext()) |
||||
{ |
||||
var note = requestStream.Current; |
||||
List<RouteNote> notes = GetOrCreateNotes(note.Location); |
||||
|
||||
List<RouteNote> prevNotes; |
||||
lock (notes) |
||||
{ |
||||
prevNotes = new List<RouteNote>(notes); |
||||
} |
||||
|
||||
foreach (var prevNote in prevNotes) |
||||
{ |
||||
await responseStream.WriteAsync(prevNote); |
||||
} |
||||
|
||||
lock (notes) |
||||
{ |
||||
notes.Add(note); |
||||
} |
||||
} |
||||
} |
||||
``` |
||||
|
||||
Here the method receives both `requestStream` and `responseStream` arguments. Reading the requests is done the same way as in the client-side streaming method `RecordRoute`. Writing the responses is done the same way as in the server-side streaming method `ListFeatures`. |
||||
|
||||
### Starting the server |
||||
|
||||
Once we've implemented all our methods, we also need to start up a gRPC server so that clients can actually use our service. The following snippet shows how we do this for our `RouteGuide` service: |
||||
|
||||
```csharp |
||||
var features = RouteGuideUtil.ParseFeatures(RouteGuideUtil.DefaultFeaturesFile); |
||||
GrpcEnvironment.Initialize(); |
||||
|
||||
Server server = new Server(); |
||||
server.AddServiceDefinition(RouteGuide.BindService(new RouteGuideImpl(features))); |
||||
int port = server.AddListeningPort("localhost", 50052); |
||||
server.Start(); |
||||
|
||||
Console.WriteLine("RouteGuide server listening on port " + port); |
||||
Console.WriteLine("Press any key to stop the server..."); |
||||
Console.ReadKey(); |
||||
|
||||
server.ShutdownAsync().Wait(); |
||||
GrpcEnvironment.Shutdown(); |
||||
``` |
||||
As you can see, we build and start our server using `Grpc.Core.Server` class. To do this, we: |
||||
|
||||
1. Create an instance of `Grpc.Core.Server`. |
||||
1. Create an instance of our service implementation class `RouteGuideImpl`. |
||||
3. Register our service implementation with the server using the `AddServiceDefinition` method and the generated method `RouteGuide.BindService`. |
||||
2. Specify the address and port we want to use to listen for client requests using the `AddListeningPort` method. |
||||
4. Call `Start` on the server instance to start an RPC server for our service. |
||||
|
||||
<a name="client"></a> |
||||
## Creating the client |
||||
|
||||
In this section, we'll look at creating a C# client for our `RouteGuide` service. You can see our complete example client code in [examples/csharp/route_guide/RouteGuideClient/Program.cs](examples/csharp/route_guide/RouteGuideClient/Program.cs). |
||||
|
||||
### Creating a stub |
||||
|
||||
To call service methods, we first need to create a *stub*. |
||||
|
||||
First, we need to create a gRPC client channel that will connect to gRPC server. Then, we use the `RouteGuide.NewStub` method of the `RouteGuide` class generated from our .proto. |
||||
|
||||
```csharp |
||||
GrpcEnvironment.Initialize(); |
||||
|
||||
using (Channel channel = new Channel("127.0.0.1:50052")) |
||||
{ |
||||
var client = RouteGuide.NewStub(channel); |
||||
|
||||
// YOUR CODE GOES HERE |
||||
} |
||||
|
||||
GrpcEnvironment.Shutdown(); |
||||
``` |
||||
|
||||
### Calling service methods |
||||
|
||||
Now let's look at how we call our service methods. gRPC C# provides asynchronous versions of each of the supported method types. For convenience, |
||||
gRPC C# also provides a synchronous method stub, but only for simple (single request/single response) RPCs. |
||||
|
||||
#### Simple RPC |
||||
|
||||
Calling the simple RPC `GetFeature` in a synchronous way is nearly as straightforward as calling a local method. |
||||
|
||||
```csharp |
||||
Point request = Point.CreateBuilder().SetLatitude(409146138).SetLongitude(-746188906).Build(); |
||||
Feature feature = client.GetFeature(request); |
||||
``` |
||||
|
||||
As you can see, we create and populate a request protocol buffer object (in our case `Point`), and call the desired method on the client object, passing it the request. If the RPC finishes with success, the response protocol buffer (in our case `Feature`) will be returned. Otherwise, an exception of type `RpcException` will be thrown, indicating the status code of the problem. |
||||
|
||||
Alternatively, if you are in async context, you can call an asynchronous version of the method (and use `await` keyword to await the result): |
||||
```csharp |
||||
Point request = Point.CreateBuilder().SetLatitude(409146138).SetLongitude(-746188906).Build(); |
||||
Feature feature = await client.GetFeatureAsync(request); |
||||
``` |
||||
|
||||
#### Streaming RPCs |
||||
|
||||
Now let's look at our streaming methods. If you've already read [Creating the server](#server) some of this may look very familiar - streaming RPCs are implemented in a similar way on both sides. The difference with respect to simple call is that the client methods return an instance of a call object, that provides access to request/response streams and/or asynchronous result (depending on the streaming type you are using). |
||||
|
||||
Here's where we call the server-side streaming method `ListFeatures`, which has property `ReponseStream` of type `IAsyncEnumerator<Feature>` |
||||
|
||||
```csharp |
||||
using (var call = client.ListFeatures(request)) |
||||
{ |
||||
while (await call.ResponseStream.MoveNext()) |
||||
{ |
||||
Feature feature = call.ResponseStream.Current; |
||||
Console.WriteLine("Received " + feature.ToString()); |
||||
} |
||||
} |
||||
``` |
||||
|
||||
The client-side streaming method `RecordRoute` is similar, except we use the property `RequestStream` to write the requests one by one using `WriteAsync` and eventually signal that no more request will be send using `CompleteAsync`. The method result can be obtained through the property |
||||
`Result`. |
||||
```csharp |
||||
using (var call = client.RecordRoute()) |
||||
{ |
||||
foreach (var point in points) |
||||
{ |
||||
await call.RequestStream.WriteAsync(point); |
||||
} |
||||
await call.RequestStream.CompleteAsync(); |
||||
|
||||
RouteSummary summary = await call.Result; |
||||
} |
||||
``` |
||||
|
||||
Finally, let's look at our bidirectional streaming RPC `RouteChat`. In this case, we write the request to `RequestStream` and receive the responses from `ResponseStream`. As you can see from the example, the streams are independent of each other. |
||||
|
||||
```csharp |
||||
using (var call = client.RouteChat()) |
||||
{ |
||||
var responseReaderTask = Task.Run(async () => |
||||
{ |
||||
while (await call.ResponseStream.MoveNext()) |
||||
{ |
||||
var note = call.ResponseStream.Current; |
||||
Console.WriteLine("Received " + note); |
||||
} |
||||
}); |
||||
|
||||
foreach (RouteNote request in requests) |
||||
{ |
||||
await call.RequestStream.WriteAsync(request); |
||||
} |
||||
await call.RequestStream.CompleteAsync(); |
||||
await responseReaderTask; |
||||
} |
||||
``` |
||||
|
||||
## Try it out! |
||||
|
||||
Build client and server: |
||||
|
||||
Open the solution `examples/csharp/route_guide/RouteGuide.sln` from Visual Studio (or Monodevelop on Linux) and hit "Build". |
||||
|
||||
Run the server, which will listen on port 50052: |
||||
``` |
||||
> cd RouteGuideServer/bin/Debug |
||||
> RouteGuideServer.exe |
||||
``` |
||||
|
||||
Run the client (in a different terminal): |
||||
``` |
||||
> cd RouteGuideClient/bin/Debug |
||||
> RouteGuideClient.exe |
||||
``` |
||||
|
||||
You can also run the server and client directly from Visual Studio. |
||||
|
||||
On Linux or Mac, use `mono RouteGuideServer.exe` and `mono RouteGuideClient.exe` to run the server and client. |
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,123 @@ |
||||
// Generated by the protocol buffer compiler. DO NOT EDIT! |
||||
// source: route_guide.proto |
||||
#region Designer generated code |
||||
|
||||
using System; |
||||
using System.Threading; |
||||
using System.Threading.Tasks; |
||||
using Grpc.Core; |
||||
|
||||
namespace examples { |
||||
public static class RouteGuide |
||||
{ |
||||
static readonly string __ServiceName = "examples.RouteGuide"; |
||||
|
||||
static readonly Marshaller<global::examples.Point> __Marshaller_Point = Marshallers.Create((arg) => arg.ToByteArray(), global::examples.Point.ParseFrom); |
||||
static readonly Marshaller<global::examples.Feature> __Marshaller_Feature = Marshallers.Create((arg) => arg.ToByteArray(), global::examples.Feature.ParseFrom); |
||||
static readonly Marshaller<global::examples.Rectangle> __Marshaller_Rectangle = Marshallers.Create((arg) => arg.ToByteArray(), global::examples.Rectangle.ParseFrom); |
||||
static readonly Marshaller<global::examples.RouteSummary> __Marshaller_RouteSummary = Marshallers.Create((arg) => arg.ToByteArray(), global::examples.RouteSummary.ParseFrom); |
||||
static readonly Marshaller<global::examples.RouteNote> __Marshaller_RouteNote = Marshallers.Create((arg) => arg.ToByteArray(), global::examples.RouteNote.ParseFrom); |
||||
|
||||
static readonly Method<global::examples.Point, global::examples.Feature> __Method_GetFeature = new Method<global::examples.Point, global::examples.Feature>( |
||||
MethodType.Unary, |
||||
"GetFeature", |
||||
__Marshaller_Point, |
||||
__Marshaller_Feature); |
||||
|
||||
static readonly Method<global::examples.Rectangle, global::examples.Feature> __Method_ListFeatures = new Method<global::examples.Rectangle, global::examples.Feature>( |
||||
MethodType.ServerStreaming, |
||||
"ListFeatures", |
||||
__Marshaller_Rectangle, |
||||
__Marshaller_Feature); |
||||
|
||||
static readonly Method<global::examples.Point, global::examples.RouteSummary> __Method_RecordRoute = new Method<global::examples.Point, global::examples.RouteSummary>( |
||||
MethodType.ClientStreaming, |
||||
"RecordRoute", |
||||
__Marshaller_Point, |
||||
__Marshaller_RouteSummary); |
||||
|
||||
static readonly Method<global::examples.RouteNote, global::examples.RouteNote> __Method_RouteChat = new Method<global::examples.RouteNote, global::examples.RouteNote>( |
||||
MethodType.DuplexStreaming, |
||||
"RouteChat", |
||||
__Marshaller_RouteNote, |
||||
__Marshaller_RouteNote); |
||||
|
||||
// client-side stub interface |
||||
public interface IRouteGuideClient |
||||
{ |
||||
global::examples.Feature GetFeature(global::examples.Point request, CancellationToken token = default(CancellationToken)); |
||||
Task<global::examples.Feature> GetFeatureAsync(global::examples.Point request, CancellationToken token = default(CancellationToken)); |
||||
AsyncServerStreamingCall<global::examples.Feature> ListFeatures(global::examples.Rectangle request, CancellationToken token = default(CancellationToken)); |
||||
AsyncClientStreamingCall<global::examples.Point, global::examples.RouteSummary> RecordRoute(CancellationToken token = default(CancellationToken)); |
||||
AsyncDuplexStreamingCall<global::examples.RouteNote, global::examples.RouteNote> RouteChat(CancellationToken token = default(CancellationToken)); |
||||
} |
||||
|
||||
// server-side interface |
||||
public interface IRouteGuide |
||||
{ |
||||
Task<global::examples.Feature> GetFeature(ServerCallContext context, global::examples.Point request); |
||||
Task ListFeatures(ServerCallContext context, global::examples.Rectangle request, IServerStreamWriter<global::examples.Feature> responseStream); |
||||
Task<global::examples.RouteSummary> RecordRoute(ServerCallContext context, IAsyncStreamReader<global::examples.Point> requestStream); |
||||
Task RouteChat(ServerCallContext context, IAsyncStreamReader<global::examples.RouteNote> requestStream, IServerStreamWriter<global::examples.RouteNote> responseStream); |
||||
} |
||||
|
||||
// client stub |
||||
public class RouteGuideClient : AbstractStub<RouteGuideClient, StubConfiguration>, IRouteGuideClient |
||||
{ |
||||
public RouteGuideClient(Channel channel) : this(channel, StubConfiguration.Default) |
||||
{ |
||||
} |
||||
public RouteGuideClient(Channel channel, StubConfiguration config) : base(channel, config) |
||||
{ |
||||
} |
||||
public global::examples.Feature GetFeature(global::examples.Point request, CancellationToken token = default(CancellationToken)) |
||||
{ |
||||
var call = CreateCall(__ServiceName, __Method_GetFeature); |
||||
return Calls.BlockingUnaryCall(call, request, token); |
||||
} |
||||
public Task<global::examples.Feature> GetFeatureAsync(global::examples.Point request, CancellationToken token = default(CancellationToken)) |
||||
{ |
||||
var call = CreateCall(__ServiceName, __Method_GetFeature); |
||||
return Calls.AsyncUnaryCall(call, request, token); |
||||
} |
||||
public AsyncServerStreamingCall<global::examples.Feature> ListFeatures(global::examples.Rectangle request, CancellationToken token = default(CancellationToken)) |
||||
{ |
||||
var call = CreateCall(__ServiceName, __Method_ListFeatures); |
||||
return Calls.AsyncServerStreamingCall(call, request, token); |
||||
} |
||||
public AsyncClientStreamingCall<global::examples.Point, global::examples.RouteSummary> RecordRoute(CancellationToken token = default(CancellationToken)) |
||||
{ |
||||
var call = CreateCall(__ServiceName, __Method_RecordRoute); |
||||
return Calls.AsyncClientStreamingCall(call, token); |
||||
} |
||||
public AsyncDuplexStreamingCall<global::examples.RouteNote, global::examples.RouteNote> RouteChat(CancellationToken token = default(CancellationToken)) |
||||
{ |
||||
var call = CreateCall(__ServiceName, __Method_RouteChat); |
||||
return Calls.AsyncDuplexStreamingCall(call, token); |
||||
} |
||||
} |
||||
|
||||
// creates service definition that can be registered with a server |
||||
public static ServerServiceDefinition BindService(IRouteGuide serviceImpl) |
||||
{ |
||||
return ServerServiceDefinition.CreateBuilder(__ServiceName) |
||||
.AddMethod(__Method_GetFeature, serviceImpl.GetFeature) |
||||
.AddMethod(__Method_ListFeatures, serviceImpl.ListFeatures) |
||||
.AddMethod(__Method_RecordRoute, serviceImpl.RecordRoute) |
||||
.AddMethod(__Method_RouteChat, serviceImpl.RouteChat).Build(); |
||||
} |
||||
|
||||
// creates a new client stub |
||||
public static IRouteGuideClient NewStub(Channel channel) |
||||
{ |
||||
return new RouteGuideClient(channel); |
||||
} |
||||
|
||||
// creates a new client stub |
||||
public static IRouteGuideClient NewStub(Channel channel, StubConfiguration config) |
||||
{ |
||||
return new RouteGuideClient(channel, config); |
||||
} |
||||
} |
||||
} |
||||
#endregion |
@ -0,0 +1,123 @@ |
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
// TODO(jtattermusch): as of now, C# protobufs don't officially support |
||||
// proto3. |
||||
syntax = "proto2"; |
||||
|
||||
package examples; |
||||
|
||||
// Interface exported by the server. |
||||
service RouteGuide { |
||||
// A simple RPC. |
||||
// |
||||
// Obtains the feature at a given position. |
||||
// |
||||
// A feature with an empty name is returned if there's no feature at the given |
||||
// position. |
||||
rpc GetFeature(Point) returns (Feature) {} |
||||
|
||||
// A server-to-client streaming RPC. |
||||
// |
||||
// Obtains the Features available within the given Rectangle. Results are |
||||
// streamed rather than returned at once (e.g. in a response message with a |
||||
// repeated field), as the rectangle may cover a large area and contain a |
||||
// huge number of features. |
||||
rpc ListFeatures(Rectangle) returns (stream Feature) {} |
||||
|
||||
// A client-to-server streaming RPC. |
||||
// |
||||
// Accepts a stream of Points on a route being traversed, returning a |
||||
// RouteSummary when traversal is completed. |
||||
rpc RecordRoute(stream Point) returns (RouteSummary) {} |
||||
|
||||
// A Bidirectional streaming RPC. |
||||
// |
||||
// Accepts a stream of RouteNotes sent while a route is being traversed, |
||||
// while receiving other RouteNotes (e.g. from other users). |
||||
rpc RouteChat(stream RouteNote) returns (stream RouteNote) {} |
||||
} |
||||
|
||||
// Points are represented as latitude-longitude pairs in the E7 representation |
||||
// (degrees multiplied by 10**7 and rounded to the nearest integer). |
||||
// Latitudes should be in the range +/- 90 degrees and longitude should be in |
||||
// the range +/- 180 degrees (inclusive). |
||||
message Point { |
||||
optional int32 latitude = 1; |
||||
optional int32 longitude = 2; |
||||
} |
||||
|
||||
// A latitude-longitude rectangle, represented as two diagonally opposite |
||||
// points "lo" and "hi". |
||||
message Rectangle { |
||||
// One corner of the rectangle. |
||||
optional Point lo = 1; |
||||
|
||||
// The other corner of the rectangle. |
||||
optional Point hi = 2; |
||||
} |
||||
|
||||
// A feature names something at a given point. |
||||
// |
||||
// If a feature could not be named, the name is empty. |
||||
message Feature { |
||||
// The name of the feature. |
||||
optional string name = 1; |
||||
|
||||
// The point where the feature is detected. |
||||
optional Point location = 2; |
||||
} |
||||
|
||||
// A RouteNote is a message sent while at a given point. |
||||
message RouteNote { |
||||
// The location from which the message is sent. |
||||
optional Point location = 1; |
||||
|
||||
// The message to be sent. |
||||
optional string message = 2; |
||||
} |
||||
|
||||
// A RouteSummary is received in response to a RecordRoute rpc. |
||||
// |
||||
// It contains the number of individual points received, the number of |
||||
// detected features, and the total distance covered as the cumulative sum of |
||||
// the distance between each point. |
||||
message RouteSummary { |
||||
// The number of points received. |
||||
optional int32 point_count = 1; |
||||
|
||||
// The number of known features passed while traversing the route. |
||||
optional int32 feature_count = 2; |
||||
|
||||
// The distance covered in metres. |
||||
optional int32 distance = 3; |
||||
|
||||
// The duration of the traversal in seconds. |
||||
optional int32 elapsed_time = 4; |
||||
} |
@ -0,0 +1,3 @@ |
||||
*~ |
||||
node_modules |
||||
npm-debug.log |
@ -0,0 +1,60 @@ |
||||
gRPC in 3 minutes (Node.js) |
||||
=========================== |
||||
|
||||
PREREQUISITES |
||||
------------- |
||||
|
||||
- `node`: This requires Node 10.x or greater. |
||||
- [homebrew][] on Mac OS X, [linuxbrew][] on Linux. These simplify the installation of the gRPC C core. |
||||
|
||||
INSTALL |
||||
------- |
||||
- On Mac OS X, install [homebrew][]. On Linux, install [linuxbrew][]. Run the following command to install gRPC Node.js. |
||||
|
||||
```sh |
||||
$ curl -fsSL https://goo.gl/getgrpc | bash -s nodejs |
||||
``` |
||||
This will download and run the [gRPC install script][], then install the latest version of gRPC Nodejs npm package. |
||||
- Clone this repository |
||||
|
||||
```sh |
||||
$ git clone https://github.com/grpc/grpc.git |
||||
``` |
||||
|
||||
- Install this package's dependencies |
||||
|
||||
```sh |
||||
$ cd examples/node |
||||
$ npm install |
||||
``` |
||||
|
||||
TRY IT! |
||||
------- |
||||
|
||||
- Run the server |
||||
|
||||
```sh |
||||
$ # from this directory (grpc_common/node). |
||||
$ node ./greeter_server.js & |
||||
``` |
||||
|
||||
- Run the client |
||||
|
||||
```sh |
||||
$ # from this directory |
||||
$ node ./greeter_client.js |
||||
``` |
||||
|
||||
NOTE |
||||
---- |
||||
This directory has a copy of `helloworld.proto` because it currently depends on |
||||
some Protocol Buffer 2.0 syntax that is deprecated in Protocol Buffer 3.0. |
||||
|
||||
TUTORIAL |
||||
-------- |
||||
You can find a more detailed tutorial in [gRPC Basics: Node.js][] |
||||
|
||||
[homebrew]:http://brew.sh |
||||
[linuxbrew]:https://github.com/Homebrew/linuxbrew#installation |
||||
[gRPC install script]:https://raw.githubusercontent.com/grpc/homebrew-grpc/master/scripts/install |
||||
[gRPC Basics: Node.js]:https://github.com/grpc/grpc/blob/master/examples/node/route_guide/README.md |
@ -0,0 +1,52 @@ |
||||
/* |
||||
* |
||||
* Copyright 2015, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
var PROTO_PATH = __dirname + '/helloworld.proto'; |
||||
|
||||
var grpc = require('grpc'); |
||||
var hello_proto = grpc.load(PROTO_PATH).helloworld; |
||||
|
||||
function main() { |
||||
var client = new hello_proto.Greeter('localhost:50051'); |
||||
var user; |
||||
if (process.argv.length >= 3) { |
||||
user = process.argv[2]; |
||||
} else { |
||||
user = 'world'; |
||||
} |
||||
client.sayHello({name: user}, function(err, response) { |
||||
console.log('Greeting:', response.message); |
||||
}); |
||||
} |
||||
|
||||
main(); |
@ -0,0 +1,50 @@ |
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
syntax = "proto3"; |
||||
|
||||
option java_package = "ex.grpc"; |
||||
|
||||
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 { |
||||
optional string name = 1; |
||||
} |
||||
|
||||
// The response message containing the greetings |
||||
message HelloReply { |
||||
optional string message = 1; |
||||
} |
@ -0,0 +1,10 @@ |
||||
{ |
||||
"name": "grpc-demo", |
||||
"version": "0.5.0", |
||||
"dependencies": { |
||||
"async": "^0.9.0", |
||||
"grpc": "~0.9.0", |
||||
"minimist": "^1.1.0", |
||||
"underscore": "^1.8.2" |
||||
} |
||||
} |
@ -0,0 +1,362 @@ |
||||
#gRPC Basics: Node.js |
||||
|
||||
This tutorial provides a basic Node.js programmer's introduction to working with gRPC. By walking through this example you'll learn how to: |
||||
|
||||
- Define a service in a .proto file. |
||||
- Use the Node.js gRPC API to write a simple client and server for your service. |
||||
|
||||
It assumes that you have read the [Getting started](https://github.com/grpc/grpc/tree/master/examples) guide and are familiar with [protocol buffers] (https://developers.google.com/protocol-buffers/docs/overview). Note that the example in this tutorial uses the proto3 version of the protocol buffers language, which is currently in alpha release:you can find out more in the [proto3 language guide](https://developers.google.com/protocol-buffers/docs/proto3) and see the [release notes](https://github.com/google/protobuf/releases) for the new version in the protocol buffers Github repository. |
||||
|
||||
This isn't a comprehensive guide to using gRPC in Node.js: more reference documentation is coming soon. |
||||
|
||||
## Why use gRPC? |
||||
|
||||
Our example is a simple route mapping application that lets clients get information about features on their route, create a summary of their route, and exchange route information such as traffic updates with the server and other clients. |
||||
|
||||
With gRPC we can define our service once in a .proto file and implement clients and servers in any of gRPC's supported languages, which in turn can be run in environments ranging from servers inside Google to your own tablet - all the complexity of communication between different languages and environments is handled for you by gRPC. We also get all the advantages of working with protocol buffers, including efficient serialization, a simple IDL, and easy interface updating. |
||||
|
||||
## Example code and setup |
||||
|
||||
The example code for our tutorial is in [examples/node/route_guide](examples/node/route_guide). To download the example, clone this repository by running the following command: |
||||
```shell |
||||
$ git clone https://github.com/grpc/grpc.git |
||||
``` |
||||
|
||||
Then change your current directory to `examples/node/route_guide`: |
||||
```shell |
||||
$ cd examples/node/route_guide |
||||
``` |
||||
|
||||
You also should have the relevant tools installed to generate the server and client interface code - if you don't already, follow the setup instructions in [the Node.js quick start guide](examples/node). |
||||
|
||||
|
||||
## Defining the service |
||||
|
||||
Our first step (as you'll know from [Getting started](https://github.com/grpc/grpc/tree/master/examples)) is to define the gRPC *service* and the method *request* and *response* types using [protocol buffers] (https://developers.google.com/protocol-buffers/docs/overview). You can see the complete .proto file in [`examples/protos/route_guide.proto`](examples/protos/route_guide.proto). |
||||
|
||||
To define a service, you specify a named `service` in your .proto file: |
||||
|
||||
```protobuf |
||||
service RouteGuide { |
||||
... |
||||
} |
||||
``` |
||||
|
||||
Then you define `rpc` methods inside your service definition, specifying their request and response types. gRPC lets you define four kinds of service method, all of which are used in the `RouteGuide` service: |
||||
|
||||
- A *simple RPC* where the client sends a request to the server using the stub and waits for a response to come back, just like a normal function call. |
||||
```protobuf |
||||
// Obtains the feature at a given position. |
||||
rpc GetFeature(Point) returns (Feature) {} |
||||
``` |
||||
|
||||
- A *server-side streaming RPC* where the client sends a request to the server and gets a stream to read a sequence of messages back. The client reads from the returned stream until there are no more messages. As you can see in our example, you specify a server-side streaming method by placing the `stream` keyword before the *response* type. |
||||
```protobuf |
||||
// Obtains the Features available within the given Rectangle. Results are |
||||
// streamed rather than returned at once (e.g. in a response message with a |
||||
// repeated field), as the rectangle may cover a large area and contain a |
||||
// huge number of features. |
||||
rpc ListFeatures(Rectangle) returns (stream Feature) {} |
||||
``` |
||||
|
||||
- A *client-side streaming RPC* where the client writes a sequence of messages and sends them to the server, again using a provided stream. Once the client has finished writing the messages, it waits for the server to read them all and return its response. You specify a server-side streaming method by placing the `stream` keyword before the *request* type. |
||||
```protobuf |
||||
// Accepts a stream of Points on a route being traversed, returning a |
||||
// RouteSummary when traversal is completed. |
||||
rpc RecordRoute(stream Point) returns (RouteSummary) {} |
||||
``` |
||||
|
||||
- A *bidirectional streaming RPC* where both sides send a sequence of messages using a read-write stream. The two streams operate independently, so clients and servers can read and write in whatever order they like: for example, the server could wait to receive all the client messages before writing its responses, or it could alternately read a message then write a message, or some other combination of reads and writes. The order of messages in each stream is preserved. You specify this type of method by placing the `stream` keyword before both the request and the response. |
||||
```protobuf |
||||
// Accepts a stream of RouteNotes sent while a route is being traversed, |
||||
// while receiving other RouteNotes (e.g. from other users). |
||||
rpc RouteChat(stream RouteNote) returns (stream RouteNote) {} |
||||
``` |
||||
|
||||
Our .proto file also contains protocol buffer message type definitions for all the request and response types used in our service methods - for example, here's the `Point` message type: |
||||
```protobuf |
||||
// Points are represented as latitude-longitude pairs in the E7 representation |
||||
// (degrees multiplied by 10**7 and rounded to the nearest integer). |
||||
// Latitudes should be in the range +/- 90 degrees and longitude should be in |
||||
// the range +/- 180 degrees (inclusive). |
||||
message Point { |
||||
int32 latitude = 1; |
||||
int32 longitude = 2; |
||||
} |
||||
``` |
||||
|
||||
|
||||
## Loading service descriptors from proto files |
||||
|
||||
The Node.js library dynamically generates service descriptors and client stub definitions from `.proto` files loaded at runtime. |
||||
|
||||
To load a `.proto` file, simply `require` the gRPC library, then use its `load()` method: |
||||
|
||||
```node |
||||
var grpc = require('grpc'); |
||||
var protoDescriptor = grpc.load(__dirname + '/route_guide.proto'); |
||||
// The protoDescriptor object has the full package hierarchy |
||||
var example = protoDescriptor.examples; |
||||
``` |
||||
|
||||
Once you've done this, the stub constructor is in the `examples` namespace (`protoDescriptor.examples.RouteGuide`) and the service descriptor (which is used to create a server) is a property of the stub (`protoDescriptor.examples.RouteGuide.service`); |
||||
|
||||
<a name="server"></a> |
||||
## Creating the server |
||||
|
||||
First let's look at how we create a `RouteGuide` server. If you're only interested in creating gRPC clients, you can skip this section and go straight to [Creating the client](#client) (though you might find it interesting anyway!). |
||||
|
||||
There are two parts to making our `RouteGuide` service do its job: |
||||
- Implementing the service interface generated from our service definition: doing the actual "work" of our service. |
||||
- Running a gRPC server to listen for requests from clients and return the service responses. |
||||
|
||||
You can find our example `RouteGuide` server in [examples/node/route_guide/route_guide_server.js](examples/node/route_guide/route_guide_server.js). Let's take a closer look at how it works. |
||||
|
||||
### Implementing RouteGuide |
||||
|
||||
As you can see, our server has a `Server` constructor generated from the `RouteGuide.service` descriptor object |
||||
|
||||
```node |
||||
var Server = grpc.buildServer([examples.RouteGuide.service]); |
||||
``` |
||||
In this case we're implementing the *asynchronous* version of `RouteGuide`, which provides our default gRPC server behaviour. |
||||
|
||||
The functions in `route_guide_server.js` implement all our service methods. Let's look at the simplest type first, `getFeature`, which just gets a `Point` from the client and returns the corresponding feature information from its database in a `Feature`. |
||||
|
||||
```node |
||||
function checkFeature(point) { |
||||
var feature; |
||||
// Check if there is already a feature object for the given point |
||||
for (var i = 0; i < feature_list.length; i++) { |
||||
feature = feature_list[i]; |
||||
if (feature.location.latitude === point.latitude && |
||||
feature.location.longitude === point.longitude) { |
||||
return feature; |
||||
} |
||||
} |
||||
var name = ''; |
||||
feature = { |
||||
name: name, |
||||
location: point |
||||
}; |
||||
return feature; |
||||
} |
||||
function getFeature(call, callback) { |
||||
callback(null, checkFeature(call.request)); |
||||
} |
||||
``` |
||||
|
||||
The method is passed a call object for the RPC, which has the `Point` parameter as a property, and a callback to which we can pass our returned `Feature`. In the method body we populate a `Feature` corresponding to the given point and pass it to the callback, with a null first parameter to indicate that there is no error. |
||||
|
||||
Now let's look at something a bit more complicated - a streaming RPC. `listFeatures` is a server-side streaming RPC, so we need to send back multiple `Feature`s to our client. |
||||
|
||||
```node |
||||
function listFeatures(call) { |
||||
var lo = call.request.lo; |
||||
var hi = call.request.hi; |
||||
var left = _.min([lo.longitude, hi.longitude]); |
||||
var right = _.max([lo.longitude, hi.longitude]); |
||||
var top = _.max([lo.latitude, hi.latitude]); |
||||
var bottom = _.min([lo.latitude, hi.latitude]); |
||||
// For each feature, check if it is in the given bounding box |
||||
_.each(feature_list, function(feature) { |
||||
if (feature.name === '') { |
||||
return; |
||||
} |
||||
if (feature.location.longitude >= left && |
||||
feature.location.longitude <= right && |
||||
feature.location.latitude >= bottom && |
||||
feature.location.latitude <= top) { |
||||
call.write(feature); |
||||
} |
||||
}); |
||||
call.end(); |
||||
} |
||||
``` |
||||
|
||||
As you can see, instead of getting the call object and callback in our method parameters, this time we get a `call` object that implements the `Writable` interface. In the method, we create as many `Feature` objects as we need to return, writing them to the `call` using its `write()` method. Finally, we call `call.end()` to indicate that we have sent all messages. |
||||
|
||||
If you look at the client-side streaming method `RecordRoute` you'll see it's quite similar to the unary call, except this time the `call` parameter implements the `Reader` interface. The `call`'s `'data'` event fires every time there is new data, and the `'end'` event fires when all data has been read. Like the unary case, we respond by calling the callback |
||||
|
||||
```node |
||||
call.on('data', function(point) { |
||||
// Process user data |
||||
}); |
||||
call.on('end', function() { |
||||
callback(null, result); |
||||
}); |
||||
``` |
||||
|
||||
Finally, let's look at our bidirectional streaming RPC `RouteChat()`. |
||||
|
||||
```node |
||||
function routeChat(call) { |
||||
call.on('data', function(note) { |
||||
var key = pointKey(note.location); |
||||
/* For each note sent, respond with all previous notes that correspond to |
||||
* the same point */ |
||||
if (route_notes.hasOwnProperty(key)) { |
||||
_.each(route_notes[key], function(note) { |
||||
call.write(note); |
||||
}); |
||||
} else { |
||||
route_notes[key] = []; |
||||
} |
||||
// Then add the new note to the list |
||||
route_notes[key].push(JSON.parse(JSON.stringify(note))); |
||||
}); |
||||
call.on('end', function() { |
||||
call.end(); |
||||
}); |
||||
} |
||||
``` |
||||
|
||||
This time we get a `call` implementing `Duplex` that can be used to read *and* write messages. The syntax for reading and writing here is exactly the same as for our client-streaming and server-streaming methods. Although each side will always get the other's messages in the order they were written, both the client and server can read and write in any order — the streams operate completely independently. |
||||
|
||||
### Starting the server |
||||
|
||||
Once we've implemented all our methods, we also need to start up a gRPC server so that clients can actually use our service. The following snippet shows how we do this for our `RouteGuide` service: |
||||
|
||||
```node |
||||
function getServer() { |
||||
return new Server({ |
||||
'examples.RouteGuide' : { |
||||
getFeature: getFeature, |
||||
listFeatures: listFeatures, |
||||
recordRoute: recordRoute, |
||||
routeChat: routeChat |
||||
} |
||||
}); |
||||
} |
||||
var routeServer = getServer(); |
||||
routeServer.bind('0.0.0.0:50051'); |
||||
routeServer.listen(); |
||||
``` |
||||
|
||||
As you can see, we build and start our server with the following steps: |
||||
|
||||
1. Create a `Server` constructor from the `RouteGuide` service descriptor. |
||||
2. Implement the service methods. |
||||
3. Create an instance of the server by calling the `Server` constructor with the method implementations. |
||||
4. Specify the address and port we want to use to listen for client requests using the instance's `bind()` method. |
||||
5. Call `listen()` on the instance to start the RPC server. |
||||
|
||||
<a name="client"></a> |
||||
## Creating the client |
||||
|
||||
In this section, we'll look at creating a Node.js client for our `RouteGuide` service. You can see our complete example client code in [examples/node/route_guide/route_guide_client.js](examples/node/route_guide/route_guide_client.js). |
||||
|
||||
### Creating a stub |
||||
|
||||
To call service methods, we first need to create a *stub*. To do this, we just need to call the RouteGuide stub constructor, specifying the server address and port. |
||||
|
||||
```node |
||||
new example.RouteGuide('localhost:50051'); |
||||
``` |
||||
|
||||
### Calling service methods |
||||
|
||||
Now let's look at how we call our service methods. Note that all of these methods are asynchronous: they use either events or callbacks to retrieve results. |
||||
|
||||
#### Simple RPC |
||||
|
||||
Calling the simple RPC `GetFeature` is nearly as straightforward as calling a local asynchronous method. |
||||
|
||||
```node |
||||
var point = {latitude: 409146138, longitude: -746188906}; |
||||
stub.getFeature(point, function(err, feature) { |
||||
if (err) { |
||||
// process error |
||||
} else { |
||||
// process feature |
||||
} |
||||
}); |
||||
``` |
||||
|
||||
As you can see, we create and populate a request object. Finally, we call the method on the stub, passing it the request and callback. If there is no error, then we can read the response information from the server from our response object. |
||||
|
||||
```node |
||||
console.log('Found feature called "' + feature.name + '" at ' + |
||||
feature.location.latitude/COORD_FACTOR + ', ' + |
||||
feature.location.longitude/COORD_FACTOR); |
||||
``` |
||||
|
||||
#### Streaming RPCs |
||||
|
||||
Now let's look at our streaming methods. If you've already read [Creating the server](#server) some of this may look very familiar - streaming RPCs are implemented in a similar way on both sides. Here's where we call the server-side streaming method `ListFeatures`, which returns a stream of geographical `Feature`s: |
||||
|
||||
```node |
||||
var call = client.listFeatures(rectangle); |
||||
call.on('data', function(feature) { |
||||
console.log('Found feature called "' + feature.name + '" at ' + |
||||
feature.location.latitude/COORD_FACTOR + ', ' + |
||||
feature.location.longitude/COORD_FACTOR); |
||||
}); |
||||
call.on('end', function() { |
||||
// The server has finished sending |
||||
}); |
||||
call.on('status', function(status) { |
||||
// process status |
||||
}); |
||||
``` |
||||
|
||||
Instead of passing the method a request and callback, we pass it a request and get a `Readable` stream object back. The client can use the `Readable`'s `'data'` event to read the server's responses. This event fires with each `Feature` message object until there are no more messages: the `'end'` event indicates that the call is done. Finally, the status event fires when the server sends the status. |
||||
|
||||
The client-side streaming method `RecordRoute` is similar, except there we pass the method a callback and get back a `Writable`. |
||||
|
||||
```node |
||||
var call = client.recordRoute(function(error, stats) { |
||||
if (error) { |
||||
callback(error); |
||||
} |
||||
console.log('Finished trip with', stats.point_count, 'points'); |
||||
console.log('Passed', stats.feature_count, 'features'); |
||||
console.log('Travelled', stats.distance, 'meters'); |
||||
console.log('It took', stats.elapsed_time, 'seconds'); |
||||
}); |
||||
function pointSender(lat, lng) { |
||||
return function(callback) { |
||||
console.log('Visiting point ' + lat/COORD_FACTOR + ', ' + |
||||
lng/COORD_FACTOR); |
||||
call.write({ |
||||
latitude: lat, |
||||
longitude: lng |
||||
}); |
||||
_.delay(callback, _.random(500, 1500)); |
||||
}; |
||||
} |
||||
var point_senders = []; |
||||
for (var i = 0; i < num_points; i++) { |
||||
var rand_point = feature_list[_.random(0, feature_list.length - 1)]; |
||||
point_senders[i] = pointSender(rand_point.location.latitude, |
||||
rand_point.location.longitude); |
||||
} |
||||
async.series(point_senders, function() { |
||||
call.end(); |
||||
}); |
||||
``` |
||||
|
||||
Once we've finished writing our client's requests to the stream using `write()`, we need to call `end()` on the stream to let gRPC know that we've finished writing. If the status is `OK`, the `stats` object will be populated with the server's response. |
||||
|
||||
Finally, let's look at our bidirectional streaming RPC `routeChat()`. In this case, we just pass a context to the method and get back a `Duplex` stream object, which we can use to both write and read messages. |
||||
|
||||
```node |
||||
var call = client.routeChat(); |
||||
``` |
||||
|
||||
The syntax for reading and writing here is exactly the same as for our client-streaming and server-streaming methods. Although each side will always get the other's messages in the order they were written, both the client and server can read and write in any order — the streams operate completely independently. |
||||
|
||||
## Try it out! |
||||
|
||||
Build client and server: |
||||
```shell |
||||
$ npm install |
||||
``` |
||||
Run the server, which will listen on port 50051: |
||||
```shell |
||||
$ node ./route_guide_server.js --db_path=route_guide_db.json |
||||
``` |
||||
Run the client (in a different terminal): |
||||
```shell |
||||
$ node ./route_guide_client.js --db_path=route_guide_db.json |
||||
``` |
@ -0,0 +1,120 @@ |
||||
// Copyright 2015, Google Inc. |
||||
// All rights reserved. |
||||
// |
||||
// Redistribution and use in source and binary forms, with or without |
||||
// modification, are permitted provided that the following conditions are |
||||
// met: |
||||
// |
||||
// * Redistributions of source code must retain the above copyright |
||||
// notice, this list of conditions and the following disclaimer. |
||||
// * Redistributions in binary form must reproduce the above |
||||
// copyright notice, this list of conditions and the following disclaimer |
||||
// in the documentation and/or other materials provided with the |
||||
// distribution. |
||||
// * Neither the name of Google Inc. nor the names of its |
||||
// contributors may be used to endorse or promote products derived from |
||||
// this software without specific prior written permission. |
||||
// |
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
|
||||
syntax = "proto3"; |
||||
|
||||
option java_package = "io.grpc.examples"; |
||||
|
||||
package examples; |
||||
|
||||
// Interface exported by the server. |
||||
service RouteGuide { |
||||
// A simple RPC. |
||||
// |
||||
// Obtains the feature at a given position. |
||||
rpc GetFeature(Point) returns (Feature) {} |
||||
|
||||
// A server-to-client streaming RPC. |
||||
// |
||||
// Obtains the Features available within the given Rectangle. Results are |
||||
// streamed rather than returned at once (e.g. in a response message with a |
||||
// repeated field), as the rectangle may cover a large area and contain a |
||||
// huge number of features. |
||||
rpc ListFeatures(Rectangle) returns (stream Feature) {} |
||||
|
||||
// A client-to-server streaming RPC. |
||||
// |
||||
// Accepts a stream of Points on a route being traversed, returning a |
||||
// RouteSummary when traversal is completed. |
||||
rpc RecordRoute(stream Point) returns (RouteSummary) {} |
||||
|
||||
// A Bidirectional streaming RPC. |
||||
// |
||||
// Accepts a stream of RouteNotes sent while a route is being traversed, |
||||
// while receiving other RouteNotes (e.g. from other users). |
||||
rpc RouteChat(stream RouteNote) returns (stream RouteNote) {} |
||||
} |
||||
|
||||
// Points are represented as latitude-longitude pairs in the E7 representation |
||||
// (degrees multiplied by 10**7 and rounded to the nearest integer). |
||||
// Latitudes should be in the range +/- 90 degrees and longitude should be in |
||||
// the range +/- 180 degrees (inclusive). |
||||
message Point { |
||||
optional int32 latitude = 1; |
||||
optional int32 longitude = 2; |
||||
} |
||||
|
||||
// A latitude-longitude rectangle, represented as two diagonally opposite |
||||
// points "lo" and "hi". |
||||
message Rectangle { |
||||
// One corner of the rectangle. |
||||
optional Point lo = 1; |
||||
|
||||
// The other corner of the rectangle. |
||||
optional Point hi = 2; |
||||
} |
||||
|
||||
// A feature names something at a given point. |
||||
// |
||||
// If a feature could not be named, the name is empty. |
||||
message Feature { |
||||
// The name of the feature. |
||||
optional string name = 1; |
||||
|
||||
// The point where the feature is detected. |
||||
optional Point location = 2; |
||||
} |
||||
|
||||
// A RouteNote is a message sent while at a given point. |
||||
message RouteNote { |
||||
// The location from which the message is sent. |
||||
optional Point location = 1; |
||||
|
||||
// The message to be sent. |
||||
optional string message = 2; |
||||
} |
||||
|
||||
// A RouteSummary is received in response to a RecordRoute rpc. |
||||
// |
||||
// It contains the number of individual points received, the number of |
||||
// detected features, and the total distance covered as the cumulative sum of |
||||
// the distance between each point. |
||||
message RouteSummary { |
||||
// The number of points received. |
||||
optional int32 point_count = 1; |
||||
|
||||
// The number of known features passed while traversing the route. |
||||
optional int32 feature_count = 2; |
||||
|
||||
// The distance covered in metres. |
||||
optional int32 distance = 3; |
||||
|
||||
// The duration of the traversal in seconds. |
||||
optional int32 elapsed_time = 4; |
||||
} |
@ -0,0 +1,231 @@ |
||||
// Copyright 2015, Google Inc.
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
var async = require('async'); |
||||
var fs = require('fs'); |
||||
var parseArgs = require('minimist'); |
||||
var path = require('path'); |
||||
var _ = require('underscore'); |
||||
var grpc = require('grpc'); |
||||
var examples = grpc.load(__dirname + '/route_guide.proto').examples; |
||||
var client = new examples.RouteGuide('localhost:50051'); |
||||
|
||||
var COORD_FACTOR = 1e7; |
||||
|
||||
/** |
||||
* Run the getFeature demo. Calls getFeature with a point known to have a |
||||
* feature and a point known not to have a feature. |
||||
* @param {function} callback Called when this demo is complete |
||||
*/ |
||||
function runGetFeature(callback) { |
||||
var next = _.after(2, callback); |
||||
function featureCallback(error, feature) { |
||||
if (error) { |
||||
callback(error); |
||||
} |
||||
if (feature.name === '') { |
||||
console.log('Found no feature at ' + |
||||
feature.location.latitude/COORD_FACTOR + ', ' + |
||||
feature.location.longitude/COORD_FACTOR); |
||||
} else { |
||||
console.log('Found feature called "' + feature.name + '" at ' + |
||||
feature.location.latitude/COORD_FACTOR + ', ' + |
||||
feature.location.longitude/COORD_FACTOR); |
||||
} |
||||
next(); |
||||
} |
||||
var point1 = { |
||||
latitude: 409146138, |
||||
longitude: -746188906 |
||||
}; |
||||
var point2 = { |
||||
latitude: 0, |
||||
longitude: 0 |
||||
}; |
||||
client.getFeature(point1, featureCallback); |
||||
client.getFeature(point2, featureCallback); |
||||
} |
||||
|
||||
/** |
||||
* Run the listFeatures demo. Calls listFeatures with a rectangle containing all |
||||
* of the features in the pre-generated database. Prints each response as it |
||||
* comes in. |
||||
* @param {function} callback Called when this demo is complete |
||||
*/ |
||||
function runListFeatures(callback) { |
||||
var rectangle = { |
||||
lo: { |
||||
latitude: 400000000, |
||||
longitude: -750000000 |
||||
}, |
||||
hi: { |
||||
latitude: 420000000, |
||||
longitude: -730000000 |
||||
} |
||||
}; |
||||
console.log('Looking for features between 40, -75 and 42, -73'); |
||||
var call = client.listFeatures(rectangle); |
||||
call.on('data', function(feature) { |
||||
console.log('Found feature called "' + feature.name + '" at ' + |
||||
feature.location.latitude/COORD_FACTOR + ', ' + |
||||
feature.location.longitude/COORD_FACTOR); |
||||
}); |
||||
call.on('end', callback); |
||||
} |
||||
|
||||
/** |
||||
* Run the recordRoute demo. Sends several randomly chosen points from the |
||||
* pre-generated feature database with a variable delay in between. Prints the |
||||
* statistics when they are sent from the server. |
||||
* @param {function} callback Called when this demo is complete |
||||
*/ |
||||
function runRecordRoute(callback) { |
||||
var argv = parseArgs(process.argv, { |
||||
string: 'db_path' |
||||
}); |
||||
fs.readFile(path.resolve(argv.db_path), function(err, data) { |
||||
if (err) callback(err); |
||||
var feature_list = JSON.parse(data); |
||||
|
||||
var num_points = 10; |
||||
var call = client.recordRoute(function(error, stats) { |
||||
if (error) { |
||||
callback(error); |
||||
} |
||||
console.log('Finished trip with', stats.point_count, 'points'); |
||||
console.log('Passed', stats.feature_count, 'features'); |
||||
console.log('Travelled', stats.distance, 'meters'); |
||||
console.log('It took', stats.elapsed_time, 'seconds'); |
||||
callback(); |
||||
}); |
||||
/** |
||||
* Constructs a function that asynchronously sends the given point and then |
||||
* delays sending its callback |
||||
* @param {number} lat The latitude to send |
||||
* @param {number} lng The longitude to send |
||||
* @return {function(function)} The function that sends the point |
||||
*/ |
||||
function pointSender(lat, lng) { |
||||
/** |
||||
* Sends the point, then calls the callback after a delay |
||||
* @param {function} callback Called when complete |
||||
*/ |
||||
return function(callback) { |
||||
console.log('Visiting point ' + lat/COORD_FACTOR + ', ' + |
||||
lng/COORD_FACTOR); |
||||
call.write({ |
||||
latitude: lat, |
||||
longitude: lng |
||||
}); |
||||
_.delay(callback, _.random(500, 1500)); |
||||
}; |
||||
} |
||||
var point_senders = []; |
||||
for (var i = 0; i < num_points; i++) { |
||||
var rand_point = feature_list[_.random(0, feature_list.length - 1)]; |
||||
point_senders[i] = pointSender(rand_point.location.latitude, |
||||
rand_point.location.longitude); |
||||
} |
||||
async.series(point_senders, function() { |
||||
call.end(); |
||||
}); |
||||
}); |
||||
} |
||||
|
||||
/** |
||||
* Run the routeChat demo. Send some chat messages, and print any chat messages |
||||
* that are sent from the server. |
||||
* @param {function} callback Called when the demo is complete |
||||
*/ |
||||
function runRouteChat(callback) { |
||||
var call = client.routeChat(); |
||||
call.on('data', function(note) { |
||||
console.log('Got message "' + note.message + '" at ' + |
||||
note.location.latitude + ', ' + note.location.longitude); |
||||
}); |
||||
|
||||
call.on('end', callback); |
||||
|
||||
var notes = [{ |
||||
location: { |
||||
latitude: 0, |
||||
longitude: 0 |
||||
}, |
||||
message: 'First message' |
||||
}, { |
||||
location: { |
||||
latitude: 0, |
||||
longitude: 1 |
||||
}, |
||||
message: 'Second message' |
||||
}, { |
||||
location: { |
||||
latitude: 1, |
||||
longitude: 0 |
||||
}, |
||||
message: 'Third message' |
||||
}, { |
||||
location: { |
||||
latitude: 0, |
||||
longitude: 0 |
||||
}, |
||||
message: 'Fourth message' |
||||
}]; |
||||
for (var i = 0; i < notes.length; i++) { |
||||
var note = notes[i]; |
||||
console.log('Sending message "' + note.message + '" at ' + |
||||
note.location.latitude + ', ' + note.location.longitude); |
||||
call.write(note); |
||||
} |
||||
call.end(); |
||||
} |
||||
|
||||
/** |
||||
* Run all of the demos in order |
||||
*/ |
||||
function main() { |
||||
async.series([ |
||||
runGetFeature, |
||||
runListFeatures, |
||||
runRecordRoute, |
||||
runRouteChat |
||||
]); |
||||
} |
||||
|
||||
if (require.main === module) { |
||||
main(); |
||||
} |
||||
|
||||
exports.runGetFeature = runGetFeature; |
||||
|
||||
exports.runListFeatures = runListFeatures; |
||||
|
||||
exports.runRecordRoute = runRecordRoute; |
||||
|
||||
exports.runRouteChat = runRouteChat; |
@ -0,0 +1,601 @@ |
||||
[{ |
||||
"location": { |
||||
"latitude": 407838351, |
||||
"longitude": -746143763 |
||||
}, |
||||
"name": "Patriots Path, Mendham, NJ 07945, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 408122808, |
||||
"longitude": -743999179 |
||||
}, |
||||
"name": "101 New Jersey 10, Whippany, NJ 07981, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 413628156, |
||||
"longitude": -749015468 |
||||
}, |
||||
"name": "U.S. 6, Shohola, PA 18458, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 419999544, |
||||
"longitude": -740371136 |
||||
}, |
||||
"name": "5 Conners Road, Kingston, NY 12401, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 414008389, |
||||
"longitude": -743951297 |
||||
}, |
||||
"name": "Mid Hudson Psychiatric Center, New Hampton, NY 10958, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 419611318, |
||||
"longitude": -746524769 |
||||
}, |
||||
"name": "287 Flugertown Road, Livingston Manor, NY 12758, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 406109563, |
||||
"longitude": -742186778 |
||||
}, |
||||
"name": "4001 Tremley Point Road, Linden, NJ 07036, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 416802456, |
||||
"longitude": -742370183 |
||||
}, |
||||
"name": "352 South Mountain Road, Wallkill, NY 12589, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 412950425, |
||||
"longitude": -741077389 |
||||
}, |
||||
"name": "Bailey Turn Road, Harriman, NY 10926, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 412144655, |
||||
"longitude": -743949739 |
||||
}, |
||||
"name": "193-199 Wawayanda Road, Hewitt, NJ 07421, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 415736605, |
||||
"longitude": -742847522 |
||||
}, |
||||
"name": "406-496 Ward Avenue, Pine Bush, NY 12566, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 413843930, |
||||
"longitude": -740501726 |
||||
}, |
||||
"name": "162 Merrill Road, Highland Mills, NY 10930, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 410873075, |
||||
"longitude": -744459023 |
||||
}, |
||||
"name": "Clinton Road, West Milford, NJ 07480, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 412346009, |
||||
"longitude": -744026814 |
||||
}, |
||||
"name": "16 Old Brook Lane, Warwick, NY 10990, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 402948455, |
||||
"longitude": -747903913 |
||||
}, |
||||
"name": "3 Drake Lane, Pennington, NJ 08534, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 406337092, |
||||
"longitude": -740122226 |
||||
}, |
||||
"name": "6324 8th Avenue, Brooklyn, NY 11220, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 406421967, |
||||
"longitude": -747727624 |
||||
}, |
||||
"name": "1 Merck Access Road, Whitehouse Station, NJ 08889, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 416318082, |
||||
"longitude": -749677716 |
||||
}, |
||||
"name": "78-98 Schalck Road, Narrowsburg, NY 12764, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 415301720, |
||||
"longitude": -748416257 |
||||
}, |
||||
"name": "282 Lakeview Drive Road, Highland Lake, NY 12743, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 402647019, |
||||
"longitude": -747071791 |
||||
}, |
||||
"name": "330 Evelyn Avenue, Hamilton Township, NJ 08619, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 412567807, |
||||
"longitude": -741058078 |
||||
}, |
||||
"name": "New York State Reference Route 987E, Southfields, NY 10975, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 416855156, |
||||
"longitude": -744420597 |
||||
}, |
||||
"name": "103-271 Tempaloni Road, Ellenville, NY 12428, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 404663628, |
||||
"longitude": -744820157 |
||||
}, |
||||
"name": "1300 Airport Road, North Brunswick Township, NJ 08902, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 407113723, |
||||
"longitude": -749746483 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 402133926, |
||||
"longitude": -743613249 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 400273442, |
||||
"longitude": -741220915 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 411236786, |
||||
"longitude": -744070769 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 411633782, |
||||
"longitude": -746784970 |
||||
}, |
||||
"name": "211-225 Plains Road, Augusta, NJ 07822, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 415830701, |
||||
"longitude": -742952812 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 413447164, |
||||
"longitude": -748712898 |
||||
}, |
||||
"name": "165 Pedersen Ridge Road, Milford, PA 18337, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 405047245, |
||||
"longitude": -749800722 |
||||
}, |
||||
"name": "100-122 Locktown Road, Frenchtown, NJ 08825, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 418858923, |
||||
"longitude": -746156790 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 417951888, |
||||
"longitude": -748484944 |
||||
}, |
||||
"name": "650-652 Willi Hill Road, Swan Lake, NY 12783, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 407033786, |
||||
"longitude": -743977337 |
||||
}, |
||||
"name": "26 East 3rd Street, New Providence, NJ 07974, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 417548014, |
||||
"longitude": -740075041 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 410395868, |
||||
"longitude": -744972325 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 404615353, |
||||
"longitude": -745129803 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 406589790, |
||||
"longitude": -743560121 |
||||
}, |
||||
"name": "611 Lawrence Avenue, Westfield, NJ 07090, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 414653148, |
||||
"longitude": -740477477 |
||||
}, |
||||
"name": "18 Lannis Avenue, New Windsor, NY 12553, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 405957808, |
||||
"longitude": -743255336 |
||||
}, |
||||
"name": "82-104 Amherst Avenue, Colonia, NJ 07067, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 411733589, |
||||
"longitude": -741648093 |
||||
}, |
||||
"name": "170 Seven Lakes Drive, Sloatsburg, NY 10974, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 412676291, |
||||
"longitude": -742606606 |
||||
}, |
||||
"name": "1270 Lakes Road, Monroe, NY 10950, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 409224445, |
||||
"longitude": -748286738 |
||||
}, |
||||
"name": "509-535 Alphano Road, Great Meadows, NJ 07838, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 406523420, |
||||
"longitude": -742135517 |
||||
}, |
||||
"name": "652 Garden Street, Elizabeth, NJ 07202, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 401827388, |
||||
"longitude": -740294537 |
||||
}, |
||||
"name": "349 Sea Spray Court, Neptune City, NJ 07753, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 410564152, |
||||
"longitude": -743685054 |
||||
}, |
||||
"name": "13-17 Stanley Street, West Milford, NJ 07480, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 408472324, |
||||
"longitude": -740726046 |
||||
}, |
||||
"name": "47 Industrial Avenue, Teterboro, NJ 07608, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 412452168, |
||||
"longitude": -740214052 |
||||
}, |
||||
"name": "5 White Oak Lane, Stony Point, NY 10980, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 409146138, |
||||
"longitude": -746188906 |
||||
}, |
||||
"name": "Berkshire Valley Management Area Trail, Jefferson, NJ, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 404701380, |
||||
"longitude": -744781745 |
||||
}, |
||||
"name": "1007 Jersey Avenue, New Brunswick, NJ 08901, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 409642566, |
||||
"longitude": -746017679 |
||||
}, |
||||
"name": "6 East Emerald Isle Drive, Lake Hopatcong, NJ 07849, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 408031728, |
||||
"longitude": -748645385 |
||||
}, |
||||
"name": "1358-1474 New Jersey 57, Port Murray, NJ 07865, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 413700272, |
||||
"longitude": -742135189 |
||||
}, |
||||
"name": "367 Prospect Road, Chester, NY 10918, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 404310607, |
||||
"longitude": -740282632 |
||||
}, |
||||
"name": "10 Simon Lake Drive, Atlantic Highlands, NJ 07716, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 409319800, |
||||
"longitude": -746201391 |
||||
}, |
||||
"name": "11 Ward Street, Mount Arlington, NJ 07856, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 406685311, |
||||
"longitude": -742108603 |
||||
}, |
||||
"name": "300-398 Jefferson Avenue, Elizabeth, NJ 07201, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 419018117, |
||||
"longitude": -749142781 |
||||
}, |
||||
"name": "43 Dreher Road, Roscoe, NY 12776, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 412856162, |
||||
"longitude": -745148837 |
||||
}, |
||||
"name": "Swan Street, Pine Island, NY 10969, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 416560744, |
||||
"longitude": -746721964 |
||||
}, |
||||
"name": "66 Pleasantview Avenue, Monticello, NY 12701, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 405314270, |
||||
"longitude": -749836354 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 414219548, |
||||
"longitude": -743327440 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 415534177, |
||||
"longitude": -742900616 |
||||
}, |
||||
"name": "565 Winding Hills Road, Montgomery, NY 12549, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 406898530, |
||||
"longitude": -749127080 |
||||
}, |
||||
"name": "231 Rocky Run Road, Glen Gardner, NJ 08826, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 407586880, |
||||
"longitude": -741670168 |
||||
}, |
||||
"name": "100 Mount Pleasant Avenue, Newark, NJ 07104, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 400106455, |
||||
"longitude": -742870190 |
||||
}, |
||||
"name": "517-521 Huntington Drive, Manchester Township, NJ 08759, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 400066188, |
||||
"longitude": -746793294 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 418803880, |
||||
"longitude": -744102673 |
||||
}, |
||||
"name": "40 Mountain Road, Napanoch, NY 12458, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 414204288, |
||||
"longitude": -747895140 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 414777405, |
||||
"longitude": -740615601 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 415464475, |
||||
"longitude": -747175374 |
||||
}, |
||||
"name": "48 North Road, Forestburgh, NY 12777, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 404062378, |
||||
"longitude": -746376177 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 405688272, |
||||
"longitude": -749285130 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 400342070, |
||||
"longitude": -748788996 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 401809022, |
||||
"longitude": -744157964 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 404226644, |
||||
"longitude": -740517141 |
||||
}, |
||||
"name": "9 Thompson Avenue, Leonardo, NJ 07737, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 410322033, |
||||
"longitude": -747871659 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 407100674, |
||||
"longitude": -747742727 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 418811433, |
||||
"longitude": -741718005 |
||||
}, |
||||
"name": "213 Bush Road, Stone Ridge, NY 12484, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 415034302, |
||||
"longitude": -743850945 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 411349992, |
||||
"longitude": -743694161 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 404839914, |
||||
"longitude": -744759616 |
||||
}, |
||||
"name": "1-17 Bergen Court, New Brunswick, NJ 08901, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 414638017, |
||||
"longitude": -745957854 |
||||
}, |
||||
"name": "35 Oakland Valley Road, Cuddebackville, NY 12729, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 412127800, |
||||
"longitude": -740173578 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 401263460, |
||||
"longitude": -747964303 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 412843391, |
||||
"longitude": -749086026 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 418512773, |
||||
"longitude": -743067823 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 404318328, |
||||
"longitude": -740835638 |
||||
}, |
||||
"name": "42-102 Main Street, Belford, NJ 07718, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 419020746, |
||||
"longitude": -741172328 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 404080723, |
||||
"longitude": -746119569 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 401012643, |
||||
"longitude": -744035134 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 404306372, |
||||
"longitude": -741079661 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 403966326, |
||||
"longitude": -748519297 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 405002031, |
||||
"longitude": -748407866 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 409532885, |
||||
"longitude": -742200683 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 416851321, |
||||
"longitude": -742674555 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 406411633, |
||||
"longitude": -741722051 |
||||
}, |
||||
"name": "3387 Richmond Terrace, Staten Island, NY 10303, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 413069058, |
||||
"longitude": -744597778 |
||||
}, |
||||
"name": "261 Van Sickle Road, Goshen, NY 10924, USA" |
||||
}, { |
||||
"location": { |
||||
"latitude": 418465462, |
||||
"longitude": -746859398 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 411733222, |
||||
"longitude": -744228360 |
||||
}, |
||||
"name": "" |
||||
}, { |
||||
"location": { |
||||
"latitude": 410248224, |
||||
"longitude": -747127767 |
||||
}, |
||||
"name": "3 Hasta Way, Newton, NJ 07860, USA" |
||||
}] |
@ -0,0 +1,249 @@ |
||||
// Copyright 2015, Google Inc.
|
||||
// All rights reserved.
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are
|
||||
// met:
|
||||
//
|
||||
// * Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
// * Redistributions in binary form must reproduce the above
|
||||
// copyright notice, this list of conditions and the following disclaimer
|
||||
// in the documentation and/or other materials provided with the
|
||||
// distribution.
|
||||
// * Neither the name of Google Inc. nor the names of its
|
||||
// contributors may be used to endorse or promote products derived from
|
||||
// this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
var fs = require('fs'); |
||||
var parseArgs = require('minimist'); |
||||
var path = require('path'); |
||||
var _ = require('underscore'); |
||||
var grpc = require('grpc'); |
||||
var examples = grpc.load(__dirname + '/route_guide.proto').examples; |
||||
|
||||
var Server = grpc.buildServer([examples.RouteGuide.service]); |
||||
|
||||
var COORD_FACTOR = 1e7; |
||||
|
||||
/** |
||||
* For simplicity, a point is a record type that looks like |
||||
* {latitude: number, longitude: number}, and a feature is a record type that |
||||
* looks like {name: string, location: point}. feature objects with name==='' |
||||
* are points with no feature. |
||||
*/ |
||||
|
||||
/** |
||||
* List of feature objects at points that have been requested so far. |
||||
*/ |
||||
var feature_list = []; |
||||
|
||||
/** |
||||
* Get a feature object at the given point, or creates one if it does not exist. |
||||
* @param {point} point The point to check |
||||
* @return {feature} The feature object at the point. Note that an empty name |
||||
* indicates no feature |
||||
*/ |
||||
function checkFeature(point) { |
||||
var feature; |
||||
// Check if there is already a feature object for the given point
|
||||
for (var i = 0; i < feature_list.length; i++) { |
||||
feature = feature_list[i]; |
||||
if (feature.location.latitude === point.latitude && |
||||
feature.location.longitude === point.longitude) { |
||||
return feature; |
||||
} |
||||
} |
||||
var name = ''; |
||||
feature = { |
||||
name: name, |
||||
location: point |
||||
}; |
||||
return feature; |
||||
} |
||||
|
||||
/** |
||||
* getFeature request handler. Gets a request with a point, and responds with a |
||||
* feature object indicating whether there is a feature at that point. |
||||
* @param {EventEmitter} call Call object for the handler to process |
||||
* @param {function(Error, feature)} callback Response callback |
||||
*/ |
||||
function getFeature(call, callback) { |
||||
callback(null, checkFeature(call.request)); |
||||
} |
||||
|
||||
/** |
||||
* listFeatures request handler. Gets a request with two points, and responds |
||||
* with a stream of all features in the bounding box defined by those points. |
||||
* @param {Writable} call Writable stream for responses with an additional |
||||
* request property for the request value. |
||||
*/ |
||||
function listFeatures(call) { |
||||
var lo = call.request.lo; |
||||
var hi = call.request.hi; |
||||
var left = _.min([lo.longitude, hi.longitude]); |
||||
var right = _.max([lo.longitude, hi.longitude]); |
||||
var top = _.max([lo.latitude, hi.latitude]); |
||||
var bottom = _.min([lo.latitude, hi.latitude]); |
||||
// For each feature, check if it is in the given bounding box
|
||||
_.each(feature_list, function(feature) { |
||||
if (feature.name === '') { |
||||
return; |
||||
} |
||||
if (feature.location.longitude >= left && |
||||
feature.location.longitude <= right && |
||||
feature.location.latitude >= bottom && |
||||
feature.location.latitude <= top) { |
||||
call.write(feature); |
||||
} |
||||
}); |
||||
call.end(); |
||||
} |
||||
|
||||
/** |
||||
* Calculate the distance between two points using the "haversine" formula. |
||||
* This code was taken from http://www.movable-type.co.uk/scripts/latlong.html.
|
||||
* @param start The starting point |
||||
* @param end The end point |
||||
* @return The distance between the points in meters |
||||
*/ |
||||
function getDistance(start, end) { |
||||
function toRadians(num) { |
||||
return num * Math.PI / 180; |
||||
} |
||||
var lat1 = start.latitude / COORD_FACTOR; |
||||
var lat2 = end.latitude / COORD_FACTOR; |
||||
var lon1 = start.longitude / COORD_FACTOR; |
||||
var lon2 = end.longitude / COORD_FACTOR; |
||||
var R = 6371000; // metres
|
||||
var φ1 = toRadians(lat1); |
||||
var φ2 = toRadians(lat2); |
||||
var Δφ = toRadians(lat2-lat1); |
||||
var Δλ = toRadians(lon2-lon1); |
||||
|
||||
var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) + |
||||
Math.cos(φ1) * Math.cos(φ2) * |
||||
Math.sin(Δλ/2) * Math.sin(Δλ/2); |
||||
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); |
||||
|
||||
return R * c; |
||||
} |
||||
|
||||
/** |
||||
* recordRoute handler. Gets a stream of points, and responds with statistics |
||||
* about the "trip": number of points, number of known features visited, total |
||||
* distance traveled, and total time spent. |
||||
* @param {Readable} call The request point stream. |
||||
* @param {function(Error, routeSummary)} callback The callback to pass the |
||||
* response to |
||||
*/ |
||||
function recordRoute(call, callback) { |
||||
var point_count = 0; |
||||
var feature_count = 0; |
||||
var distance = 0; |
||||
var previous = null; |
||||
// Start a timer
|
||||
var start_time = process.hrtime(); |
||||
call.on('data', function(point) { |
||||
point_count += 1; |
||||
if (checkFeature(point).name !== '') { |
||||
feature_count += 1; |
||||
} |
||||
/* For each point after the first, add the incremental distance from the |
||||
* previous point to the total distance value */ |
||||
if (previous != null) { |
||||
distance += getDistance(previous, point); |
||||
} |
||||
previous = point; |
||||
}); |
||||
call.on('end', function() { |
||||
callback(null, { |
||||
point_count: point_count, |
||||
feature_count: feature_count, |
||||
// Cast the distance to an integer
|
||||
distance: distance|0, |
||||
// End the timer
|
||||
elapsed_time: process.hrtime(start_time)[0] |
||||
}); |
||||
}); |
||||
} |
||||
|
||||
var route_notes = {}; |
||||
|
||||
/** |
||||
* Turn the point into a dictionary key. |
||||
* @param {point} point The point to use |
||||
* @return {string} The key for an object |
||||
*/ |
||||
function pointKey(point) { |
||||
return point.latitude + ' ' + point.longitude; |
||||
} |
||||
|
||||
/** |
||||
* routeChat handler. Receives a stream of message/location pairs, and responds |
||||
* with a stream of all previous messages at each of those locations. |
||||
* @param {Duplex} call The stream for incoming and outgoing messages |
||||
*/ |
||||
function routeChat(call) { |
||||
call.on('data', function(note) { |
||||
var key = pointKey(note.location); |
||||
/* For each note sent, respond with all previous notes that correspond to |
||||
* the same point */ |
||||
if (route_notes.hasOwnProperty(key)) { |
||||
_.each(route_notes[key], function(note) { |
||||
call.write(note); |
||||
}); |
||||
} else { |
||||
route_notes[key] = []; |
||||
} |
||||
// Then add the new note to the list
|
||||
route_notes[key].push(JSON.parse(JSON.stringify(note))); |
||||
}); |
||||
call.on('end', function() { |
||||
call.end(); |
||||
}); |
||||
} |
||||
|
||||
/** |
||||
* Get a new server with the handler functions in this file bound to the methods |
||||
* it serves. |
||||
* @return {Server} The new server object |
||||
*/ |
||||
function getServer() { |
||||
return new Server({ |
||||
'examples.RouteGuide' : { |
||||
getFeature: getFeature, |
||||
listFeatures: listFeatures, |
||||
recordRoute: recordRoute, |
||||
routeChat: routeChat |
||||
} |
||||
}); |
||||
} |
||||
|
||||
if (require.main === module) { |
||||
// If this is run as a script, start a server on an unused port
|
||||
var routeServer = getServer(); |
||||
routeServer.bind('0.0.0.0:50051'); |
||||
var argv = parseArgs(process.argv, { |
||||
string: 'db_path' |
||||
}); |
||||
fs.readFile(path.resolve(argv.db_path), function(err, data) { |
||||
if (err) throw err; |
||||
feature_list = JSON.parse(data); |
||||
routeServer.listen(); |
||||
}); |
||||
} |
||||
|
||||
exports.getServer = getServer; |
@ -0,0 +1,366 @@ |
||||
// !$*UTF8*$! |
||||
{ |
||||
archiveVersion = 1; |
||||
classes = { |
||||
}; |
||||
objectVersion = 46; |
||||
objects = { |
||||
|
||||
/* Begin PBXBuildFile section */ |
||||
63E1E9821B28CB2100EF0978 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 63E1E9811B28CB2100EF0978 /* main.m */; }; |
||||
63E1E9851B28CB2100EF0978 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 63E1E9841B28CB2100EF0978 /* AppDelegate.m */; }; |
||||
63E1E9881B28CB2100EF0978 /* SelectUserViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 63E1E9871B28CB2100EF0978 /* SelectUserViewController.m */; }; |
||||
63E1E98B1B28CB2100EF0978 /* MakeRPCViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 63E1E98A1B28CB2100EF0978 /* MakeRPCViewController.m */; }; |
||||
63E1E98E1B28CB2100EF0978 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 63E1E98C1B28CB2100EF0978 /* Main.storyboard */; }; |
||||
63E1E9901B28CB2100EF0978 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 63E1E98F1B28CB2100EF0978 /* Images.xcassets */; }; |
||||
63F5DE481B28F5C100CDD07E /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 63F5DE471B28F5C100CDD07E /* GoogleService-Info.plist */; }; |
||||
832213142AB24DB816D02635 /* libPods-AuthSample.a in Frameworks */ = {isa = PBXBuildFile; fileRef = F217A6ECA7F5BD1D5FB5071B /* libPods-AuthSample.a */; }; |
||||
/* End PBXBuildFile section */ |
||||
|
||||
/* Begin PBXFileReference section */ |
||||
63E1E97C1B28CB2100EF0978 /* AuthSample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AuthSample.app; sourceTree = BUILT_PRODUCTS_DIR; }; |
||||
63E1E9801B28CB2100EF0978 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; }; |
||||
63E1E9811B28CB2100EF0978 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; }; |
||||
63E1E9831B28CB2100EF0978 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; }; |
||||
63E1E9841B28CB2100EF0978 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; }; |
||||
63E1E9861B28CB2100EF0978 /* SelectUserViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SelectUserViewController.h; sourceTree = "<group>"; }; |
||||
63E1E9871B28CB2100EF0978 /* SelectUserViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SelectUserViewController.m; sourceTree = "<group>"; }; |
||||
63E1E9891B28CB2100EF0978 /* MakeRPCViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MakeRPCViewController.h; sourceTree = "<group>"; }; |
||||
63E1E98A1B28CB2100EF0978 /* MakeRPCViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MakeRPCViewController.m; sourceTree = "<group>"; }; |
||||
63E1E98D1B28CB2100EF0978 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; }; |
||||
63E1E98F1B28CB2100EF0978 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; }; |
||||
63F5DE471B28F5C100CDD07E /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "GoogleService-Info.plist"; sourceTree = "<group>"; }; |
||||
A387D6CECBCF2EAF2983033A /* Pods-AuthSample.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AuthSample.debug.xcconfig"; path = "Pods/Target Support Files/Pods-AuthSample/Pods-AuthSample.debug.xcconfig"; sourceTree = "<group>"; }; |
||||
B444176735DA81FBE4B8B80C /* Pods-AuthSample.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AuthSample.release.xcconfig"; path = "Pods/Target Support Files/Pods-AuthSample/Pods-AuthSample.release.xcconfig"; sourceTree = "<group>"; }; |
||||
F217A6ECA7F5BD1D5FB5071B /* libPods-AuthSample.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-AuthSample.a"; sourceTree = BUILT_PRODUCTS_DIR; }; |
||||
/* End PBXFileReference section */ |
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */ |
||||
63E1E9791B28CB2000EF0978 /* Frameworks */ = { |
||||
isa = PBXFrameworksBuildPhase; |
||||
buildActionMask = 2147483647; |
||||
files = ( |
||||
832213142AB24DB816D02635 /* libPods-AuthSample.a in Frameworks */, |
||||
); |
||||
runOnlyForDeploymentPostprocessing = 0; |
||||
}; |
||||
/* End PBXFrameworksBuildPhase section */ |
||||
|
||||
/* Begin PBXGroup section */ |
||||
021FA0E0B3F5D3D477DDAC10 /* Pods */ = { |
||||
isa = PBXGroup; |
||||
children = ( |
||||
A387D6CECBCF2EAF2983033A /* Pods-AuthSample.debug.xcconfig */, |
||||
B444176735DA81FBE4B8B80C /* Pods-AuthSample.release.xcconfig */, |
||||
); |
||||
name = Pods; |
||||
sourceTree = "<group>"; |
||||
}; |
||||
4F443572636F3D60F26E870D /* Frameworks */ = { |
||||
isa = PBXGroup; |
||||
children = ( |
||||
F217A6ECA7F5BD1D5FB5071B /* libPods-AuthSample.a */, |
||||
); |
||||
name = Frameworks; |
||||
sourceTree = "<group>"; |
||||
}; |
||||
63E1E9731B28CB2000EF0978 = { |
||||
isa = PBXGroup; |
||||
children = ( |
||||
63E1E97E1B28CB2100EF0978 /* AuthSample */, |
||||
63E1E97D1B28CB2100EF0978 /* Products */, |
||||
021FA0E0B3F5D3D477DDAC10 /* Pods */, |
||||
4F443572636F3D60F26E870D /* Frameworks */, |
||||
); |
||||
sourceTree = "<group>"; |
||||
}; |
||||
63E1E97D1B28CB2100EF0978 /* Products */ = { |
||||
isa = PBXGroup; |
||||
children = ( |
||||
63E1E97C1B28CB2100EF0978 /* AuthSample.app */, |
||||
); |
||||
name = Products; |
||||
sourceTree = "<group>"; |
||||
}; |
||||
63E1E97E1B28CB2100EF0978 /* AuthSample */ = { |
||||
isa = PBXGroup; |
||||
children = ( |
||||
63E1E9861B28CB2100EF0978 /* SelectUserViewController.h */, |
||||
63E1E9871B28CB2100EF0978 /* SelectUserViewController.m */, |
||||
63E1E9891B28CB2100EF0978 /* MakeRPCViewController.h */, |
||||
63E1E98A1B28CB2100EF0978 /* MakeRPCViewController.m */, |
||||
63E1E97F1B28CB2100EF0978 /* Supporting Files */, |
||||
); |
||||
name = AuthSample; |
||||
sourceTree = SOURCE_ROOT; |
||||
}; |
||||
63E1E97F1B28CB2100EF0978 /* Supporting Files */ = { |
||||
isa = PBXGroup; |
||||
children = ( |
||||
63E1E98C1B28CB2100EF0978 /* Main.storyboard */, |
||||
63F5DE471B28F5C100CDD07E /* GoogleService-Info.plist */, |
||||
63E1E98F1B28CB2100EF0978 /* Images.xcassets */, |
||||
63E1E9801B28CB2100EF0978 /* Info.plist */, |
||||
63E1E9831B28CB2100EF0978 /* AppDelegate.h */, |
||||
63E1E9841B28CB2100EF0978 /* AppDelegate.m */, |
||||
63E1E9811B28CB2100EF0978 /* main.m */, |
||||
); |
||||
name = "Supporting Files"; |
||||
path = Misc; |
||||
sourceTree = "<group>"; |
||||
}; |
||||
/* End PBXGroup section */ |
||||
|
||||
/* Begin PBXNativeTarget section */ |
||||
63E1E97B1B28CB2000EF0978 /* AuthSample */ = { |
||||
isa = PBXNativeTarget; |
||||
buildConfigurationList = 63E1E9A21B28CB2100EF0978 /* Build configuration list for PBXNativeTarget "AuthSample" */; |
||||
buildPhases = ( |
||||
DAABBA7B5788A39108D7CA83 /* Check Pods Manifest.lock */, |
||||
63E1E9781B28CB2000EF0978 /* Sources */, |
||||
63E1E9791B28CB2000EF0978 /* Frameworks */, |
||||
63E1E97A1B28CB2000EF0978 /* Resources */, |
||||
AEFCCC69DD59CE8F6EB769D7 /* Copy Pods Resources */, |
||||
); |
||||
buildRules = ( |
||||
); |
||||
dependencies = ( |
||||
); |
||||
name = AuthSample; |
||||
productName = AuthSample; |
||||
productReference = 63E1E97C1B28CB2100EF0978 /* AuthSample.app */; |
||||
productType = "com.apple.product-type.application"; |
||||
}; |
||||
/* End PBXNativeTarget section */ |
||||
|
||||
/* Begin PBXProject section */ |
||||
63E1E9741B28CB2000EF0978 /* Project object */ = { |
||||
isa = PBXProject; |
||||
attributes = { |
||||
LastUpgradeCheck = 0630; |
||||
ORGANIZATIONNAME = gRPC; |
||||
TargetAttributes = { |
||||
63E1E97B1B28CB2000EF0978 = { |
||||
CreatedOnToolsVersion = 6.3.1; |
||||
}; |
||||
}; |
||||
}; |
||||
buildConfigurationList = 63E1E9771B28CB2000EF0978 /* Build configuration list for PBXProject "AuthSample" */; |
||||
compatibilityVersion = "Xcode 3.2"; |
||||
developmentRegion = English; |
||||
hasScannedForEncodings = 0; |
||||
knownRegions = ( |
||||
en, |
||||
Base, |
||||
); |
||||
mainGroup = 63E1E9731B28CB2000EF0978; |
||||
productRefGroup = 63E1E97D1B28CB2100EF0978 /* Products */; |
||||
projectDirPath = ""; |
||||
projectRoot = ""; |
||||
targets = ( |
||||
63E1E97B1B28CB2000EF0978 /* AuthSample */, |
||||
); |
||||
}; |
||||
/* End PBXProject section */ |
||||
|
||||
/* Begin PBXResourcesBuildPhase section */ |
||||
63E1E97A1B28CB2000EF0978 /* Resources */ = { |
||||
isa = PBXResourcesBuildPhase; |
||||
buildActionMask = 2147483647; |
||||
files = ( |
||||
63E1E98E1B28CB2100EF0978 /* Main.storyboard in Resources */, |
||||
63E1E9901B28CB2100EF0978 /* Images.xcassets in Resources */, |
||||
63F5DE481B28F5C100CDD07E /* GoogleService-Info.plist in Resources */, |
||||
); |
||||
runOnlyForDeploymentPostprocessing = 0; |
||||
}; |
||||
/* End PBXResourcesBuildPhase section */ |
||||
|
||||
/* Begin PBXShellScriptBuildPhase section */ |
||||
AEFCCC69DD59CE8F6EB769D7 /* Copy Pods Resources */ = { |
||||
isa = PBXShellScriptBuildPhase; |
||||
buildActionMask = 2147483647; |
||||
files = ( |
||||
); |
||||
inputPaths = ( |
||||
); |
||||
name = "Copy Pods Resources"; |
||||
outputPaths = ( |
||||
); |
||||
runOnlyForDeploymentPostprocessing = 0; |
||||
shellPath = /bin/sh; |
||||
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-AuthSample/Pods-AuthSample-resources.sh\"\n"; |
||||
showEnvVarsInLog = 0; |
||||
}; |
||||
DAABBA7B5788A39108D7CA83 /* Check Pods Manifest.lock */ = { |
||||
isa = PBXShellScriptBuildPhase; |
||||
buildActionMask = 2147483647; |
||||
files = ( |
||||
); |
||||
inputPaths = ( |
||||
); |
||||
name = "Check Pods Manifest.lock"; |
||||
outputPaths = ( |
||||
); |
||||
runOnlyForDeploymentPostprocessing = 0; |
||||
shellPath = /bin/sh; |
||||
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n"; |
||||
showEnvVarsInLog = 0; |
||||
}; |
||||
/* End PBXShellScriptBuildPhase section */ |
||||
|
||||
/* Begin PBXSourcesBuildPhase section */ |
||||
63E1E9781B28CB2000EF0978 /* Sources */ = { |
||||
isa = PBXSourcesBuildPhase; |
||||
buildActionMask = 2147483647; |
||||
files = ( |
||||
63E1E98B1B28CB2100EF0978 /* MakeRPCViewController.m in Sources */, |
||||
63E1E9851B28CB2100EF0978 /* AppDelegate.m in Sources */, |
||||
63E1E9881B28CB2100EF0978 /* SelectUserViewController.m in Sources */, |
||||
63E1E9821B28CB2100EF0978 /* main.m in Sources */, |
||||
); |
||||
runOnlyForDeploymentPostprocessing = 0; |
||||
}; |
||||
/* End PBXSourcesBuildPhase section */ |
||||
|
||||
/* Begin PBXVariantGroup section */ |
||||
63E1E98C1B28CB2100EF0978 /* Main.storyboard */ = { |
||||
isa = PBXVariantGroup; |
||||
children = ( |
||||
63E1E98D1B28CB2100EF0978 /* Base */, |
||||
); |
||||
name = Main.storyboard; |
||||
sourceTree = "<group>"; |
||||
}; |
||||
/* End PBXVariantGroup section */ |
||||
|
||||
/* Begin XCBuildConfiguration section */ |
||||
63E1E9A01B28CB2100EF0978 /* Debug */ = { |
||||
isa = XCBuildConfiguration; |
||||
buildSettings = { |
||||
ALWAYS_SEARCH_USER_PATHS = NO; |
||||
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; |
||||
CLANG_CXX_LIBRARY = "libc++"; |
||||
CLANG_ENABLE_MODULES = YES; |
||||
CLANG_ENABLE_OBJC_ARC = YES; |
||||
CLANG_WARN_BOOL_CONVERSION = YES; |
||||
CLANG_WARN_CONSTANT_CONVERSION = YES; |
||||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; |
||||
CLANG_WARN_EMPTY_BODY = YES; |
||||
CLANG_WARN_ENUM_CONVERSION = YES; |
||||
CLANG_WARN_INT_CONVERSION = YES; |
||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; |
||||
CLANG_WARN_UNREACHABLE_CODE = YES; |
||||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; |
||||
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; |
||||
COPY_PHASE_STRIP = NO; |
||||
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; |
||||
ENABLE_STRICT_OBJC_MSGSEND = YES; |
||||
GCC_C_LANGUAGE_STANDARD = gnu99; |
||||
GCC_DYNAMIC_NO_PIC = NO; |
||||
GCC_NO_COMMON_BLOCKS = YES; |
||||
GCC_OPTIMIZATION_LEVEL = 0; |
||||
GCC_PREPROCESSOR_DEFINITIONS = ( |
||||
"DEBUG=1", |
||||
"$(inherited)", |
||||
); |
||||
GCC_SYMBOLS_PRIVATE_EXTERN = NO; |
||||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES; |
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; |
||||
GCC_WARN_UNDECLARED_SELECTOR = YES; |
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; |
||||
GCC_WARN_UNUSED_FUNCTION = YES; |
||||
GCC_WARN_UNUSED_VARIABLE = YES; |
||||
IPHONEOS_DEPLOYMENT_TARGET = 8.3; |
||||
MTL_ENABLE_DEBUG_INFO = YES; |
||||
ONLY_ACTIVE_ARCH = YES; |
||||
SDKROOT = iphoneos; |
||||
TARGETED_DEVICE_FAMILY = "1,2"; |
||||
}; |
||||
name = Debug; |
||||
}; |
||||
63E1E9A11B28CB2100EF0978 /* Release */ = { |
||||
isa = XCBuildConfiguration; |
||||
buildSettings = { |
||||
ALWAYS_SEARCH_USER_PATHS = NO; |
||||
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; |
||||
CLANG_CXX_LIBRARY = "libc++"; |
||||
CLANG_ENABLE_MODULES = YES; |
||||
CLANG_ENABLE_OBJC_ARC = YES; |
||||
CLANG_WARN_BOOL_CONVERSION = YES; |
||||
CLANG_WARN_CONSTANT_CONVERSION = YES; |
||||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; |
||||
CLANG_WARN_EMPTY_BODY = YES; |
||||
CLANG_WARN_ENUM_CONVERSION = YES; |
||||
CLANG_WARN_INT_CONVERSION = YES; |
||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; |
||||
CLANG_WARN_UNREACHABLE_CODE = YES; |
||||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; |
||||
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; |
||||
COPY_PHASE_STRIP = NO; |
||||
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; |
||||
ENABLE_NS_ASSERTIONS = NO; |
||||
ENABLE_STRICT_OBJC_MSGSEND = YES; |
||||
GCC_C_LANGUAGE_STANDARD = gnu99; |
||||
GCC_NO_COMMON_BLOCKS = YES; |
||||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES; |
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; |
||||
GCC_WARN_UNDECLARED_SELECTOR = YES; |
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; |
||||
GCC_WARN_UNUSED_FUNCTION = YES; |
||||
GCC_WARN_UNUSED_VARIABLE = YES; |
||||
IPHONEOS_DEPLOYMENT_TARGET = 8.3; |
||||
MTL_ENABLE_DEBUG_INFO = NO; |
||||
SDKROOT = iphoneos; |
||||
TARGETED_DEVICE_FAMILY = "1,2"; |
||||
VALIDATE_PRODUCT = YES; |
||||
}; |
||||
name = Release; |
||||
}; |
||||
63E1E9A31B28CB2100EF0978 /* Debug */ = { |
||||
isa = XCBuildConfiguration; |
||||
baseConfigurationReference = A387D6CECBCF2EAF2983033A /* Pods-AuthSample.debug.xcconfig */; |
||||
buildSettings = { |
||||
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; |
||||
INFOPLIST_FILE = Misc/Info.plist; |
||||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; |
||||
PRODUCT_NAME = "$(TARGET_NAME)"; |
||||
}; |
||||
name = Debug; |
||||
}; |
||||
63E1E9A41B28CB2100EF0978 /* Release */ = { |
||||
isa = XCBuildConfiguration; |
||||
baseConfigurationReference = B444176735DA81FBE4B8B80C /* Pods-AuthSample.release.xcconfig */; |
||||
buildSettings = { |
||||
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; |
||||
INFOPLIST_FILE = Misc/Info.plist; |
||||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; |
||||
PRODUCT_NAME = "$(TARGET_NAME)"; |
||||
}; |
||||
name = Release; |
||||
}; |
||||
/* End XCBuildConfiguration section */ |
||||
|
||||
/* Begin XCConfigurationList section */ |
||||
63E1E9771B28CB2000EF0978 /* Build configuration list for PBXProject "AuthSample" */ = { |
||||
isa = XCConfigurationList; |
||||
buildConfigurations = ( |
||||
63E1E9A01B28CB2100EF0978 /* Debug */, |
||||
63E1E9A11B28CB2100EF0978 /* Release */, |
||||
); |
||||
defaultConfigurationIsVisible = 0; |
||||
defaultConfigurationName = Release; |
||||
}; |
||||
63E1E9A21B28CB2100EF0978 /* Build configuration list for PBXNativeTarget "AuthSample" */ = { |
||||
isa = XCConfigurationList; |
||||
buildConfigurations = ( |
||||
63E1E9A31B28CB2100EF0978 /* Debug */, |
||||
63E1E9A41B28CB2100EF0978 /* Release */, |
||||
); |
||||
defaultConfigurationIsVisible = 0; |
||||
defaultConfigurationName = Release; |
||||
}; |
||||
/* End XCConfigurationList section */ |
||||
}; |
||||
rootObject = 63E1E9741B28CB2000EF0978 /* Project object */; |
||||
} |
@ -0,0 +1,7 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<Workspace |
||||
version = "1.0"> |
||||
<FileRef |
||||
location = "self:AuthSample.xcodeproj"> |
||||
</FileRef> |
||||
</Workspace> |
@ -0,0 +1,35 @@ |
||||
Pod::Spec.new do |s| |
||||
s.name = "AuthTestService" |
||||
s.version = "0.0.1" |
||||
s.license = "New BSD" |
||||
|
||||
s.ios.deployment_target = "6.0" |
||||
s.osx.deployment_target = "10.8" |
||||
|
||||
# Base directory where the .proto files are. |
||||
src = "../../protos" |
||||
|
||||
# Directory where the generated files will be place. |
||||
dir = "Pods/" + s.name |
||||
|
||||
# Run protoc with the Objective-C and gRPC plugins to generate protocol messages and gRPC clients. |
||||
s.prepare_command = <<-CMD |
||||
mkdir -p #{dir} |
||||
protoc -I #{src} --objc_out=#{dir} --objcgrpc_out=#{dir} #{src}/auth_sample.proto |
||||
CMD |
||||
|
||||
s.subspec "Messages" do |ms| |
||||
ms.source_files = "#{dir}/*.pbobjc.{h,m}", "#{dir}/**/*.pbobjc.{h,m}" |
||||
ms.header_mappings_dir = dir |
||||
ms.requires_arc = false |
||||
ms.dependency "Protobuf", "~> 3.0.0-alpha-3" |
||||
end |
||||
|
||||
s.subspec "Services" do |ss| |
||||
ss.source_files = "#{dir}/*.pbrpc.{h,m}", "#{dir}/**/*.pbrpc.{h,m}" |
||||
ss.header_mappings_dir = dir |
||||
ss.requires_arc = true |
||||
ss.dependency "gRPC", "~> 0.6" |
||||
ss.dependency "#{s.name}/Messages" |
||||
end |
||||
end |
@ -0,0 +1,40 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#import <UIKit/UIKit.h> |
||||
|
||||
extern NSString * const kTestScope; |
||||
|
||||
@interface MakeRPCViewController : UIViewController |
||||
@property(weak, nonatomic) IBOutlet UILabel *mainLabel; |
||||
@end |
@ -0,0 +1,100 @@ |
||||
/* |
||||
* |
||||
* Copyright 2015, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#import "MakeRPCViewController.h" |
||||
|
||||
#import <AuthTestService/AuthSample.pbrpc.h> |
||||
#import <Google/SignIn.h> |
||||
#include <grpc/status.h> |
||||
#import <ProtoRPC/ProtoRPC.h> |
||||
|
||||
NSString * const kTestScope = @"https://www.googleapis.com/auth/xapi.zoo"; |
||||
|
||||
static NSString * const kTestHostAddress = @"grpc-test.sandbox.google.com"; |
||||
|
||||
// Category for RPC errors to create the descriptions as we want them to appear on our view. |
||||
@interface NSError (AuthSample) |
||||
- (NSString *)UIDescription; |
||||
@end |
||||
|
||||
@implementation NSError (AuthSample) |
||||
- (NSString *)UIDescription { |
||||
if (self.code == GRPC_STATUS_UNAUTHENTICATED) { |
||||
// Authentication error. OAuth2 specifies we'll receive a challenge header. |
||||
// |userInfo[kGRPCStatusMetadataKey]| is the dictionary of response metadata. |
||||
NSString *challengeHeader = self.userInfo[kGRPCStatusMetadataKey][@"www-authenticate"] ?: @""; |
||||
return [@"Invalid credentials. Server challenge:\n" stringByAppendingString:challengeHeader]; |
||||
} else { |
||||
// Any other error. |
||||
return [NSString stringWithFormat:@"Unexpected RPC error %li: %@", |
||||
(long)self.code, self.localizedDescription]; |
||||
} |
||||
} |
||||
@end |
||||
|
||||
@implementation MakeRPCViewController |
||||
|
||||
- (void)viewWillAppear:(BOOL)animated { |
||||
|
||||
// Create a service client and a proto request as usual. |
||||
AUTHTestService *client = [[AUTHTestService alloc] initWithHost:kTestHostAddress]; |
||||
|
||||
AUTHRequest *request = [AUTHRequest message]; |
||||
request.fillUsername = YES; |
||||
request.fillOauthScope = YES; |
||||
|
||||
// Create a not-yet-started RPC. We want to set the request headers on this object before starting |
||||
// it. |
||||
ProtoRPC *call = |
||||
[client RPCToUnaryCallWithRequest:request handler:^(AUTHResponse *response, NSError *error) { |
||||
if (response) { |
||||
// This test server responds with the email and scope of the access token it receives. |
||||
self.mainLabel.text = [NSString stringWithFormat:@"Used scope: %@ on behalf of user %@", |
||||
response.oauthScope, response.username]; |
||||
|
||||
} else { |
||||
self.mainLabel.text = error.UIDescription; |
||||
} |
||||
}]; |
||||
|
||||
// Set the access token to be used. |
||||
NSString *accessToken = GIDSignIn.sharedInstance.currentUser.authentication.accessToken; |
||||
call.requestMetadata[@"Authorization"] = [@"Bearer " stringByAppendingString:accessToken]; |
||||
|
||||
// Start the RPC. |
||||
[call start]; |
||||
|
||||
self.mainLabel.text = @"Waiting for RPC to complete..."; |
||||
} |
||||
|
||||
@end |
@ -0,0 +1,38 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#import <UIKit/UIKit.h> |
||||
|
||||
@interface AppDelegate : UIResponder <UIApplicationDelegate> |
||||
@property (strong, nonatomic) UIWindow *window; |
||||
@end |
@ -0,0 +1,61 @@ |
||||
/* |
||||
* |
||||
* Copyright 2015, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#import "AppDelegate.h" |
||||
|
||||
#import <Google/SignIn.h> |
||||
|
||||
@implementation AppDelegate |
||||
|
||||
// As instructed in https://developers.google.com/identity/sign-in/ios/sign-in |
||||
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { |
||||
NSError* configureError; |
||||
[GGLContext.sharedInstance configureWithError: &configureError]; |
||||
NSAssert(!configureError, @"Error configuring Google services: %@", configureError); |
||||
|
||||
return YES; |
||||
} |
||||
|
||||
// As instructed in https://developers.google.com/identity/sign-in/ios/sign-in |
||||
- (BOOL)application:(UIApplication *)application |
||||
openURL:(NSURL *)url |
||||
sourceApplication:(NSString *)sourceApplication |
||||
annotation:(id)annotation { |
||||
// This will properly handle the URL that the application receives at the end of the |
||||
// authentication process. |
||||
return [GIDSignIn.sharedInstance handleURL:url |
||||
sourceApplication:sourceApplication |
||||
annotation:annotation]; |
||||
} |
||||
|
||||
@end |
@ -0,0 +1,154 @@ |
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> |
||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7702" systemVersion="14D131" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="49e-Tb-3d3"> |
||||
<dependencies> |
||||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7701"/> |
||||
<capability name="Constraints to layout margins" minToolsVersion="6.0"/> |
||||
</dependencies> |
||||
<scenes> |
||||
<!--Select User--> |
||||
<scene sceneID="hNz-n2-bh7"> |
||||
<objects> |
||||
<viewController id="9pv-A4-QxB" userLabel="Select User" customClass="SelectUserViewController" sceneMemberID="viewController"> |
||||
<layoutGuides> |
||||
<viewControllerLayoutGuide type="top" id="Ia1-K6-d13"/> |
||||
<viewControllerLayoutGuide type="bottom" id="4ug-Mw-9AY"/> |
||||
</layoutGuides> |
||||
<view key="view" contentMode="scaleToFill" id="tsR-hK-woN"> |
||||
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/> |
||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> |
||||
<subviews> |
||||
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleToFill" text="Please sign in." textAlignment="center" lineBreakMode="tailTruncation" minimumFontSize="10" translatesAutoresizingMaskIntoConstraints="NO" id="KQZ-1w-vlD" userLabel="Main Label"> |
||||
<rect key="frame" x="222" y="190" width="157" height="28"/> |
||||
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/> |
||||
<fontDescription key="fontDescription" name="Helvetica" family="Helvetica" pointSize="24"/> |
||||
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/> |
||||
<nil key="highlightedColor"/> |
||||
</label> |
||||
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="A5M-7J-77L" userLabel="Sub Label"> |
||||
<rect key="frame" x="301" y="226" width="0.0" height="0.0"/> |
||||
<fontDescription key="fontDescription" type="system" pointSize="14"/> |
||||
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/> |
||||
<nil key="highlightedColor"/> |
||||
</label> |
||||
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="W20-aa-6Xr"> |
||||
<rect key="frame" x="150" y="370" width="300" height="50"/> |
||||
<subviews> |
||||
<view contentMode="center" translatesAutoresizingMaskIntoConstraints="NO" id="25e-pE-eb8" customClass="GIDSignInButton"> |
||||
<rect key="frame" x="42" y="0.0" width="216" height="77"/> |
||||
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/> |
||||
<constraints> |
||||
<constraint firstAttribute="width" constant="216" placeholder="YES" id="Gfn-Hn-HXd"/> |
||||
<constraint firstAttribute="height" constant="77" placeholder="YES" id="cy1-xY-er0"/> |
||||
</constraints> |
||||
</view> |
||||
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="RKb-EE-mCZ"> |
||||
<rect key="frame" x="0.0" y="0.0" width="300" height="42"/> |
||||
<color key="backgroundColor" red="0.0" green="0.50196081399917603" blue="1" alpha="1" colorSpace="calibratedRGB"/> |
||||
<constraints> |
||||
<constraint firstAttribute="width" constant="300" id="ILy-QC-YPq"/> |
||||
<constraint firstAttribute="height" constant="42" id="MFs-aL-fQF"/> |
||||
</constraints> |
||||
<fontDescription key="fontDescription" type="boldSystem" pointSize="14"/> |
||||
<color key="tintColor" white="1" alpha="1" colorSpace="calibratedWhite"/> |
||||
<state key="normal" title="Sign out"> |
||||
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/> |
||||
</state> |
||||
<connections> |
||||
<action selector="didTapSignOut" destination="9pv-A4-QxB" eventType="touchUpInside" id="lcC-7k-Rki"/> |
||||
</connections> |
||||
</button> |
||||
</subviews> |
||||
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/> |
||||
<constraints> |
||||
<constraint firstAttribute="centerX" secondItem="25e-pE-eb8" secondAttribute="centerX" id="47f-A8-C2R"/> |
||||
<constraint firstAttribute="height" constant="50" id="EDJ-oo-Ix4"/> |
||||
<constraint firstItem="25e-pE-eb8" firstAttribute="top" secondItem="W20-aa-6Xr" secondAttribute="top" id="KSP-qb-mCJ"/> |
||||
<constraint firstItem="RKb-EE-mCZ" firstAttribute="top" secondItem="W20-aa-6Xr" secondAttribute="top" id="QDr-HV-DXb"/> |
||||
<constraint firstAttribute="width" constant="300" id="zWU-pZ-oQ7"/> |
||||
<constraint firstAttribute="centerX" secondItem="RKb-EE-mCZ" secondAttribute="centerX" id="zwy-cD-xao"/> |
||||
</constraints> |
||||
</view> |
||||
</subviews> |
||||
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/> |
||||
<constraints> |
||||
<constraint firstAttribute="centerX" secondItem="KQZ-1w-vlD" secondAttribute="centerX" id="6BV-lF-sBN"/> |
||||
<constraint firstItem="KQZ-1w-vlD" firstAttribute="top" secondItem="Ia1-K6-d13" secondAttribute="bottom" constant="170" id="82x-jY-eWa"/> |
||||
<constraint firstAttribute="centerX" secondItem="W20-aa-6Xr" secondAttribute="centerX" id="aHL-Ts-LoW"/> |
||||
<constraint firstItem="A5M-7J-77L" firstAttribute="top" secondItem="KQZ-1w-vlD" secondAttribute="bottom" constant="8" symbolic="YES" id="cfb-er-3JN"/> |
||||
<constraint firstItem="A5M-7J-77L" firstAttribute="centerX" secondItem="KQZ-1w-vlD" secondAttribute="centerX" id="e1l-AV-tCB"/> |
||||
<constraint firstItem="W20-aa-6Xr" firstAttribute="top" secondItem="Ia1-K6-d13" secondAttribute="bottom" constant="350" id="pab-Li-xZ5"/> |
||||
</constraints> |
||||
</view> |
||||
<tabBarItem key="tabBarItem" title="Select User" image="first" id="acW-dT-cKf"/> |
||||
<connections> |
||||
<outlet property="mainLabel" destination="KQZ-1w-vlD" id="fWK-H9-WXN"/> |
||||
<outlet property="signInButton" destination="25e-pE-eb8" id="ovt-t5-7BV"/> |
||||
<outlet property="signOutButton" destination="RKb-EE-mCZ" id="afy-Q6-rK8"/> |
||||
<outlet property="subLabel" destination="A5M-7J-77L" id="DIj-ZX-1NK"/> |
||||
</connections> |
||||
</viewController> |
||||
<placeholder placeholderIdentifier="IBFirstResponder" id="W5J-7L-Pyd" sceneMemberID="firstResponder"/> |
||||
</objects> |
||||
<point key="canvasLocation" x="750" y="-320"/> |
||||
</scene> |
||||
<!--Make RPC--> |
||||
<scene sceneID="wg7-f3-ORb"> |
||||
<objects> |
||||
<viewController id="8rJ-Kc-sve" userLabel="Make RPC" customClass="MakeRPCViewController" sceneMemberID="viewController"> |
||||
<layoutGuides> |
||||
<viewControllerLayoutGuide type="top" id="L7p-HK-0SC"/> |
||||
<viewControllerLayoutGuide type="bottom" id="Djb-ko-YwX"/> |
||||
</layoutGuides> |
||||
<view key="view" contentMode="scaleToFill" id="QS5-Rx-YEW"> |
||||
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/> |
||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> |
||||
<subviews> |
||||
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleToFill" misplaced="YES" text="Waiting for RPC to complete..." lineBreakMode="wordWrap" numberOfLines="0" minimumFontSize="10" translatesAutoresizingMaskIntoConstraints="NO" id="zEq-FU-wV5"> |
||||
<rect key="frame" x="16" y="286" width="568" height="28"/> |
||||
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/> |
||||
<fontDescription key="fontDescription" name="Helvetica" family="Helvetica" pointSize="18"/> |
||||
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/> |
||||
<nil key="highlightedColor"/> |
||||
</label> |
||||
</subviews> |
||||
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/> |
||||
<constraints> |
||||
<constraint firstItem="zEq-FU-wV5" firstAttribute="leading" secondItem="QS5-Rx-YEW" secondAttribute="leadingMargin" id="173-PJ-A9Y"/> |
||||
<constraint firstAttribute="trailingMargin" secondItem="zEq-FU-wV5" secondAttribute="trailing" id="Tct-jU-lL7"/> |
||||
<constraint firstAttribute="centerY" secondItem="zEq-FU-wV5" secondAttribute="centerY" id="qzY-Ky-pLD"/> |
||||
</constraints> |
||||
</view> |
||||
<tabBarItem key="tabBarItem" title="Make RPC" image="second" id="cPa-gy-q4n" userLabel="Make RPC"/> |
||||
<connections> |
||||
<outlet property="mainLabel" destination="zEq-FU-wV5" id="l1H-U6-oAa"/> |
||||
</connections> |
||||
</viewController> |
||||
<placeholder placeholderIdentifier="IBFirstResponder" id="4Nw-L8-lE0" sceneMemberID="firstResponder"/> |
||||
</objects> |
||||
<point key="canvasLocation" x="750" y="360"/> |
||||
</scene> |
||||
<!--Tab Bar Controller--> |
||||
<scene sceneID="yl2-sM-qoP"> |
||||
<objects> |
||||
<tabBarController id="49e-Tb-3d3" sceneMemberID="viewController"> |
||||
<nil key="simulatedBottomBarMetrics"/> |
||||
<tabBar key="tabBar" contentMode="scaleToFill" id="W28-zg-YXA"> |
||||
<rect key="frame" x="0.0" y="975" width="768" height="49"/> |
||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/> |
||||
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/> |
||||
</tabBar> |
||||
<connections> |
||||
<segue destination="9pv-A4-QxB" kind="relationship" relationship="viewControllers" id="u7Y-xg-7CH"/> |
||||
<segue destination="8rJ-Kc-sve" kind="relationship" relationship="viewControllers" id="lzU-1b-eKA"/> |
||||
</connections> |
||||
</tabBarController> |
||||
<placeholder placeholderIdentifier="IBFirstResponder" id="HuB-VB-40B" sceneMemberID="firstResponder"/> |
||||
</objects> |
||||
<point key="canvasLocation" x="0.0" y="0.0"/> |
||||
</scene> |
||||
</scenes> |
||||
<resources> |
||||
<image name="first" width="30" height="30"/> |
||||
<image name="second" width="30" height="30"/> |
||||
</resources> |
||||
</document> |
@ -0,0 +1,10 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
||||
<plist version="1.0"> |
||||
<dict> |
||||
<key>CLIENT_ID</key> |
||||
<string>15087385131-lh9bpkiai9nls53uadju0if6k7un3uih.apps.googleusercontent.com</string> |
||||
<key>REVERSED_CLIENT_ID</key> |
||||
<string>com.googleusercontent.apps.15087385131-lh9bpkiai9nls53uadju0if6k7un3uih</string> |
||||
</dict> |
||||
</plist> |
@ -0,0 +1,68 @@ |
||||
{ |
||||
"images" : [ |
||||
{ |
||||
"idiom" : "iphone", |
||||
"size" : "29x29", |
||||
"scale" : "2x" |
||||
}, |
||||
{ |
||||
"idiom" : "iphone", |
||||
"size" : "29x29", |
||||
"scale" : "3x" |
||||
}, |
||||
{ |
||||
"idiom" : "iphone", |
||||
"size" : "40x40", |
||||
"scale" : "2x" |
||||
}, |
||||
{ |
||||
"idiom" : "iphone", |
||||
"size" : "40x40", |
||||
"scale" : "3x" |
||||
}, |
||||
{ |
||||
"idiom" : "iphone", |
||||
"size" : "60x60", |
||||
"scale" : "2x" |
||||
}, |
||||
{ |
||||
"idiom" : "iphone", |
||||
"size" : "60x60", |
||||
"scale" : "3x" |
||||
}, |
||||
{ |
||||
"idiom" : "ipad", |
||||
"size" : "29x29", |
||||
"scale" : "1x" |
||||
}, |
||||
{ |
||||
"idiom" : "ipad", |
||||
"size" : "29x29", |
||||
"scale" : "2x" |
||||
}, |
||||
{ |
||||
"idiom" : "ipad", |
||||
"size" : "40x40", |
||||
"scale" : "1x" |
||||
}, |
||||
{ |
||||
"idiom" : "ipad", |
||||
"size" : "40x40", |
||||
"scale" : "2x" |
||||
}, |
||||
{ |
||||
"idiom" : "ipad", |
||||
"size" : "76x76", |
||||
"scale" : "1x" |
||||
}, |
||||
{ |
||||
"idiom" : "ipad", |
||||
"size" : "76x76", |
||||
"scale" : "2x" |
||||
} |
||||
], |
||||
"info" : { |
||||
"version" : 1, |
||||
"author" : "xcode" |
||||
} |
||||
} |
@ -0,0 +1,12 @@ |
||||
{ |
||||
"images" : [ |
||||
{ |
||||
"idiom" : "universal", |
||||
"filename" : "first.pdf" |
||||
} |
||||
], |
||||
"info" : { |
||||
"version" : 1, |
||||
"author" : "xcode" |
||||
} |
||||
} |
Binary file not shown.
@ -0,0 +1,12 @@ |
||||
{ |
||||
"images" : [ |
||||
{ |
||||
"idiom" : "universal", |
||||
"filename" : "second.pdf" |
||||
} |
||||
], |
||||
"info" : { |
||||
"version" : 1, |
||||
"author" : "xcode" |
||||
} |
||||
} |
Binary file not shown.
@ -0,0 +1,80 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
||||
<plist version="1.0"> |
||||
<dict> |
||||
<key>CFBundleDevelopmentRegion</key> |
||||
<string>en</string> |
||||
<key>CFBundleExecutable</key> |
||||
<string>$(EXECUTABLE_NAME)</string> |
||||
<key>CFBundleIdentifier</key> |
||||
<string>io.grpc.$(PRODUCT_NAME:rfc1034identifier)</string> |
||||
<key>CFBundleInfoDictionaryVersion</key> |
||||
<string>6.0</string> |
||||
<key>CFBundleName</key> |
||||
<string>$(PRODUCT_NAME)</string> |
||||
<key>CFBundlePackageType</key> |
||||
<string>APPL</string> |
||||
<key>CFBundleShortVersionString</key> |
||||
<string>1.0</string> |
||||
<key>CFBundleSignature</key> |
||||
<string>????</string> |
||||
<key>CFBundleURLTypes</key> |
||||
<array> |
||||
<dict> |
||||
<key>CFBundleTypeRole</key> |
||||
<string>Editor</string> |
||||
<key>CFBundleURLName</key> |
||||
<string>REVERSED_CLIENT_ID</string> |
||||
<key>CFBundleURLSchemes</key> |
||||
<array> |
||||
<string>com.googleusercontent.apps.15087385131-lh9bpkiai9nls53uadju0if6k7un3uih</string> |
||||
</array> |
||||
</dict> |
||||
<dict> |
||||
<key>CFBundleTypeRole</key> |
||||
<string>Editor</string> |
||||
<key>CFBundleURLName</key> |
||||
<string>BUNDLE_ID</string> |
||||
<key>CFBundleURLSchemes</key> |
||||
<array> |
||||
<string>io.grpc.AuthSample</string> |
||||
</array> |
||||
</dict> |
||||
</array> |
||||
<key>CFBundleVersion</key> |
||||
<string>1</string> |
||||
<key>LSRequiresIPhoneOS</key> |
||||
<true/> |
||||
<key>UILaunchStoryboardName</key> |
||||
<string>Main</string> |
||||
<key>UIMainStoryboardFile</key> |
||||
<string>Main</string> |
||||
<key>UIRequiredDeviceCapabilities</key> |
||||
<array> |
||||
<string>armv7</string> |
||||
</array> |
||||
<key>UIStatusBarTintParameters</key> |
||||
<dict> |
||||
<key>UINavigationBar</key> |
||||
<dict> |
||||
<key>Style</key> |
||||
<string>UIBarStyleDefault</string> |
||||
<key>Translucent</key> |
||||
<false/> |
||||
</dict> |
||||
</dict> |
||||
<key>UISupportedInterfaceOrientations</key> |
||||
<array> |
||||
<string>UIInterfaceOrientationPortrait</string> |
||||
<string>UIInterfaceOrientationLandscapeLeft</string> |
||||
<string>UIInterfaceOrientationLandscapeRight</string> |
||||
</array> |
||||
<key>UISupportedInterfaceOrientations~ipad</key> |
||||
<array> |
||||
<string>UIInterfaceOrientationPortrait</string> |
||||
<string>UIInterfaceOrientationPortraitUpsideDown</string> |
||||
<string>UIInterfaceOrientationLandscapeLeft</string> |
||||
<string>UIInterfaceOrientationLandscapeRight</string> |
||||
</array> |
||||
</dict> |
||||
</plist> |
@ -0,0 +1,41 @@ |
||||
/* |
||||
* |
||||
* Copyright 2015, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#import <UIKit/UIKit.h> |
||||
#import "AppDelegate.h" |
||||
|
||||
int main(int argc, char * argv[]) { |
||||
@autoreleasepool { |
||||
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); |
||||
} |
||||
} |
@ -0,0 +1,10 @@ |
||||
source 'https://github.com/CocoaPods/Specs.git' |
||||
platform :ios, '8.0' |
||||
|
||||
target 'AuthSample' do |
||||
# Depend on the generated AuthTestService library. |
||||
pod 'AuthTestService', :path => '.' |
||||
|
||||
# Depend on Google's OAuth2 library |
||||
pod 'Google/SignIn' |
||||
end |
@ -0,0 +1,189 @@ |
||||
#OAuth2 on gRPC: Objective-C |
||||
|
||||
This example application demostrates how to use OAuth2 on gRPC to make authenticated API calls on |
||||
behalf of a user. By walking through it you'll learn how to use the Objective-C gRPC API to: |
||||
|
||||
- Initialize and configure a remote call object before the RPC is started. |
||||
- Set request metadata elements on a call, which are semantically equivalent to HTTP request |
||||
headers. |
||||
- Read response metadata from a call, which is equivalent to HTTP response headers and trailers. |
||||
|
||||
It assumes you know the basics on how to make gRPC API calls using the Objective-C client library, |
||||
as shown in the [Hello World](examples/objective-c/helloworld) |
||||
or [Route Guide](examples/objective-c/route_guide) tutorials, |
||||
and are familiar with OAuth2 concepts like _access token_. |
||||
|
||||
- [Example code and setup](#setup) |
||||
- [Try it out!](#try) |
||||
- [Create an RPC object and start it later](#rpc-object) |
||||
- [Set request metadata of a call: Authorization header with an access token](#request-metadata) |
||||
- [Get response metadata of a call: Auth challenge header](#response-metadata) |
||||
|
||||
<a name="setup"></a> |
||||
## Example code and setup |
||||
|
||||
The example code for our tutorial is in [examples/objective-c/auth_sample](examples/objective-c/auth_sample). |
||||
To download the example, clone this repository by running the following command: |
||||
```shell |
||||
$ git clone https://github.com/grpc/grpc.git |
||||
``` |
||||
|
||||
Then change your current directory to `examples/objective-c/auth_sample`: |
||||
```shell |
||||
$ cd examples/objective-c/auth_sample |
||||
``` |
||||
|
||||
Our example is a simple application with two views. The first one lets a user sign in and out using |
||||
the OAuth2 flow of Google's [iOS SignIn library](https://developers.google.com/identity/sign-in/ios/). |
||||
(Google's library is used in this example because the test gRPC service we are going to call expects |
||||
Google account credentials, but neither gRPC nor the Objective-C client library is tied to any |
||||
specific OAuth2 provider). The second view makes a gRPC request to the test server, using the |
||||
access token obtained by the first view. |
||||
|
||||
Note: OAuth2 libraries need the application to register and obtain an ID from the identity provider |
||||
(in the case of this example app, Google). The app's XCode project is configured using that ID, so |
||||
you shouldn't copy this project "as is" for your own app: it would result in your app being |
||||
identified in the consent screen as "gRPC-AuthSample", and not having access to real Google |
||||
services. Instead, configure your own XCode project following the [instructions here](https://developers.google.com/identity/sign-in/ios/). |
||||
|
||||
As with the other examples, you also should have [Cocoapods](https://cocoapods.org/#install) |
||||
installed, as well as the relevant tools to generate the client library code. You can obtain the |
||||
latter by following [these setup instructions](https://github.com/grpc/homebrew-grpc). |
||||
|
||||
|
||||
<a name="try"></a> |
||||
## Try it out! |
||||
|
||||
To try the sample app, first have Cocoapods generate and install the client library for our .proto |
||||
files: |
||||
|
||||
```shell |
||||
$ pod install |
||||
``` |
||||
|
||||
(This might have to compile OpenSSL, which takes around 15 minutes if Cocoapods doesn't have it yet |
||||
on your computer's cache). |
||||
|
||||
Finally, open the XCode workspace created by Cocoapods, and run the app. |
||||
|
||||
The first view, `SelectUserViewController.h/m`, asks you to sign in with your Google account, and to |
||||
give the "gRPC-AuthSample" app the following permissions: |
||||
|
||||
- View your email address. |
||||
- View your basic profile info. |
||||
- "Test scope for access to the Zoo service". |
||||
|
||||
This last permission, corresponding to the scope `https://www.googleapis.com/auth/xapi.zoo` doesn't |
||||
grant any real capability: it's only used for testing. You can log out at any time. |
||||
|
||||
The second view, `MakeRPCViewController.h/m`, makes a gRPC request to a test server at |
||||
https://grpc-test.sandbox.google.com, sending the access token along with the request. The test |
||||
service simply validates the token and writes in its response which user it belongs to, and which |
||||
scopes it gives access to. (The client application already knows those two values; it's a way to |
||||
verify that everything went as expected). |
||||
|
||||
The next sections guide you step-by-step through how the gRPC call in `MakeRPCViewController` is |
||||
performed. |
||||
|
||||
<a name="rpc-object"></a> |
||||
## Create an RPC object and start it later |
||||
|
||||
The other basic tutorials show how to invoke an RPC by calling an asynchronous method in a generated |
||||
client object. This shows how to initialize an object that represents the RPC, and configure it |
||||
before starting the network request. |
||||
|
||||
Assume you have a proto service definition like this: |
||||
|
||||
```protobuf |
||||
option objc_class_prefix = "AUTH"; |
||||
|
||||
service TestService { |
||||
rpc UnaryCall(Request) returns (Response); |
||||
} |
||||
``` |
||||
|
||||
A `unaryCallWithRequest:handler:` method, with which you're already familiar, is generated for the |
||||
`AUTHTestService` class: |
||||
|
||||
```objective-c |
||||
[client unaryCallWithRequest:request handler:^(AUTHResponse *response, NSError *error) { |
||||
... |
||||
}]; |
||||
``` |
||||
|
||||
In addition, an `RPCToUnaryCallWithRequest:handler:` method is generated, which returns a |
||||
not-yet-started RPC object: |
||||
|
||||
```objective-c |
||||
#import <ProtoRPC/ProtoRPC.h> |
||||
|
||||
ProtoRPC *call = |
||||
[client RPCToUnaryCallWithRequest:request handler:^(AUTHResponse *response, NSError *error) { |
||||
... |
||||
}]; |
||||
``` |
||||
|
||||
The RPC represented by this object can be started at any later time like this: |
||||
|
||||
```objective-c |
||||
[call start]; |
||||
``` |
||||
|
||||
<a name="request-metadata"></a> |
||||
## Set request metadata of a call: Authorization header with an access token |
||||
|
||||
The `ProtoRPC` class has a `requestMetadata` property (inherited from `GRPCCall`) defined like this: |
||||
|
||||
```objective-c |
||||
- (NSMutableDictionary *)requestMetadata; // nonatomic |
||||
- (void)setRequestMetadata:(NSDictionary *)requestMetadata; // nonatomic, copy |
||||
``` |
||||
|
||||
Setting it to a dictionary of metadata keys and values will have them sent on the wire when the call |
||||
is started. gRPC metadata are pieces of information about the call sent by the client to the server |
||||
(and vice versa). They take the form of key-value pairs and are essentially opaque to gRPC itself. |
||||
|
||||
```objective-c |
||||
call.requestMetadata = @{@"My-Header": @"Value for this header", |
||||
@"Another-Header": @"Its value"}; |
||||
``` |
||||
|
||||
For convenience, the property is initialized with an empty `NSMutableDictionary`, so that request |
||||
metadata elements can be set like this: |
||||
|
||||
```objective-c |
||||
call.requestMetadata[@"My-Header"] = @"Value for this header"; |
||||
``` |
||||
|
||||
If you have an access token, OAuth2 specifies it is to be sent in this format: |
||||
|
||||
```objective-c |
||||
call.requestMetadata[@"Authorization"] = [@"Bearer " stringByAppendingString:accessToken]; |
||||
``` |
||||
|
||||
<a name="response-metadata"></a> |
||||
## Get response metadata of a call: Auth challenge header |
||||
|
||||
The `ProtoRPC` class also inherits a `responseMetadata` property, analogous to the request metadata |
||||
we just looked at. It's defined like this: |
||||
|
||||
```objective-c |
||||
@property(atomic, readonly) NSDictionary *responseMetadata; |
||||
``` |
||||
|
||||
To access OAuth2's authentication challenge header you write: |
||||
|
||||
```objective-c |
||||
call.responseMetadata[@"www-authenticate"] |
||||
``` |
||||
|
||||
Note that, as gRPC metadata elements are mapped to HTTP/2 headers (or trailers), the keys of the |
||||
response metadata are always ASCII strings in lowercase. |
||||
|
||||
Many uses cases of response metadata are getting more details about an RPC error. For convenience, |
||||
when a `NSError` instance is passed to an RPC handler block, the response metadata dictionary can |
||||
also be accessed this way: |
||||
|
||||
```objective-c |
||||
error.userInfo[kGRPCStatusMetadataKey] |
||||
``` |
@ -0,0 +1,42 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#import <Google/SignIn.h> |
||||
#import <UIKit/UIKit.h> |
||||
|
||||
@interface SelectUserViewController : UIViewController <GIDSignInDelegate, GIDSignInUIDelegate> |
||||
@property(weak, nonatomic) IBOutlet GIDSignInButton *signInButton; |
||||
@property(weak, nonatomic) IBOutlet UIButton *signOutButton; |
||||
@property(weak, nonatomic) IBOutlet UILabel *mainLabel; |
||||
@property(weak, nonatomic) IBOutlet UILabel *subLabel; |
||||
@end |
@ -0,0 +1,86 @@ |
||||
/* |
||||
* |
||||
* Copyright 2015, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#import "SelectUserViewController.h" |
||||
|
||||
#import "MakeRPCViewController.h" |
||||
|
||||
@implementation SelectUserViewController |
||||
|
||||
- (void)viewDidLoad { |
||||
[super viewDidLoad]; |
||||
|
||||
self.signOutButton.layer.cornerRadius = 5; |
||||
self.signOutButton.hidden = YES; |
||||
|
||||
// As instructed in https://developers.google.com/identity/sign-in/ios/sign-in |
||||
GIDSignIn *signIn = GIDSignIn.sharedInstance; |
||||
signIn.delegate = self; |
||||
signIn.uiDelegate = self; |
||||
|
||||
// As instructed in https://developers.google.com/identity/sign-in/ios/additional-scopes |
||||
if (![signIn.scopes containsObject:kTestScope]) { |
||||
signIn.scopes = [signIn.scopes arrayByAddingObject:kTestScope]; |
||||
} |
||||
|
||||
[signIn signInSilently]; |
||||
} |
||||
|
||||
- (void)signIn:(GIDSignIn *)signIn |
||||
didSignInForUser:(GIDGoogleUser *)user |
||||
withError:(NSError *)error { |
||||
if (error) { |
||||
// The user probably cancelled the sign-in flow. |
||||
return; |
||||
} |
||||
|
||||
self.mainLabel.text = [NSString stringWithFormat:@"User: %@", user.profile.email]; |
||||
NSString *scopes = [user.accessibleScopes componentsJoinedByString:@", "]; |
||||
scopes = scopes.length ? scopes : @"(none)"; |
||||
self.subLabel.text = [NSString stringWithFormat:@"Scopes: %@", scopes]; |
||||
|
||||
self.signInButton.hidden = YES; |
||||
self.signOutButton.hidden = NO; |
||||
} |
||||
|
||||
- (IBAction)didTapSignOut { |
||||
[GIDSignIn.sharedInstance signOut]; |
||||
|
||||
self.mainLabel.text = @"Please sign in."; |
||||
self.subLabel.text = @""; |
||||
|
||||
self.signInButton.hidden = NO; |
||||
self.signOutButton.hidden = YES; |
||||
} |
||||
|
||||
@end |
@ -0,0 +1,35 @@ |
||||
Pod::Spec.new do |s| |
||||
s.name = "HelloWorld" |
||||
s.version = "0.0.1" |
||||
s.license = "New BSD" |
||||
|
||||
s.ios.deployment_target = "6.0" |
||||
s.osx.deployment_target = "10.8" |
||||
|
||||
# Base directory where the .proto files are. |
||||
src = "../../protos" |
||||
|
||||
# Directory where the generated files will be place. |
||||
dir = "Pods/" + s.name |
||||
|
||||
# Run protoc with the Objective-C and gRPC plugins to generate protocol messages and gRPC clients. |
||||
s.prepare_command = <<-CMD |
||||
mkdir -p #{dir} |
||||
protoc -I #{src} --objc_out=#{dir} --objcgrpc_out=#{dir} #{src}/helloworld.proto |
||||
CMD |
||||
|
||||
s.subspec "Messages" do |ms| |
||||
ms.source_files = "#{dir}/*.pbobjc.{h,m}", "#{dir}/**/*.pbobjc.{h,m}" |
||||
ms.header_mappings_dir = dir |
||||
ms.requires_arc = false |
||||
ms.dependency "Protobuf", "~> 3.0.0-alpha-3" |
||||
end |
||||
|
||||
s.subspec "Services" do |ss| |
||||
ss.source_files = "#{dir}/*.pbrpc.{h,m}", "#{dir}/**/*.pbrpc.{h,m}" |
||||
ss.header_mappings_dir = dir |
||||
ss.requires_arc = true |
||||
ss.dependency "gRPC", "~> 0.6" |
||||
ss.dependency "#{s.name}/Messages" |
||||
end |
||||
end |
@ -0,0 +1,349 @@ |
||||
// !$*UTF8*$! |
||||
{ |
||||
archiveVersion = 1; |
||||
classes = { |
||||
}; |
||||
objectVersion = 46; |
||||
objects = { |
||||
|
||||
/* Begin PBXBuildFile section */ |
||||
5E3690661B2A23800040F884 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E3690651B2A23800040F884 /* main.m */; }; |
||||
5E3690691B2A23800040F884 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E3690681B2A23800040F884 /* AppDelegate.m */; }; |
||||
5E36906C1B2A23800040F884 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 5E36906B1B2A23800040F884 /* ViewController.m */; }; |
||||
5E36906F1B2A23800040F884 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5E36906D1B2A23800040F884 /* Main.storyboard */; }; |
||||
5E3690711B2A23800040F884 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5E3690701B2A23800040F884 /* Images.xcassets */; }; |
||||
EF61CF6AE2536A31D47F0E63 /* libPods-HelloWorld.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 6B4E1F55F8A2EC95A0E7EE88 /* libPods-HelloWorld.a */; }; |
||||
/* End PBXBuildFile section */ |
||||
|
||||
/* Begin PBXFileReference section */ |
||||
0C432EF610DB15C0F47A66BB /* Pods-HelloWorld.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-HelloWorld.release.xcconfig"; path = "Pods/Target Support Files/Pods-HelloWorld/Pods-HelloWorld.release.xcconfig"; sourceTree = "<group>"; }; |
||||
5E3690601B2A23800040F884 /* HelloWorld.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = HelloWorld.app; sourceTree = BUILT_PRODUCTS_DIR; }; |
||||
5E3690641B2A23800040F884 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; }; |
||||
5E3690651B2A23800040F884 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; }; |
||||
5E3690671B2A23800040F884 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; }; |
||||
5E3690681B2A23800040F884 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; }; |
||||
5E36906B1B2A23800040F884 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = "<group>"; }; |
||||
5E36906E1B2A23800040F884 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; }; |
||||
5E3690701B2A23800040F884 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; }; |
||||
6B4E1F55F8A2EC95A0E7EE88 /* libPods-HelloWorld.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-HelloWorld.a"; sourceTree = BUILT_PRODUCTS_DIR; }; |
||||
DBDE3E48389499064CD664B8 /* Pods-HelloWorld.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-HelloWorld.debug.xcconfig"; path = "Pods/Target Support Files/Pods-HelloWorld/Pods-HelloWorld.debug.xcconfig"; sourceTree = "<group>"; }; |
||||
/* End PBXFileReference section */ |
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */ |
||||
5E36905D1B2A23800040F884 /* Frameworks */ = { |
||||
isa = PBXFrameworksBuildPhase; |
||||
buildActionMask = 2147483647; |
||||
files = ( |
||||
EF61CF6AE2536A31D47F0E63 /* libPods-HelloWorld.a in Frameworks */, |
||||
); |
||||
runOnlyForDeploymentPostprocessing = 0; |
||||
}; |
||||
/* End PBXFrameworksBuildPhase section */ |
||||
|
||||
/* Begin PBXGroup section */ |
||||
5E3690571B2A23800040F884 = { |
||||
isa = PBXGroup; |
||||
children = ( |
||||
5E3690651B2A23800040F884 /* main.m */, |
||||
5E3690621B2A23800040F884 /* HelloWorld */, |
||||
5E3690611B2A23800040F884 /* Products */, |
||||
BD9CE6458E7C4FF49A1DF69F /* Pods */, |
||||
66CEC7120220DDD2221DD075 /* Frameworks */, |
||||
); |
||||
sourceTree = "<group>"; |
||||
}; |
||||
5E3690611B2A23800040F884 /* Products */ = { |
||||
isa = PBXGroup; |
||||
children = ( |
||||
5E3690601B2A23800040F884 /* HelloWorld.app */, |
||||
); |
||||
name = Products; |
||||
sourceTree = "<group>"; |
||||
}; |
||||
5E3690621B2A23800040F884 /* HelloWorld */ = { |
||||
isa = PBXGroup; |
||||
children = ( |
||||
5E3690631B2A23800040F884 /* Supporting Files */, |
||||
); |
||||
path = HelloWorld; |
||||
sourceTree = "<group>"; |
||||
}; |
||||
5E3690631B2A23800040F884 /* Supporting Files */ = { |
||||
isa = PBXGroup; |
||||
children = ( |
||||
5E3690701B2A23800040F884 /* Images.xcassets */, |
||||
5E36906D1B2A23800040F884 /* Main.storyboard */, |
||||
5E36906B1B2A23800040F884 /* ViewController.m */, |
||||
5E3690681B2A23800040F884 /* AppDelegate.m */, |
||||
5E3690671B2A23800040F884 /* AppDelegate.h */, |
||||
5E3690641B2A23800040F884 /* Info.plist */, |
||||
); |
||||
name = "Supporting Files"; |
||||
sourceTree = "<group>"; |
||||
}; |
||||
66CEC7120220DDD2221DD075 /* Frameworks */ = { |
||||
isa = PBXGroup; |
||||
children = ( |
||||
6B4E1F55F8A2EC95A0E7EE88 /* libPods-HelloWorld.a */, |
||||
); |
||||
name = Frameworks; |
||||
sourceTree = "<group>"; |
||||
}; |
||||
BD9CE6458E7C4FF49A1DF69F /* Pods */ = { |
||||
isa = PBXGroup; |
||||
children = ( |
||||
DBDE3E48389499064CD664B8 /* Pods-HelloWorld.debug.xcconfig */, |
||||
0C432EF610DB15C0F47A66BB /* Pods-HelloWorld.release.xcconfig */, |
||||
); |
||||
name = Pods; |
||||
sourceTree = "<group>"; |
||||
}; |
||||
/* End PBXGroup section */ |
||||
|
||||
/* Begin PBXNativeTarget section */ |
||||
5E36905F1B2A23800040F884 /* HelloWorld */ = { |
||||
isa = PBXNativeTarget; |
||||
buildConfigurationList = 5E3690831B2A23810040F884 /* Build configuration list for PBXNativeTarget "HelloWorld" */; |
||||
buildPhases = ( |
||||
ACF9162361FB8F24C70657DE /* Check Pods Manifest.lock */, |
||||
5E36905C1B2A23800040F884 /* Sources */, |
||||
5E36905D1B2A23800040F884 /* Frameworks */, |
||||
5E36905E1B2A23800040F884 /* Resources */, |
||||
4C7D815378D98AB3BFC1A7D5 /* Copy Pods Resources */, |
||||
); |
||||
buildRules = ( |
||||
); |
||||
dependencies = ( |
||||
); |
||||
name = HelloWorld; |
||||
productName = HelloWorld; |
||||
productReference = 5E3690601B2A23800040F884 /* HelloWorld.app */; |
||||
productType = "com.apple.product-type.application"; |
||||
}; |
||||
/* End PBXNativeTarget section */ |
||||
|
||||
/* Begin PBXProject section */ |
||||
5E3690581B2A23800040F884 /* Project object */ = { |
||||
isa = PBXProject; |
||||
attributes = { |
||||
LastUpgradeCheck = 0620; |
||||
ORGANIZATIONNAME = Google; |
||||
TargetAttributes = { |
||||
5E36905F1B2A23800040F884 = { |
||||
CreatedOnToolsVersion = 6.2; |
||||
}; |
||||
}; |
||||
}; |
||||
buildConfigurationList = 5E36905B1B2A23800040F884 /* Build configuration list for PBXProject "HelloWorld" */; |
||||
compatibilityVersion = "Xcode 3.2"; |
||||
developmentRegion = English; |
||||
hasScannedForEncodings = 0; |
||||
knownRegions = ( |
||||
en, |
||||
Base, |
||||
); |
||||
mainGroup = 5E3690571B2A23800040F884; |
||||
productRefGroup = 5E3690611B2A23800040F884 /* Products */; |
||||
projectDirPath = ""; |
||||
projectRoot = ""; |
||||
targets = ( |
||||
5E36905F1B2A23800040F884 /* HelloWorld */, |
||||
); |
||||
}; |
||||
/* End PBXProject section */ |
||||
|
||||
/* Begin PBXResourcesBuildPhase section */ |
||||
5E36905E1B2A23800040F884 /* Resources */ = { |
||||
isa = PBXResourcesBuildPhase; |
||||
buildActionMask = 2147483647; |
||||
files = ( |
||||
5E36906F1B2A23800040F884 /* Main.storyboard in Resources */, |
||||
5E3690711B2A23800040F884 /* Images.xcassets in Resources */, |
||||
); |
||||
runOnlyForDeploymentPostprocessing = 0; |
||||
}; |
||||
/* End PBXResourcesBuildPhase section */ |
||||
|
||||
/* Begin PBXShellScriptBuildPhase section */ |
||||
4C7D815378D98AB3BFC1A7D5 /* Copy Pods Resources */ = { |
||||
isa = PBXShellScriptBuildPhase; |
||||
buildActionMask = 2147483647; |
||||
files = ( |
||||
); |
||||
inputPaths = ( |
||||
); |
||||
name = "Copy Pods Resources"; |
||||
outputPaths = ( |
||||
); |
||||
runOnlyForDeploymentPostprocessing = 0; |
||||
shellPath = /bin/sh; |
||||
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-HelloWorld/Pods-HelloWorld-resources.sh\"\n"; |
||||
showEnvVarsInLog = 0; |
||||
}; |
||||
ACF9162361FB8F24C70657DE /* Check Pods Manifest.lock */ = { |
||||
isa = PBXShellScriptBuildPhase; |
||||
buildActionMask = 2147483647; |
||||
files = ( |
||||
); |
||||
inputPaths = ( |
||||
); |
||||
name = "Check Pods Manifest.lock"; |
||||
outputPaths = ( |
||||
); |
||||
runOnlyForDeploymentPostprocessing = 0; |
||||
shellPath = /bin/sh; |
||||
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n"; |
||||
showEnvVarsInLog = 0; |
||||
}; |
||||
/* End PBXShellScriptBuildPhase section */ |
||||
|
||||
/* Begin PBXSourcesBuildPhase section */ |
||||
5E36905C1B2A23800040F884 /* Sources */ = { |
||||
isa = PBXSourcesBuildPhase; |
||||
buildActionMask = 2147483647; |
||||
files = ( |
||||
5E36906C1B2A23800040F884 /* ViewController.m in Sources */, |
||||
5E3690691B2A23800040F884 /* AppDelegate.m in Sources */, |
||||
5E3690661B2A23800040F884 /* main.m in Sources */, |
||||
); |
||||
runOnlyForDeploymentPostprocessing = 0; |
||||
}; |
||||
/* End PBXSourcesBuildPhase section */ |
||||
|
||||
/* Begin PBXVariantGroup section */ |
||||
5E36906D1B2A23800040F884 /* Main.storyboard */ = { |
||||
isa = PBXVariantGroup; |
||||
children = ( |
||||
5E36906E1B2A23800040F884 /* Base */, |
||||
); |
||||
name = Main.storyboard; |
||||
sourceTree = "<group>"; |
||||
}; |
||||
/* End PBXVariantGroup section */ |
||||
|
||||
/* Begin XCBuildConfiguration section */ |
||||
5E3690811B2A23810040F884 /* Debug */ = { |
||||
isa = XCBuildConfiguration; |
||||
buildSettings = { |
||||
ALWAYS_SEARCH_USER_PATHS = NO; |
||||
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; |
||||
CLANG_CXX_LIBRARY = "libc++"; |
||||
CLANG_ENABLE_MODULES = YES; |
||||
CLANG_ENABLE_OBJC_ARC = YES; |
||||
CLANG_WARN_BOOL_CONVERSION = YES; |
||||
CLANG_WARN_CONSTANT_CONVERSION = YES; |
||||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; |
||||
CLANG_WARN_EMPTY_BODY = YES; |
||||
CLANG_WARN_ENUM_CONVERSION = YES; |
||||
CLANG_WARN_INT_CONVERSION = YES; |
||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; |
||||
CLANG_WARN_UNREACHABLE_CODE = YES; |
||||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; |
||||
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; |
||||
COPY_PHASE_STRIP = NO; |
||||
ENABLE_STRICT_OBJC_MSGSEND = YES; |
||||
GCC_C_LANGUAGE_STANDARD = gnu99; |
||||
GCC_DYNAMIC_NO_PIC = NO; |
||||
GCC_OPTIMIZATION_LEVEL = 0; |
||||
GCC_PREPROCESSOR_DEFINITIONS = ( |
||||
"DEBUG=1", |
||||
"$(inherited)", |
||||
); |
||||
GCC_SYMBOLS_PRIVATE_EXTERN = NO; |
||||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES; |
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; |
||||
GCC_WARN_UNDECLARED_SELECTOR = YES; |
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; |
||||
GCC_WARN_UNUSED_FUNCTION = YES; |
||||
GCC_WARN_UNUSED_VARIABLE = YES; |
||||
IPHONEOS_DEPLOYMENT_TARGET = 8.2; |
||||
MTL_ENABLE_DEBUG_INFO = YES; |
||||
ONLY_ACTIVE_ARCH = YES; |
||||
SDKROOT = iphoneos; |
||||
TARGETED_DEVICE_FAMILY = "1,2"; |
||||
}; |
||||
name = Debug; |
||||
}; |
||||
5E3690821B2A23810040F884 /* Release */ = { |
||||
isa = XCBuildConfiguration; |
||||
buildSettings = { |
||||
ALWAYS_SEARCH_USER_PATHS = NO; |
||||
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; |
||||
CLANG_CXX_LIBRARY = "libc++"; |
||||
CLANG_ENABLE_MODULES = YES; |
||||
CLANG_ENABLE_OBJC_ARC = YES; |
||||
CLANG_WARN_BOOL_CONVERSION = YES; |
||||
CLANG_WARN_CONSTANT_CONVERSION = YES; |
||||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; |
||||
CLANG_WARN_EMPTY_BODY = YES; |
||||
CLANG_WARN_ENUM_CONVERSION = YES; |
||||
CLANG_WARN_INT_CONVERSION = YES; |
||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; |
||||
CLANG_WARN_UNREACHABLE_CODE = YES; |
||||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; |
||||
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; |
||||
COPY_PHASE_STRIP = NO; |
||||
ENABLE_NS_ASSERTIONS = NO; |
||||
ENABLE_STRICT_OBJC_MSGSEND = YES; |
||||
GCC_C_LANGUAGE_STANDARD = gnu99; |
||||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES; |
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; |
||||
GCC_WARN_UNDECLARED_SELECTOR = YES; |
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; |
||||
GCC_WARN_UNUSED_FUNCTION = YES; |
||||
GCC_WARN_UNUSED_VARIABLE = YES; |
||||
IPHONEOS_DEPLOYMENT_TARGET = 8.2; |
||||
MTL_ENABLE_DEBUG_INFO = NO; |
||||
SDKROOT = iphoneos; |
||||
TARGETED_DEVICE_FAMILY = "1,2"; |
||||
VALIDATE_PRODUCT = YES; |
||||
}; |
||||
name = Release; |
||||
}; |
||||
5E3690841B2A23810040F884 /* Debug */ = { |
||||
isa = XCBuildConfiguration; |
||||
baseConfigurationReference = DBDE3E48389499064CD664B8 /* Pods-HelloWorld.debug.xcconfig */; |
||||
buildSettings = { |
||||
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; |
||||
INFOPLIST_FILE = HelloWorld/Info.plist; |
||||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; |
||||
PRODUCT_NAME = "$(TARGET_NAME)"; |
||||
}; |
||||
name = Debug; |
||||
}; |
||||
5E3690851B2A23810040F884 /* Release */ = { |
||||
isa = XCBuildConfiguration; |
||||
baseConfigurationReference = 0C432EF610DB15C0F47A66BB /* Pods-HelloWorld.release.xcconfig */; |
||||
buildSettings = { |
||||
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; |
||||
INFOPLIST_FILE = HelloWorld/Info.plist; |
||||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; |
||||
PRODUCT_NAME = "$(TARGET_NAME)"; |
||||
}; |
||||
name = Release; |
||||
}; |
||||
/* End XCBuildConfiguration section */ |
||||
|
||||
/* Begin XCConfigurationList section */ |
||||
5E36905B1B2A23800040F884 /* Build configuration list for PBXProject "HelloWorld" */ = { |
||||
isa = XCConfigurationList; |
||||
buildConfigurations = ( |
||||
5E3690811B2A23810040F884 /* Debug */, |
||||
5E3690821B2A23810040F884 /* Release */, |
||||
); |
||||
defaultConfigurationIsVisible = 0; |
||||
defaultConfigurationName = Release; |
||||
}; |
||||
5E3690831B2A23810040F884 /* Build configuration list for PBXNativeTarget "HelloWorld" */ = { |
||||
isa = XCConfigurationList; |
||||
buildConfigurations = ( |
||||
5E3690841B2A23810040F884 /* Debug */, |
||||
5E3690851B2A23810040F884 /* Release */, |
||||
); |
||||
defaultConfigurationIsVisible = 0; |
||||
defaultConfigurationName = Release; |
||||
}; |
||||
/* End XCConfigurationList section */ |
||||
}; |
||||
rootObject = 5E3690581B2A23800040F884 /* Project object */; |
||||
} |
@ -0,0 +1,7 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<Workspace |
||||
version = "1.0"> |
||||
<FileRef |
||||
location = "self:HelloWorld.xcodeproj"> |
||||
</FileRef> |
||||
</Workspace> |
@ -0,0 +1,38 @@ |
||||
/*
|
||||
* |
||||
* Copyright 2015, Google Inc. |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or without |
||||
* modification, are permitted provided that the following conditions are |
||||
* met: |
||||
* |
||||
* * Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* * Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following disclaimer |
||||
* in the documentation and/or other materials provided with the |
||||
* distribution. |
||||
* * Neither the name of Google Inc. nor the names of its |
||||
* contributors may be used to endorse or promote products derived from |
||||
* this software without specific prior written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
*/ |
||||
|
||||
#import <UIKit/UIKit.h> |
||||
|
||||
@interface AppDelegate : UIResponder <UIApplicationDelegate> |
||||
@property (strong, nonatomic) UIWindow *window; |
||||
@end |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue