summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJelmer Vernooij <jelmer@samba.org>2008-05-26 01:52:35 +0200
committerJelmer Vernooij <jelmer@samba.org>2008-05-26 01:52:35 +0200
commitd9a6f04ddd8074c36fc8073ec9bd183438801817 (patch)
treeede946419365647df0f910bd996b3f43d035e777
parentf22e4d0ce3db3b199d8bac1224064c57c5f373fc (diff)
downloadsamba-d9a6f04ddd8074c36fc8073ec9bd183438801817.tar.gz
samba-d9a6f04ddd8074c36fc8073ec9bd183438801817.tar.bz2
samba-d9a6f04ddd8074c36fc8073ec9bd183438801817.zip
Provide access to server_id from python bindings, add more tests.
(This used to be commit adcd87ad07abbf60a0152deae4b975a2401d701b)
-rw-r--r--source4/lib/messaging/irpc.h1
-rw-r--r--source4/lib/messaging/messaging.c5
-rw-r--r--source4/lib/messaging/pyirpc.c34
-rw-r--r--source4/lib/messaging/tests/bindings.py34
4 files changed, 68 insertions, 6 deletions
diff --git a/source4/lib/messaging/irpc.h b/source4/lib/messaging/irpc.h
index f44c0af3ec..65e98dce2c 100644
--- a/source4/lib/messaging/irpc.h
+++ b/source4/lib/messaging/irpc.h
@@ -125,6 +125,7 @@ NTSTATUS irpc_add_name(struct messaging_context *msg_ctx, const char *name);
struct server_id *irpc_servers_byname(struct messaging_context *msg_ctx, TALLOC_CTX *mem_ctx, const char *name);
void irpc_remove_name(struct messaging_context *msg_ctx, const char *name);
NTSTATUS irpc_send_reply(struct irpc_message *m, NTSTATUS status);
+struct server_id messaging_get_server_id(struct messaging_context *msg_ctx);
#endif
diff --git a/source4/lib/messaging/messaging.c b/source4/lib/messaging/messaging.c
index e7b654894f..4b90e8c4fd 100644
--- a/source4/lib/messaging/messaging.c
+++ b/source4/lib/messaging/messaging.c
@@ -1113,3 +1113,8 @@ void irpc_remove_name(struct messaging_context *msg_ctx, const char *name)
tdb_unlock_bystring(t->tdb, name);
talloc_free(t);
}
+
+struct server_id messaging_get_server_id(struct messaging_context *msg_ctx)
+{
+ return msg_ctx->server_id;
+}
diff --git a/source4/lib/messaging/pyirpc.c b/source4/lib/messaging/pyirpc.c
index 6e979a1ef8..5ef940817c 100644
--- a/source4/lib/messaging/pyirpc.c
+++ b/source4/lib/messaging/pyirpc.c
@@ -23,6 +23,7 @@
#include <Python.h>
#include "libcli/util/pyerrors.h"
#include "lib/messaging/irpc.h"
+#include "lib/messaging/messaging.h"
#include "lib/events/events.h"
#include "cluster/cluster.h"
#include "param/param.h"
@@ -37,11 +38,17 @@ static bool server_id_from_py(PyObject *object, struct server_id *server_id)
return false;
}
- return PyArg_ParseTuple(object, "iii", &server_id->id, &server_id->id2, &server_id->node);
+ if (PyTuple_Size(object) == 3) {
+ return PyArg_ParseTuple(object, "iii", &server_id->id, &server_id->id2, &server_id->node);
+ } else {
+ int id, id2;
+ if (!PyArg_ParseTuple(object, "ii", &id, &id2))
+ return false;
+ *server_id = cluster_id(id, id2);
+ return true;
+ }
}
-
-
typedef struct {
PyObject_HEAD
TALLOC_CTX *mem_ctx;
@@ -238,14 +245,30 @@ static PyMethodDef py_messaging_methods[] = {
{ "send", (PyCFunction)py_messaging_send, METH_VARARGS|METH_KEYWORDS,
"S.send(target, msg_type, data) -> None\nSend a message" },
{ "register", (PyCFunction)py_messaging_register, METH_VARARGS|METH_KEYWORDS,
- "S.register(msg_type, callback) -> None\nRegister a message handler" },
+ "S.register(callback, msg_type=None) -> msg_type\nRegister a message handler" },
{ "deregister", (PyCFunction)py_messaging_deregister, METH_VARARGS|METH_KEYWORDS,
- "S.deregister(msg_type, callback) -> None\nDeregister a message handler" },
+ "S.deregister(callback, msg_type) -> None\nDeregister a message handler" },
{ "add_name", (PyCFunction)py_messaging_add_name, METH_VARARGS|METH_KEYWORDS, "S.add_name(name) -> None\nListen on another name" },
{ "remove_name", (PyCFunction)py_messaging_remove_name, METH_VARARGS|METH_KEYWORDS, "S.remove_name(name) -> None\nStop listening on a name" },
{ NULL, NULL, 0, NULL }
};
+static PyObject *py_messaging_server_id(PyObject *obj, void *closure)
+{
+ messaging_Object *iface = (messaging_Object *)obj;
+ struct server_id server_id = messaging_get_server_id(iface->msg_ctx);
+
+ return Py_BuildValue("(iii)", server_id.id, server_id.id2,
+ server_id.node);
+}
+
+static PyGetSetDef py_messaging_getset[] = {
+ { discard_const_p(char, "server_id"), py_messaging_server_id, NULL,
+ discard_const_p(char, "local server id") },
+ { NULL },
+};
+
+
PyTypeObject messaging_Type = {
PyObject_HEAD_INIT(NULL) 0,
.tp_name = "irpc.Messaging",
@@ -254,6 +277,7 @@ PyTypeObject messaging_Type = {
.tp_new = py_messaging_connect,
.tp_dealloc = py_messaging_dealloc,
.tp_methods = py_messaging_methods,
+ .tp_getset = py_messaging_getset,
};
diff --git a/source4/lib/messaging/tests/bindings.py b/source4/lib/messaging/tests/bindings.py
index ffb4dae326..3d58843799 100644
--- a/source4/lib/messaging/tests/bindings.py
+++ b/source4/lib/messaging/tests/bindings.py
@@ -22,4 +22,36 @@ from samba.irpc import Messaging
from unittest import TestCase
class MessagingTests(TestCase):
- pass
+ def get_context(self, *args, **kwargs):
+ kwargs["messaging_path"] = "."
+ return Messaging(*args, **kwargs)
+
+ def test_register(self):
+ x = self.get_context()
+ def callback():
+ pass
+ msg_type = x.register(callback)
+ x.deregister(callback, msg_type)
+
+ def test_assign_server_id(self):
+ x = self.get_context()
+ self.assertTrue(isinstance(x.server_id, tuple))
+ self.assertEquals(3, len(x.server_id))
+
+ def test_ping_speed(self):
+ server_ctx = self.get_context((0, 1))
+ def ping_callback(src, data):
+ server_ctx.send(src, data)
+ def exit_callback():
+ print "received exit"
+ msg_ping = server_ctx.register(ping_callback)
+ msg_exit = server_ctx.register(exit_callback)
+
+ def pong_callback():
+ print "received pong"
+ client_ctx = self.get_context((0, 2))
+ msg_pong = client_ctx.register(pong_callback)
+
+ client_ctx.send((0,1), msg_ping, "testing")
+ client_ctx.send((0,1), msg_ping, "")
+