Author: ArcRiley Date: 2009-01-08 22:47:57 -0500 (Thu, 08 Jan 2009) New Revision: 1446
Modified: trunk/concordance/src/Core.c trunk/concordance/src/Core.h Log: now supports callback for received data (no sending from callback yet) Modified: trunk/concordance/src/Core.c =================================================================== --- trunk/concordance/src/Core.c 2009-01-08 20:16:43 UTC (rev 1445) +++ trunk/concordance/src/Core.c 2009-01-09 03:47:57 UTC (rev 1446) @@ -49,6 +49,13 @@ self->context = NULL; self->mainloop = NULL; self->saslCntx = NULL; + + /* create callback queues + + GAsyncQueue* g_async_queue_new (void); + */ + self->queueRecv = g_async_queue_new(); + self->queueSend = g_async_queue_new(); } return (PyObject*) self; } @@ -197,13 +204,22 @@ if (self->s2s.sock > -1) close(self->s2s.sock); + /* free gsasl context + + void gsasl_done (Gsasl* ctx); + */ if (self->saslCntx != NULL) - /* free gsasl context - - void gsasl_done (Gsasl* ctx); - */ gsasl_done(self->saslCntx); + /* unref callback queues + + void g_async_queue_unref (GAsyncQueue *queue); + */ + if (self->queueRecv) + g_async_queue_unref(self->queueRecv); + if (self->queueSend) + g_async_queue_unref(self->queueSend); + PyObject_Del(self); } @@ -217,6 +233,78 @@ return PyObject_GenericSetAttr((PyObject*) self, key, value); } + static PyObject* + conCore_defaultHandle(PyObject* self, PyObject* args) { + /* Placeholder method, just return empty string */ + return PyUnicode_FromString(""); + } + + + static PyObject* + conCore_call(PyObject* s, PyObject* args, PyObject* kwds) { /*\ + cdef : \*/ + static char* kwlist[] = {0}; + conCoreObject* self = (conCoreObject*) s; + PyObject* handle; + PyObject* sinput; + PyObject* tinput; + PyObject* output; + GString* popped; + + /* ensure there are no arguments save self */ + if (!PyArg_ParseTupleAndKeywords(args, kwds, ":__call__", kwlist)) + return NULL; + + /* continuous loop */ + while (TRUE) { + /* release the GIL while waiting for the next callback + + gpointer g_async_queue_pop (GAsyncQueue *queue); + */ + Py_BEGIN_ALLOW_THREADS + popped = g_async_queue_pop(self->queueRecv); + Py_END_ALLOW_THREADS + + /* build argument tuple + + PyObject* PyUnicode_FromStringAndSize (const char *u, + Py_ssize_t size) + PyObject* PyTuple_Pack (Py_ssize_t n, + ...) + */ + sinput = PyUnicode_FromStringAndSize(popped->str, popped->len); + tinput = PyTuple_Pack(1, sinput); + + /* call our handler + + PyObject* PyObject_GetAttrString (PyObject *o, + const char *attr_name) + PyObject* PyEval_CallObject (PyObject*, + PyObject*); + */ + handle = PyObject_GetAttrString(s, "clientHandle"); + output = PyEval_CallObject(handle, tinput); + + /* do nothing with the output, yet. we need a GMainLoop event for it */ + + /* free the objects we created and the GString* we popped + + void Py_DECREF (PyObject *o); + gchar* g_string_free (GString *string, + gboolean free_segment); + */ + Py_DECREF(output); + Py_DECREF(handle); + Py_DECREF(tinput); + Py_DECREF(sinput); + g_string_free(popped, TRUE); + } + + /* this should never be reached */ + Py_INCREF(Py_None); + return Py_None; + } + /* # ########################################################################### @@ -383,7 +471,8 @@ const gchar *val, gssize len); */ - session->wbuff = g_string_append_len(session->wbuff, str+sent, len-sent); + session->wbuff = g_string_append_len(session->wbuff, + str+sent, len-sent); /* add a watch to send more when the channel is ready for it @@ -773,8 +862,20 @@ static void conCore_handle_client(conSession* session) { /*\ cdef : \*/ - GString* buff; - printf("------ client callback: \"%s\"\n", session->ebuff->str); + conCoreObject* self = session->core; + + /* push element buffer to callback queue + + void g_async_queue_push (GAsyncQueue *queue, + gpointer data); + */ + g_async_queue_push(self->queueRecv, session->ebuff); + + /* we've sent our element buffer into the queue, we need a new one + + GString* g_string_new (const gchar *init); + */ + session->ebuff = g_string_new(""); } /* @@ -864,7 +965,8 @@ const gchar *format, ...); */ - g_string_printf(session->ebuff, "<%s", element[1]); + g_string_printf(session->ebuff, "<%s xmlns='%s'", + element[1], element[0]); for (i = 0; atts[i]; i += 2) g_string_append_printf(session->ebuff, " %s='%s'", atts[i], atts[i+1]); @@ -1047,11 +1149,15 @@ # ########################################################################### # - # Class Methods and Type + # Class MethodDef and Type # */ static PyMethodDef conCore_methods[] = { - { NULL, NULL }, + {"clientHandle", + conCore_defaultHandle, + METH_VARARGS, + PyDoc_STR("clientHandle docs")}, + { NULL, NULL }, /* sentinel */ }; @@ -1071,7 +1177,7 @@ 0, /*tp_as_sequence*/ 0, /*tp_as_mapping*/ 0, /*tp_hash*/ - 0, /*tp_call*/ + conCore_call, /*tp_call*/ 0, /*tp_str*/ (getattrofunc) conCore_getattro, /*tp_getattro*/ (setattrofunc) conCore_setattro, /*tp_setattro*/ Modified: trunk/concordance/src/Core.h =================================================================== --- trunk/concordance/src/Core.h 2009-01-08 20:16:43 UTC (rev 1445) +++ trunk/concordance/src/Core.h 2009-01-09 03:47:57 UTC (rev 1446) @@ -55,6 +55,8 @@ GMainLoop* mainloop; /* handle to this core's Glim mainloop */ GThread* thread; /* thread ID for mainloop thread */ Gsasl* saslCntx; /* sasl context for this Core */ + GAsyncQueue* queueRecv; /* callback queue input */ + GAsyncQueue* queueSend; /* callback queue output */ } conCoreObject; _______________________________________________ PySoy-SVN mailing list PySoy-SVN@pysoy.org http://www.pysoy.org/mailman/listinfo/pysoy-svn