Debian Codegraph @

protolib/src/python/protopipe.cpp

raw
1
2 // Python binding code for ProtoPipe class
3
4 #include "protopy.h"
5 #include "protoPipe.h"
6 #include <protoSocket.h>
7
8 extern "C" {
9
10
11 typedef struct {
12 PyObject_HEAD
13 ProtoPipe *thisptr;
14 bool isConnected;
15 } Pipe;
16
17 static void Pipe_dealloc(Pipe *self) {
18 if (self->isConnected)
19 self->thisptr->Close();
20
21 delete self->thisptr;
22 Py_TYPE(self)->tp_free((PyObject*)self);
23 }
24
25 static PyObject* Pipe_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) {
26 Pipe *self = (Pipe*)type->tp_alloc(type, 0);
27
28 if (self == NULL)
29 return NULL;
30
31 self->thisptr = NULL;
32 self->isConnected = false;
33
34 return (PyObject*)self;
35 }
36
37 static int Pipe_init(Pipe *self, PyObject *args, PyObject *kwargs) {
38 const char *typestr = NULL;
39 static char *kwlist[] = {(char*)"type", NULL};
40
41 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &typestr))
42 return -1;
43
44 if (strcmp(typestr, "MESSAGE") == 0)
45 self->thisptr = new ProtoPipe(ProtoPipe::MESSAGE);
46 else if (strcmp(typestr, "STREAM") == 0)
47 self->thisptr = new ProtoPipe(ProtoPipe::STREAM);
48 else
49 return -2;
50
51 return 0;
52 }
53
54 static PyObject* Pipe_GetType(Pipe *self) {
55 ProtoPipe::Type type = self->thisptr->GetType();
56
57 switch (type) {
58 case ProtoPipe::MESSAGE:
59 return Py_BuildValue("s", "MESSAGE");
60 break;
61
62 case ProtoPipe::STREAM:
63 return Py_BuildValue("s", "STREAM");
64 break;
65
66 default:
67 PyErr_SetString(ProtoError, "Invalid Pipe Type");
68 return NULL;
69 break;
70 }
71 }
72
73 static PyObject* Pipe_GetName(Pipe *self) {
74
75 #if PY_MAJOR_VERSION >= 3
76 PyObject *rv = PyUnicode_FromString(self->thisptr->GetName()); //Human readable?
77 #else
78 PyObject *rv = PyString_FromString(self->thisptr->GetName()); //Human readable?
79 #endif
80
81 if (rv == NULL)
82 PyErr_SetString(ProtoError, "Could not get Pipe name.");
83
84 return rv;
85 }
86
87 static PyObject* Pipe_Connect(Pipe *self, PyObject *args) {
88 const char * name;
89
90 if (!PyArg_ParseTuple(args, "s", &name))
91 return NULL;
92
93 if (!self->thisptr->Connect(name)) {
94 PyErr_SetString(ProtoError, "Could not connect to server.");
95 return NULL;
96 }
97
98 self->isConnected = true;
99 Py_RETURN_NONE;
100 }
101
102 static PyObject* Pipe_Listen(Pipe *self, PyObject *args)
103 {
104 const char *name;
105 if (!PyArg_ParseTuple(args, "s", &name))
106 return NULL;
107 if (!self->thisptr->Listen(name)) {
108 PyErr_SetString(ProtoError, "Could not start listener.");
109 return NULL;
110 }
111 self->isConnected = true;
112 Py_RETURN_NONE;
113 }
114
115 // Thisc currently only allows a single connection to be accepted
116 // TBD - provide option to accept multiple connections
117 static PyObject* Pipe_Accept(Pipe *self, PyObject *args) {
118
119 if (!self->thisptr->Accept())
120 {
121 PyErr_SetString(ProtoError, "ProtoPipe::Accept() error");
122 return NULL;
123 }
124 //PyErr_SetString(PyExc_NotImplementedError, "");
125 //return NULL;
126 Py_RETURN_NONE;
127 }
128
129 static PyObject* Pipe_Close(Pipe *self) {
130 self->thisptr->Close();
131 self->isConnected = false;
132 Py_RETURN_NONE;
133 }
134
135 static PyObject* Pipe_Send(Pipe *self, PyObject *args) {
136 const char *buffer;
137 unsigned int size;
138
139 if (!PyArg_ParseTuple(args, "s#", &buffer, &size))
140 return NULL;
141
142 if (!self->thisptr->Send(buffer, size)) {
143 PyErr_SetString(ProtoError, "Could not send buffer.");
144 return NULL;
145 }
146 Py_RETURN_NONE;
147 }
148
149 static PyObject* Pipe_Recv(Pipe *self, PyObject *args) {
150 char *buffer;
151 unsigned int size;
152 bool result = false;
153
154 if (!PyArg_ParseTuple(args, "I", &size))
155 return NULL;
156
157 buffer = new char[size];
158
159 // Release the GIL since this can block...
160 Py_BEGIN_ALLOW_THREADS
161 result = self->thisptr->Recv(buffer, size);
162 Py_END_ALLOW_THREADS
163
164 if (!result) {
165 PyErr_SetString(ProtoError, "Could not recv.");
166 return NULL;
167 }
168
169 #if PY_MAJOR_VERSION >= 3
170 PyObject *rv = PyBytes_FromStringAndSize(buffer, size); //Human readable?
171 #else
172 PyObject *rv = PyString_FromStringAndSize(buffer, size); //Human readable?
173 #endif
174 delete[] buffer;
175 return rv;
176 }
177
178 static PyObject* Pipe_GetHandle(Pipe *self) {
179 return Py_BuildValue("i", self->thisptr->GetHandle());
180 }
181
182 static PyMethodDef Pipe_methods[] = {
183 {"GetType", (PyCFunction)Pipe_GetType, METH_NOARGS,
184 "Returns the type of the pipe."},
185 {"GetName", (PyCFunction)Pipe_GetName, METH_NOARGS,
186 "Returns the name of the pipe."},
187 {"Connect", (PyCFunction)Pipe_Connect, METH_VARARGS,
188 "Connects the pipe."},
189 {"Listen", (PyCFunction)Pipe_Listen, METH_VARARGS,
190 "Starts a listener on the pipe."},
191 {"Accept", (PyCFunction)Pipe_Accept, METH_VARARGS,
192 "Accepts multiple incoming connections."},
193 {"Close", (PyCFunction)Pipe_Close, METH_NOARGS,
194 "Closes the pipe."},
195 {"Send", (PyCFunction)Pipe_Send, METH_VARARGS,
196 "Send data on the pipe."},
197 {"Recv", (PyCFunction)Pipe_Recv, METH_VARARGS,
198 "Receive data on the pipe."},
199 {"GetHandle", (PyCFunction)Pipe_GetHandle, METH_NOARGS,
200 "Gets the file descriptor for this socket."},
201 {NULL}
202 };
203
204 static PyTypeObject PipeType = {
205 PyVarObject_HEAD_INIT(NULL,0) /*ob_size*/
206 "protokit.Pipe", /*tp_name*/
207 sizeof(Pipe), /*tp_basicsize*/
208 0, /*tp_itemsize*/
209 (destructor)Pipe_dealloc, /*tp_dealloc*/
210 0, /*tp_print*/
211 0, /*tp_getattr*/
212 0, /*tp_setattr*/
213 0, /*tp_compare*/
214 0, /*tp_repr*/
215 0, /*tp_as_number*/
216 0, /*tp_as_sequence*/
217 0, /*tp_as_mapping*/
218 0, /*tp_hash */
219 0, /*tp_call*/
220 0, /*tp_str*/
221 0, /*tp_getattro*/
222 0, /*tp_setattro*/
223 0, /*tp_as_buffer*/
224 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
225 "ProtoPipe wrapper", /* tp_doc */
226 0, /* tp_traverse */
227 0, /* tp_clear */
228 0, /* tp_richcompare */
229 0, /* tp_weaklistoffset */
230 0, /* tp_iter */
231 0, /* tp_iternext */
232 Pipe_methods, /* tp_methods */
233 0, /* tp_members */
234 0, /* tp_getset */
235 0, /* tp_base */
236 0, /* tp_dict */
237 0, /* tp_descr_get */
238 0, /* tp_descr_set */
239 0, /* tp_dictoffset */
240 (initproc)Pipe_init, /* tp_init */
241 0, /* tp_alloc */
242 Pipe_new, /* tp_new */
243 };
244 } // end extern "C"