Python secure server API changes

pull/835/head
Nathaniel Manista 10 years ago
parent e7460e87d3
commit a55c0949ee
  1. 6
      src/python/src/grpc/_adapter/_c_test.py
  2. 4
      src/python/src/grpc/_adapter/_low_test.py
  3. 43
      src/python/src/grpc/_adapter/_server.c
  4. 7
      src/python/src/grpc/_adapter/fore.py

@ -92,7 +92,7 @@ class _CTest(unittest.TestCase):
_c.init() _c.init()
completion_queue = _c.CompletionQueue() completion_queue = _c.CompletionQueue()
server = _c.Server(completion_queue, None) server = _c.Server(completion_queue)
server.add_http2_addr('[::]:0') server.add_http2_addr('[::]:0')
server.start() server.start()
server.stop() server.stop()
@ -102,7 +102,7 @@ class _CTest(unittest.TestCase):
service_tag = object() service_tag = object()
completion_queue = _c.CompletionQueue() completion_queue = _c.CompletionQueue()
server = _c.Server(completion_queue, None) server = _c.Server(completion_queue)
server.add_http2_addr('[::]:0') server.add_http2_addr('[::]:0')
server.start() server.start()
server.service(service_tag) server.service(service_tag)
@ -119,7 +119,7 @@ class _CTest(unittest.TestCase):
del completion_queue del completion_queue
completion_queue = _c.CompletionQueue() completion_queue = _c.CompletionQueue()
server = _c.Server(completion_queue, None) server = _c.Server(completion_queue)
server.add_http2_addr('[::]:0') server.add_http2_addr('[::]:0')
server.start() server.start()
thread = threading.Thread(target=completion_queue.get, args=(_FUTURE,)) thread = threading.Thread(target=completion_queue.get, args=(_FUTURE,))

@ -82,7 +82,7 @@ class EchoTest(unittest.TestCase):
self.host = 'localhost' self.host = 'localhost'
self.server_completion_queue = _low.CompletionQueue() self.server_completion_queue = _low.CompletionQueue()
self.server = _low.Server(self.server_completion_queue, None) self.server = _low.Server(self.server_completion_queue)
port = self.server.add_http2_addr('[::]:0') port = self.server.add_http2_addr('[::]:0')
self.server.start() self.server.start()
@ -260,7 +260,7 @@ class CancellationTest(unittest.TestCase):
self.host = 'localhost' self.host = 'localhost'
self.server_completion_queue = _low.CompletionQueue() self.server_completion_queue = _low.CompletionQueue()
self.server = _low.Server(self.server_completion_queue, None) self.server = _low.Server(self.server_completion_queue)
port = self.server.add_http2_addr('[::]:0') port = self.server.add_http2_addr('[::]:0')
self.server.start() self.server.start()

@ -42,30 +42,16 @@
static int pygrpc_server_init(Server *self, PyObject *args, PyObject *kwds) { static int pygrpc_server_init(Server *self, PyObject *args, PyObject *kwds) {
const PyObject *completion_queue; const PyObject *completion_queue;
PyObject *server_credentials; static char *kwlist[] = {"completion_queue", NULL};
static char *kwlist[] = {"completion_queue", "server_credentials", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O:Server", kwlist, if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!:Server", kwlist,
&pygrpc_CompletionQueueType, &pygrpc_CompletionQueueType,
&completion_queue, &server_credentials)) { &completion_queue)) {
return -1;
}
if (server_credentials == Py_None) {
self->c_server = grpc_server_create(
((CompletionQueue *)completion_queue)->c_completion_queue, NULL);
return 0;
} else if (PyObject_TypeCheck(server_credentials,
&pygrpc_ServerCredentialsType)) {
self->c_server = grpc_secure_server_create(
((ServerCredentials *)server_credentials)->c_server_credentials,
((CompletionQueue *)completion_queue)->c_completion_queue, NULL);
return 0;
} else {
PyErr_Format(PyExc_TypeError,
"server_credentials must be _grpc.ServerCredentials, not %s",
Py_TYPE(server_credentials)->tp_name);
return -1; return -1;
} }
self->c_server = grpc_server_create(
((CompletionQueue *)completion_queue)->c_completion_queue, NULL);
return 0;
} }
static void pygrpc_server_dealloc(Server *self) { static void pygrpc_server_dealloc(Server *self) {
@ -92,13 +78,21 @@ static PyObject *pygrpc_server_add_http2_addr(Server *self, PyObject *args) {
} }
static PyObject *pygrpc_server_add_secure_http2_addr(Server *self, static PyObject *pygrpc_server_add_secure_http2_addr(Server *self,
PyObject *args) { PyObject *args,
PyObject *kwargs) {
const char *addr; const char *addr;
PyObject *server_credentials;
static char *kwlist[] = {"addr", "server_credentials", NULL};
int port; int port;
if (!PyArg_ParseTuple(args, "s:add_secure_http2_addr", &addr)) {
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO!:add_secure_http2_addr",
kwlist, &addr, &pygrpc_ServerCredentialsType,
&server_credentials)) {
return NULL; return NULL;
} }
port = grpc_server_add_secure_http2_port(self->c_server, addr); port = grpc_server_add_secure_http2_port(
self->c_server, addr,
((ServerCredentials *)server_credentials)->c_server_credentials);
if (port == 0) { if (port == 0) {
PyErr_SetString(PyExc_RuntimeError, "Couldn't add port to server!"); PyErr_SetString(PyExc_RuntimeError, "Couldn't add port to server!");
return NULL; return NULL;
@ -138,8 +132,7 @@ static PyMethodDef methods[] = {
METH_VARARGS, "Add a secure HTTP2 address."}, METH_VARARGS, "Add a secure HTTP2 address."},
{"start", (PyCFunction)pygrpc_server_start, METH_NOARGS, {"start", (PyCFunction)pygrpc_server_start, METH_NOARGS,
"Starts the server."}, "Starts the server."},
{"service", (PyCFunction)pygrpc_server_service, METH_O, {"service", (PyCFunction)pygrpc_server_service, METH_O, "Services a call."},
"Services a call."},
{"stop", (PyCFunction)pygrpc_server_stop, METH_NOARGS, "Stops the server."}, {"stop", (PyCFunction)pygrpc_server_stop, METH_NOARGS, "Stops the server."},
{NULL}}; {NULL}};

@ -280,13 +280,14 @@ class ForeLink(ticket_interfaces.ForeLink, activated.Activated):
0 if self._requested_port is None else self._requested_port) 0 if self._requested_port is None else self._requested_port)
self._completion_queue = _low.CompletionQueue() self._completion_queue = _low.CompletionQueue()
if self._root_certificates is None and not self._key_chain_pairs: if self._root_certificates is None and not self._key_chain_pairs:
self._server = _low.Server(self._completion_queue, None) self._server = _low.Server(self._completion_queue)
self._port = self._server.add_http2_addr(address) self._port = self._server.add_http2_addr(address)
else: else:
server_credentials = _low.ServerCredentials( server_credentials = _low.ServerCredentials(
self._root_certificates, self._key_chain_pairs) self._root_certificates, self._key_chain_pairs)
self._server = _low.Server(self._completion_queue, server_credentials) self._server = _low.Server(self._completion_queue)
self._port = self._server.add_secure_http2_addr(address) self._port = self._server.add_secure_http2_addr(
address, server_credentials)
self._server.start() self._server.start()
self._server.service(None) self._server.service(None)

Loading…
Cancel
Save