2 Unix SMB/CIFS implementation.
3 Copyright © Jelmer Vernooij <jelmer@samba.org> 2008
5 Based on the equivalent for EJS:
6 Copyright © Andrew Tridgell <tridge@samba.org> 2005
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.
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.
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/>.
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"
36 void initmessaging(void);
38 extern PyTypeObject imessaging_Type
;
40 static bool server_id_from_py(PyObject
*object
, struct server_id
*server_id
)
42 if (!PyTuple_Check(object
)) {
43 PyErr_SetString(PyExc_ValueError
, "Expected tuple");
47 if (PyTuple_Size(object
) == 3) {
48 return PyArg_ParseTuple(object
, "iii", &server_id
->pid
, &server_id
->task_id
, &server_id
->vnn
);
51 if (!PyArg_ParseTuple(object
, "ii", &pid
, &task_id
))
53 *server_id
= cluster_id(pid
, task_id
);
61 struct imessaging_context
*msg_ctx
;
64 static PyObject
*py_imessaging_connect(PyTypeObject
*self
, PyObject
*args
, PyObject
*kwargs
)
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
;
73 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "|OO:connect",
74 discard_const_p(char *, kwnames
), &own_id
, &py_lp_ctx
)) {
78 ret
= PyObject_New(imessaging_Object
, &imessaging_Type
);
82 ret
->mem_ctx
= talloc_new(NULL
);
84 lp_ctx
= lpcfg_from_py_object(ret
->mem_ctx
, py_lp_ctx
);
86 PyErr_SetString(PyExc_RuntimeError
, "imessaging_connect unable to interpret loadparm_context");
87 talloc_free(ret
->mem_ctx
);
91 ev
= s4_event_context_init(ret
->mem_ctx
);
93 if (own_id
!= Py_None
) {
94 struct server_id server_id
;
96 if (!server_id_from_py(own_id
, &server_id
))
99 ret
->msg_ctx
= imessaging_init(ret
->mem_ctx
,
104 ret
->msg_ctx
= imessaging_client_init(ret
->mem_ctx
,
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
);
115 return (PyObject
*)ret
;
118 static void py_imessaging_dealloc(PyObject
*self
)
120 imessaging_Object
*iface
= (imessaging_Object
*)self
;
121 talloc_free(iface
->msg_ctx
);
122 self
->ob_type
->tp_free(self
);
125 static PyObject
*py_imessaging_send(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
127 imessaging_Object
*iface
= (imessaging_Object
*)self
;
132 struct server_id server
;
133 const char *kwnames
[] = { "target", "msg_type", "data", NULL
};
136 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "Ois#:send",
137 discard_const_p(char *, kwnames
), &target
, &msg_type
, &data
.data
, &length
)) {
142 data
.length
= length
;
144 if (!server_id_from_py(target
, &server
))
147 status
= imessaging_send(iface
->msg_ctx
, server
, msg_type
, &data
);
148 if (NT_STATUS_IS_ERR(status
)) {
149 PyErr_SetNTSTATUS(status
);
156 static void py_msg_callback_wrapper(struct imessaging_context
*msg
, void *private_data
,
158 struct server_id server_id
, DATA_BLOB
*data
)
160 PyObject
*callback
= (PyObject
*)private_data
;
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
);
167 static PyObject
*py_imessaging_register(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
169 imessaging_Object
*iface
= (imessaging_Object
*)self
;
173 const char *kwnames
[] = { "callback", "msg_type", NULL
};
175 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "O|i:register",
176 discard_const_p(char *, kwnames
), &callback
, &msg_type
)) {
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
;
188 status
= imessaging_register(iface
->msg_ctx
, callback
,
189 msg_type
, py_msg_callback_wrapper
);
191 if (NT_STATUS_IS_ERR(status
)) {
192 PyErr_SetNTSTATUS(status
);
196 return PyLong_FromLong(msg_type
);
199 static PyObject
*py_imessaging_deregister(PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
201 imessaging_Object
*iface
= (imessaging_Object
*)self
;
204 const char *kwnames
[] = { "callback", "msg_type", NULL
};
206 if (!PyArg_ParseTupleAndKeywords(args
, kwargs
, "O|i:deregister",
207 discard_const_p(char *, kwnames
), &callback
, &msg_type
)) {
211 imessaging_deregister(iface
->msg_ctx
, msg_type
, callback
);
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
}
228 static PyObject
*py_imessaging_server_id(PyObject
*obj
, void *closure
)
230 imessaging_Object
*iface
= (imessaging_Object
*)obj
;
231 struct server_id server_id
= imessaging_get_server_id(iface
->msg_ctx
);
233 return Py_BuildValue("(iii)", server_id
.pid
, server_id
.task_id
,
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") },
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"
257 void initmessaging(void)
261 if (PyType_Ready(&imessaging_Type
) < 0)
264 mod
= Py_InitModule3("messaging", NULL
, "Internal RPC");
268 Py_INCREF((PyObject
*)&imessaging_Type
);
269 PyModule_AddObject(mod
, "Messaging", (PyObject
*)&imessaging_Type
);