FD.io VPP  v16.09
Vector Packet Processing
pneum_wrap.c
Go to the documentation of this file.
1 #include <Python.h>
2 #include "pneum.h"
3 
4 static PyObject *pneum_callback = NULL;
5 
6 int
7 wrap_pneum_callback (char *data, int len)
8 {
9  PyGILState_STATE gstate;
10  PyObject *result;//, *arglist;
11 
12  gstate = PyGILState_Ensure();
13 
14  /* Time to call the callback */
15 #if PY_VERSION_HEX >= 0x03000000
16  result = PyObject_CallFunction(pneum_callback, "y#", data, len);
17 #else
18  result = PyObject_CallFunction(pneum_callback, "s#", data, len);
19 #endif
20  if (result)
21  Py_DECREF(result);
22  else
23  PyErr_Print();
24 
25  PyGILState_Release(gstate);
26  return (0);
27 }
28 
29 static PyObject *
30 wrap_connect (PyObject *self, PyObject *args)
31 {
32  char *name;
33  int rv;
34  PyObject *temp;
35 
36  if (!PyArg_ParseTuple(args, "sO:set_callback", &name, &temp))
37  return (NULL);
38 
39  if (!PyCallable_Check(temp)) {
40  PyErr_SetString(PyExc_TypeError, "parameter must be callable");
41  return NULL;
42  }
43 
44  Py_XINCREF(temp); /* Add a reference to new callback */
45  Py_XDECREF(pneum_callback); /* Dispose of previous callback */
46  pneum_callback = temp; /* Remember new callback */
47 
48  Py_BEGIN_ALLOW_THREADS
49  rv = pneum_connect(name);
50  Py_END_ALLOW_THREADS
51  return PyLong_FromLong(rv);
52 }
53 
54 static PyObject *
55 wrap_disconnect (PyObject *self, PyObject *args)
56 {
57  int rv;
58  Py_BEGIN_ALLOW_THREADS
59  rv = pneum_disconnect();
60  Py_END_ALLOW_THREADS
61  return PyLong_FromLong(rv);
62 }
63 static PyObject *
64 wrap_write (PyObject *self, PyObject *args)
65 {
66  char *data;
67  int len, rv;
68 
69  if (!PyArg_ParseTuple(args, "s#", &data, &len))
70  return NULL;
71  Py_BEGIN_ALLOW_THREADS
72  rv = pneum_write(data, len);
73  Py_END_ALLOW_THREADS
74 
75  return PyLong_FromLong(rv);
76 }
77 
78 void vl_msg_api_free(void *);
79 
80 static PyObject *
81 wrap_read (PyObject *self, PyObject *args)
82 {
83  char *data;
84  int len, rv;
85 
86  Py_BEGIN_ALLOW_THREADS
87  rv = pneum_read(&data, &len);
88  Py_END_ALLOW_THREADS
89 
90  if (rv != 0) { Py_RETURN_NONE; }
91 #if PY_VERSION_HEX >= 0x03000000
92  PyObject *ret = Py_BuildValue("y#", data, len);
93 #else
94  PyObject *ret = Py_BuildValue("s#", data, len);
95 #endif
96  if (!ret) { Py_RETURN_NONE; }
97 
98  vl_msg_api_free(data);
99  return ret;
100 }
101 
102 static PyMethodDef vpp_api_Methods[] = {
103  {"connect", wrap_connect, METH_VARARGS, "Connect to the VPP API."},
104  {"disconnect", wrap_disconnect, METH_VARARGS, "Disconnect from the VPP API."},
105  {"write", wrap_write, METH_VARARGS, "Write data to the VPP API."},
106  {"read", wrap_read, METH_VARARGS, "Read data from the VPP API."},
107  {NULL, NULL, 0, NULL} /* Sentinel */
108 };
109 
110 #if PY_VERSION_HEX >= 0x03000000
111 PyMODINIT_FUNC
112 PyInit_vpp_api (void)
113 #else
114 void
116 #endif
117 {
118 #if PY_VERSION_HEX >= 0x03000000
119  static struct PyModuleDef vpp_api_module = {
120 # if PY_VERSION_HEX >= 0x03020000
121  PyModuleDef_HEAD_INIT,
122 # else
123  {
124  PyObject_HEAD_INIT(NULL)
125  NULL, /* m_init */
126  0, /* m_index */
127  NULL, /* m_copy */
128  },
129 # endif
130  (char *) "vpp_api",
131  NULL,
132  -1,
133  vpp_api_Methods,
134  NULL,
135  NULL,
136  NULL,
137  NULL
138  };
139 #endif
140 
141  /* Ensure threading is initialised */
142  if (!PyEval_ThreadsInitialized()) {
143  PyEval_InitThreads();
144  }
145 
146 #if PY_VERSION_HEX >= 0x03000000
147  return PyModule_Create(&vpp_api_module);
148 #else
149  Py_InitModule((char *) "vpp_api", vpp_api_Methods);
150  return;
151 #endif
152 }
#define NULL
Definition: clib.h:55
static PyObject * wrap_disconnect(PyObject *self, PyObject *args)
Definition: pneum_wrap.c:55
int pneum_read(char **p, int *l)
Definition: pneum.c:179
int pneum_disconnect(void)
Definition: pneum.c:154
void vl_msg_api_free(void *)
static PyObject * wrap_write(PyObject *self, PyObject *args)
Definition: pneum_wrap.c:64
static PyObject * wrap_read(PyObject *self, PyObject *args)
Definition: pneum_wrap.c:81
static PyMethodDef vpp_api_Methods[]
Definition: pneum_wrap.c:102
void initvpp_api(void)
Definition: pneum_wrap.c:115
int pneum_connect(char *name)
Definition: pneum.c:119
static PyObject * pneum_callback
Definition: pneum_wrap.c:4
int pneum_write(char *p, int l)
Definition: pneum.c:221
int wrap_pneum_callback(char *data, int len)
Definition: pneum_wrap.c:7
static PyObject * wrap_connect(PyObject *self, PyObject *args)
Definition: pneum_wrap.c:30