Skip to content

Commit

Permalink
Merge pull request grpc#1031 from nathanielmanistaatgoogle/service_name
Browse files Browse the repository at this point in the history
Add service name to Python early_adopter
  • Loading branch information
soltanmm committed Mar 17, 2015
2 parents 78f1caa + c4fada6 commit d942720
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 95 deletions.
27 changes: 18 additions & 9 deletions src/compiler/python_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ bool GetModuleAndMessagePath(const Descriptor* type,
return true;
}

bool PrintServerFactory(const ServiceDescriptor* service, Printer* out) {
bool PrintServerFactory(const std::string& package_qualified_service_name,
const ServiceDescriptor* service, Printer* out) {
out->Print("def early_adopter_create_$Service$_server(servicer, port, "
"root_certificates, key_chain_pairs):\n",
"Service", service->name());
Expand Down Expand Up @@ -293,17 +294,18 @@ bool PrintServerFactory(const ServiceDescriptor* service, Printer* out) {
out->Print("),\n");
}
out->Print("}\n");
// out->Print("return implementations.insecure_server("
// "method_service_descriptions, port)\n");
out->Print(
"return implementations.secure_server("
"method_service_descriptions, port, root_certificates,"
" key_chain_pairs)\n");
"\"$PackageQualifiedServiceName$\","
" method_service_descriptions, port, root_certificates,"
" key_chain_pairs)\n",
"PackageQualifiedServiceName", package_qualified_service_name);
}
return true;
}

bool PrintStubFactory(const ServiceDescriptor* service, Printer* out) {
bool PrintStubFactory(const std::string& package_qualified_service_name,
const ServiceDescriptor* service, Printer* out) {
map<std::string, std::string> dict = ListToDict({
"Service", service->name(),
});
Expand Down Expand Up @@ -369,7 +371,9 @@ bool PrintStubFactory(const ServiceDescriptor* service, Printer* out) {
out->Print("}\n");
out->Print(
"return implementations.insecure_stub("
"method_invocation_descriptions, host, port)\n");
"\"$PackageQualifiedServiceName$\","
" method_invocation_descriptions, host, port)\n",
"PackageQualifiedServiceName", package_qualified_service_name);
}
return true;
}
Expand All @@ -392,13 +396,18 @@ pair<bool, std::string> GetServices(const FileDescriptor* file) {
if (!PrintPreamble(file, &out)) {
return make_pair(false, "");
}
auto package = file->package();
if (!package.empty()) {
package = package.append(".");
}
for (int i = 0; i < file->service_count(); ++i) {
auto service = file->service(i);
auto package_qualified_service_name = package + service->name();
if (!(PrintServicer(service, &out) &&
PrintServer(service, &out) &&
PrintStub(service, &out) &&
PrintServerFactory(service, &out) &&
PrintStubFactory(service, &out))) {
PrintServerFactory(package_qualified_service_name, service, &out) &&
PrintStubFactory(package_qualified_service_name, service, &out))) {
return make_pair(false, "");
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/python/interop/interop/_insecure_interop_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ class InsecureInteropTest(
unittest.TestCase):

def setUp(self):
self.server = implementations.insecure_server(methods.SERVER_METHODS, 0)
self.server = implementations.insecure_server(
methods.SERVICE_NAME, methods.SERVER_METHODS, 0)
self.server.start()
port = self.server.port()
self.stub = implementations.insecure_stub(
methods.CLIENT_METHODS, 'localhost', port)
methods.SERVICE_NAME, methods.CLIENT_METHODS, 'localhost', port)

def tearDown(self):
self.server.stop()
Expand Down
6 changes: 3 additions & 3 deletions src/python/interop/interop/_secure_interop_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ class SecureInteropTest(

def setUp(self):
self.server = implementations.secure_server(
methods.SERVER_METHODS, 0, resources.private_key(),
resources.certificate_chain())
methods.SERVICE_NAME, methods.SERVER_METHODS, 0,
resources.private_key(), resources.certificate_chain())
self.server.start()
port = self.server.port()
self.stub = implementations.secure_stub(
methods.CLIENT_METHODS, 'localhost', port,
methods.SERVICE_NAME, methods.CLIENT_METHODS, 'localhost', port,
resources.test_root_certificates(), None, None,
server_host_override=_SERVER_HOST_OVERRIDE)

Expand Down
7 changes: 4 additions & 3 deletions src/python/interop/interop/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,13 @@ def _stub(args):
root_certificates = resources.prod_root_certificates()

stub = implementations.secure_stub(
methods.CLIENT_METHODS, args.server_host, args.server_port,
root_certificates, None, None,
methods.SERVICE_NAME, methods.CLIENT_METHODS, args.server_host,
args.server_port, root_certificates, None, None,
server_host_override=args.server_host_override)
else:
stub = implementations.insecure_stub(
methods.CLIENT_METHODS, args.server_host, args.server_port)
methods.SERVICE_NAME, methods.CLIENT_METHODS, args.server_host,
args.server_port)
return stub


Expand Down
38 changes: 19 additions & 19 deletions src/python/interop/interop/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,31 +122,31 @@ def _full_duplex_call(request_iterator, unused_context):
messages_pb2.StreamingOutputCallResponse.SerializeToString)


_SERVICE_NAME = '/grpc.testing.TestService'
SERVICE_NAME = 'grpc.testing.TestService'

EMPTY_CALL_METHOD_NAME = _SERVICE_NAME + '/EmptyCall'
UNARY_CALL_METHOD_NAME = _SERVICE_NAME + '/UnaryCall'
STREAMING_OUTPUT_CALL_METHOD_NAME = _SERVICE_NAME + '/StreamingOutputCall'
STREAMING_INPUT_CALL_METHOD_NAME = _SERVICE_NAME + '/StreamingInputCall'
FULL_DUPLEX_CALL_METHOD_NAME = _SERVICE_NAME + '/FullDuplexCall'
HALF_DUPLEX_CALL_METHOD_NAME = _SERVICE_NAME + '/HalfDuplexCall'
_EMPTY_CALL_METHOD_NAME = 'EmptyCall'
_UNARY_CALL_METHOD_NAME = 'UnaryCall'
_STREAMING_OUTPUT_CALL_METHOD_NAME = 'StreamingOutputCall'
_STREAMING_INPUT_CALL_METHOD_NAME = 'StreamingInputCall'
_FULL_DUPLEX_CALL_METHOD_NAME = 'FullDuplexCall'
_HALF_DUPLEX_CALL_METHOD_NAME = 'HalfDuplexCall'

CLIENT_METHODS = {
EMPTY_CALL_METHOD_NAME: _CLIENT_EMPTY_CALL,
UNARY_CALL_METHOD_NAME: _CLIENT_UNARY_CALL,
STREAMING_OUTPUT_CALL_METHOD_NAME: _CLIENT_STREAMING_OUTPUT_CALL,
STREAMING_INPUT_CALL_METHOD_NAME: _CLIENT_STREAMING_INPUT_CALL,
FULL_DUPLEX_CALL_METHOD_NAME: _CLIENT_FULL_DUPLEX_CALL,
HALF_DUPLEX_CALL_METHOD_NAME: _CLIENT_HALF_DUPLEX_CALL,
_EMPTY_CALL_METHOD_NAME: _CLIENT_EMPTY_CALL,
_UNARY_CALL_METHOD_NAME: _CLIENT_UNARY_CALL,
_STREAMING_OUTPUT_CALL_METHOD_NAME: _CLIENT_STREAMING_OUTPUT_CALL,
_STREAMING_INPUT_CALL_METHOD_NAME: _CLIENT_STREAMING_INPUT_CALL,
_FULL_DUPLEX_CALL_METHOD_NAME: _CLIENT_FULL_DUPLEX_CALL,
_HALF_DUPLEX_CALL_METHOD_NAME: _CLIENT_HALF_DUPLEX_CALL,
}

SERVER_METHODS = {
EMPTY_CALL_METHOD_NAME: _SERVER_EMPTY_CALL,
UNARY_CALL_METHOD_NAME: _SERVER_UNARY_CALL,
STREAMING_OUTPUT_CALL_METHOD_NAME: _SERVER_STREAMING_OUTPUT_CALL,
STREAMING_INPUT_CALL_METHOD_NAME: _SERVER_STREAMING_INPUT_CALL,
FULL_DUPLEX_CALL_METHOD_NAME: _SERVER_FULL_DUPLEX_CALL,
HALF_DUPLEX_CALL_METHOD_NAME: _SERVER_HALF_DUPLEX_CALL,
_EMPTY_CALL_METHOD_NAME: _SERVER_EMPTY_CALL,
_UNARY_CALL_METHOD_NAME: _SERVER_UNARY_CALL,
_STREAMING_OUTPUT_CALL_METHOD_NAME: _SERVER_STREAMING_OUTPUT_CALL,
_STREAMING_INPUT_CALL_METHOD_NAME: _SERVER_STREAMING_INPUT_CALL,
_FULL_DUPLEX_CALL_METHOD_NAME: _SERVER_FULL_DUPLEX_CALL,
_HALF_DUPLEX_CALL_METHOD_NAME: _SERVER_HALF_DUPLEX_CALL,
}


Expand Down
5 changes: 3 additions & 2 deletions src/python/interop/interop/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ def serve():
private_key = resources.private_key()
certificate_chain = resources.certificate_chain()
server = implementations.secure_server(
methods.SERVER_METHODS, args.port, private_key, certificate_chain)
methods.SERVICE_NAME, methods.SERVER_METHODS, args.port, private_key,
certificate_chain)
else:
server = implementations.insecure_server(
methods.SERVER_METHODS, args.port)
methods.SERVICE_NAME, methods.SERVER_METHODS, args.port)

server.start()
logging.info('Server serving.')
Expand Down
82 changes: 56 additions & 26 deletions src/python/src/grpc/early_adopter/_face_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,28 @@
from grpc.early_adopter import interfaces


def _qualified_name(service_name, method_name):
return '/%s/%s' % (service_name, method_name)


# TODO(nathaniel): This structure is getting bloated; it could be shrunk if
# implementations._Stub used a generic rather than a dynamic underlying
# face-layer stub.
class InvocationBreakdown(object):
"""An intermediate representation of invocation-side views of RPC methods.
Attributes:
cardinalities: A dictionary from RPC method name to interfaces.Cardinality
value.
request_serializers: A dictionary from RPC method name to callable
behavior to be used serializing request values for the RPC.
response_deserializers: A dictionary from RPC method name to callable
behavior to be used deserializing response values for the RPC.
qualified_names: A dictionary from unqualified RPC method name to
service-qualified RPC method name.
face_cardinalities: A dictionary from service-qualified RPC method name to
to cardinality.Cardinality value.
request_serializers: A dictionary from service-qualified RPC method name to
callable behavior to be used serializing request values for the RPC.
response_deserializers: A dictionary from service-qualified RPC method name
to callable behavior to be used deserializing response values for the
RPC.
"""
__metaclass__ = abc.ABCMeta

Expand All @@ -56,20 +68,21 @@ class _EasyInvocationBreakdown(
InvocationBreakdown,
collections.namedtuple(
'_EasyInvocationBreakdown',
('cardinalities', 'request_serializers', 'response_deserializers'))):
('cardinalities', 'qualified_names', 'face_cardinalities',
'request_serializers', 'response_deserializers'))):
pass


class ServiceBreakdown(object):
"""An intermediate representation of service-side views of RPC methods.
Attributes:
implementations: A dictionary from RPC method name to
implementations: A dictionary from service-qualified RPC method name to
face_interfaces.MethodImplementation implementing the RPC method.
request_deserializers: A dictionary from RPC method name to callable
behavior to be used deserializing request values for the RPC.
response_serializers: A dictionary from RPC method name to callable
behavior to be used serializing response values for the RPC.
request_deserializers: A dictionary from service-qualified RPC method name
to callable behavior to be used deserializing request values for the RPC.
response_serializers: A dictionary from service-qualified RPC method name
to callable behavior to be used serializing response values for the RPC.
"""
__metaclass__ = abc.ABCMeta

Expand All @@ -82,28 +95,38 @@ class _EasyServiceBreakdown(
pass


def break_down_invocation(method_descriptions):
def break_down_invocation(service_name, method_descriptions):
"""Derives an InvocationBreakdown from several RPC method descriptions.
Args:
service_name: The package-qualified full name of the service.
method_descriptions: A dictionary from RPC method name to
interfaces.RpcMethodInvocationDescription describing the RPCs.
Returns:
An InvocationBreakdown corresponding to the given method descriptions.
"""
cardinalities = {}
qualified_names = {}
face_cardinalities = {}
request_serializers = {}
response_deserializers = {}
for name, method_description in method_descriptions.iteritems():
qualified_name = _qualified_name(service_name, name)
method_cardinality = method_description.cardinality()
cardinalities[name] = method_description.cardinality()
request_serializers[name] = method_description.serialize_request
response_deserializers[name] = method_description.deserialize_response
qualified_names[name] = qualified_name
face_cardinalities[qualified_name] = _reexport.common_cardinality(
method_cardinality)
request_serializers[qualified_name] = method_description.serialize_request
response_deserializers[qualified_name] = (
method_description.deserialize_response)
return _EasyInvocationBreakdown(
cardinalities, request_serializers, response_deserializers)
cardinalities, qualified_names, face_cardinalities, request_serializers,
response_deserializers)


def break_down_service(method_descriptions):
def break_down_service(service_name, method_descriptions):
"""Derives a ServiceBreakdown from several RPC method descriptions.
Args:
Expand All @@ -117,37 +140,44 @@ def break_down_service(method_descriptions):
request_deserializers = {}
response_serializers = {}
for name, method_description in method_descriptions.iteritems():
cardinality = method_description.cardinality()
if cardinality is interfaces.Cardinality.UNARY_UNARY:
qualified_name = _qualified_name(service_name, name)
method_cardinality = method_description.cardinality()
if method_cardinality is interfaces.Cardinality.UNARY_UNARY:
def service(
request, face_rpc_context,
service_behavior=method_description.service_unary_unary):
return service_behavior(
request, _reexport.rpc_context(face_rpc_context))
implementations[name] = face_utilities.unary_unary_inline(service)
elif cardinality is interfaces.Cardinality.UNARY_STREAM:
implementations[qualified_name] = face_utilities.unary_unary_inline(
service)
elif method_cardinality is interfaces.Cardinality.UNARY_STREAM:
def service(
request, face_rpc_context,
service_behavior=method_description.service_unary_stream):
return service_behavior(
request, _reexport.rpc_context(face_rpc_context))
implementations[name] = face_utilities.unary_stream_inline(service)
elif cardinality is interfaces.Cardinality.STREAM_UNARY:
implementations[qualified_name] = face_utilities.unary_stream_inline(
service)
elif method_cardinality is interfaces.Cardinality.STREAM_UNARY:
def service(
request_iterator, face_rpc_context,
service_behavior=method_description.service_stream_unary):
return service_behavior(
request_iterator, _reexport.rpc_context(face_rpc_context))
implementations[name] = face_utilities.stream_unary_inline(service)
elif cardinality is interfaces.Cardinality.STREAM_STREAM:
implementations[qualified_name] = face_utilities.stream_unary_inline(
service)
elif method_cardinality is interfaces.Cardinality.STREAM_STREAM:
def service(
request_iterator, face_rpc_context,
service_behavior=method_description.service_stream_stream):
return service_behavior(
request_iterator, _reexport.rpc_context(face_rpc_context))
implementations[name] = face_utilities.stream_stream_inline(service)
request_deserializers[name] = method_description.deserialize_request
response_serializers[name] = method_description.serialize_response
implementations[qualified_name] = face_utilities.stream_stream_inline(
service)
request_deserializers[qualified_name] = (
method_description.deserialize_request)
response_serializers[qualified_name] = (
method_description.serialize_response)

return _EasyServiceBreakdown(
implementations, request_deserializers, response_serializers)
5 changes: 5 additions & 0 deletions src/python/src/grpc/early_adopter/_reexport.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ def async(self, request_iterator, timeout):
return _ReexportedFuture(self._underlying.future(request_iterator, timeout))


def common_cardinality(early_adopter_cardinality):
return _EARLY_ADOPTER_CARDINALITY_TO_COMMON_CARDINALITY[
early_adopter_cardinality]


def common_cardinalities(early_adopter_cardinalities):
common_cardinalities = {}
for name, early_adopter_cardinality in early_adopter_cardinalities.iteritems():
Expand Down
Loading

0 comments on commit d942720

Please sign in to comment.