aboutsummaryrefslogtreecommitdiff
path: root/ivy.py
blob: 24924e548f58151816bc6712e8c22bf040fd99fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import threading
#TODO
#Le wrapper ne gere pas du tout les IvyClients
#Il ne donne aucune information sur l'agent qui
#a envoyé le message, s'est connecté ou déconnecté
#Voir comment créer un objet python pouvant être envoyé
#Idée : A la connexion : On crée l'objet Python, à la déconnexion on le détruit
#A la reception d'un message, on recherche l'objet python associéa
import ivycpy
import sys
import traceback
class Ivy:
    class IvyClient:
        def __init__(self, ptr):
            self.ptr=ptr

        def getPtr(self):
            return self.ptr
        
        def getName(self):
            return ivycpy.IvyGetApplicationName(self.ptr)

        def getHost(self):
            return ivycpy.IvyGetApplicationHost(self.ptr)

        def getMessages(self):
            return ivycpy.IvyGetApplicationMessages(self.ptr)

        def sendDie(self,message):
            ivycpy.IvySendDieMsg(self.ptr)
            
        def sendDirectMsg(self, id, message):
            ivycpy.IvySendDirectMsg(self.ptr,id,message)

        def sendError(self, id, error):
            ivycpy.IvySendError(self.ptr,id,error)

    class Domain:
        def __init__(self, domainaddr, port):
            self.domainaddr=domainaddr
            self.port=port

        def __str__(self):
            return self.domainaddr+":"+self.port

        def getDomainaddr(self):
            return self.domainaddr

        def getPort(self):
            return self.port

    def __call__(self):
        self.theThread=threading.Thread(target=self.mainloop)
        self.theThread.setDaemon(1)
        self.theThread.start()
        
    def __init__(self, name, message, aIvyApplicationListener):
        self.lIvyApplicationlisteners=[]
        self.dIvyClient={}
        self.addApplicationListener(aIvyApplicationListener)
        self.sReadyMessage=message
        self.sAppName=name
        self.dRegexp={}
        ivycpy.IvyInit(name,
                message,
                0,
                self._appliCallback,
                self._die)

    def start(self, sdomainbus):
        #Todo Creation des objets Domaine
        #a partir de la chaine de caracteres
        self.sDomainBus=sdomainbus
        ivycpy.IvyStart(sdomainbus)
        
    def mainloop(self):
        ivycpy.IvyMainLoop()
    
    def addApplicationListener(self, aApplicationListener):
        self.lIvyApplicationlisteners.append(aApplicationListener)

    def removeApplicationListener(self, aApplicationListener):
        try:
            self.lIvyApplicationlisteners.remove(aApplicationListener)
        except ValueError, x:
            IvyException(str(x))()

    def _appliCallback(self, ivyclient, connected):
        if not self.dIvyClient.has_key(ivyclient):
            ivyClient=Ivy.IvyClient(ivyclient)
            self.dIvyClient[ivyclient]=ivyClient
        else:
            ivyClient=self.dIvyClient[ivyclient]
            
        if connected == ivycpy.IvyApplicationConnected:
            self._connect(ivyClient)
        else:
            self._disconnect(ivyClient)
            
    def _connect(self, *arg):
        for listener in self.lIvyApplicationlisteners:
            listener.connect(*arg)

    def _disconnect(self, *arg):
        for listener in self.lIvyApplicationlisteners:
            listener.disconnect(*arg)

    def _die(self,ivyclient):
        try:
            ivyClient=self.dIvyClient[ivyclient]
        except KeyError:
            ivyClient=Ivy.IvyClient(ivyclient)
            self.dIvyClient[ivyclient]=ivyClient

        for listener in self.lIvyApplicationlisteners:
            listener.die(ivyclient)

    def _directMessage(self, ivyclient, *arg):
        try:
            ivyClient=self.dIvyClient[ivyclient]
        except KeyError:
            ivyClient=Ivy.IvyClient(ivyclient)
            self.dIvyClient[ivyclient]=ivyClient
        for listener in self.lIvyApplicationlisteners:
            listener.directMessage(ivyClient,*arg)

    def bindMsg(self, regexp, aIvyMessageListener):
        id=ivycpy.IvyBindMsg(aIvyMessageListener.receive,
                                 regexp)
        self.dRegexp[regexp]=id
        return id

    def unBindMsg(self, param):
        if type(param) == type(1):
            ivycpy.unBindMsg(self, id)
        else:
            if self.dRegexp.has_key(param):
                id=self.dRegexp[param]
                ivycpy.unBindMsg(self, id)
            else:
                IvyException("This expression not existed")()

    def sendMsg(self, message):
        return ivycpy.IvySendMsg(message)

    def getSelfIvyClient(self):
        return self.getIvyClientsByName(self.sAppName)

    def getIvyClients(self):
        livyclient=ivycpy.IvyGetApplicationList().split()
        lIvyClient=[]
        for client in livyclient:
            print "client %s"%client
            ivyClient=self.getIvyClientsByName(client)
            lIvyClient.append(ivyClient)
        return lIvyClient

    def getIvyClientsByName(self, name):
        ivyclient=ivycpy.IvyGetApplication(name)
        try:
            ivyClient=self.dIvyClient[ivyclient]
        except KeyError:
            ivyClient=IvyClient(ivyclient)
            self.dIvyClient[ivyclient]=ivyClient
        return ivyClient
        
class IvyApplicationAdapter :
    """IvyApplicationAdapter
    Classe d'objets abstraite définissant l'interface des objets à fournir à l'objet de classe Ivy
    lors de son initialisation et par les methodes addApplicationListener et removeApplicationListener
    """
    
    def connect(self,client):
        pass
    def disconnect(self,client):
        pass
    def die(self,id):
        pass
    def directMessage(self, id, msg):
        pass

class IvyMessageAdapter:
    def receive(self, client, *arg):
        pass
    
class IvyException :
    def __call__(self):
        raise self

    def __str__(self):
        return self.message
    
    def __init__(self, message):
        self.message=message