s4-messaging: fix pymessaging docstring
[tridge/samba.git] / source4 / lib / messaging / pymessaging.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Copyright © Jelmer Vernooij <jelmer@samba.org> 2008
4
5    Based on the equivalent for EJS:
6    Copyright © Andrew Tridgell <tridge@samba.org> 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <Python.h>
23 #include "includes.h"
24 #include "scripting/python/modules.h"
25 #include "libcli/util/pyerrors.h"
26 #include "librpc/rpc/pyrpc_util.h"
27 #include "librpc/ndr/libndr.h"
28 #include "lib/messaging/messaging.h"
29 #include "lib/events/events.h"
30 #include "cluster/cluster.h"
31 #include "param/param.h"
32 #include "param/pyparam.h"
33 #include "librpc/rpc/dcerpc.h"
34 #include "librpc/gen_ndr/server_id.h"
35
36 void initmessaging(void);
37
38 extern PyTypeObject imessaging_Type;
39
40 static bool server_id_from_py(PyObject *object, struct server_id *server_id)
41 {
42         if (!PyTuple_Check(object)) {
43                 PyErr_SetString(PyExc_ValueError, "Expected tuple");
44                 return false;
45         }
46
47         if (PyTuple_Size(object) == 3) {
48                 return PyArg_ParseTuple(object, "iii", &server_id->pid, &server_id->task_id, &server_id->vnn);
49         } else {
50                 int pid, task_id;
51                 if (!PyArg_ParseTuple(object, "ii", &pid, &task_id))
52                         return false;
53                 *server_id = cluster_id(pid, task_id);
54                 return true;
55         }
56 }
57
58 typedef struct {
59         PyObject_HEAD
60         TALLOC_CTX *mem_ctx;
61         struct imessaging_context *msg_ctx;
62 } imessaging_Object;
63
64 static PyObject *py_imessaging_connect(PyTypeObject *self, PyObject *args, PyObject *kwargs)
65 {
66         struct tevent_context *ev;
67         const char *kwnames[] = { "own_id", "lp_ctx", NULL };
68         PyObject *own_id = Py_None;
69         PyObject *py_lp_ctx = Py_None;
70         imessaging_Object *ret;
71         struct loadparm_context *lp_ctx;
72
73         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO:connect", 
74                 discard_const_p(char *, kwnames), &own_id, &py_lp_ctx)) {
75                 return NULL;
76         }
77
78         ret = PyObject_New(imessaging_Object, &imessaging_Type);
79         if (ret == NULL)
80                 return NULL;
81
82         ret->mem_ctx = talloc_new(NULL);
83
84         lp_ctx = lpcfg_from_py_object(ret->mem_ctx, py_lp_ctx);
85         if (lp_ctx == NULL) {
86                 PyErr_SetString(PyExc_RuntimeError, "imessaging_connect unable to interpret loadparm_context");
87                 talloc_free(ret->mem_ctx);
88                 return NULL;
89         }
90
91         ev = s4_event_context_init(ret->mem_ctx);
92
93         if (own_id != Py_None) {
94                 struct server_id server_id;
95
96                 if (!server_id_from_py(own_id, &server_id)) 
97                         return NULL;
98
99                 ret->msg_ctx = imessaging_init(ret->mem_ctx,
100                                                lp_ctx,
101                                                server_id,
102                                                ev, true);
103         } else {
104                 ret->msg_ctx = imessaging_client_init(ret->mem_ctx,
105                                                       lp_ctx,
106                                                       ev);
107         }
108
109         if (ret->msg_ctx == NULL) {
110                 PyErr_SetString(PyExc_RuntimeError, "imessaging_connect unable to create a messaging context");
111                 talloc_free(ret->mem_ctx);
112                 return NULL;
113         }
114
115         return (PyObject *)ret;
116 }
117
118 static void py_imessaging_dealloc(PyObject *self)
119 {
120         imessaging_Object *iface = (imessaging_Object *)self;
121         talloc_free(iface->msg_ctx);
122         self->ob_type->tp_free(self);
123 }
124
125 static PyObject *py_imessaging_send(PyObject *self, PyObject *args, PyObject *kwargs)
126 {
127         imessaging_Object *iface = (imessaging_Object *)self;
128         uint32_t msg_type;
129         DATA_BLOB data;
130         PyObject *target;
131         NTSTATUS status;
132         struct server_id server;
133         const char *kwnames[] = { "target", "msg_type", "data", NULL };
134         int length;
135
136         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Ois#:send", 
137                 discard_const_p(char *, kwnames), &target, &msg_type, &data.data, &length)) {
138
139                 return NULL;
140         }
141
142         data.length = length;
143
144         if (!server_id_from_py(target, &server))
145                 return NULL;
146
147         status = imessaging_send(iface->msg_ctx, server, msg_type, &data);
148         if (NT_STATUS_IS_ERR(status)) {
149                 PyErr_SetNTSTATUS(status);
150                 return NULL;
151         }
152
153         Py_RETURN_NONE;
154 }
155
156 static void py_msg_callback_wrapper(struct imessaging_context *msg, void *private_data,
157                                uint32_t msg_type, 
158                                struct server_id server_id, DATA_BLOB *data)
159 {
160         PyObject *callback = (PyObject *)private_data;
161
162         PyObject_CallFunction(callback, discard_const_p(char, "i(iii)s#"), msg_type,
163                               server_id.pid, server_id.task_id, server_id.vnn,
164                               data->data, data->length);
165 }
166
167 static PyObject *py_imessaging_register(PyObject *self, PyObject *args, PyObject *kwargs)
168 {
169         imessaging_Object *iface = (imessaging_Object *)self;
170         int msg_type = -1;
171         PyObject *callback;
172         NTSTATUS status;
173         const char *kwnames[] = { "callback", "msg_type", NULL };
174         
175         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:register", 
176                 discard_const_p(char *, kwnames), &callback, &msg_type)) {
177                 return NULL;
178         }
179
180         Py_INCREF(callback);
181
182         if (msg_type == -1) {
183                 uint32_t msg_type32 = msg_type;
184                 status = imessaging_register_tmp(iface->msg_ctx, callback,
185                                                 py_msg_callback_wrapper, &msg_type32);
186                 msg_type = msg_type32;
187         } else {
188                 status = imessaging_register(iface->msg_ctx, callback,
189                                     msg_type, py_msg_callback_wrapper);
190         }
191         if (NT_STATUS_IS_ERR(status)) {
192                 PyErr_SetNTSTATUS(status);
193                 return NULL;
194         }
195
196         return PyLong_FromLong(msg_type);
197 }
198
199 static PyObject *py_imessaging_deregister(PyObject *self, PyObject *args, PyObject *kwargs)
200 {
201         imessaging_Object *iface = (imessaging_Object *)self;
202         int msg_type = -1;
203         PyObject *callback;
204         const char *kwnames[] = { "callback", "msg_type", NULL };
205
206         if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i:deregister",
207                 discard_const_p(char *, kwnames), &callback, &msg_type)) {
208                 return NULL;
209         }
210
211         imessaging_deregister(iface->msg_ctx, msg_type, callback);
212
213         Py_DECREF(callback);
214
215         Py_RETURN_NONE;
216 }
217
218 static PyMethodDef py_imessaging_methods[] = {
219         { "send", (PyCFunction)py_imessaging_send, METH_VARARGS|METH_KEYWORDS,
220                 "S.send(target, msg_type, data) -> None\nSend a message" },
221         { "register", (PyCFunction)py_imessaging_register, METH_VARARGS|METH_KEYWORDS,
222                 "S.register(callback, msg_type=None) -> msg_type\nRegister a message handler" },
223         { "deregister", (PyCFunction)py_imessaging_deregister, METH_VARARGS|METH_KEYWORDS,
224                 "S.deregister(callback, msg_type) -> None\nDeregister a message handler" },
225         { NULL, NULL, 0, NULL }
226 };
227
228 static PyObject *py_imessaging_server_id(PyObject *obj, void *closure)
229 {
230         imessaging_Object *iface = (imessaging_Object *)obj;
231         struct server_id server_id = imessaging_get_server_id(iface->msg_ctx);
232
233         return Py_BuildValue("(iii)", server_id.pid, server_id.task_id,
234                              server_id.vnn);
235 }
236
237 static PyGetSetDef py_imessaging_getset[] = {
238         { discard_const_p(char, "server_id"), py_imessaging_server_id, NULL,
239           discard_const_p(char, "local server id") },
240         { NULL },
241 };
242
243
244 PyTypeObject imessaging_Type = {
245         PyObject_HEAD_INIT(NULL) 0,
246         .tp_name = "messaging.Messaging",
247         .tp_basicsize = sizeof(imessaging_Object),
248         .tp_flags = Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,
249         .tp_new = py_imessaging_connect,
250         .tp_dealloc = py_imessaging_dealloc,
251         .tp_methods = py_imessaging_methods,
252         .tp_getset = py_imessaging_getset,
253         .tp_doc = "Messaging(own_id=None)\n" \
254                   "Create a new object that can be used to communicate with the peers in the specified messaging path.\n"
255 };
256
257 void initmessaging(void)
258 {
259         PyObject *mod;
260
261         if (PyType_Ready(&imessaging_Type) < 0)
262                 return;
263
264         mod = Py_InitModule3("messaging", NULL, "Internal RPC");
265         if (mod == NULL)
266                 return;
267
268         Py_INCREF((PyObject *)&imessaging_Type);
269         PyModule_AddObject(mod, "Messaging", (PyObject *)&imessaging_Type);
270 }