aboutsummaryrefslogtreecommitdiff
path: root/src/IvyCpy.py
blob: f83d4320e32db3c035178219794a09cf1ecc5532 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# -*- coding: iso-8859-1 -*-
REVISION = "$Id: "

# import
import threading
import ivycpy
import sys
import traceback
import re
import os

##
# class Ivy
# this class is an Object oriented interface for the Ivy-c Library
#
class Ivy:
    
    ##
    # __init__()
    # initialisation method
    # this method creates an Ivy Object and initializes an ivybus
    # @param name: name of the application to connect
    # @param message: the message displayed on connection
    # @param aIvyApplicationListener: the reference to the object used to treat connection / disconnection event for applications
    #
    def __init__(self, name, message, aIvyApplicationListener):
        self.lIvyApplicationlisteners=[]
        self.dIvyClient={}
        self.addApplicationListener(aIvyApplicationListener)
        self.sReadyMessage=message
        self.sAppName=name
        self.dRegexp={}
        self.lBindListeners=[]
        self.sendToSelf = False
        self.selfIvyClient = Ivy.SelfIvyClient(self)
        self.dIvyClient[str(self.selfIvyClient)] = self.selfIvyClient
        ivycpy.IvyInit(name,
                       message,
                       0,
                       self._appliCallback,
                       self._die)

    ##
    # method called on instruction Ivy()
    # this method creates and starts a thread listening for ivy events
    #
    def __call__(self):    
        self.theThread=threading.Thread(target=self.mainloop)
        self.theThread.setDaemon(1)
        self.theThread.start()
        
    ##
    # this method returns the IvyClient associated to an ivy-c client
    # @param ivyclient: the reference of the ivy-c client in the local database
    # @return the Ivy.IvyClient object corresponding to the ivyclient identifier
    #
    def getIvyClient(self, ivyclient):
        ivyClient = None
        if not self.dIvyClient.has_key(str(ivyclient)):
            ivyClient = Ivy.IvyClient(ivyclient)
            self.dIvyClient[str(ivyclient)] = ivyClient
        else:
            ivyClient = self.dIvyClient[str(ivyclient)]
        return ivyClient

    ##
    # getter of self Ivy client attribute
    # @return this IvyClient object  
    #
    def getSelfIvyClient(self):
        return self.selfIvyClient

    ##
    # getter of an Ivy client using his name
    # @param name: name of the client
    # @return the reference of the Ivy.IvyClient with the given name
    #
    def getIvyClientsByName(self, name):
        ivyclient = ivycpy.IvyGetApplication(name)
        return self.getIvyClient(ivyclient)

    ##
    # method returning the ivy-c clients connected to the ivybus
    # @return the list of Ivy.IvyClient connected to the ivybus 
    #
    def getIvyClients(self):
        lIvyClient = [ self.getIvyClientsByName(client)
                       for client in ivycpy.IvyGetApplicationList(",").split(",")]
        # suppression du dernier element car element vide
        lIvyClient.pop()
        return lIvyClient

    ##
    # methods starting the connection to ivybus
    # @param sdomainbus: address used for braodcast
    #
    def start(self, sdomainbus):
        #a partir de la chaine de caracteres
        addr, port = sdomainbus.split(":")
        self.sDomainBus= Ivy.Domain(addr, port)
        ivycpy.IvySetBindCallback(self._bindCallback)
        ivycpy.IvyBindDirectMsg(self._directMessage)
        ivycpy.IvyStart(sdomainbus)        
    
    ##
    # methods stopping the connection to ivybus
    #
    def stop(self):
        ivycpy.IvyStop()        

    ##
    # method launching the ivy mainloop in charge of listening events on the ivybus
    #
    def mainloop(self):
        ivycpy.IvyMainLoop()

    ##
    # method adding a ivy binding event listener to database
    # @param aBindListener: listener for ivy event
    #
    def addBindListener(self, aBindListener):
        self.lBindListeners.append(aBindListener)

    ##
    # method removing an ivy binding event listener from database
    # @param aBindListener: the listener to remove
    #
    def removeBindListener(self, aBindListener):
        self.lBindListeners.remove(aBindListener)
    
    ##
    # method adding application listener to database
    # @param aApplicationListener: the listener to application connection
    #
    def addApplicationListener(self, aApplicationListener):
        self.lIvyApplicationlisteners.append(aApplicationListener)

    ##
    # method removing application listener from database
    # @param aApplicationListener: the listener to remove
    #
    def removeApplicationListener(self, aApplicationListener):
        try:
            self.lIvyApplicationlisteners.remove(aApplicationListener)
        except ValueError, x:
            IvyException(str(x))()

    ##
    # protected method called on bindings events
    # @param client: reference to the ivy-c subscribing client 
    # @param id : unique id of the binding 
    # @param regexp: regular expression to match
    # @param event: binding or removal of an existing binding
    #
    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)
    
    ##
    # method called on applications connection
    # @param ivyclient: reference to the client connected or disconnected
    # @param connected: connection or disconnection
    #
    def _appliCallback(self, ivyclient, connected):
        ivyClient = self.getIvyClient(ivyclient)
        if connected == ivycpy.IvyApplicationConnected:
            self._connect(ivyClient)
        else:
            self._disconnect(ivyClient)
    
    ##
    # method calling the connect method of the applications listeners
    # @param arg: anything
    #
    def _connect(self, *arg):
        for listener in self.lIvyApplicationlisteners:
            listener.connect(*arg)

    ##
    # method calling the disconnect method of the applications listeners
    # @param arg: anything
    #
    def _disconnect(self, *arg):
        for listener in self.lIvyApplicationlisteners:
            listener.disconnect(*arg)

    ##
    # method called on die message
    # @param ivyclient: reference to  the client to kill
    # @param id: id of the message
    #
    def _die(self, ivyclient, id):
        ivyClient = self.getIvyClient(ivyclient)
        for listener in self.lIvyApplicationlisteners:
            listener.die(ivyClient, id)

    ##
    # method called on direct message
    # @param ivyclient: reference of the client
    # @param id: id of the message
    # @param msg: the message
    #
    def _directMessage(self, ivyclient, id, msg):
        ivyClient = self.getIvyClient(ivyclient)
        for listener in self.lIvyApplicationlisteners:
            listener.directMessage(ivyClient, id, msg)
    
    ##
    # method which binds message for application
    # @param regexp: the regular expression to match
    # @param aIvyMessageListener: the listener to call on reception
    # @return the id of the subscription
    #
    def bindMsg(self, regexp, aIvyMessageListener):
        id = ivycpy.IvyBindMsg(aIvyMessageListener.receive,regexp)
        self.dRegexp[regexp] = id
        self.selfIvyClient.bindMsg(regexp,aIvyMessageListener,id)
        return id
    
    ##
    # method which unbinds message for client
    # @param param: regexp or id of the message to unbind
    #
    def unBindMsg(self, param):
        if type(param) == type(1):
            ivycpy.IvyUnBindMsg(param)
            self.selfIvyClient.unbindMsg(param)
        else:
            if self.dRegexp.has_key(param):
                self.selfIvyClient.unbindMsg(param)
                id=self.dRegexp[param]
                ivycpy.IvyUnBindMsg(id)
            else:
                IvyException("This expression did not exist")()

    ##
    # method sending message on the ivybus
    # @param message: the message to send
    # @return the result of the operation 0 if error, 1 if ok
    #
    def sendMsg(self, message):
        if self.getSendToSelf() is True:
            self.selfIvyClient.sendMsg(message)
        return ivycpy.IvySendMsg(message)

    ##
    # method sending a die message to the ivy-c client
    # @param ivyclient : ivy-c client to kill
    #
    def sendDie(self, ivyclient):
        if type(ivyclient) == type(self.selfIvyClient):
            self.selfIvyClient.sendDie()
        else:
            ivycpy.IvySendDieMsg(ivyclient)
    
    ##
    # methods sending a message directly to a client
    # @param ivyclient : the ivy-c client to contact
    # @param id : id with no real meanings
    # @param message : the message to send
    #
    def sendDirectMsg(self, ivyclient, id, message):
        if type(ivyclient) == type(self.selfIvyClient):
            self.selfIvyClient.sendDirectMsg(id, message)
        else :
            ivycpy.IvySendDirectMsg(ivyclient,id,message)

    ##
    # methods sending an error message directly to a client
    # @param ivyclient : the ivy-c client to contact
    # @param id : id with no real meanings
    # @param error : the message to send
    #
    def sendError(self, ivyclient, id, error):
        if type(ivyclient) == type(self.selfIvyClient):
            self.selfIvyClient.sendError(id, error)
        else:
            ivycpy.IvySendError(ivyclient,id,error)

    ##
    # method launching a timer
    # @param nbticks : number of ticks
    # @param delay: delay before launching
    # @param callback: method to call at the end
    # @return the id of the timer
    #
    def timer_repeat(self, nbticks, delay, callback):
        try:
            timerid = ivycpy.IvyTimerRepeatAfter(nbticks, long(delay), callback)
        except:
            IvyException("An error occurred in IvyTimerRepeatAfter")()
            timerid = None            
            
        return (timerid)

    ##
    # method modifying the timer
    # @param timerid: id of the timer to modify
    # @param newdelay: waiting delay
    #
    def timer_modify(self, timerid, newdelay):
        ivycpy.IvyTimerModify(timerid, long(newdelay))

    ##
    # method removing timer
    # @param timerid: id of the timer to remove
    #
    def timer_remove(self, timerid):
        ivycpy.IvyTimerRemove(timerid)
    
    ##
    # method setting the value of boolean sendToSelf
    # @param value : boolean representing the falg sendToSelf
    #
    def setSendToSelf(self, value):
        self.sendToSelf = value
    
    ##
    # method returning the value of the flag sendToSelf
    # @return boolean representing the value of the flag sendToSelf
    #
    def getSendToSelf(self):
        return self.sendToSelf
    
    ##
    # class IvyClient
    # this class encapsulates an ivy-c client and gives methodes to access this client
    #
    class IvyClient:
        
        ##
        # initialisation of the IvyClient
        # @param ptr: (ivy-c client) pointer on the ivy-c client
        #
        def __init__(self, ptr):
            self.ptr = ptr

        ##
        # getter of the ivy-c client attribute
        # @return a pointer on an ivy-c client 
        #
        def getPtr(self):
            return self.ptr
        
        ##
        # getter of the ivy-c client name
        # @return a string representing the name of the ivy-c client
        #
        def getName(self):
            return ivycpy.IvyGetApplicationName(self.ptr)

        ##
        # getter of the ivy-c client host
        # @return a string representing the host of the ivy-c client
        #
        def getHost(self):
            return ivycpy.IvyGetApplicationHost(self.ptr)
        
        ##
        # getter of the ivy-c client messages bindings
        # @return a table of strings representing the message for this ivy-c client
        #
        def getMessages(self):
            return ivycpy.IvyGetApplicationMessages(self.ptr)
        
        ##
        # method sending a die message to the ivy-c client
        # @param ivyclient : ivy-c client to kill
        #
        def sendDie(self):
            ivycpy.IvySendDieMsg(self.ptr)
        
        ##
        # methods sending a message directly to a client
        # @param ivyclient : the ivy-c client to contact
        # @param id : id with no real meanings
        # @param message : the message to send
        #
        def sendDirectMsg(self, id, message):
            ivycpy.IvySendDirectMsg(self.ptr,id,message)

        ##
        # methods sending an error message directly to a client
        # @param ivyclient : the ivy-c client to contact
        # @param id : id with no real meanings
        # @param error : the message to send
        #
        def sendError(self, id, error):
            ivycpy.IvySendError(self.ptr,id,error)

    ##
    # class SelfIvyClient
    # this class works like an IvyClient class but represents the application client
    #
    class SelfIvyClient(IvyClient):
        
        ##
        # initialization of the SelfIvyClient
        # @param ptr: pointer on the Ivy object
        #
        def __init__(self, ptr):
            self.ptr = ptr
            self.dBindings = {}
            self.dId2Regexp = {}
            self.dRegexp2Id = {}
        
        ##
        # getter of the ptr attribute
        # @return a pointer on an Ivy object 
        # 
        def getPtr(self):
            return self.ptr
        
        ##
        # getter of the self Client name
        # @return a string representing the name of the selfclient
        #
        def getName(self):
            return self.ptr.sAppName

        ##
        # getter of the self Client host
        # @return a string representing the host of the selfclient
        #
        def getHost(self):
            return os.uname()[1]
        
        ##
        # getter of the self Client messages bindings
        # @return a tuple of strings representing the message for this selfIvyClient
        #
        def getMessages(self):
            return tuple(self.ptr.dRegexp.keys())
        
        ##
        # method sending a die message to the self Client
        #
        def sendDie(self):
            self.ptr._die(self,0)
        
        ##
        # methods sending a message directly to self Client
        # @param id : id with no real meanings
        # @param message : the message to send
        #
        def sendDirectMsg(self, id, message):
            self.ptr._directMessage(self,id,message)
        
        ##
        # methods sending an error message directly to self Client
        # @param id : id with no real meanings
        # @param error : the message to send
        #
        def sendError(self, id, error):
            self.ptr._directMessage(self,id,error)
        
        ##
        # method sending a message to self
        # @param message : string representing the message to send
        #
        def sendMsg(self,message):
            for regexp in self.dBindings.keys():
                r =  re.compile(regexp)
                captures = r.match(message)
                if captures:
                    captures=captures.groups()
                    if len(captures)==0:
                        captures=''
                    self.dBindings[regexp](message)
        
        ##
        # method in charge of treating bindings
        # @param regexp : String representing the regular expression to match
        # @param aIvyMessageListener : the reference to object to call on reception
        # @param id : id with no real meanings
        #
        def bindMsg(self, regexp, aIvyMessageListener, id):
            self.dId2Regexp[id] = regexp
            self.dRegexp2Id[regexp] = id
            self.dBindings[regexp] = aIvyMessageListener.receive
        
        ##
        # method sending a message to self
        # @param message : id or regexp to delete
        #
        def unbindMsg(self, param):
            if type(param) == type(1):
                regexp = self.dId2Regexp.pop(param)
                self.dRegexp2Id.pop(regexp)
                self.dBindings.pop(regexp)
            else:
                if self.dRegexp.has_key(param):
                    id = self.dRegexp2Id.pop(param)
                    self.dRegexp2Id.pop(id)
                    self.dBindings.pop(param)
                else:
                    IvyException("This expression did not exist")()
    
    ##
    # class domain
    # this class contains and allows access to ivybus properties
    #
    class Domain:
        ##
        # initialisation method
        # @param domainaddr: Ip adress for broadcast
        # @param port: port for braodcast
        #
        def __init__(self, domainaddr, port):
            self.domainaddr=domainaddr
            self.port=port
        
        ##
        # method call on print
        # display address and port used by ivybus
        # @return the string to print
        #
        def __str__(self):
            return self.domainaddr+":"+self.port

        ##
        # getter of broadcast Ip adress
        # @return the IP address
        #
        def getDomainaddr(self):
            return self.domainaddr
        
        ##
        # getter of broadcast port
        # @return the IP port
        #
        def getPort(self):
            return self.port


##
# IvyApplicationAdapter
# Classe d'objets abstraite definissant l'interface des objets a fournir a l'objet de classe Ivy
# lors de son initialisation et par les methodes addApplicationListener et removeApplicationListener
#
class IvyApplicationAdapter :
    
    ##
    # abstract method called on application connection
    # @param client: name of the client
    #
    def connect(self,client):
        pass
    
    ##
    # abstract method called on application disconnection
    # @param client: name of the client
    #
    def disconnect(self,client):
        pass
    
    ##
    # abstract method called on die message
    # @param client: name of the client
    # @param id: id of the message
    #
    def die(self,client, id):
        pass
    
    ##
    # abstract method called on direct message
    # @param client: name of the client
    # @param id: id of the client
    # @param msg: message sent
    #
    def directMessage(self, client, id, msg):
        pass

##
# IvyBindAdapter
# Classe d'objets abstraite definissant l'interface des objets
# a fournir a l'objet de classe Ivy
# via addBindListener et removeBindListener
#
class IvyBindAdapter :
    
    ##
    # invoked when a Ivy Client performs a bind
    # @param client: name of the client
    # @param id: id of the client
    # @param regexp: regular expression to match
    #
    def bindPerformed(self,client, id, regexp):
        pass
    
    ##
    # invoked when a Ivy Client performs a unbind
    # @param client: name of the client
    # @param id: id of the client
    # @param regexp: regular expression to match
    #
    def unbindPerformed(self,client, id, regexp):
        pass


##
# class IvyMessageAdapter
# classe abstraite definissant l'interface a fournir à la classe Ivy
# pour la reception de message
#
class IvyMessageAdapter:
        
    ##
    # invoked when a message matching regexp is send on the bus
    # @param client: name of the sender
    # @param arg: message received
    #
    def receive(self, client, *arg):
        pass


##
# classe gerant les exceptions Ivy
# 
class IvyException :
        
    ##
    # on error send an IvyException
    #
    def __call__(self):
        raise self

    ##
    # print the message of the exception
    # @return the message to print
    #
    def __str__(self):
        return self.message
    
    ##
    # initialisation method
    # @param message :  the message to print
    #
    def __init__(self, message):
        self.message=message