aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/python/src/grpc/_adapter/_call.c
blob: bf96c1a3fa5d43168459116b11a99ab25ff4a9c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 *
 * 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 "grpc/_adapter/_call.h"

#include <math.h>
#include <Python.h>
#include <grpc/grpc.h>

#include "grpc/_adapter/_channel.h"
#include "grpc/_adapter/_completion_queue.h"
#include "grpc/_adapter/_error.h"

static int pygrpc_call_init(Call *self, PyObject *args, PyObject *kwds) {
  const PyObject *channel;
  const char *method;
  const char *host;
  double deadline;
  static char *kwlist[] = {"channel", "method", "host", "deadline", NULL};

  if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!ssd:Call", kwlist,
                                   &pygrpc_ChannelType, &channel, &method,
                                   &host, &deadline)) {
    return -1;
  }

  /* TODO(nathaniel): Hoist the gpr_timespec <-> PyFloat arithmetic into its own
   * function with its own test coverage.
   */
  self->c_call = grpc_channel_create_call_old(
      ((Channel *)channel)->c_channel, method, host,
      gpr_time_from_nanos(deadline * GPR_NS_PER_SEC));

  return 0;
}

static void pygrpc_call_dealloc(Call *self) {
  if (self->c_call != NULL) {
    grpc_call_destroy(self->c_call);
  }
  self->ob_type->tp_free((PyObject *)self);
}

static const PyObject *pygrpc_call_invoke(Call *self, PyObject *args) {
  const PyObject *completion_queue;
  const PyObject *metadata_tag;
  const PyObject *finish_tag;
  grpc_call_error call_error;
  const PyObject *result;

  if (!(PyArg_ParseTuple(args, "O!OO:invoke", &pygrpc_CompletionQueueType,
                         &completion_queue, &metadata_tag, &finish_tag))) {
    return NULL;
  }

  call_error = grpc_call_invoke_old(
      self->c_call, ((CompletionQueue *)completion_queue)->c_completion_queue,
      (void *)metadata_tag, (void *)finish_tag, 0);

  result = pygrpc_translate_call_error(call_error);
  if (result != NULL) {
    Py_INCREF(metadata_tag);
    Py_INCREF(finish_tag);
  }
  return result;
}

static const PyObject *pygrpc_call_write(Call *self, PyObject *args) {
  const char *bytes;
  int length;
  const PyObject *tag;
  gpr_slice slice;
  grpc_byte_buffer *byte_buffer;
  grpc_call_error call_error;
  const PyObject *result;

  if (!(PyArg_ParseTuple(args, "s#O:write", &bytes, &length, &tag))) {
    return NULL;
  }

  slice = gpr_slice_from_copied_buffer(bytes, length);
  byte_buffer = grpc_byte_buffer_create(&slice, 1);
  gpr_slice_unref(slice);

  call_error =
      grpc_call_start_write_old(self->c_call, byte_buffer, (void *)tag, 0);

  grpc_byte_buffer_destroy(byte_buffer);

  result = pygrpc_translate_call_error(call_error);
  if (result != NULL) {
    Py_INCREF(tag);
  }
  return result;
}

static const PyObject *pygrpc_call_complete(Call *self, PyObject *tag) {
  grpc_call_error call_error;
  const PyObject *result;

  call_error = grpc_call_writes_done_old(self->c_call, (void *)tag);

  result = pygrpc_translate_call_error(call_error);
  if (result != NULL) {
    Py_INCREF(tag);
  }
  return result;
}

static const PyObject *pygrpc_call_accept(Call *self, PyObject *args) {
  const PyObject *completion_queue;
  const PyObject *tag;
  grpc_call_error call_error;
  const PyObject *result;

  if (!(PyArg_ParseTuple(args, "O!O:accept", &pygrpc_CompletionQueueType,
                         &completion_queue, &tag))) {
    return NULL;
  }

  call_error = grpc_call_server_accept_old(
      self->c_call, ((CompletionQueue *)completion_queue)->c_completion_queue,
      (void *)tag);
  result = pygrpc_translate_call_error(call_error);

  if (result != NULL) {
    Py_INCREF(tag);
  }

  return result;
}

static const PyObject *pygrpc_call_add_metadata(Call *self, PyObject *args) {
  const char* key = NULL;
  const char* value = NULL;
  int value_length = 0;
  grpc_metadata metadata;
  if (!PyArg_ParseTuple(args, "ss#", &key, &value, &value_length)) {
    return NULL;
  }
  metadata.key = key;
  metadata.value = value;
  metadata.value_length = value_length;
  return pygrpc_translate_call_error(
      grpc_call_add_metadata_old(self->c_call, &metadata, 0));
}

static const PyObject *pygrpc_call_premetadata(Call *self) {
  return pygrpc_translate_call_error(
      grpc_call_server_end_initial_metadata_old(self->c_call, 0));
}

static const PyObject *pygrpc_call_read(Call *self, PyObject *tag) {
  grpc_call_error call_error;
  const PyObject *result;

  call_error = grpc_call_start_read_old(self->c_call, (void *)tag);

  result = pygrpc_translate_call_error(call_error);
  if (result != NULL) {
    Py_INCREF(tag);
  }
  return result;
}

static const PyObject *pygrpc_call_status(Call *self, PyObject *args) {
  PyObject *status;
  PyObject *code;
  PyObject *details;
  const PyObject *tag;
  grpc_status_code c_code;
  char *c_message;
  grpc_call_error call_error;
  const PyObject *result;

  if (!(PyArg_ParseTuple(args, "OO:status", &status, &tag))) {
    return NULL;
  }

  code = PyObject_GetAttrString(status, "code");
  if (code == NULL) {
    return NULL;
  }
  details = PyObject_GetAttrString(status, "details");
  if (details == NULL) {
    Py_DECREF(code);
    return NULL;
  }
  c_code = PyInt_AsLong(code);
  Py_DECREF(code);
  if (c_code == -1 && PyErr_Occurred()) {
    Py_DECREF(details);
    return NULL;
  }
  c_message = PyBytes_AsString(details);
  Py_DECREF(details);
  if (c_message == NULL) {
    return NULL;
  }

  call_error = grpc_call_start_write_status_old(self->c_call, c_code, c_message,
                                                (void *)tag);

  result = pygrpc_translate_call_error(call_error);
  if (result != NULL) {
    Py_INCREF(tag);
  }
  return result;
}

static const PyObject *pygrpc_call_cancel(Call *self) {
  return pygrpc_translate_call_error(grpc_call_cancel(self->c_call));
}

static PyMethodDef methods[] = {
    {"invoke", (PyCFunction)pygrpc_call_invoke, METH_VARARGS,
     "Invoke this call."},
    {"write", (PyCFunction)pygrpc_call_write, METH_VARARGS,
     "Write bytes to this call."},
    {"complete", (PyCFunction)pygrpc_call_complete, METH_O,
     "Complete writes to this call."},
    {"accept", (PyCFunction)pygrpc_call_accept, METH_VARARGS, "Accept an RPC."},
    {"add_metadata", (PyCFunction)pygrpc_call_add_metadata, METH_VARARGS,
     "Add metadata to the call. May not be called after invoke on the client "
     "side. On the server side: when called before premetadata it provides "
     "'leading' metadata, when called after premetadata but before status it "
     "provides 'trailing metadata'; may not be called after status."},
    {"premetadata", (PyCFunction)pygrpc_call_premetadata, METH_VARARGS,
     "Indicate the end of leading metadata in the response."},
    {"read", (PyCFunction)pygrpc_call_read, METH_O,
     "Read bytes from this call."},
    {"status", (PyCFunction)pygrpc_call_status, METH_VARARGS,
     "Report this call's status."},
    {"cancel", (PyCFunction)pygrpc_call_cancel, METH_NOARGS,
     "Cancel this call."},
    {NULL}};

PyTypeObject pygrpc_CallType = {
    PyVarObject_HEAD_INIT(NULL, 0)
    "_grpc.Call",                    /*tp_name*/
    sizeof(Call),                    /*tp_basicsize*/
    0,                               /*tp_itemsize*/
    (destructor)pygrpc_call_dealloc, /*tp_dealloc*/
    0,                               /*tp_print*/
    0,                               /*tp_getattr*/
    0,                               /*tp_setattr*/
    0,                               /*tp_compare*/
    0,                               /*tp_repr*/
    0,                               /*tp_as_number*/
    0,                               /*tp_as_sequence*/
    0,                               /*tp_as_mapping*/
    0,                               /*tp_hash */
    0,                               /*tp_call*/
    0,                               /*tp_str*/
    0,                               /*tp_getattro*/
    0,                               /*tp_setattro*/
    0,                               /*tp_as_buffer*/
    Py_TPFLAGS_DEFAULT,              /*tp_flags*/
    "Wrapping of grpc_call.",        /* tp_doc */
    0,                               /* tp_traverse */
    0,                               /* tp_clear */
    0,                               /* tp_richcompare */
    0,                               /* tp_weaklistoffset */
    0,                               /* tp_iter */
    0,                               /* tp_iternext */
    methods,                         /* tp_methods */
    0,                               /* tp_members */
    0,                               /* tp_getset */
    0,                               /* tp_base */
    0,                               /* tp_dict */
    0,                               /* tp_descr_get */
    0,                               /* tp_descr_set */
    0,                               /* tp_dictoffset */
    (initproc)pygrpc_call_init,      /* tp_init */
    0,                               /* tp_alloc */
    PyType_GenericNew,               /* tp_new */
};

int pygrpc_add_call(PyObject *module) {
  if (PyType_Ready(&pygrpc_CallType) < 0) {
    return -1;
  }
  if (PyModule_AddObject(module, "Call", (PyObject *)&pygrpc_CallType) == -1) {
    return -1;
  }
  return 0;
}