2003-10-22 20:45:13 +02:00
|
|
|
## common/hub.py
|
|
|
|
##
|
|
|
|
## Gajim Team:
|
2005-01-07 22:52:38 +01:00
|
|
|
## - Yann Le Boulanger <asterix@lagaule.org>
|
2004-06-18 11:25:15 +02:00
|
|
|
## - Vincent Hanquez <tab@snarc.org>
|
2003-10-22 20:45:13 +02:00
|
|
|
##
|
2005-01-07 22:52:38 +01:00
|
|
|
## Copyright (C) 2003-2005 Gajim Team
|
2003-10-22 20:45:13 +02:00
|
|
|
##
|
|
|
|
## This program is free software; you can redistribute it and/or modify
|
|
|
|
## it under the terms of the GNU General Public License as published
|
|
|
|
## by the Free Software Foundation; version 2 only.
|
|
|
|
##
|
|
|
|
## This program is distributed in the hope that it will be useful,
|
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
|
|
|
|
|
|
|
import Queue
|
|
|
|
import common.plugin
|
|
|
|
import common.thread
|
|
|
|
|
|
|
|
""" Hub definitions """
|
|
|
|
|
|
|
|
class GajimHub:
|
2004-07-08 21:56:30 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.queues = {}
|
|
|
|
# {event1:[queue1, queue2]}
|
|
|
|
self.events = {}
|
|
|
|
self.queueIn = self.newQueue('in', 100)
|
2004-08-02 01:49:21 +02:00
|
|
|
self.saveQueue = Queue.Queue(100)
|
|
|
|
self.events_to_store = ['WARNING', 'MSG', 'MSGERROR', 'SUBSCRIBED', 'UNSUBSCRIBED', 'SUBSCRIBE']
|
|
|
|
self.queue_to_send = None
|
2004-07-08 21:56:30 +02:00
|
|
|
# END __init__
|
2003-10-22 20:45:13 +02:00
|
|
|
|
2004-07-08 21:56:30 +02:00
|
|
|
def newQueue(self, name, size):
|
|
|
|
""" Creates a new queue """
|
|
|
|
qu = Queue.Queue(size)
|
|
|
|
self.queues[name] = qu
|
|
|
|
return qu
|
|
|
|
# END newQueue
|
2003-10-22 20:45:13 +02:00
|
|
|
|
2004-07-08 21:56:30 +02:00
|
|
|
def newPlugin(self, name):
|
|
|
|
"""Creates a new Plugin """
|
2004-07-17 23:00:38 +02:00
|
|
|
if name in self.queues.keys():
|
|
|
|
return 0
|
2004-07-08 21:56:30 +02:00
|
|
|
qu = self.newQueue(name, 100)
|
|
|
|
pl = common.plugin.GajimPlugin(name, qu, self.queueIn)
|
|
|
|
return pl
|
|
|
|
# END newPlugin
|
2003-10-22 20:45:13 +02:00
|
|
|
|
2004-07-08 21:56:30 +02:00
|
|
|
def register(self, name, event):
|
|
|
|
""" Records a plugin from an event """
|
2004-07-28 20:31:31 +02:00
|
|
|
if not self.queues.has_key(name):
|
|
|
|
return
|
2004-07-08 21:56:30 +02:00
|
|
|
qu = self.queues[name]
|
2004-08-02 01:49:21 +02:00
|
|
|
if event == 'VISUAL' and not self.saveQueue.empty():
|
|
|
|
# we save the queue in whitch we must send saved events
|
|
|
|
# after the roster is sent
|
|
|
|
self.queue_to_send = qu
|
|
|
|
if self.events.has_key(event):
|
2004-07-17 17:31:47 +02:00
|
|
|
if not qu in self.events[event]:
|
|
|
|
self.events[event].append(qu)
|
2004-07-08 21:56:30 +02:00
|
|
|
else :
|
|
|
|
self.events[event] = [qu]
|
|
|
|
# END register
|
|
|
|
|
2004-07-28 20:31:31 +02:00
|
|
|
def unregisterEvents(self, name, event):
|
|
|
|
""" Records a plugin from an event """
|
|
|
|
if not self.queues.has_key(name):
|
|
|
|
return
|
|
|
|
qu = self.queues[name]
|
|
|
|
if self.events.has_key(event) :
|
|
|
|
if qu in self.events[event]:
|
|
|
|
self.events[event].remove(qu)
|
|
|
|
# END register
|
|
|
|
|
|
|
|
def unregister(self, name):
|
|
|
|
if not self.queues.has_key(name):
|
|
|
|
return
|
|
|
|
qu = self.queues[name]
|
|
|
|
for event in self.events:
|
|
|
|
if qu in self.events[event]:
|
|
|
|
self.events[event].remove(qu)
|
|
|
|
del self.queues[name]
|
|
|
|
# END unregister
|
|
|
|
|
2004-08-01 23:45:58 +02:00
|
|
|
def sendPlugin(self, event, con, data, qu=None):
|
2004-07-17 17:31:47 +02:00
|
|
|
""" Sends an event to registered plugins"""
|
2004-07-08 21:56:30 +02:00
|
|
|
if self.events.has_key(event):
|
2004-08-02 01:49:21 +02:00
|
|
|
if event in self.events_to_store and len(self.events['VISUAL']) == 0:
|
|
|
|
# Save event if no visual plugin is registered
|
|
|
|
self.saveQueue.put((event, con, data))
|
2004-08-01 23:45:58 +02:00
|
|
|
for queue in self.events[event]:
|
|
|
|
if qu == None or qu == queue:
|
|
|
|
queue.put((event, con, data))
|
2004-08-02 01:49:21 +02:00
|
|
|
if event == 'ROSTER' and self.queue_to_send in self.events[event]:
|
|
|
|
# send saved events
|
|
|
|
while not self.saveQueue.empty():
|
|
|
|
ev = self.saveQueue.get()
|
|
|
|
self.queue_to_send.put(ev)
|
|
|
|
self.queue_to_send = None
|
2004-07-08 21:56:30 +02:00
|
|
|
# END sendPlugin
|
2003-10-22 20:45:13 +02:00
|
|
|
# END GajimHub
|