Add wrapper for DBusServer.
[dbus-python-phuang.git] / _dbus_bindings / server-methods.c
blob1e10d06c1c15d30b9e45c5d9cef9324e033550a4
1 /* Implementation of normal Python-accessible methods on the _dbus_bindings
2 * Connection type; separated out to keep the file size manageable.
4 * Copyright (C) 2008 Huang Peng <phuang@redhat.com> .
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use, copy,
10 * modify, merge, publish, distribute, sublicense, and/or sell copies
11 * of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
27 #include "dbus_bindings-internal.h"
28 #include "server-internal.h"
30 PyDoc_STRVAR(Server_disconnect__doc__,
31 "disconnect()\n\n"
32 "Releases the server's address and stops listening for new clients.\n");
33 static PyObject *
34 Server_disconnect (Server *self, PyObject *args UNUSED)
36 TRACE(self);
37 DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->server);
38 Py_BEGIN_ALLOW_THREADS
39 dbus_server_disconnect(self->server);
40 Py_END_ALLOW_THREADS
42 Py_INCREF(Py_None);
43 return Py_None;
48 PyDoc_STRVAR(Server_get_address__doc__,
49 "get_address() -> String\n\n"
50 "Return the address of the server.\n");
51 static PyObject *
52 Server_get_address (Server *self, PyObject *args UNUSED)
54 char *address;
55 PyObject *ret;
57 TRACE(self);
58 DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->server);
59 Py_BEGIN_ALLOW_THREADS
60 address = dbus_server_get_address(self->server);
61 Py_END_ALLOW_THREADS
62 ret = PyString_FromString(address);
63 dbus_free (address);
64 return ret;
67 PyDoc_STRVAR(Server_get_id__doc__,
68 "get_id() -> String\n\n"
69 "Return the id of the server.\n");
70 static PyObject *
71 Server_get_id (Server *self, PyObject *args UNUSED)
73 char *id;
74 PyObject *ret;
76 TRACE(self);
77 DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->server);
78 Py_BEGIN_ALLOW_THREADS
79 id = dbus_server_get_id(self->server);
80 Py_END_ALLOW_THREADS
82 ret = PyString_FromString(id);
83 dbus_free (id);
85 return ret;
88 PyDoc_STRVAR(Server_get_is_connected__doc__,
89 "get_is_connected() -> bool\n\n"
90 "Return True if server is istill listening new connections.\n");
91 static PyObject *
92 Server_get_is_connected (Server *self, PyObject *args UNUSED)
94 PyObject *ret;
96 TRACE(self);
97 DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->server);
98 Py_BEGIN_ALLOW_THREADS
99 if (dbus_server_get_is_connected(self->server))
100 ret = Py_True;
101 else
102 ret = Py_False;
103 Py_END_ALLOW_THREADS
105 Py_INCREF(ret);
107 return ret;
111 PyDoc_STRVAR(Server_set_new_connection_function__doc__,
112 "set_new_connection_function()\n\n"
113 "Set a function for handling new connections.\n");
114 static PyObject *
115 Server_set_new_connection_function (Server *self, PyObject *args)
118 PyObject *callback;
119 PyTypeObject *conn_type = NULL;
121 if (!PyArg_ParseTuple(args, "O|O:set_new_connection_function",
122 &callback, &conn_type)) {
123 return NULL;
126 if (callback != Py_None && ! PyCallable_Check (callback)) {
127 PyErr_SetString (PyExc_TypeError,
128 "The first argument must be a callable object");
129 return NULL;
132 if (!conn_type || (PyObject *)conn_type == Py_None) {
133 conn_type = &DBusPyConnection_Type;
136 if (!PyType_IsSubtype (conn_type, &DBusPyConnection_Type)) {
137 PyErr_SetString (PyExc_TypeError,
138 "The second argument must be None or Subclass of _dbus_bindings_.Connection");
139 return NULL;
142 TRACE(self);
143 DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->server);
144 Py_BEGIN_ALLOW_THREADS
145 Py_XDECREF (self->new_connection_callback);
146 if (callback != Py_None) {
147 Py_INCREF (callback);
148 self->new_connection_callback = callback;
150 else
151 self->new_connection_callback = NULL;
153 Py_XDECREF (self->new_connection_type);
154 Py_INCREF (conn_type);
155 self->new_connection_type = conn_type;
157 Py_END_ALLOW_THREADS
159 Py_INCREF (Py_None);
160 return Py_None;
164 PyDoc_STRVAR(Server_set_auth_mechanisms__doc__,
165 "set_auth_mechanisms (...)\n\n"
166 "Set the authentication mechanisms that this server offers to clients.\n");
167 static PyObject *
168 Server_set_auth_mechanisms (Server *self, PyObject *args)
171 int len, i;
172 const char ** _mechanisms = NULL;
174 len = PyTuple_Size (args);
176 if (len > 0) {
177 _mechanisms = (const char **)malloc (sizeof (char *) * (len + 1));
178 for (i = 0; i < len; i++) {
179 PyObject *mechanism = PyTuple_GetItem (args, i);
180 if (!PyString_Check (mechanism)) {
181 free (_mechanisms);
182 PyErr_SetString(PyExc_TypeError,
183 "arguments must be strings");
184 return NULL;
186 _mechanisms[i] = PyString_AS_STRING (mechanism);
188 _mechanisms[len] = NULL;
191 TRACE(self);
192 DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->server);
193 Py_BEGIN_ALLOW_THREADS
194 dbus_server_set_auth_mechanisms (self->server, _mechanisms);
195 Py_END_ALLOW_THREADS
197 if (_mechanisms != NULL)
198 free (_mechanisms);
199 Py_INCREF (Py_None);
200 return Py_None;
203 struct PyMethodDef DBusPyServer_tp_methods[] = {
204 #define ENTRY(name, flags) {#name, (PyCFunction)Server_##name, flags, Server_##name##__doc__}
205 ENTRY(disconnect, METH_NOARGS),
206 ENTRY(get_address, METH_NOARGS),
207 ENTRY(get_id, METH_NOARGS),
208 ENTRY(get_is_connected, METH_NOARGS),
209 ENTRY(set_new_connection_function, METH_VARARGS),
210 ENTRY(set_auth_mechanisms, METH_VARARGS),
211 {NULL},
212 #undef ENTRY
215 /* vim:set ft=c cino< sw=4 sts=4 et: */