| 1 | #!/usr/bin/env python |
|---|
| 2 | ## common/hub.py |
|---|
| 3 | ## |
|---|
| 4 | ## Gajim Team: |
|---|
| 5 | ## - Yann Le Boulanger <asterix@crans.org> |
|---|
| 6 | ## - Vincent Hanquez <tab@snarc.org> |
|---|
| 7 | ## |
|---|
| 8 | ## Copyright (C) 2003 Gajim Team |
|---|
| 9 | ## |
|---|
| 10 | ## This program is free software; you can redistribute it and/or modify |
|---|
| 11 | ## it under the terms of the GNU General Public License as published |
|---|
| 12 | ## by the Free Software Foundation; version 2 only. |
|---|
| 13 | ## |
|---|
| 14 | ## This program is distributed in the hope that it will be useful, |
|---|
| 15 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | ## GNU General Public License for more details. |
|---|
| 18 | ## |
|---|
| 19 | |
|---|
| 20 | import Queue |
|---|
| 21 | import common.plugin |
|---|
| 22 | import common.thread |
|---|
| 23 | |
|---|
| 24 | """ Hub definitions """ |
|---|
| 25 | |
|---|
| 26 | class GajimHub: |
|---|
| 27 | def __init__(self): |
|---|
| 28 | self.queues = {} |
|---|
| 29 | """ {event1:[queue1, queue2]} """ |
|---|
| 30 | self.events = {} |
|---|
| 31 | self.queueIn = self.newQueue('in', 100) |
|---|
| 32 | # END __init__ |
|---|
| 33 | |
|---|
| 34 | def newQueue(self, name, size): |
|---|
| 35 | """ Creates a new queue """ |
|---|
| 36 | qu = Queue.Queue(size) |
|---|
| 37 | self.queues[name] = qu |
|---|
| 38 | return qu |
|---|
| 39 | # END newQueue |
|---|
| 40 | |
|---|
| 41 | def newPlugin(self, name): |
|---|
| 42 | """Creates a new Plugin """ |
|---|
| 43 | qu = self.newQueue(name, 100) |
|---|
| 44 | pl = common.plugin.GajimPlugin(name, qu, self.queueIn) |
|---|
| 45 | return pl |
|---|
| 46 | # END newPlugin |
|---|
| 47 | |
|---|
| 48 | def register(self, name, event): |
|---|
| 49 | """ Records a plugin from an event """ |
|---|
| 50 | qu = self.queues[name] |
|---|
| 51 | if self.events.has_key(event) : |
|---|
| 52 | self.events[event].append(qu) |
|---|
| 53 | else : |
|---|
| 54 | self.events[event] = [qu] |
|---|
| 55 | # END register |
|---|
| 56 | |
|---|
| 57 | def sendPlugin(self, event, con, data): |
|---|
| 58 | """ Sends an event to registered plugins |
|---|
| 59 | NOTIFY : ('NOTIFY', (user, status, message)) |
|---|
| 60 | MSG : ('MSG', (user, msg)) |
|---|
| 61 | ROSTER : ('ROSTER', {jid:{'status':_, 'name':_, 'show':_, 'groups':[], 'online':_, 'ask':_, 'sub':_} ,jid:{}}) |
|---|
| 62 | SUBSCRIBED : ('SUBSCRIBED', {'jid':_, 'nom':_, 'server':_, 'resource':_, 'status':_, 'show':_})""" |
|---|
| 63 | |
|---|
| 64 | if self.events.has_key(event): |
|---|
| 65 | for i in self.events[event]: |
|---|
| 66 | i.put((event, con, data)) |
|---|
| 67 | # END sendPlugin |
|---|
| 68 | # END GajimHub |
|---|