Commit 5f26aa0a authored by Vincent Pelletier's avatar Vincent Pelletier

Genericise Dispatcher class (remove references to queue).


git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@1020 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 078084e1
...@@ -15,26 +15,32 @@ ...@@ -15,26 +15,32 @@
# along with this program; if not, write to the Free Software # along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
MARKER = []
class Dispatcher: class Dispatcher:
"""Dispatcher class use to redirect request to thread.""" """Register a packet, connection pair as expecting a response packet."""
def __init__(self): def __init__(self):
# This dict is used to associate conn/message id to client thread queue
# and thus redispatch answer to the original thread
self.message_table = {} self.message_table = {}
def getQueue(self, conn, packet): def pop(self, conn, packet, default=MARKER):
"""Retrieve register-time provided payload."""
key = (id(conn), packet.getId()) key = (id(conn), packet.getId())
return self.message_table.pop(key, None) if default is MARKER:
result = self.message_table.pop(key)
else:
result = self.message_table.pop(key, default)
return result
def register(self, conn, msg_id, queue): def register(self, conn, msg_id, payload):
"""Register an expectation for a reply. Thanks to GIL, it is """Register an expectation for a reply. Thanks to GIL, it is
safe not to use a lock here.""" safe not to use a lock here."""
key = (id(conn), msg_id) key = (id(conn), msg_id)
self.message_table[key] = queue self.message_table[key] = payload
def registered(self, conn): def registered(self, conn):
"""Check if a connection is registered into message table.""" """Check if a connection is registered into message table."""
# XXX: serch algorythm could be improved by improving data structure.
searched_id = id(conn) searched_id = id(conn)
for conn_id, msg_id in self.message_table.iterkeys(): for conn_id, msg_id in self.message_table.iterkeys():
if searched_id == conn_id: if searched_id == conn_id:
......
...@@ -38,7 +38,7 @@ class BaseHandler(EventHandler): ...@@ -38,7 +38,7 @@ class BaseHandler(EventHandler):
def packetReceived(self, conn, packet): def packetReceived(self, conn, packet):
"""Redirect all received packet to dispatcher thread.""" """Redirect all received packet to dispatcher thread."""
if packet.isResponse(): if packet.isResponse():
queue = self.dispatcher.getQueue(conn, packet) queue = self.dispatcher.pop(conn, packet, None)
if queue is None: if queue is None:
raise UnexpectedPacketError('Unexpected response packet') raise UnexpectedPacketError('Unexpected response packet')
queue.put((conn, packet)) queue.put((conn, packet))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment