From 7959308d452d7f8ccdb34643388589631763dc59 Mon Sep 17 00:00:00 2001 From: vidon Date: Tue, 2 Nov 2004 16:09:50 +0000 Subject: Ajout du support des callbacks sur les evenements d'abonnements --- ivy.py | 42 +++++++++++++++++++++++++++--- ivycpy.i | 90 ++++++++++++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 101 insertions(+), 31 deletions(-) diff --git a/ivy.py b/ivy.py index 24924e5..433ae8f 100644 --- a/ivy.py +++ b/ivy.py @@ -1,4 +1,6 @@ import threading +# -*- coding: iso-8859-1 -*- + #TODO #Le wrapper ne gere pas du tout les IvyClients #Il ne donne aucune information sur l'agent qui @@ -61,6 +63,7 @@ class Ivy: self.sReadyMessage=message self.sAppName=name self.dRegexp={} + self.lBindListeners=[] ivycpy.IvyInit(name, message, 0, @@ -71,11 +74,26 @@ class Ivy: #Todo Creation des objets Domaine #a partir de la chaine de caracteres self.sDomainBus=sdomainbus - ivycpy.IvyStart(sdomainbus) + ivycpy.IvySetBindCallback(self._bindCallback) + ivycpy.IvyStart(sdomainbus) def mainloop(self): ivycpy.IvyMainLoop() - + + def _bindCallback(self, client, id, regexp, event): + if (event == ivycpy.IvyAddBind ): + for bl in self.lBindListeners: + bl.bindPerformed(client, id, regexp) + elif (event == ivycpy.IvyRemoveBind ): + for bl in self.lBindListeners: + bl.unbindPerformed(client, id, regexp) + + def addBindListener(self, aBindListener): + self.lBindListeners.append(aBindListener) + + def removeBindListener(self, aBindListener): + self.lBindListeners.remove(aBindListener) + def addApplicationListener(self, aApplicationListener): self.lIvyApplicationlisteners.append(aApplicationListener) @@ -176,9 +194,27 @@ class IvyApplicationAdapter : pass def die(self,id): pass - def directMessage(self, id, msg): + def directMessage(self, client, id, msg): pass +class IvyBindAdapter : + """IvyBindAdapter + Classe d'objets abstraite définissant l'interface des objets + à  fournir à  l'objet de classe Ivy + via addBindListener et removeBindListener + """ + def bindPerformed(self,client, id, regexp): + """ + invoked when a Ivy Client performs a bind + """ + pass + + def unbindPerformed(self,client, id, regexp): + """ + invoked when a Ivy Client performs a unbind + """ + pass + class IvyMessageAdapter: def receive(self, client, *arg): pass diff --git a/ivycpy.i b/ivycpy.i index 488f022..4d34a90 100644 --- a/ivycpy.i +++ b/ivycpy.i @@ -50,6 +50,8 @@ typedef struct { typedef enum { IvyApplicationConnected, IvyApplicationDisconnected } IvyApplicationEvent; +typedef enum { IvyAddBind, IvyRemoveBind } IvyBindEvent; + typedef struct _timer *TimerId; /* a voir si a laisser ici */ @@ -288,9 +290,6 @@ pytkIvyApplicationCallback(IvyClientPtr app, void *user_data, PyTuple_SetItem(arglist,1,eventobj); result = PyEval_CallObject(func,arglist); - if(PyErr_Occurred()){ - PyErr_Print(); - } Py_DECREF(arglist); } @@ -323,11 +322,61 @@ pyIvyApplicationCallback(IvyClientPtr app, void *user_data, PyTuple_SetItem(arglist,1,eventobj); result = PyEval_CallObject(func,arglist); - if(PyErr_Occurred()){ - PyErr_Print(); - } Py_DECREF(arglist); } + +static void +pyIvyBindCallback(IvyClientPtr app, void *user_data, + int id, char* regexp, IvyBindEvent event) +{ + PyObject *func, *arglist; + PyObject *result, *eventobj, *idobj, *regexpobj; + PyObject *resultobj; + static void *descr = 0; +#ifdef __DEBUG__ + printf("-pyIvyBindCallback\n"); +#endif + func = (PyObject *) user_data; + /* cette verif est inutile ; elle a deja ete faite lors de + l'enregistrement du callback */ + if (!PyCallable_Check(func)) { + PyErr_SetString(PyExc_TypeError, "Need a callable object!"); + return ; + }; + /* user_data->func ; */ + /* la signature python doit etre proc(n) elle ne retourne rien */ + descr = SWIG_TypeQuery("IvyClientPtr"); + resultobj = SWIG_NewPointerObj((void *) app, descr, 0); + + idobj = Py_BuildValue("i", (int) id); + regexpobj=PyString_FromString(regexp); + eventobj = Py_BuildValue("i", (int) event); + arglist = PyTuple_New(4); + + PyTuple_SetItem(arglist,0,resultobj); + PyTuple_SetItem(arglist,1,idobj); + PyTuple_SetItem(arglist,2,regexpobj); + PyTuple_SetItem(arglist,3,eventobj); + + result = PyEval_CallObject(func,arglist); + Py_DECREF(arglist); +} + +static void +pyIvySetBindCallback(PyObject *PyFunc) /* void *user_data, */ +{ + + IvySetBindCallback(pyIvyBindCallback, (void *) PyFunc); + Py_INCREF(PyFunc); +} + +static void +pyIvyDelBindCallback() /* void *user_data, */ +{ + + IvyDelBindCallback(); +} + static void pytkIvyDieCallback(IvyClientPtr app, void *user_data, int id) { @@ -360,9 +409,6 @@ pytkIvyDieCallback(IvyClientPtr app, void *user_data, int id) PyTuple_SetItem(arglist,1,idobj); result = PyEval_CallObject(func,arglist); - if(PyErr_Occurred()){ - PyErr_Print(); - } Py_DECREF(arglist); } @@ -396,9 +442,6 @@ pyIvyDieCallback(IvyClientPtr app, void *user_data, int id) PyTuple_SetItem(arglist,1,idobj); result = PyEval_CallObject(func,arglist); - if(PyErr_Occurred()){ - PyErr_Print(); - } Py_DECREF(arglist); } @@ -442,9 +485,6 @@ pytkMsgCallback( IvyClientPtr app, void *user_data, int argc, char **argv ) Py_DECREF(arglist); */ result = PyEval_CallObject(func, pyargv) ; - if(PyErr_Occurred()){ - PyErr_Print(); - } Py_DECREF(pyargv); } @@ -495,9 +535,6 @@ pyMsgCallback( IvyClientPtr app, void *user_data, int argc, char **argv ) to the Python call back */ result = PyEval_CallObject(func, pyargv) ; - if(PyErr_Occurred()){ - PyErr_Print(); - } Py_DECREF(pyargv); /*Py_DECREF(resultobj);*/ } @@ -534,10 +571,8 @@ pytkMsgDirectCallback( IvyClientPtr app, void *user_data, int id, char *msg ) PyTuple_SetItem(arglist,1,PyString_FromString(msg)); result = PyEval_CallObject(func,arglist); - if(PyErr_Occurred()){ - PyErr_Print(); - } Py_DECREF(arglist); + Py_DECREF(resultobj); } static void @@ -569,10 +604,8 @@ pyMsgDirectCallback( IvyClientPtr app, void *user_data, int id, char *msg ) PyTuple_SetItem(arglist,1,PyString_FromString(msg)); result = PyEval_CallObject(func,arglist); - if(PyErr_Occurred()){ - PyErr_Print(); - } Py_DECREF(arglist); + Py_DECREF(resultobj); } /* facade to IvyBindMsg avoiding vararg problem */ @@ -610,7 +643,6 @@ pyIvyUnBindMsg (int binding_id) MsgRcvPtr rcvid ; rcvid = getRcvMsg(binding_id) ; if (rcvid != NULL) { - printf("-IvyUnbindMsg %x \n" , (int) rcvid) ; IvyUnbindMsg(rcvid); delRcvMsg(binding_id); }; @@ -730,7 +762,6 @@ pyIvyTimerModify( int timerid, int time ) TimerId timer ; timer = getIvyTimer(timerid); if (timer != NULL) { - printf("-modifying timer %x \n", (int) timer); TimerModify(timer, (long) time); } } @@ -740,7 +771,6 @@ pyIvyTimerRemove( int timerid) TimerId timer ; timer = getIvyTimer(timerid); if (timer != NULL) { - printf("-removing timer %x \n", (int) timer); TimerRemove(timer); delIvyTimer(timerid); } @@ -958,7 +988,6 @@ pyIvyInit( IvyInit(AppName,ready, pytkIvyApplicationCallback, (void *) PyFuncOnCx, pytkIvyDieCallback, (void *) PyFuncOnDie); - printf("-pyIvyInit after IvyInit Tk\n"); } else { IvyInit(AppName,ready, @@ -1107,6 +1136,11 @@ pyIvyTimerModify( int timerid, int time ); void pyIvyTimerRemove( int timerid); +static void +pyIvySetBindCallback(PyObject *PyFunc); +static void +pyIvyDelBindCallback(); + // for emacs // Local Variables: // mode: C -- cgit v1.1