1 # -*- coding: iso-8859-15 -*-
2 # CONFIGURATION MANAGEMENT OF EDF VERSION
3 # ======================================================================
4 # COPYRIGHT (C) 1991 - 2002 EDF R&D WWW.CODE-ASTER.ORG
5 # THIS PROGRAM IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY
6 # IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE AS PUBLISHED BY
7 # THE FREE SOFTWARE FOUNDATION; EITHER VERSION 2 OF THE LICENSE, OR
8 # (AT YOUR OPTION) ANY LATER VERSION.
10 # THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT
11 # WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
12 # MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU
13 # GENERAL PUBLIC LICENSE FOR MORE DETAILS.
15 # YOU SHOULD HAVE RECEIVED A COPY OF THE GNU GENERAL PUBLIC LICENSE
16 # ALONG WITH THIS PROGRAM; IF NOT, WRITE TO EDF R&D CODE_ASTER,
17 # 1 AVENUE DU GENERAL DE GAULLE, 92141 CLAMART CEDEX, FRANCE.
20 # ======================================================================
22 La classe CONNECTOR sert à enregistrer les observateurs d'objets et à délivrer
23 les messages émis à ces objets.
25 Le principe général est le suivant : un objet (subscriber) s'enregistre aupres du
26 connecteur global (theconnector) pour observer un objet emetteur de messages (publisher)
27 sur un canal donné (channel). Il demande à etre notifie par appel d'une fonction (listener).
28 La séquence est donc :
30 - enregistrement du subscriber pour le publisher : theconnector.Connect(publisher,channel,listener,args)
31 - émission du message par le publisher : theconnector.Emit(publisher,channel,cargs)
33 args et cargs sont des tuples contenant les arguments de la fonction listener qui sera appelée
46 def Connect(self, object, channel, function, args):
47 ###print "Connect",object, channel, function, args
49 if self.connections.has_key(idx):
50 channels = self.connections[idx]
52 channels = self.connections[idx] = {}
54 if channels.has_key(channel):
55 receivers = channels[channel]
57 receivers = channels[channel] = []
59 info = (function, args)
61 receivers.remove(info)
62 receivers.append(info)
63 ###print "Connect",receivers
66 def Disconnect(self, object, channel, function, args):
68 receivers = self.connections[id(object)][channel]
70 raise ConnectorError, \
71 'no receivers for channel %s of %s' % (channel, object)
73 receivers.remove((function, args))
75 raise ConnectorError,\
76 'receiver %s%s is not connected to channel %s of %s' \
77 % (function, args, channel, object)
80 # the list of receivers is empty now, remove the channel
81 channels = self.connections[id(object)]
84 # the object has no more channels
85 del self.connections[id(object)]
87 def Emit(self, object, channel, *args):
88 ###print "Emit",object, channel, args
90 receivers = self.connections[id(object)][channel]
93 ###print "Emit",object, channel, receivers
94 # Attention : copie pour eviter les pbs lies aux deconnexion reconnexion
95 # pendant l'execution des emit
96 for func, fargs in copy(receivers):
98 apply(func, args + fargs)
100 traceback.print_exc()
102 _the_connector =CONNECTOR()
103 Connect = _the_connector.Connect
104 Emit = _the_connector.Emit
105 Disconnect = _the_connector.Disconnect
107 if __name__ == "__main__":
115 Connect(a,"add",b.add,())