FD.io VPP  v16.12-rc0-308-g931be3a
Vector Packet Processing
pneum_wrap.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <Python.h>
17 #include "../pneum/pneum.h"
18 
19 static PyObject *pneum_callback = NULL;
20 
21 int
22 wrap_pneum_callback (char *data, int len)
23 {
24  PyGILState_STATE gstate;
25  PyObject *result;//, *arglist;
26 
27  gstate = PyGILState_Ensure();
28 
29  /* Time to call the callback */
30 #if PY_VERSION_HEX >= 0x03000000
31  result = PyObject_CallFunction(pneum_callback, "y#", data, len);
32 #else
33  result = PyObject_CallFunction(pneum_callback, "s#", data, len);
34 #endif
35  if (result)
36  Py_DECREF(result);
37  else
38  PyErr_Print();
39 
40  PyGILState_Release(gstate);
41  return (0);
42 }
43 
44 static PyObject *
45 wrap_connect (PyObject *self, PyObject *args)
46 {
47  char * name, * chroot_prefix = NULL;
48  int rv;
49  PyObject * temp;
50 
51  if (!PyArg_ParseTuple(args, "sO|s:wrap_connect", &name, &temp, &chroot_prefix))
52  return (NULL);
53 
54  if (!PyCallable_Check(temp)) {
55  PyErr_SetString(PyExc_TypeError, "parameter must be callable");
56  return NULL;
57  }
58 
59  Py_XINCREF(temp); /* Add a reference to new callback */
60  Py_XDECREF(pneum_callback); /* Dispose of previous callback */
61  pneum_callback = temp; /* Remember new callback */
62 
63  Py_BEGIN_ALLOW_THREADS
64  rv = pneum_connect(name, chroot_prefix);
65  Py_END_ALLOW_THREADS
66  return PyLong_FromLong(rv);
67 }
68 
69 static PyObject *
70 wrap_disconnect (PyObject *self, PyObject *args)
71 {
72  int rv;
73  Py_BEGIN_ALLOW_THREADS
74  rv = pneum_disconnect();
75  Py_END_ALLOW_THREADS
76  return PyLong_FromLong(rv);
77 }
78 static PyObject *
79 wrap_write (PyObject *self, PyObject *args)
80 {
81  char *data;
82  int len, rv;
83 
84  if (!PyArg_ParseTuple(args, "s#", &data, &len))
85  return NULL;
86  Py_BEGIN_ALLOW_THREADS
87  rv = pneum_write(data, len);
88  Py_END_ALLOW_THREADS
89 
90  return PyLong_FromLong(rv);
91 }
92 
93 void vl_msg_api_free(void *);
94 
95 static PyObject *
96 wrap_read (PyObject *self, PyObject *args)
97 {
98  char *data;
99  int len, rv;
100 
101  Py_BEGIN_ALLOW_THREADS
102  rv = pneum_read(&data, &len);
103  Py_END_ALLOW_THREADS
104 
105  if (rv != 0) { Py_RETURN_NONE; }
106 #if PY_VERSION_HEX >= 0x03000000
107  PyObject *ret = Py_BuildValue("y#", data, len);
108 #else
109  PyObject *ret = Py_BuildValue("s#", data, len);
110 #endif
111  if (!ret) { Py_RETURN_NONE; }
112 
113  vl_msg_api_free(data);
114  return ret;
115 }
116 
117 static PyMethodDef vpp_api_Methods[] = {
118  {"connect", wrap_connect, METH_VARARGS, "Connect to the VPP API."},
119  {"disconnect", wrap_disconnect, METH_VARARGS, "Disconnect from the VPP API."},
120  {"write", wrap_write, METH_VARARGS, "Write data to the VPP API."},
121  {"read", wrap_read, METH_VARARGS, "Read data from the VPP API."},
122  {NULL, NULL, 0, NULL} /* Sentinel */
123 };
124 
125 #if PY_VERSION_HEX >= 0x03000000
126 PyMODINIT_FUNC
127 PyInit_vpp_api (void)
128 #else
129 void
131 #endif
132 {
133 #if PY_VERSION_HEX >= 0x03000000
134  static struct PyModuleDef vpp_api_module = {
135 #if PY_VERSION_HEX >= 0x03020000
136  PyModuleDef_HEAD_INIT,
137 #else
138  {
139  PyObject_HEAD_INIT(NULL)
140  NULL, /* m_init */
141  0, /* m_index */
142  NULL, /* m_copy */
143  },
144 #endif
145  (char *) "vpp_api",
146  NULL,
147  -1,
148  vpp_api_Methods,
149  NULL,
150  NULL,
151  NULL,
152  NULL
153  };
154 #endif
155 
156  /* Ensure threading is initialised */
157  if (!PyEval_ThreadsInitialized()) {
158  PyEval_InitThreads();
159  }
160 
161 #if PY_VERSION_HEX >= 0x03000000
162  return PyModule_Create(&vpp_api_module);
163 #else
164  Py_InitModule((char *) "vpp_api", vpp_api_Methods);
165  return;
166 #endif
167 }
#define NULL
Definition: clib.h:55
static PyObject * wrap_disconnect(PyObject *self, PyObject *args)
Definition: pneum_wrap.c:70
int pneum_read(char **p, int *l)
Definition: pneum.c:174
int pneum_connect(char *name, char *chroot_prefix)
Definition: pneum.c:119
int pneum_disconnect(void)
Definition: pneum.c:151
void vl_msg_api_free(void *)
static PyObject * wrap_write(PyObject *self, PyObject *args)
Definition: pneum_wrap.c:79
static PyObject * wrap_read(PyObject *self, PyObject *args)
Definition: pneum_wrap.c:96
static PyMethodDef vpp_api_Methods[]
Definition: pneum_wrap.c:117
void initvpp_api(void)
Definition: pneum_wrap.c:130
static PyObject * pneum_callback
Definition: pneum_wrap.c:19
int pneum_write(char *p, int l)
Definition: pneum.c:216
int wrap_pneum_callback(char *data, int len)
Definition: pneum_wrap.c:22
static PyObject * wrap_connect(PyObject *self, PyObject *args)
Definition: pneum_wrap.c:45