mirror of https://github.com/grpc/grpc.git
commit
b37391ef45
169 changed files with 4930 additions and 17268 deletions
@ -0,0 +1,95 @@ |
|||||||
|
# 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. |
||||||
|
{ |
||||||
|
"variables" : { |
||||||
|
'config': '<!(echo $CONFIG)' |
||||||
|
}, |
||||||
|
"targets" : [ |
||||||
|
{ |
||||||
|
'include_dirs': [ |
||||||
|
"<!(node -e \"require('nan')\")" |
||||||
|
], |
||||||
|
'cflags': [ |
||||||
|
'-std=c++0x', |
||||||
|
'-Wall', |
||||||
|
'-pthread', |
||||||
|
'-g', |
||||||
|
'-zdefs', |
||||||
|
'-Werror', |
||||||
|
'-Wno-error=deprecated-declarations' |
||||||
|
], |
||||||
|
'ldflags': [ |
||||||
|
'-g' |
||||||
|
], |
||||||
|
"conditions": [ |
||||||
|
['OS != "win"', { |
||||||
|
'conditions': [ |
||||||
|
['config=="gcov"', { |
||||||
|
'cflags': [ |
||||||
|
'-ftest-coverage', |
||||||
|
'-fprofile-arcs', |
||||||
|
'-O0' |
||||||
|
], |
||||||
|
'ldflags': [ |
||||||
|
'-ftest-coverage', |
||||||
|
'-fprofile-arcs' |
||||||
|
] |
||||||
|
} |
||||||
|
] |
||||||
|
] |
||||||
|
}], |
||||||
|
['OS == "mac"', { |
||||||
|
'xcode_settings': { |
||||||
|
'MACOSX_DEPLOYMENT_TARGET': '10.9', |
||||||
|
'OTHER_CFLAGS': [ |
||||||
|
'-std=c++11', |
||||||
|
'-stdlib=libc++' |
||||||
|
] |
||||||
|
} |
||||||
|
}] |
||||||
|
], |
||||||
|
"target_name": "grpc_node", |
||||||
|
"sources": [ |
||||||
|
"src/node/ext/byte_buffer.cc", |
||||||
|
"src/node/ext/call.cc", |
||||||
|
"src/node/ext/call_credentials.cc", |
||||||
|
"src/node/ext/channel.cc", |
||||||
|
"src/node/ext/channel_credentials.cc", |
||||||
|
"src/node/ext/completion_queue_async_worker.cc", |
||||||
|
"src/node/ext/node_grpc.cc", |
||||||
|
"src/node/ext/server.cc", |
||||||
|
"src/node/ext/server_credentials.cc", |
||||||
|
"src/node/ext/timeval.cc" |
||||||
|
], |
||||||
|
"dependencies": [ |
||||||
|
"grpc.gyp:grpc" |
||||||
|
] |
||||||
|
} |
||||||
|
] |
||||||
|
} |
File diff suppressed because it is too large
Load Diff
@ -1,10 +0,0 @@ |
|||||||
{ |
|
||||||
"name": "grpc-demo", |
|
||||||
"version": "0.11.0", |
|
||||||
"dependencies": { |
|
||||||
"async": "^0.9.0", |
|
||||||
"grpc": "~0.11.0", |
|
||||||
"minimist": "^1.1.0", |
|
||||||
"underscore": "^1.8.2" |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,55 @@ |
|||||||
|
gRPC in 3 minutes (Python) |
||||||
|
======================== |
||||||
|
|
||||||
|
Background |
||||||
|
------------- |
||||||
|
For this sample, we've already generated the server and client stubs from |
||||||
|
[helloworld.proto][] and we'll be using a specific reference platform. |
||||||
|
|
||||||
|
Prerequisites |
||||||
|
------------- |
||||||
|
|
||||||
|
- Debian 8.2 "Jessie" platform with `root` access |
||||||
|
- `git` |
||||||
|
- `python2.7` |
||||||
|
- `pip` |
||||||
|
- Python development headers |
||||||
|
|
||||||
|
Set-up |
||||||
|
------- |
||||||
|
```sh |
||||||
|
$ # install the gRPC Core: |
||||||
|
$ sudo apt-get install libgrpc-dev |
||||||
|
$ # install gRPC Python: |
||||||
|
$ sudo pip install -U grpcio==0.11.0b1 |
||||||
|
$ # Since this "hello, world" example uses protocol buffers: |
||||||
|
$ sudo pip install -U protobuf==3.0.0a3 |
||||||
|
$ # Clone the repository to get the example code: |
||||||
|
$ git clone https://github.com/grpc/grpc |
||||||
|
$ # Navigate to the "hello, world" Python example: |
||||||
|
$ cd grpc/examples/python/helloworld |
||||||
|
``` |
||||||
|
|
||||||
|
Try it! |
||||||
|
------- |
||||||
|
|
||||||
|
- Run the server |
||||||
|
|
||||||
|
```sh |
||||||
|
$ python2.7 greeter_server.py & |
||||||
|
``` |
||||||
|
|
||||||
|
- Run the client |
||||||
|
|
||||||
|
```sh |
||||||
|
$ python2.7 greeter_client.py |
||||||
|
``` |
||||||
|
|
||||||
|
Tutorial |
||||||
|
-------- |
||||||
|
|
||||||
|
You can find a more detailed tutorial in [gRPC Basics: Python][] |
||||||
|
|
||||||
|
[helloworld.proto]:../protos/helloworld.proto |
||||||
|
[Install gRPC Python]:../../src/python#installation |
||||||
|
[gRPC Basics: Python]:http://www.grpc.io/docs/tutorials/basic/python.html |
@ -0,0 +1,202 @@ |
|||||||
|
# Generated by the protocol buffer compiler. DO NOT EDIT! |
||||||
|
# source: helloworld.proto |
||||||
|
|
||||||
|
from google.protobuf import descriptor as _descriptor |
||||||
|
from google.protobuf import message as _message |
||||||
|
from google.protobuf import reflection as _reflection |
||||||
|
from google.protobuf import symbol_database as _symbol_database |
||||||
|
from google.protobuf import descriptor_pb2 |
||||||
|
# @@protoc_insertion_point(imports) |
||||||
|
|
||||||
|
_sym_db = _symbol_database.Default() |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
DESCRIPTOR = _descriptor.FileDescriptor( |
||||||
|
name='helloworld.proto', |
||||||
|
package='helloworld', |
||||||
|
syntax='proto3', |
||||||
|
serialized_pb=b'\n\x10helloworld.proto\x12\nhelloworld\"\x1c\n\x0cHelloRequest\x12\x0c\n\x04name\x18\x01 \x01(\t\"\x1d\n\nHelloReply\x12\x0f\n\x07message\x18\x01 \x01(\t2I\n\x07Greeter\x12>\n\x08SayHello\x12\x18.helloworld.HelloRequest\x1a\x16.helloworld.HelloReply\"\x00\x42\x18\n\x10io.grpc.examples\xa2\x02\x03HLWb\x06proto3' |
||||||
|
) |
||||||
|
_sym_db.RegisterFileDescriptor(DESCRIPTOR) |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
_HELLOREQUEST = _descriptor.Descriptor( |
||||||
|
name='HelloRequest', |
||||||
|
full_name='helloworld.HelloRequest', |
||||||
|
filename=None, |
||||||
|
file=DESCRIPTOR, |
||||||
|
containing_type=None, |
||||||
|
fields=[ |
||||||
|
_descriptor.FieldDescriptor( |
||||||
|
name='name', full_name='helloworld.HelloRequest.name', index=0, |
||||||
|
number=1, type=9, cpp_type=9, label=1, |
||||||
|
has_default_value=False, default_value=b"".decode('utf-8'), |
||||||
|
message_type=None, enum_type=None, containing_type=None, |
||||||
|
is_extension=False, extension_scope=None, |
||||||
|
options=None), |
||||||
|
], |
||||||
|
extensions=[ |
||||||
|
], |
||||||
|
nested_types=[], |
||||||
|
enum_types=[ |
||||||
|
], |
||||||
|
options=None, |
||||||
|
is_extendable=False, |
||||||
|
syntax='proto3', |
||||||
|
extension_ranges=[], |
||||||
|
oneofs=[ |
||||||
|
], |
||||||
|
serialized_start=32, |
||||||
|
serialized_end=60, |
||||||
|
) |
||||||
|
|
||||||
|
|
||||||
|
_HELLOREPLY = _descriptor.Descriptor( |
||||||
|
name='HelloReply', |
||||||
|
full_name='helloworld.HelloReply', |
||||||
|
filename=None, |
||||||
|
file=DESCRIPTOR, |
||||||
|
containing_type=None, |
||||||
|
fields=[ |
||||||
|
_descriptor.FieldDescriptor( |
||||||
|
name='message', full_name='helloworld.HelloReply.message', index=0, |
||||||
|
number=1, type=9, cpp_type=9, label=1, |
||||||
|
has_default_value=False, default_value=b"".decode('utf-8'), |
||||||
|
message_type=None, enum_type=None, containing_type=None, |
||||||
|
is_extension=False, extension_scope=None, |
||||||
|
options=None), |
||||||
|
], |
||||||
|
extensions=[ |
||||||
|
], |
||||||
|
nested_types=[], |
||||||
|
enum_types=[ |
||||||
|
], |
||||||
|
options=None, |
||||||
|
is_extendable=False, |
||||||
|
syntax='proto3', |
||||||
|
extension_ranges=[], |
||||||
|
oneofs=[ |
||||||
|
], |
||||||
|
serialized_start=62, |
||||||
|
serialized_end=91, |
||||||
|
) |
||||||
|
|
||||||
|
DESCRIPTOR.message_types_by_name['HelloRequest'] = _HELLOREQUEST |
||||||
|
DESCRIPTOR.message_types_by_name['HelloReply'] = _HELLOREPLY |
||||||
|
|
||||||
|
HelloRequest = _reflection.GeneratedProtocolMessageType('HelloRequest', (_message.Message,), dict( |
||||||
|
DESCRIPTOR = _HELLOREQUEST, |
||||||
|
__module__ = 'helloworld_pb2' |
||||||
|
# @@protoc_insertion_point(class_scope:helloworld.HelloRequest) |
||||||
|
)) |
||||||
|
_sym_db.RegisterMessage(HelloRequest) |
||||||
|
|
||||||
|
HelloReply = _reflection.GeneratedProtocolMessageType('HelloReply', (_message.Message,), dict( |
||||||
|
DESCRIPTOR = _HELLOREPLY, |
||||||
|
__module__ = 'helloworld_pb2' |
||||||
|
# @@protoc_insertion_point(class_scope:helloworld.HelloReply) |
||||||
|
)) |
||||||
|
_sym_db.RegisterMessage(HelloReply) |
||||||
|
|
||||||
|
|
||||||
|
DESCRIPTOR.has_options = True |
||||||
|
DESCRIPTOR._options = _descriptor._ParseOptions(descriptor_pb2.FileOptions(), b'\n\020io.grpc.examples\242\002\003HLW') |
||||||
|
import abc |
||||||
|
from grpc.beta import implementations as beta_implementations |
||||||
|
from grpc.early_adopter import implementations as early_adopter_implementations |
||||||
|
from grpc.framework.alpha import utilities as alpha_utilities |
||||||
|
from grpc.framework.common import cardinality |
||||||
|
from grpc.framework.interfaces.face import utilities as face_utilities |
||||||
|
class EarlyAdopterGreeterServicer(object): |
||||||
|
"""<fill me in later!>""" |
||||||
|
__metaclass__ = abc.ABCMeta |
||||||
|
@abc.abstractmethod |
||||||
|
def SayHello(self, request, context): |
||||||
|
raise NotImplementedError() |
||||||
|
class EarlyAdopterGreeterServer(object): |
||||||
|
"""<fill me in later!>""" |
||||||
|
__metaclass__ = abc.ABCMeta |
||||||
|
@abc.abstractmethod |
||||||
|
def start(self): |
||||||
|
raise NotImplementedError() |
||||||
|
@abc.abstractmethod |
||||||
|
def stop(self): |
||||||
|
raise NotImplementedError() |
||||||
|
class EarlyAdopterGreeterStub(object): |
||||||
|
"""<fill me in later!>""" |
||||||
|
__metaclass__ = abc.ABCMeta |
||||||
|
@abc.abstractmethod |
||||||
|
def SayHello(self, request): |
||||||
|
raise NotImplementedError() |
||||||
|
SayHello.async = None |
||||||
|
def early_adopter_create_Greeter_server(servicer, port, private_key=None, certificate_chain=None): |
||||||
|
import helloworld_pb2 |
||||||
|
import helloworld_pb2 |
||||||
|
method_service_descriptions = { |
||||||
|
"SayHello": alpha_utilities.unary_unary_service_description( |
||||||
|
servicer.SayHello, |
||||||
|
helloworld_pb2.HelloRequest.FromString, |
||||||
|
helloworld_pb2.HelloReply.SerializeToString, |
||||||
|
), |
||||||
|
} |
||||||
|
return early_adopter_implementations.server("helloworld.Greeter", method_service_descriptions, port, private_key=private_key, certificate_chain=certificate_chain) |
||||||
|
def early_adopter_create_Greeter_stub(host, port, metadata_transformer=None, secure=False, root_certificates=None, private_key=None, certificate_chain=None, server_host_override=None): |
||||||
|
import helloworld_pb2 |
||||||
|
import helloworld_pb2 |
||||||
|
method_invocation_descriptions = { |
||||||
|
"SayHello": alpha_utilities.unary_unary_invocation_description( |
||||||
|
helloworld_pb2.HelloRequest.SerializeToString, |
||||||
|
helloworld_pb2.HelloReply.FromString, |
||||||
|
), |
||||||
|
} |
||||||
|
return early_adopter_implementations.stub("helloworld.Greeter", method_invocation_descriptions, host, port, metadata_transformer=metadata_transformer, secure=secure, root_certificates=root_certificates, private_key=private_key, certificate_chain=certificate_chain, server_host_override=server_host_override) |
||||||
|
|
||||||
|
class BetaGreeterServicer(object): |
||||||
|
"""<fill me in later!>""" |
||||||
|
__metaclass__ = abc.ABCMeta |
||||||
|
@abc.abstractmethod |
||||||
|
def SayHello(self, request, context): |
||||||
|
raise NotImplementedError() |
||||||
|
|
||||||
|
class BetaGreeterStub(object): |
||||||
|
"""The interface to which stubs will conform.""" |
||||||
|
__metaclass__ = abc.ABCMeta |
||||||
|
@abc.abstractmethod |
||||||
|
def SayHello(self, request, timeout): |
||||||
|
raise NotImplementedError() |
||||||
|
SayHello.future = None |
||||||
|
|
||||||
|
def beta_create_Greeter_server(servicer, pool=None, pool_size=None, default_timeout=None, maximum_timeout=None): |
||||||
|
import helloworld_pb2 |
||||||
|
import helloworld_pb2 |
||||||
|
request_deserializers = { |
||||||
|
('helloworld.Greeter', 'SayHello'): helloworld_pb2.HelloRequest.FromString, |
||||||
|
} |
||||||
|
response_serializers = { |
||||||
|
('helloworld.Greeter', 'SayHello'): helloworld_pb2.HelloReply.SerializeToString, |
||||||
|
} |
||||||
|
method_implementations = { |
||||||
|
('helloworld.Greeter', 'SayHello'): face_utilities.unary_unary_inline(servicer.SayHello), |
||||||
|
} |
||||||
|
server_options = beta_implementations.server_options(request_deserializers=request_deserializers, response_serializers=response_serializers, thread_pool=pool, thread_pool_size=pool_size, default_timeout=default_timeout, maximum_timeout=maximum_timeout) |
||||||
|
return beta_implementations.server(method_implementations, options=server_options) |
||||||
|
|
||||||
|
def beta_create_Greeter_stub(channel, host=None, metadata_transformer=None, pool=None, pool_size=None): |
||||||
|
import helloworld_pb2 |
||||||
|
import helloworld_pb2 |
||||||
|
request_serializers = { |
||||||
|
('helloworld.Greeter', 'SayHello'): helloworld_pb2.HelloRequest.SerializeToString, |
||||||
|
} |
||||||
|
response_deserializers = { |
||||||
|
('helloworld.Greeter', 'SayHello'): helloworld_pb2.HelloReply.FromString, |
||||||
|
} |
||||||
|
cardinalities = { |
||||||
|
'SayHello': cardinality.Cardinality.UNARY_UNARY, |
||||||
|
} |
||||||
|
stub_options = beta_implementations.stub_options(host=host, metadata_transformer=metadata_transformer, request_serializers=request_serializers, response_deserializers=response_deserializers, thread_pool=pool, thread_pool_size=pool_size) |
||||||
|
return beta_implementations.dynamic_stub(channel, 'helloworld.Greeter', cardinalities, options=stub_options) |
||||||
|
# @@protoc_insertion_point(module_scope) |
@ -0,0 +1,93 @@ |
|||||||
|
#region Copyright notice and license |
||||||
|
|
||||||
|
// 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. |
||||||
|
|
||||||
|
#endregion |
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Threading; |
||||||
|
|
||||||
|
using Google.Apis.Auth.OAuth2; |
||||||
|
using Grpc.Core; |
||||||
|
using Grpc.Core.Utils; |
||||||
|
|
||||||
|
namespace Grpc.Auth |
||||||
|
{ |
||||||
|
/// <summary> |
||||||
|
/// Factory methods to create instances of <see cref="ChannelCredentials"/> and <see cref="CallCredentials"/> classes. |
||||||
|
/// </summary> |
||||||
|
public static class GrpcCredentials |
||||||
|
{ |
||||||
|
/// <summary> |
||||||
|
/// Creates a <see cref="MetadataCredentials"/> instance that will obtain access tokens |
||||||
|
/// from any credential that implements <c>ITokenAccess</c>. (e.g. <c>GoogleCredential</c>). |
||||||
|
/// </summary> |
||||||
|
/// <param name="credential">The credential to use to obtain access tokens.</param> |
||||||
|
/// <returns>The <c>MetadataCredentials</c> instance.</returns> |
||||||
|
public static MetadataCredentials Create(ITokenAccess credential) |
||||||
|
{ |
||||||
|
return new MetadataCredentials(AuthInterceptors.FromCredential(credential)); |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Convenience method to create a <see cref="ChannelCredentials"/> instance from |
||||||
|
/// <c>ITokenAccess</c> credential and <c>SslCredentials</c> instance. |
||||||
|
/// </summary> |
||||||
|
/// <param name="credential">The credential to use to obtain access tokens.</param> |
||||||
|
/// <param name="sslCredentials">The <c>SslCredentials</c> instance.</param> |
||||||
|
/// <returns>The channel credentials for access token based auth over a secure channel.</returns> |
||||||
|
public static ChannelCredentials Create(ITokenAccess credential, SslCredentials sslCredentials) |
||||||
|
{ |
||||||
|
return ChannelCredentials.Create(sslCredentials, Create(credential)); |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Creates an instance of <see cref="MetadataCredentials"/> that will use given access token to authenticate |
||||||
|
/// with a gRPC service. |
||||||
|
/// </summary> |
||||||
|
/// <param name="accessToken">OAuth2 access token.</param> |
||||||
|
/// /// <returns>The <c>MetadataCredentials</c> instance.</returns> |
||||||
|
public static MetadataCredentials FromAccessToken(string accessToken) |
||||||
|
{ |
||||||
|
return new MetadataCredentials(AuthInterceptors.FromAccessToken(accessToken)); |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Converts a <c>ITokenAccess</c> object into a <see cref="MetadataCredentials"/> object supported |
||||||
|
/// by gRPC. |
||||||
|
/// </summary> |
||||||
|
/// <param name="credential"></param> |
||||||
|
/// <returns></returns> |
||||||
|
public static MetadataCredentials ToGrpcCredentials(this ITokenAccess credential) |
||||||
|
{ |
||||||
|
return GrpcCredentials.Create(credential); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,142 @@ |
|||||||
|
#region Copyright notice and license |
||||||
|
|
||||||
|
// 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. |
||||||
|
|
||||||
|
#endregion |
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Threading.Tasks; |
||||||
|
|
||||||
|
using Grpc.Core.Internal; |
||||||
|
using Grpc.Core.Utils; |
||||||
|
|
||||||
|
namespace Grpc.Core |
||||||
|
{ |
||||||
|
/// <summary> |
||||||
|
/// Client-side call credentials. Provide authorization with per-call granularity. |
||||||
|
/// </summary> |
||||||
|
public abstract class CallCredentials |
||||||
|
{ |
||||||
|
/// <summary> |
||||||
|
/// Composes multiple multiple <c>CallCredentials</c> objects into |
||||||
|
/// a single <c>CallCredentials</c> object. |
||||||
|
/// </summary> |
||||||
|
/// <param name="credentials">credentials to compose</param> |
||||||
|
/// <returns>The new <c>CompositeCallCredentials</c></returns> |
||||||
|
public static CallCredentials Compose(params CallCredentials[] credentials) |
||||||
|
{ |
||||||
|
return new CompositeCallCredentials(credentials); |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Creates native object for the credentials. |
||||||
|
/// </summary> |
||||||
|
/// <returns>The native credentials.</returns> |
||||||
|
internal abstract CredentialsSafeHandle ToNativeCredentials(); |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Asynchronous authentication interceptor for <see cref="MetadataCredentials"/>. |
||||||
|
/// </summary> |
||||||
|
/// <param name="authUri">URL of a service to which current remote call needs to authenticate</param> |
||||||
|
/// <param name="metadata">Metadata to populate with entries that will be added to outgoing call's headers.</param> |
||||||
|
/// <returns></returns> |
||||||
|
public delegate Task AsyncAuthInterceptor(string authUri, Metadata metadata); |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Client-side credentials that delegate metadata based auth to an interceptor. |
||||||
|
/// The interceptor is automatically invoked for each remote call that uses <c>MetadataCredentials.</c> |
||||||
|
/// </summary> |
||||||
|
public class MetadataCredentials : CallCredentials |
||||||
|
{ |
||||||
|
readonly AsyncAuthInterceptor interceptor; |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Initializes a new instance of <c>MetadataCredentials</c> class. |
||||||
|
/// </summary> |
||||||
|
/// <param name="interceptor">authentication interceptor</param> |
||||||
|
public MetadataCredentials(AsyncAuthInterceptor interceptor) |
||||||
|
{ |
||||||
|
this.interceptor = interceptor; |
||||||
|
} |
||||||
|
|
||||||
|
internal override CredentialsSafeHandle ToNativeCredentials() |
||||||
|
{ |
||||||
|
NativeMetadataCredentialsPlugin plugin = new NativeMetadataCredentialsPlugin(interceptor); |
||||||
|
return plugin.Credentials; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Credentials that allow composing multiple credentials objects into one <see cref="CallCredentials"/> object. |
||||||
|
/// </summary> |
||||||
|
internal sealed class CompositeCallCredentials : CallCredentials |
||||||
|
{ |
||||||
|
readonly List<CallCredentials> credentials; |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Initializes a new instance of <c>CompositeCallCredentials</c> class. |
||||||
|
/// The resulting credentials object will be composite of all the credentials specified as parameters. |
||||||
|
/// </summary> |
||||||
|
/// <param name="credentials">credentials to compose</param> |
||||||
|
public CompositeCallCredentials(params CallCredentials[] credentials) |
||||||
|
{ |
||||||
|
Preconditions.CheckArgument(credentials.Length >= 2, "Composite credentials object can only be created from 2 or more credentials."); |
||||||
|
this.credentials = new List<CallCredentials>(credentials); |
||||||
|
} |
||||||
|
|
||||||
|
internal override CredentialsSafeHandle ToNativeCredentials() |
||||||
|
{ |
||||||
|
return ToNativeRecursive(0); |
||||||
|
} |
||||||
|
|
||||||
|
// Recursive descent makes managing lifetime of intermediate CredentialSafeHandle instances easier. |
||||||
|
// In practice, we won't usually see composites from more than two credentials anyway. |
||||||
|
private CredentialsSafeHandle ToNativeRecursive(int startIndex) |
||||||
|
{ |
||||||
|
if (startIndex == credentials.Count - 1) |
||||||
|
{ |
||||||
|
return credentials[startIndex].ToNativeCredentials(); |
||||||
|
} |
||||||
|
|
||||||
|
using (var cred1 = credentials[startIndex].ToNativeCredentials()) |
||||||
|
using (var cred2 = ToNativeRecursive(startIndex + 1)) |
||||||
|
{ |
||||||
|
var nativeComposite = CredentialsSafeHandle.CreateComposite(cred1, cred2); |
||||||
|
if (nativeComposite.IsInvalid) |
||||||
|
{ |
||||||
|
throw new ArgumentException("Error creating native composite credentials. Likely, this is because you are trying to compose incompatible credentials."); |
||||||
|
} |
||||||
|
return nativeComposite; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,238 @@ |
|||||||
|
#region Copyright notice and license |
||||||
|
|
||||||
|
// 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. |
||||||
|
|
||||||
|
#endregion |
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.Threading.Tasks; |
||||||
|
|
||||||
|
using Grpc.Core.Internal; |
||||||
|
using Grpc.Core.Utils; |
||||||
|
|
||||||
|
namespace Grpc.Core |
||||||
|
{ |
||||||
|
/// <summary> |
||||||
|
/// Client-side channel credentials. Used for creation of a secure channel. |
||||||
|
/// </summary> |
||||||
|
public abstract class ChannelCredentials |
||||||
|
{ |
||||||
|
static readonly ChannelCredentials InsecureInstance = new InsecureCredentialsImpl(); |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Returns instance of credentials that provides no security and |
||||||
|
/// will result in creating an unsecure channel with no encryption whatsoever. |
||||||
|
/// </summary> |
||||||
|
public static ChannelCredentials Insecure |
||||||
|
{ |
||||||
|
get |
||||||
|
{ |
||||||
|
return InsecureInstance; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Creates a new instance of <c>ChannelCredentials</c> class by composing |
||||||
|
/// given channel credentials with call credentials. |
||||||
|
/// </summary> |
||||||
|
/// <param name="channelCredentials">Channel credentials.</param> |
||||||
|
/// <param name="callCredentials">Call credentials.</param> |
||||||
|
/// <returns>The new composite <c>ChannelCredentials</c></returns> |
||||||
|
public static ChannelCredentials Create(ChannelCredentials channelCredentials, CallCredentials callCredentials) |
||||||
|
{ |
||||||
|
return new CompositeChannelCredentials(channelCredentials, callCredentials); |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Creates a new instance of <c>ChannelCredentials</c> by wrapping |
||||||
|
/// an instance of <c>CallCredentials</c>. |
||||||
|
/// </summary> |
||||||
|
/// <param name="callCredentials">Call credentials.</param> |
||||||
|
/// <returns>The <c>ChannelCredentials</c> wrapping given call credentials.</returns> |
||||||
|
public static ChannelCredentials Create(CallCredentials callCredentials) |
||||||
|
{ |
||||||
|
return new WrappedCallCredentials(callCredentials); |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Creates native object for the credentials. May return null if insecure channel |
||||||
|
/// should be created. |
||||||
|
/// </summary> |
||||||
|
/// <returns>The native credentials.</returns> |
||||||
|
internal abstract CredentialsSafeHandle ToNativeCredentials(); |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Returns <c>true</c> if this credential type allows being composed by <c>CompositeCredentials</c>. |
||||||
|
/// </summary> |
||||||
|
internal virtual bool IsComposable |
||||||
|
{ |
||||||
|
get { return false; } |
||||||
|
} |
||||||
|
|
||||||
|
private sealed class InsecureCredentialsImpl : ChannelCredentials |
||||||
|
{ |
||||||
|
internal override CredentialsSafeHandle ToNativeCredentials() |
||||||
|
{ |
||||||
|
return null; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Client-side SSL credentials. |
||||||
|
/// </summary> |
||||||
|
public sealed class SslCredentials : ChannelCredentials |
||||||
|
{ |
||||||
|
readonly string rootCertificates; |
||||||
|
readonly KeyCertificatePair keyCertificatePair; |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Creates client-side SSL credentials loaded from |
||||||
|
/// disk file pointed to by the GRPC_DEFAULT_SSL_ROOTS_FILE_PATH environment variable. |
||||||
|
/// If that fails, gets the roots certificates from a well known place on disk. |
||||||
|
/// </summary> |
||||||
|
public SslCredentials() : this(null, null) |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Creates client-side SSL credentials from |
||||||
|
/// a string containing PEM encoded root certificates. |
||||||
|
/// </summary> |
||||||
|
public SslCredentials(string rootCertificates) : this(rootCertificates, null) |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Creates client-side SSL credentials. |
||||||
|
/// </summary> |
||||||
|
/// <param name="rootCertificates">string containing PEM encoded server root certificates.</param> |
||||||
|
/// <param name="keyCertificatePair">a key certificate pair.</param> |
||||||
|
public SslCredentials(string rootCertificates, KeyCertificatePair keyCertificatePair) |
||||||
|
{ |
||||||
|
this.rootCertificates = rootCertificates; |
||||||
|
this.keyCertificatePair = keyCertificatePair; |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// PEM encoding of the server root certificates. |
||||||
|
/// </summary> |
||||||
|
public string RootCertificates |
||||||
|
{ |
||||||
|
get |
||||||
|
{ |
||||||
|
return this.rootCertificates; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Client side key and certificate pair. |
||||||
|
/// If null, client will not use key and certificate pair. |
||||||
|
/// </summary> |
||||||
|
public KeyCertificatePair KeyCertificatePair |
||||||
|
{ |
||||||
|
get |
||||||
|
{ |
||||||
|
return this.keyCertificatePair; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Composing composite makes no sense. |
||||||
|
internal override bool IsComposable |
||||||
|
{ |
||||||
|
get { return true; } |
||||||
|
} |
||||||
|
|
||||||
|
internal override CredentialsSafeHandle ToNativeCredentials() |
||||||
|
{ |
||||||
|
return CredentialsSafeHandle.CreateSslCredentials(rootCertificates, keyCertificatePair); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Credentials that allow composing one <see cref="ChannelCredentials"/> object and |
||||||
|
/// one or more <see cref="CallCredentials"/> objects into a single <see cref="ChannelCredentials"/>. |
||||||
|
/// </summary> |
||||||
|
internal sealed class CompositeChannelCredentials : ChannelCredentials |
||||||
|
{ |
||||||
|
readonly ChannelCredentials channelCredentials; |
||||||
|
readonly CallCredentials callCredentials; |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Initializes a new instance of <c>CompositeChannelCredentials</c> class. |
||||||
|
/// The resulting credentials object will be composite of all the credentials specified as parameters. |
||||||
|
/// </summary> |
||||||
|
/// <param name="channelCredentials">channelCredentials to compose</param> |
||||||
|
/// <param name="callCredentials">channelCredentials to compose</param> |
||||||
|
public CompositeChannelCredentials(ChannelCredentials channelCredentials, CallCredentials callCredentials) |
||||||
|
{ |
||||||
|
this.channelCredentials = Preconditions.CheckNotNull(channelCredentials); |
||||||
|
this.callCredentials = Preconditions.CheckNotNull(callCredentials); |
||||||
|
Preconditions.CheckArgument(channelCredentials.IsComposable, "Supplied channel credentials do not allow composition."); |
||||||
|
} |
||||||
|
|
||||||
|
internal override CredentialsSafeHandle ToNativeCredentials() |
||||||
|
{ |
||||||
|
using (var cred1 = channelCredentials.ToNativeCredentials()) |
||||||
|
using (var cred2 = callCredentials.ToNativeCredentials()) |
||||||
|
{ |
||||||
|
var nativeComposite = CredentialsSafeHandle.CreateComposite(cred1, cred2); |
||||||
|
if (nativeComposite.IsInvalid) |
||||||
|
{ |
||||||
|
throw new ArgumentException("Error creating native composite credentials. Likely, this is because you are trying to compose incompatible credentials."); |
||||||
|
} |
||||||
|
return nativeComposite; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Credentials wrapping <see cref="CallCredentials"/> as <see cref="ChannelCredentials"/>. |
||||||
|
/// </summary> |
||||||
|
internal sealed class WrappedCallCredentials : ChannelCredentials |
||||||
|
{ |
||||||
|
readonly CallCredentials callCredentials; |
||||||
|
|
||||||
|
/// <summary> |
||||||
|
/// Wraps instance of <c>CallCredentials</c> as <c>ChannelCredentials</c>. |
||||||
|
/// </summary> |
||||||
|
/// <param name="callCredentials">credentials to wrap</param> |
||||||
|
public WrappedCallCredentials(CallCredentials callCredentials) |
||||||
|
{ |
||||||
|
this.callCredentials = Preconditions.CheckNotNull(callCredentials); |
||||||
|
} |
||||||
|
|
||||||
|
internal override CredentialsSafeHandle ToNativeCredentials() |
||||||
|
{ |
||||||
|
return callCredentials.ToNativeCredentials(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -1,138 +0,0 @@ |
|||||||
#region Copyright notice and license |
|
||||||
|
|
||||||
// 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. |
|
||||||
|
|
||||||
#endregion |
|
||||||
|
|
||||||
using System; |
|
||||||
using Grpc.Core.Internal; |
|
||||||
|
|
||||||
namespace Grpc.Core |
|
||||||
{ |
|
||||||
/// <summary> |
|
||||||
/// Client-side credentials. Used for creation of a secure channel. |
|
||||||
/// </summary> |
|
||||||
public abstract class Credentials |
|
||||||
{ |
|
||||||
static readonly Credentials InsecureInstance = new InsecureCredentialsImpl(); |
|
||||||
|
|
||||||
/// <summary> |
|
||||||
/// Returns instance of credential that provides no security and |
|
||||||
/// will result in creating an unsecure channel with no encryption whatsoever. |
|
||||||
/// </summary> |
|
||||||
public static Credentials Insecure |
|
||||||
{ |
|
||||||
get |
|
||||||
{ |
|
||||||
return InsecureInstance; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary> |
|
||||||
/// Creates native object for the credentials. May return null if insecure channel |
|
||||||
/// should be created. |
|
||||||
/// </summary> |
|
||||||
/// <returns>The native credentials.</returns> |
|
||||||
internal abstract CredentialsSafeHandle ToNativeCredentials(); |
|
||||||
|
|
||||||
private sealed class InsecureCredentialsImpl : Credentials |
|
||||||
{ |
|
||||||
internal override CredentialsSafeHandle ToNativeCredentials() |
|
||||||
{ |
|
||||||
return null; |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary> |
|
||||||
/// Client-side SSL credentials. |
|
||||||
/// </summary> |
|
||||||
public sealed class SslCredentials : Credentials |
|
||||||
{ |
|
||||||
readonly string rootCertificates; |
|
||||||
readonly KeyCertificatePair keyCertificatePair; |
|
||||||
|
|
||||||
/// <summary> |
|
||||||
/// Creates client-side SSL credentials loaded from |
|
||||||
/// disk file pointed to by the GRPC_DEFAULT_SSL_ROOTS_FILE_PATH environment variable. |
|
||||||
/// If that fails, gets the roots certificates from a well known place on disk. |
|
||||||
/// </summary> |
|
||||||
public SslCredentials() : this(null, null) |
|
||||||
{ |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary> |
|
||||||
/// Creates client-side SSL credentials from |
|
||||||
/// a string containing PEM encoded root certificates. |
|
||||||
/// </summary> |
|
||||||
public SslCredentials(string rootCertificates) : this(rootCertificates, null) |
|
||||||
{ |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary> |
|
||||||
/// Creates client-side SSL credentials. |
|
||||||
/// </summary> |
|
||||||
/// <param name="rootCertificates">string containing PEM encoded server root certificates.</param> |
|
||||||
/// <param name="keyCertificatePair">a key certificate pair.</param> |
|
||||||
public SslCredentials(string rootCertificates, KeyCertificatePair keyCertificatePair) |
|
||||||
{ |
|
||||||
this.rootCertificates = rootCertificates; |
|
||||||
this.keyCertificatePair = keyCertificatePair; |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary> |
|
||||||
/// PEM encoding of the server root certificates. |
|
||||||
/// </summary> |
|
||||||
public string RootCertificates |
|
||||||
{ |
|
||||||
get |
|
||||||
{ |
|
||||||
return this.rootCertificates; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/// <summary> |
|
||||||
/// Client side key and certificate pair. |
|
||||||
/// If null, client will not use key and certificate pair. |
|
||||||
/// </summary> |
|
||||||
public KeyCertificatePair KeyCertificatePair |
|
||||||
{ |
|
||||||
get |
|
||||||
{ |
|
||||||
return this.keyCertificatePair; |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
internal override CredentialsSafeHandle ToNativeCredentials() |
|
||||||
{ |
|
||||||
return CredentialsSafeHandle.CreateSslCredentials(rootCertificates, keyCertificatePair); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,113 @@ |
|||||||
|
#region Copyright notice and license |
||||||
|
// 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. |
||||||
|
#endregion |
||||||
|
using System; |
||||||
|
using System.Runtime.InteropServices; |
||||||
|
using System.Threading; |
||||||
|
using System.Threading.Tasks; |
||||||
|
|
||||||
|
using Grpc.Core.Logging; |
||||||
|
using Grpc.Core.Utils; |
||||||
|
|
||||||
|
namespace Grpc.Core.Internal |
||||||
|
{ |
||||||
|
internal delegate void NativeMetadataInterceptor(IntPtr statePtr, IntPtr serviceUrlPtr, IntPtr callbackPtr, IntPtr userDataPtr, bool isDestroy); |
||||||
|
|
||||||
|
internal class NativeMetadataCredentialsPlugin |
||||||
|
{ |
||||||
|
const string GetMetadataExceptionMsg = "Exception occured in metadata credentials plugin."; |
||||||
|
static readonly ILogger Logger = GrpcEnvironment.Logger.ForType<NativeMetadataCredentialsPlugin>(); |
||||||
|
|
||||||
|
[DllImport("grpc_csharp_ext.dll")] |
||||||
|
static extern CredentialsSafeHandle grpcsharp_metadata_credentials_create_from_plugin(NativeMetadataInterceptor interceptor); |
||||||
|
|
||||||
|
[DllImport("grpc_csharp_ext.dll", CharSet = CharSet.Ansi)] |
||||||
|
static extern void grpcsharp_metadata_credentials_notify_from_plugin(IntPtr callbackPtr, IntPtr userData, MetadataArraySafeHandle metadataArray, StatusCode statusCode, string errorDetails); |
||||||
|
|
||||||
|
AsyncAuthInterceptor interceptor; |
||||||
|
GCHandle gcHandle; |
||||||
|
NativeMetadataInterceptor nativeInterceptor; |
||||||
|
CredentialsSafeHandle credentials; |
||||||
|
|
||||||
|
public NativeMetadataCredentialsPlugin(AsyncAuthInterceptor interceptor) |
||||||
|
{ |
||||||
|
this.interceptor = Preconditions.CheckNotNull(interceptor, "interceptor"); |
||||||
|
this.nativeInterceptor = NativeMetadataInterceptorHandler; |
||||||
|
|
||||||
|
// Make sure the callback doesn't get garbage collected until it is destroyed. |
||||||
|
this.gcHandle = GCHandle.Alloc(this.nativeInterceptor, GCHandleType.Normal); |
||||||
|
this.credentials = grpcsharp_metadata_credentials_create_from_plugin(nativeInterceptor); |
||||||
|
} |
||||||
|
|
||||||
|
public CredentialsSafeHandle Credentials |
||||||
|
{ |
||||||
|
get { return credentials; } |
||||||
|
} |
||||||
|
|
||||||
|
private void NativeMetadataInterceptorHandler(IntPtr statePtr, IntPtr serviceUrlPtr, IntPtr callbackPtr, IntPtr userDataPtr, bool isDestroy) |
||||||
|
{ |
||||||
|
if (isDestroy) |
||||||
|
{ |
||||||
|
gcHandle.Free(); |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
try |
||||||
|
{ |
||||||
|
string serviceUrl = Marshal.PtrToStringAnsi(serviceUrlPtr); |
||||||
|
StartGetMetadata(serviceUrl, callbackPtr, userDataPtr); |
||||||
|
} |
||||||
|
catch (Exception e) |
||||||
|
{ |
||||||
|
grpcsharp_metadata_credentials_notify_from_plugin(callbackPtr, userDataPtr, MetadataArraySafeHandle.Create(Metadata.Empty), StatusCode.Unknown, GetMetadataExceptionMsg); |
||||||
|
Logger.Error(e, GetMetadataExceptionMsg); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
private async void StartGetMetadata(string serviceUrl, IntPtr callbackPtr, IntPtr userDataPtr) |
||||||
|
{ |
||||||
|
try |
||||||
|
{ |
||||||
|
var metadata = new Metadata(); |
||||||
|
await interceptor(serviceUrl, metadata); |
||||||
|
|
||||||
|
using (var metadataArray = MetadataArraySafeHandle.Create(metadata)) |
||||||
|
{ |
||||||
|
grpcsharp_metadata_credentials_notify_from_plugin(callbackPtr, userDataPtr, metadataArray, StatusCode.OK, null); |
||||||
|
} |
||||||
|
} |
||||||
|
catch (Exception e) |
||||||
|
{ |
||||||
|
grpcsharp_metadata_credentials_notify_from_plugin(callbackPtr, userDataPtr, MetadataArraySafeHandle.Create(Metadata.Empty), StatusCode.Unknown, GetMetadataExceptionMsg); |
||||||
|
Logger.Error(e, GetMetadataExceptionMsg); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,97 @@ |
|||||||
|
#region Copyright notice and license |
||||||
|
|
||||||
|
// 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. |
||||||
|
|
||||||
|
#endregion |
||||||
|
|
||||||
|
using System; |
||||||
|
using System.Collections.Generic; |
||||||
|
using System.IO; |
||||||
|
using System.Linq; |
||||||
|
using System.Threading; |
||||||
|
using System.Threading.Tasks; |
||||||
|
using Grpc.Core; |
||||||
|
using Grpc.Core.Utils; |
||||||
|
using Grpc.Testing; |
||||||
|
using NUnit.Framework; |
||||||
|
|
||||||
|
namespace Grpc.IntegrationTesting |
||||||
|
{ |
||||||
|
public class MetadataCredentialsTest |
||||||
|
{ |
||||||
|
const string Host = "localhost"; |
||||||
|
Server server; |
||||||
|
Channel channel; |
||||||
|
TestService.ITestServiceClient client; |
||||||
|
|
||||||
|
[TestFixtureSetUp] |
||||||
|
public void Init() |
||||||
|
{ |
||||||
|
var serverCredentials = new SslServerCredentials(new[] { new KeyCertificatePair(File.ReadAllText(TestCredentials.ServerCertChainPath), File.ReadAllText(TestCredentials.ServerPrivateKeyPath)) }); |
||||||
|
server = new Server |
||||||
|
{ |
||||||
|
Services = { TestService.BindService(new TestServiceImpl()) }, |
||||||
|
Ports = { { Host, ServerPort.PickUnused, serverCredentials } } |
||||||
|
}; |
||||||
|
server.Start(); |
||||||
|
|
||||||
|
var options = new List<ChannelOption> |
||||||
|
{ |
||||||
|
new ChannelOption(ChannelOptions.SslTargetNameOverride, TestCredentials.DefaultHostOverride) |
||||||
|
}; |
||||||
|
|
||||||
|
var asyncAuthInterceptor = new AsyncAuthInterceptor(async (authUri, metadata) => |
||||||
|
{ |
||||||
|
await Task.Delay(100); // make sure the operation is asynchronous. |
||||||
|
metadata.Add("authorization", "SECRET_TOKEN"); |
||||||
|
}); |
||||||
|
|
||||||
|
var clientCredentials = ChannelCredentials.Create( |
||||||
|
new SslCredentials(File.ReadAllText(TestCredentials.ClientCertAuthorityPath)), |
||||||
|
new MetadataCredentials(asyncAuthInterceptor)); |
||||||
|
channel = new Channel(Host, server.Ports.Single().BoundPort, clientCredentials, options); |
||||||
|
client = TestService.NewClient(channel); |
||||||
|
} |
||||||
|
|
||||||
|
[TestFixtureTearDown] |
||||||
|
public void Cleanup() |
||||||
|
{ |
||||||
|
channel.ShutdownAsync().Wait(); |
||||||
|
server.ShutdownAsync().Wait(); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void MetadataCredentials() |
||||||
|
{ |
||||||
|
var response = client.UnaryCall(new SimpleRequest { ResponseSize = 10 }); |
||||||
|
Assert.AreEqual(10, response.Payload.Body.Length); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -1,132 +0,0 @@ |
|||||||
|
|
||||||
// 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. |
|
||||||
|
|
||||||
// Message definitions to be used by integration test service definitions. |
|
||||||
|
|
||||||
syntax = "proto3"; |
|
||||||
|
|
||||||
package grpc.testing; |
|
||||||
|
|
||||||
// The type of payload that should be returned. |
|
||||||
enum PayloadType { |
|
||||||
// Compressable text format. |
|
||||||
COMPRESSABLE = 0; |
|
||||||
|
|
||||||
// Uncompressable binary format. |
|
||||||
UNCOMPRESSABLE = 1; |
|
||||||
|
|
||||||
// Randomly chosen from all other formats defined in this enum. |
|
||||||
RANDOM = 2; |
|
||||||
} |
|
||||||
|
|
||||||
// A block of data, to simply increase gRPC message size. |
|
||||||
message Payload { |
|
||||||
// The type of data in body. |
|
||||||
PayloadType type = 1; |
|
||||||
// Primary contents of payload. |
|
||||||
bytes body = 2; |
|
||||||
} |
|
||||||
|
|
||||||
// Unary request. |
|
||||||
message SimpleRequest { |
|
||||||
// Desired payload type in the response from the server. |
|
||||||
// If response_type is RANDOM, server randomly chooses one from other formats. |
|
||||||
PayloadType response_type = 1; |
|
||||||
|
|
||||||
// Desired payload size in the response from the server. |
|
||||||
// If response_type is COMPRESSABLE, this denotes the size before compression. |
|
||||||
int32 response_size = 2; |
|
||||||
|
|
||||||
// Optional input payload sent along with the request. |
|
||||||
Payload payload = 3; |
|
||||||
|
|
||||||
// Whether SimpleResponse should include username. |
|
||||||
bool fill_username = 4; |
|
||||||
|
|
||||||
// Whether SimpleResponse should include OAuth scope. |
|
||||||
bool fill_oauth_scope = 5; |
|
||||||
} |
|
||||||
|
|
||||||
// Unary response, as configured by the request. |
|
||||||
message SimpleResponse { |
|
||||||
// Payload to increase message size. |
|
||||||
Payload payload = 1; |
|
||||||
// The user the request came from, for verifying authentication was |
|
||||||
// successful when the client expected it. |
|
||||||
string username = 2; |
|
||||||
// OAuth scope. |
|
||||||
string oauth_scope = 3; |
|
||||||
} |
|
||||||
|
|
||||||
// Client-streaming request. |
|
||||||
message StreamingInputCallRequest { |
|
||||||
// Optional input payload sent along with the request. |
|
||||||
Payload payload = 1; |
|
||||||
|
|
||||||
// Not expecting any payload from the response. |
|
||||||
} |
|
||||||
|
|
||||||
// Client-streaming response. |
|
||||||
message StreamingInputCallResponse { |
|
||||||
// Aggregated size of payloads received from the client. |
|
||||||
int32 aggregated_payload_size = 1; |
|
||||||
} |
|
||||||
|
|
||||||
// Configuration for a particular response. |
|
||||||
message ResponseParameters { |
|
||||||
// Desired payload sizes in responses from the server. |
|
||||||
// If response_type is COMPRESSABLE, this denotes the size before compression. |
|
||||||
int32 size = 1; |
|
||||||
|
|
||||||
// Desired interval between consecutive responses in the response stream in |
|
||||||
// microseconds. |
|
||||||
int32 interval_us = 2; |
|
||||||
} |
|
||||||
|
|
||||||
// Server-streaming request. |
|
||||||
message StreamingOutputCallRequest { |
|
||||||
// Desired payload type in the response from the server. |
|
||||||
// If response_type is RANDOM, the payload from each response in the stream |
|
||||||
// might be of different types. This is to simulate a mixed type of payload |
|
||||||
// stream. |
|
||||||
PayloadType response_type = 1; |
|
||||||
|
|
||||||
// Configuration for each expected response message. |
|
||||||
repeated ResponseParameters response_parameters = 2; |
|
||||||
|
|
||||||
// Optional input payload sent along with the request. |
|
||||||
Payload payload = 3; |
|
||||||
} |
|
||||||
|
|
||||||
// Server-streaming response, as configured by the request and parameters. |
|
||||||
message StreamingOutputCallResponse { |
|
||||||
// Payload to increase response size. |
|
||||||
Payload payload = 1; |
|
||||||
} |
|
@ -1,71 +0,0 @@ |
|||||||
|
|
||||||
// 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. |
|
||||||
|
|
||||||
// An integration test service that covers all the method signature permutations |
|
||||||
// of unary/streaming requests/responses. |
|
||||||
syntax = "proto3"; |
|
||||||
|
|
||||||
import "empty.proto"; |
|
||||||
import "messages.proto"; |
|
||||||
|
|
||||||
package grpc.testing; |
|
||||||
|
|
||||||
// A simple service to test the various types of RPCs and experiment with |
|
||||||
// performance with various types of payload. |
|
||||||
service TestService { |
|
||||||
// One empty request followed by one empty response. |
|
||||||
rpc EmptyCall(grpc.testing.Empty) returns (grpc.testing.Empty); |
|
||||||
|
|
||||||
// One request followed by one response. |
|
||||||
rpc UnaryCall(SimpleRequest) returns (SimpleResponse); |
|
||||||
|
|
||||||
// One request followed by a sequence of responses (streamed download). |
|
||||||
// The server returns the payload with client desired type and sizes. |
|
||||||
rpc StreamingOutputCall(StreamingOutputCallRequest) |
|
||||||
returns (stream StreamingOutputCallResponse); |
|
||||||
|
|
||||||
// A sequence of requests followed by one response (streamed upload). |
|
||||||
// The server returns the aggregated size of client payload as the result. |
|
||||||
rpc StreamingInputCall(stream StreamingInputCallRequest) |
|
||||||
returns (StreamingInputCallResponse); |
|
||||||
|
|
||||||
// A sequence of requests with each request served by the server immediately. |
|
||||||
// As one request could lead to multiple responses, this interface |
|
||||||
// demonstrates the idea of full duplexing. |
|
||||||
rpc FullDuplexCall(stream StreamingOutputCallRequest) |
|
||||||
returns (stream StreamingOutputCallResponse); |
|
||||||
|
|
||||||
// A sequence of requests followed by a sequence of responses. |
|
||||||
// The server buffers all the client requests and then serves them in order. A |
|
||||||
// stream of responses are returned to the client when the server starts with |
|
||||||
// first request. |
|
||||||
rpc HalfDuplexCall(stream StreamingOutputCallRequest) |
|
||||||
returns (stream StreamingOutputCallResponse); |
|
||||||
} |
|
@ -1,28 +0,0 @@ |
|||||||
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. |
|
@ -1,16 +0,0 @@ |
|||||||
# Command Line Tools |
|
||||||
|
|
||||||
# Service Packager |
|
||||||
|
|
||||||
The command line tool `bin/service_packager`, when called with the following command line: |
|
||||||
|
|
||||||
```bash |
|
||||||
service_packager proto_file -o output_path -n name -v version [-i input_path...] |
|
||||||
``` |
|
||||||
|
|
||||||
Populates `output_path` with a node package consisting of a `package.json` populated with `name` and `version`, an `index.js`, a `LICENSE` file copied from gRPC, and a `service.json`, which is compiled from `proto_file` and the given `input_path`s. `require('output_path')` returns an object that is equivalent to |
|
||||||
|
|
||||||
```js |
|
||||||
{ client: require('grpc').load('service.json'), |
|
||||||
auth: require('google-auth-library') } |
|
||||||
``` |
|
@ -1,2 +0,0 @@ |
|||||||
#!/usr/bin/env node |
|
||||||
require(__dirname+'/../cli/service_packager.js').main(process.argv.slice(2)); |
|
@ -1,100 +0,0 @@ |
|||||||
{ |
|
||||||
"variables" : { |
|
||||||
'config': '<!(echo $CONFIG)' |
|
||||||
}, |
|
||||||
"targets" : [ |
|
||||||
{ |
|
||||||
'include_dirs': [ |
|
||||||
"<!(node -e \"require('nan')\")" |
|
||||||
], |
|
||||||
'cflags': [ |
|
||||||
'-std=c++0x', |
|
||||||
'-Wall', |
|
||||||
'-pthread', |
|
||||||
'-g', |
|
||||||
'-zdefs', |
|
||||||
'-Werror', |
|
||||||
'-Wno-error=deprecated-declarations' |
|
||||||
], |
|
||||||
'ldflags': [ |
|
||||||
'-g' |
|
||||||
], |
|
||||||
"conditions": [ |
|
||||||
['OS != "win"', { |
|
||||||
'variables': { |
|
||||||
'pkg_config_grpc': '<!(pkg-config --exists grpc >/dev/null 2>&1 && echo true || echo false)' |
|
||||||
}, |
|
||||||
'conditions': [ |
|
||||||
['config=="gcov"', { |
|
||||||
'cflags': [ |
|
||||||
'-ftest-coverage', |
|
||||||
'-fprofile-arcs', |
|
||||||
'-O0' |
|
||||||
], |
|
||||||
'ldflags': [ |
|
||||||
'-ftest-coverage', |
|
||||||
'-fprofile-arcs' |
|
||||||
] |
|
||||||
} |
|
||||||
], |
|
||||||
['pkg_config_grpc == "true"', { |
|
||||||
'link_settings': { |
|
||||||
'libraries': [ |
|
||||||
'<!@(pkg-config --libs-only-l --static grpc)' |
|
||||||
] |
|
||||||
}, |
|
||||||
'cflags': [ |
|
||||||
'<!@(pkg-config --cflags grpc)' |
|
||||||
], |
|
||||||
'libraries': [ |
|
||||||
'<!@(pkg-config --libs-only-L --static grpc)' |
|
||||||
], |
|
||||||
'ldflags': [ |
|
||||||
'<!@(pkg-config --libs-only-other --static grpc)' |
|
||||||
] |
|
||||||
}, { |
|
||||||
'link_settings': { |
|
||||||
'libraries': [ |
|
||||||
'-lpthread', |
|
||||||
'-lgrpc', |
|
||||||
'-lgpr' |
|
||||||
], |
|
||||||
}, |
|
||||||
'conditions':[ |
|
||||||
['OS != "mac"', { |
|
||||||
'link_settings': { |
|
||||||
'libraries': [ |
|
||||||
'-lrt' |
|
||||||
] |
|
||||||
} |
|
||||||
}] |
|
||||||
] |
|
||||||
} |
|
||||||
] |
|
||||||
] |
|
||||||
}], |
|
||||||
['OS == "mac"', { |
|
||||||
'xcode_settings': { |
|
||||||
'MACOSX_DEPLOYMENT_TARGET': '10.9', |
|
||||||
'OTHER_CFLAGS': [ |
|
||||||
'-std=c++11', |
|
||||||
'-stdlib=libc++' |
|
||||||
] |
|
||||||
} |
|
||||||
}] |
|
||||||
], |
|
||||||
"target_name": "grpc", |
|
||||||
"sources": [ |
|
||||||
"ext/byte_buffer.cc", |
|
||||||
"ext/call.cc", |
|
||||||
"ext/channel.cc", |
|
||||||
"ext/completion_queue_async_worker.cc", |
|
||||||
"ext/credentials.cc", |
|
||||||
"ext/node_grpc.cc", |
|
||||||
"ext/server.cc", |
|
||||||
"ext/server_credentials.cc", |
|
||||||
"ext/timeval.cc" |
|
||||||
] |
|
||||||
} |
|
||||||
] |
|
||||||
} |
|
@ -1,142 +0,0 @@ |
|||||||
/* |
|
||||||
* |
|
||||||
* 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. |
|
||||||
* |
|
||||||
*/ |
|
||||||
|
|
||||||
'use strict'; |
|
||||||
|
|
||||||
var fs = require('fs'); |
|
||||||
var path = require('path'); |
|
||||||
|
|
||||||
var _ = require('lodash'); |
|
||||||
var async = require('async'); |
|
||||||
var pbjs = require('protobufjs/cli/pbjs'); |
|
||||||
var parseArgs = require('minimist'); |
|
||||||
var Mustache = require('mustache'); |
|
||||||
|
|
||||||
var package_json = require('../package.json'); |
|
||||||
|
|
||||||
var template_path = path.resolve(__dirname, 'service_packager'); |
|
||||||
|
|
||||||
var package_tpl_path = path.join(template_path, 'package.json.template'); |
|
||||||
|
|
||||||
var arg_format = { |
|
||||||
string: ['include', 'out', 'name', 'version'], |
|
||||||
alias: { |
|
||||||
include: 'i', |
|
||||||
out: 'o', |
|
||||||
name: 'n', |
|
||||||
version: 'v' |
|
||||||
} |
|
||||||
}; |
|
||||||
|
|
||||||
// TODO(mlumish): autogenerate README.md from proto file
|
|
||||||
|
|
||||||
/** |
|
||||||
* Render package.json file from template using provided parameters. |
|
||||||
* @param {Object} params Map of parameter names to values |
|
||||||
* @param {function(Error, string)} callback Callback to pass rendered template |
|
||||||
* text to |
|
||||||
*/ |
|
||||||
function generatePackage(params, callback) { |
|
||||||
fs.readFile(package_tpl_path, {encoding: 'utf-8'}, function(err, template) { |
|
||||||
if (err) { |
|
||||||
callback(err); |
|
||||||
} else { |
|
||||||
var rendered = Mustache.render(template, params); |
|
||||||
callback(null, rendered); |
|
||||||
} |
|
||||||
}); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Copy a file |
|
||||||
* @param {string} src_path The filepath to copy from |
|
||||||
* @param {string} dest_path The filepath to copy to |
|
||||||
*/ |
|
||||||
function copyFile(src_path, dest_path) { |
|
||||||
fs.createReadStream(src_path).pipe(fs.createWriteStream(dest_path)); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Run the script. Copies the index.js and LICENSE files to the output path, |
|
||||||
* renders the package.json template to the output path, and generates a |
|
||||||
* service.json file from the input proto files using pbjs. The arguments are |
|
||||||
* taken directly from the command line, and handled as follows: |
|
||||||
* -i (--include) : An include path for pbjs (can be dpulicated) |
|
||||||
* -o (--output): The output path |
|
||||||
* -n (--name): The name of the package |
|
||||||
* -v (--version): The package version |
|
||||||
* @param {Array} argv The argument vector |
|
||||||
*/ |
|
||||||
function main(argv) { |
|
||||||
var args = parseArgs(argv, arg_format); |
|
||||||
var out_path = path.resolve(args.out); |
|
||||||
var include_dirs = []; |
|
||||||
if (args.include) { |
|
||||||
include_dirs = _.map(_.flatten([args.include]), function(p) { |
|
||||||
return path.resolve(p); |
|
||||||
}); |
|
||||||
} |
|
||||||
args.grpc_version = package_json.version; |
|
||||||
generatePackage(args, function(err, rendered) { |
|
||||||
if (err) throw err; |
|
||||||
fs.writeFile(path.join(out_path, 'package.json'), rendered, function(err) { |
|
||||||
if (err) throw err; |
|
||||||
}); |
|
||||||
}); |
|
||||||
copyFile(path.join(template_path, 'index.js'), |
|
||||||
path.join(out_path, 'index.js')); |
|
||||||
copyFile(path.join(__dirname, '..', 'LICENSE'), |
|
||||||
path.join(out_path, 'LICENSE')); |
|
||||||
|
|
||||||
var service_stream = fs.createWriteStream(path.join(out_path, |
|
||||||
'service.json')); |
|
||||||
var pbjs_args = _.flatten(['node', 'pbjs', |
|
||||||
args._[0], |
|
||||||
'-legacy', |
|
||||||
_.map(include_dirs, function(dir) { |
|
||||||
return "-path=" + dir; |
|
||||||
})]); |
|
||||||
var old_stdout = process.stdout; |
|
||||||
process.__defineGetter__('stdout', function() { |
|
||||||
return service_stream; |
|
||||||
}); |
|
||||||
var pbjs_status = pbjs.main(pbjs_args); |
|
||||||
process.__defineGetter__('stdout', function() { |
|
||||||
return old_stdout; |
|
||||||
}); |
|
||||||
if (pbjs_status !== pbjs.STATUS_OK) { |
|
||||||
throw new Error('pbjs failed with status code ' + pbjs_status); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
exports.main = main; |
|
@ -1,36 +0,0 @@ |
|||||||
/* |
|
||||||
* |
|
||||||
* 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 grpc = require('grpc'); |
|
||||||
exports.client = grpc.load(__dirname + '/service.json', 'json'); |
|
||||||
exports.auth = require('google-auth-library'); |
|
@ -1,17 +0,0 @@ |
|||||||
{ |
|
||||||
"name": "{{{name}}}", |
|
||||||
"version": "{{{version}}}", |
|
||||||
"author": "Google Inc.", |
|
||||||
"description": "Client library for {{{name}}} built on gRPC", |
|
||||||
"license": "Apache-2.0", |
|
||||||
"dependencies": { |
|
||||||
"grpc": "{{{grpc_version}}}", |
|
||||||
"google-auth-library": "^0.9.2" |
|
||||||
}, |
|
||||||
"main": "index.js", |
|
||||||
"files": [ |
|
||||||
"LICENSE", |
|
||||||
"index.js", |
|
||||||
"service.json" |
|
||||||
] |
|
||||||
} |
|
@ -1,47 +0,0 @@ |
|||||||
/* |
|
||||||
* |
|
||||||
* 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 grpc = require('..'); |
|
||||||
var examples = grpc.load(__dirname + '/stock.proto').examples; |
|
||||||
|
|
||||||
/** |
|
||||||
* This exports a client constructor for the Stock service. The usage looks like |
|
||||||
* |
|
||||||
* var StockClient = require('stock_client.js'); |
|
||||||
* var stockClient = new StockClient(server_address, |
|
||||||
* grpc.Credentials.createInsecure()); |
|
||||||
* stockClient.getLastTradePrice({symbol: 'GOOG'}, function(error, response) { |
|
||||||
* console.log(error || response); |
|
||||||
* }); |
|
||||||
*/ |
|
||||||
module.exports = examples.Stock; |
|
@ -1,87 +0,0 @@ |
|||||||
/* |
|
||||||
* |
|
||||||
* 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. |
|
||||||
* |
|
||||||
*/ |
|
||||||
|
|
||||||
'use strict'; |
|
||||||
|
|
||||||
var _ = require('lodash'); |
|
||||||
var grpc = require('..'); |
|
||||||
var examples = grpc.load(__dirname + '/stock.proto').examples; |
|
||||||
|
|
||||||
function getLastTradePrice(call, callback) { |
|
||||||
callback(null, {symbol: call.request.symbol, price: 88}); |
|
||||||
} |
|
||||||
|
|
||||||
function watchFutureTrades(call) { |
|
||||||
for (var i = 0; i < call.request.num_trades_to_watch; i++) { |
|
||||||
call.write({price: 88.00 + i * 10.00}); |
|
||||||
} |
|
||||||
call.end(); |
|
||||||
} |
|
||||||
|
|
||||||
function getHighestTradePrice(call, callback) { |
|
||||||
var trades = []; |
|
||||||
call.on('data', function(data) { |
|
||||||
trades.push({symbol: data.symbol, price: _.random(0, 100)}); |
|
||||||
}); |
|
||||||
call.on('end', function() { |
|
||||||
if(_.isEmpty(trades)) { |
|
||||||
callback(null, {}); |
|
||||||
} else { |
|
||||||
callback(null, _.max(trades, function(trade){return trade.price;})); |
|
||||||
} |
|
||||||
}); |
|
||||||
} |
|
||||||
|
|
||||||
function getLastTradePriceMultiple(call) { |
|
||||||
call.on('data', function(data) { |
|
||||||
call.write({price: 88}); |
|
||||||
}); |
|
||||||
call.on('end', function() { |
|
||||||
call.end(); |
|
||||||
}); |
|
||||||
} |
|
||||||
|
|
||||||
var stockServer = new grpc.Server(); |
|
||||||
stockServer.addProtoService(examples.Stock.service, { |
|
||||||
getLastTradePrice: getLastTradePrice, |
|
||||||
getLastTradePriceMultiple: getLastTradePriceMultiple, |
|
||||||
watchFutureTrades: watchFutureTrades, |
|
||||||
getHighestTradePrice: getHighestTradePrice |
|
||||||
}); |
|
||||||
|
|
||||||
if (require.main === module) { |
|
||||||
stockServer.bind('0.0.0.0:50051', grpc.ServerCredentials.createInsecure()); |
|
||||||
stockServer.start(); |
|
||||||
} |
|
||||||
|
|
||||||
module.exports = stockServer; |
|
@ -0,0 +1,259 @@ |
|||||||
|
/*
|
||||||
|
* |
||||||
|
* 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 <node.h> |
||||||
|
|
||||||
|
#include "grpc/grpc.h" |
||||||
|
#include "grpc/grpc_security.h" |
||||||
|
#include "grpc/support/log.h" |
||||||
|
#include "call_credentials.h" |
||||||
|
#include "call.h" |
||||||
|
|
||||||
|
namespace grpc { |
||||||
|
namespace node { |
||||||
|
|
||||||
|
using Nan::Callback; |
||||||
|
using Nan::EscapableHandleScope; |
||||||
|
using Nan::HandleScope; |
||||||
|
using Nan::Maybe; |
||||||
|
using Nan::MaybeLocal; |
||||||
|
using Nan::ObjectWrap; |
||||||
|
using Nan::Persistent; |
||||||
|
using Nan::Utf8String; |
||||||
|
|
||||||
|
using v8::Exception; |
||||||
|
using v8::External; |
||||||
|
using v8::Function; |
||||||
|
using v8::FunctionTemplate; |
||||||
|
using v8::Integer; |
||||||
|
using v8::Local; |
||||||
|
using v8::Object; |
||||||
|
using v8::ObjectTemplate; |
||||||
|
using v8::Value; |
||||||
|
|
||||||
|
Nan::Callback *CallCredentials::constructor; |
||||||
|
Persistent<FunctionTemplate> CallCredentials::fun_tpl; |
||||||
|
|
||||||
|
CallCredentials::CallCredentials(grpc_credentials *credentials) |
||||||
|
: wrapped_credentials(credentials) {} |
||||||
|
|
||||||
|
CallCredentials::~CallCredentials() { |
||||||
|
grpc_credentials_release(wrapped_credentials); |
||||||
|
} |
||||||
|
|
||||||
|
void CallCredentials::Init(Local<Object> exports) { |
||||||
|
HandleScope scope; |
||||||
|
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New); |
||||||
|
tpl->SetClassName(Nan::New("CallCredentials").ToLocalChecked()); |
||||||
|
tpl->InstanceTemplate()->SetInternalFieldCount(1); |
||||||
|
Nan::SetPrototypeMethod(tpl, "compose", Compose); |
||||||
|
fun_tpl.Reset(tpl); |
||||||
|
Local<Function> ctr = Nan::GetFunction(tpl).ToLocalChecked(); |
||||||
|
Nan::Set(ctr, Nan::New("createFromPlugin").ToLocalChecked(), |
||||||
|
Nan::GetFunction( |
||||||
|
Nan::New<FunctionTemplate>(CreateFromPlugin)).ToLocalChecked()); |
||||||
|
Nan::Set(exports, Nan::New("CallCredentials").ToLocalChecked(), ctr); |
||||||
|
constructor = new Nan::Callback(ctr); |
||||||
|
} |
||||||
|
|
||||||
|
bool CallCredentials::HasInstance(Local<Value> val) { |
||||||
|
HandleScope scope; |
||||||
|
return Nan::New(fun_tpl)->HasInstance(val); |
||||||
|
} |
||||||
|
|
||||||
|
Local<Value> CallCredentials::WrapStruct(grpc_credentials *credentials) { |
||||||
|
EscapableHandleScope scope; |
||||||
|
const int argc = 1; |
||||||
|
if (credentials == NULL) { |
||||||
|
return scope.Escape(Nan::Null()); |
||||||
|
} |
||||||
|
Local<Value> argv[argc] = { |
||||||
|
Nan::New<External>(reinterpret_cast<void *>(credentials))}; |
||||||
|
MaybeLocal<Object> maybe_instance = Nan::NewInstance( |
||||||
|
constructor->GetFunction(), argc, argv); |
||||||
|
if (maybe_instance.IsEmpty()) { |
||||||
|
return scope.Escape(Nan::Null()); |
||||||
|
} else { |
||||||
|
return scope.Escape(maybe_instance.ToLocalChecked()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
grpc_credentials *CallCredentials::GetWrappedCredentials() { |
||||||
|
return wrapped_credentials; |
||||||
|
} |
||||||
|
|
||||||
|
NAN_METHOD(CallCredentials::New) { |
||||||
|
if (info.IsConstructCall()) { |
||||||
|
if (!info[0]->IsExternal()) { |
||||||
|
return Nan::ThrowTypeError( |
||||||
|
"CallCredentials can only be created with the provided functions"); |
||||||
|
} |
||||||
|
Local<External> ext = info[0].As<External>(); |
||||||
|
grpc_credentials *creds_value = |
||||||
|
reinterpret_cast<grpc_credentials *>(ext->Value()); |
||||||
|
CallCredentials *credentials = new CallCredentials(creds_value); |
||||||
|
credentials->Wrap(info.This()); |
||||||
|
info.GetReturnValue().Set(info.This()); |
||||||
|
return; |
||||||
|
} else { |
||||||
|
const int argc = 1; |
||||||
|
Local<Value> argv[argc] = {info[0]}; |
||||||
|
MaybeLocal<Object> maybe_instance = constructor->GetFunction()->NewInstance( |
||||||
|
argc, argv); |
||||||
|
if (maybe_instance.IsEmpty()) { |
||||||
|
// There's probably a pending exception
|
||||||
|
return; |
||||||
|
} else { |
||||||
|
info.GetReturnValue().Set(maybe_instance.ToLocalChecked()); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
NAN_METHOD(CallCredentials::Compose) { |
||||||
|
if (!CallCredentials::HasInstance(info.This())) { |
||||||
|
return Nan::ThrowTypeError( |
||||||
|
"compose can only be called on CallCredentials objects"); |
||||||
|
} |
||||||
|
if (!CallCredentials::HasInstance(info[0])) { |
||||||
|
return Nan::ThrowTypeError( |
||||||
|
"compose's first argument must be a CallCredentials object"); |
||||||
|
} |
||||||
|
CallCredentials *self = ObjectWrap::Unwrap<CallCredentials>(info.This()); |
||||||
|
CallCredentials *other = ObjectWrap::Unwrap<CallCredentials>( |
||||||
|
Nan::To<Object>(info[0]).ToLocalChecked()); |
||||||
|
grpc_credentials *creds = grpc_composite_credentials_create( |
||||||
|
self->wrapped_credentials, other->wrapped_credentials, NULL); |
||||||
|
info.GetReturnValue().Set(WrapStruct(creds)); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
NAN_METHOD(CallCredentials::CreateFromPlugin) { |
||||||
|
if (!info[0]->IsFunction()) { |
||||||
|
return Nan::ThrowTypeError( |
||||||
|
"createFromPlugin's argument must be a function"); |
||||||
|
} |
||||||
|
grpc_metadata_credentials_plugin plugin; |
||||||
|
plugin_state *state = new plugin_state; |
||||||
|
state->callback = new Nan::Callback(info[0].As<Function>()); |
||||||
|
plugin.get_metadata = plugin_get_metadata; |
||||||
|
plugin.destroy = plugin_destroy_state; |
||||||
|
plugin.state = reinterpret_cast<void*>(state); |
||||||
|
grpc_credentials *creds = grpc_metadata_credentials_create_from_plugin(plugin, |
||||||
|
NULL); |
||||||
|
info.GetReturnValue().Set(WrapStruct(creds)); |
||||||
|
} |
||||||
|
|
||||||
|
NAN_METHOD(PluginCallback) { |
||||||
|
// Arguments: status code, error details, metadata
|
||||||
|
if (!info[0]->IsUint32()) { |
||||||
|
return Nan::ThrowTypeError( |
||||||
|
"The callback's first argument must be a status code"); |
||||||
|
} |
||||||
|
if (!info[1]->IsString()) { |
||||||
|
return Nan::ThrowTypeError( |
||||||
|
"The callback's second argument must be a string"); |
||||||
|
} |
||||||
|
if (!info[2]->IsObject()) { |
||||||
|
return Nan::ThrowTypeError( |
||||||
|
"The callback's third argument must be an object"); |
||||||
|
} |
||||||
|
shared_ptr<Resources> resources(new Resources); |
||||||
|
grpc_status_code code = static_cast<grpc_status_code>( |
||||||
|
Nan::To<uint32_t>(info[0]).FromJust()); |
||||||
|
char *details = *Utf8String(info[1]); |
||||||
|
grpc_metadata_array array; |
||||||
|
if (!CreateMetadataArray(Nan::To<Object>(info[2]).ToLocalChecked(), |
||||||
|
&array, resources)){ |
||||||
|
return Nan::ThrowError("Failed to parse metadata"); |
||||||
|
} |
||||||
|
grpc_credentials_plugin_metadata_cb cb = |
||||||
|
reinterpret_cast<grpc_credentials_plugin_metadata_cb>( |
||||||
|
Nan::Get(info.Callee(), |
||||||
|
Nan::New("cb").ToLocalChecked() |
||||||
|
).ToLocalChecked().As<External>()->Value()); |
||||||
|
void *user_data = |
||||||
|
Nan::Get(info.Callee(), |
||||||
|
Nan::New("user_data").ToLocalChecked() |
||||||
|
).ToLocalChecked().As<External>()->Value(); |
||||||
|
cb(user_data, array.metadata, array.count, code, details); |
||||||
|
} |
||||||
|
|
||||||
|
NAUV_WORK_CB(SendPluginCallback) { |
||||||
|
Nan::HandleScope scope; |
||||||
|
plugin_callback_data *data = reinterpret_cast<plugin_callback_data*>( |
||||||
|
async->data); |
||||||
|
// Attach cb and user_data to plugin_callback so that it can access them later
|
||||||
|
v8::Local<v8::Function> plugin_callback = Nan::GetFunction( |
||||||
|
Nan::New<v8::FunctionTemplate>(PluginCallback)).ToLocalChecked(); |
||||||
|
Nan::Set(plugin_callback, Nan::New("cb").ToLocalChecked(), |
||||||
|
Nan::New<v8::External>(reinterpret_cast<void*>(data->cb))); |
||||||
|
Nan::Set(plugin_callback, Nan::New("user_data").ToLocalChecked(), |
||||||
|
Nan::New<v8::External>(data->user_data)); |
||||||
|
const int argc = 2; |
||||||
|
v8::Local<v8::Value> argv[argc] = { |
||||||
|
Nan::New(data->service_url).ToLocalChecked(), |
||||||
|
plugin_callback |
||||||
|
}; |
||||||
|
Nan::Callback *callback = data->state->callback; |
||||||
|
callback->Call(argc, argv); |
||||||
|
delete data; |
||||||
|
uv_unref((uv_handle_t *)async); |
||||||
|
uv_close((uv_handle_t *)async, (uv_close_cb)free); |
||||||
|
} |
||||||
|
|
||||||
|
void plugin_get_metadata(void *state, const char *service_url, |
||||||
|
grpc_credentials_plugin_metadata_cb cb, |
||||||
|
void *user_data) { |
||||||
|
uv_async_t *async = static_cast<uv_async_t*>(malloc(sizeof(uv_async_t))); |
||||||
|
uv_async_init(uv_default_loop(), |
||||||
|
async, |
||||||
|
SendPluginCallback); |
||||||
|
plugin_callback_data *data = new plugin_callback_data; |
||||||
|
data->state = reinterpret_cast<plugin_state*>(state); |
||||||
|
data->service_url = service_url; |
||||||
|
data->cb = cb; |
||||||
|
data->user_data = user_data; |
||||||
|
async->data = data; |
||||||
|
/* libuv says that it will coalesce calls to uv_async_send. If there is ever a
|
||||||
|
* problem with a callback not getting called, that is probably the reason */ |
||||||
|
uv_async_send(async); |
||||||
|
} |
||||||
|
|
||||||
|
void plugin_destroy_state(void *ptr) { |
||||||
|
plugin_state *state = reinterpret_cast<plugin_state *>(ptr); |
||||||
|
delete state->callback; |
||||||
|
} |
||||||
|
|
||||||
|
} // namespace node
|
||||||
|
} // namespace grpc
|
@ -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. |
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
#ifndef GRPC_NODE_CALL_CREDENTIALS_H_ |
||||||
|
#define GRPC_NODE_CALL_CREDENTIALS_H_ |
||||||
|
|
||||||
|
#include <node.h> |
||||||
|
#include <nan.h> |
||||||
|
#include "grpc/grpc_security.h" |
||||||
|
|
||||||
|
namespace grpc { |
||||||
|
namespace node { |
||||||
|
|
||||||
|
class CallCredentials : public Nan::ObjectWrap { |
||||||
|
public: |
||||||
|
static void Init(v8::Local<v8::Object> exports); |
||||||
|
static bool HasInstance(v8::Local<v8::Value> val); |
||||||
|
/* Wrap a grpc_credentials struct in a javascript object */ |
||||||
|
static v8::Local<v8::Value> WrapStruct(grpc_credentials *credentials); |
||||||
|
|
||||||
|
/* Returns the grpc_credentials struct that this object wraps */ |
||||||
|
grpc_credentials *GetWrappedCredentials(); |
||||||
|
|
||||||
|
private: |
||||||
|
explicit CallCredentials(grpc_credentials *credentials); |
||||||
|
~CallCredentials(); |
||||||
|
|
||||||
|
// Prevent copying
|
||||||
|
CallCredentials(const CallCredentials &); |
||||||
|
CallCredentials &operator=(const CallCredentials &); |
||||||
|
|
||||||
|
static NAN_METHOD(New); |
||||||
|
static NAN_METHOD(CreateSsl); |
||||||
|
static NAN_METHOD(CreateFromPlugin); |
||||||
|
|
||||||
|
static NAN_METHOD(Compose); |
||||||
|
static Nan::Callback *constructor; |
||||||
|
// Used for typechecking instances of this javascript class
|
||||||
|
static Nan::Persistent<v8::FunctionTemplate> fun_tpl; |
||||||
|
|
||||||
|
grpc_credentials *wrapped_credentials; |
||||||
|
}; |
||||||
|
|
||||||
|
/* Auth metadata plugin functionality */ |
||||||
|
|
||||||
|
typedef struct plugin_state { |
||||||
|
Nan::Callback *callback; |
||||||
|
} plugin_state; |
||||||
|
|
||||||
|
typedef struct plugin_callback_data { |
||||||
|
plugin_state *state; |
||||||
|
const char *service_url; |
||||||
|
grpc_credentials_plugin_metadata_cb cb; |
||||||
|
void *user_data; |
||||||
|
} plugin_callback_data; |
||||||
|
|
||||||
|
void plugin_get_metadata(void *state, const char *service_url, |
||||||
|
grpc_credentials_plugin_metadata_cb cb, |
||||||
|
void *user_data); |
||||||
|
|
||||||
|
void plugin_destroy_state(void *state); |
||||||
|
|
||||||
|
NAN_METHOD(PluginCallback); |
||||||
|
|
||||||
|
NAUV_WORK_CB(SendPluginCallback); |
||||||
|
|
||||||
|
} // namespace node
|
||||||
|
} // namepsace grpc
|
||||||
|
|
||||||
|
#endif // GRPC_NODE_CALL_CREDENTIALS_H_
|
@ -1,43 +0,0 @@ |
|||||||
|
|
||||||
// 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"; |
|
||||||
|
|
||||||
package grpc.testing; |
|
||||||
|
|
||||||
// An empty message that you can re-use to avoid defining duplicated empty |
|
||||||
// messages in your project. A typical example is to use it as argument or the |
|
||||||
// return value of a service API. For instance: |
|
||||||
// |
|
||||||
// service Foo { |
|
||||||
// rpc Bar (grpc.testing.Empty) returns (grpc.testing.Empty) { }; |
|
||||||
// }; |
|
||||||
// |
|
||||||
message Empty {} |
|
@ -1,132 +0,0 @@ |
|||||||
|
|
||||||
// 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. |
|
||||||
|
|
||||||
// Message definitions to be used by integration test service definitions. |
|
||||||
|
|
||||||
syntax = "proto3"; |
|
||||||
|
|
||||||
package grpc.testing; |
|
||||||
|
|
||||||
// The type of payload that should be returned. |
|
||||||
enum PayloadType { |
|
||||||
// Compressable text format. |
|
||||||
COMPRESSABLE = 0; |
|
||||||
|
|
||||||
// Uncompressable binary format. |
|
||||||
UNCOMPRESSABLE = 1; |
|
||||||
|
|
||||||
// Randomly chosen from all other formats defined in this enum. |
|
||||||
RANDOM = 2; |
|
||||||
} |
|
||||||
|
|
||||||
// A block of data, to simply increase gRPC message size. |
|
||||||
message Payload { |
|
||||||
// The type of data in body. |
|
||||||
PayloadType type = 1; |
|
||||||
// Primary contents of payload. |
|
||||||
bytes body = 2; |
|
||||||
} |
|
||||||
|
|
||||||
// Unary request. |
|
||||||
message SimpleRequest { |
|
||||||
// Desired payload type in the response from the server. |
|
||||||
// If response_type is RANDOM, server randomly chooses one from other formats. |
|
||||||
PayloadType response_type = 1; |
|
||||||
|
|
||||||
// Desired payload size in the response from the server. |
|
||||||
// If response_type is COMPRESSABLE, this denotes the size before compression. |
|
||||||
int32 response_size = 2; |
|
||||||
|
|
||||||
// Optional input payload sent along with the request. |
|
||||||
Payload payload = 3; |
|
||||||
|
|
||||||
// Whether SimpleResponse should include username. |
|
||||||
bool fill_username = 4; |
|
||||||
|
|
||||||
// Whether SimpleResponse should include OAuth scope. |
|
||||||
bool fill_oauth_scope = 5; |
|
||||||
} |
|
||||||
|
|
||||||
// Unary response, as configured by the request. |
|
||||||
message SimpleResponse { |
|
||||||
// Payload to increase message size. |
|
||||||
Payload payload = 1; |
|
||||||
// The user the request came from, for verifying authentication was |
|
||||||
// successful when the client expected it. |
|
||||||
string username = 2; |
|
||||||
// OAuth scope. |
|
||||||
string oauth_scope = 3; |
|
||||||
} |
|
||||||
|
|
||||||
// Client-streaming request. |
|
||||||
message StreamingInputCallRequest { |
|
||||||
// Optional input payload sent along with the request. |
|
||||||
Payload payload = 1; |
|
||||||
|
|
||||||
// Not expecting any payload from the response. |
|
||||||
} |
|
||||||
|
|
||||||
// Client-streaming response. |
|
||||||
message StreamingInputCallResponse { |
|
||||||
// Aggregated size of payloads received from the client. |
|
||||||
int32 aggregated_payload_size = 1; |
|
||||||
} |
|
||||||
|
|
||||||
// Configuration for a particular response. |
|
||||||
message ResponseParameters { |
|
||||||
// Desired payload sizes in responses from the server. |
|
||||||
// If response_type is COMPRESSABLE, this denotes the size before compression. |
|
||||||
int32 size = 1; |
|
||||||
|
|
||||||
// Desired interval between consecutive responses in the response stream in |
|
||||||
// microseconds. |
|
||||||
int32 interval_us = 2; |
|
||||||
} |
|
||||||
|
|
||||||
// Server-streaming request. |
|
||||||
message StreamingOutputCallRequest { |
|
||||||
// Desired payload type in the response from the server. |
|
||||||
// If response_type is RANDOM, the payload from each response in the stream |
|
||||||
// might be of different types. This is to simulate a mixed type of payload |
|
||||||
// stream. |
|
||||||
PayloadType response_type = 1; |
|
||||||
|
|
||||||
// Configuration for each expected response message. |
|
||||||
repeated ResponseParameters response_parameters = 2; |
|
||||||
|
|
||||||
// Optional input payload sent along with the request. |
|
||||||
Payload payload = 3; |
|
||||||
} |
|
||||||
|
|
||||||
// Server-streaming response, as configured by the request and parameters. |
|
||||||
message StreamingOutputCallResponse { |
|
||||||
// Payload to increase response size. |
|
||||||
Payload payload = 1; |
|
||||||
} |
|
@ -1,72 +0,0 @@ |
|||||||
|
|
||||||
// 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. |
|
||||||
|
|
||||||
// An integration test service that covers all the method signature permutations |
|
||||||
// of unary/streaming requests/responses. |
|
||||||
|
|
||||||
syntax = "proto3"; |
|
||||||
|
|
||||||
import "empty.proto"; |
|
||||||
import "messages.proto"; |
|
||||||
|
|
||||||
package grpc.testing; |
|
||||||
|
|
||||||
// A simple service to test the various types of RPCs and experiment with |
|
||||||
// performance with various types of payload. |
|
||||||
service TestService { |
|
||||||
// One empty request followed by one empty response. |
|
||||||
rpc EmptyCall(grpc.testing.Empty) returns (grpc.testing.Empty); |
|
||||||
|
|
||||||
// One request followed by one response. |
|
||||||
rpc UnaryCall(SimpleRequest) returns (SimpleResponse); |
|
||||||
|
|
||||||
// One request followed by a sequence of responses (streamed download). |
|
||||||
// The server returns the payload with client desired type and sizes. |
|
||||||
rpc StreamingOutputCall(StreamingOutputCallRequest) |
|
||||||
returns (stream StreamingOutputCallResponse); |
|
||||||
|
|
||||||
// A sequence of requests followed by one response (streamed upload). |
|
||||||
// The server returns the aggregated size of client payload as the result. |
|
||||||
rpc StreamingInputCall(stream StreamingInputCallRequest) |
|
||||||
returns (StreamingInputCallResponse); |
|
||||||
|
|
||||||
// A sequence of requests with each request served by the server immediately. |
|
||||||
// As one request could lead to multiple responses, this interface |
|
||||||
// demonstrates the idea of full duplexing. |
|
||||||
rpc FullDuplexCall(stream StreamingOutputCallRequest) |
|
||||||
returns (stream StreamingOutputCallResponse); |
|
||||||
|
|
||||||
// A sequence of requests followed by a sequence of responses. |
|
||||||
// The server buffers all the client requests and then serves them in order. A |
|
||||||
// stream of responses are returned to the client when the server starts with |
|
||||||
// first request. |
|
||||||
rpc HalfDuplexCall(stream StreamingOutputCallRequest) |
|
||||||
returns (stream StreamingOutputCallResponse); |
|
||||||
} |
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue