Commit 27de0880 authored by Grégory Wisniewski's avatar Grégory Wisniewski

Define is(Client|Server|Listening)Connection on the BaseConnection class to

ensure any acces to those methods can be done on any connection from the event
manager without crash the applications. This way is not less consistent than the
previous where isServerConnection was defined on ClientConnection and so on...
Replace any isinstance() calls with a connection as parameter by call to the
attributes above. 


git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@1088 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 5160976a
......@@ -33,6 +33,7 @@ def not_closed(func):
return func(self, *args, **kw)
return decorator
def lockCheckWrapper(func):
"""
This function is to be used as a wrapper around
......@@ -53,6 +54,7 @@ def lockCheckWrapper(func):
return func(self, *args, **kw)
return wrapper
class BaseConnection(object):
"""A base connection."""
......@@ -119,13 +121,21 @@ class BaseConnection(object):
return None
def isListeningConnection(self):
raise NotImplementedError
return False
def isServerConnection(self):
return False
def isClientConnection(self):
return False
def hasPendingMessages(self):
return False
class ListeningConnection(BaseConnection):
"""A listen connection."""
def __init__(self, event_manager, handler, addr, connector_handler, **kw):
logging.debug('listening to %s:%d', *addr)
BaseConnection.__init__(self, event_manager, handler,
......@@ -150,8 +160,10 @@ class ListeningConnection(BaseConnection):
def isListeningConnection(self):
return True
class Connection(BaseConnection):
"""A connection."""
def __init__(self, event_manager, handler,
connector = None, addr = None,
connector_handler = None):
......@@ -412,14 +424,10 @@ class Connection(BaseConnection):
self.expectMessage(msg_id, timeout, 0)
self._addPacket(packet)
def isServerConnection(self):
raise NotImplementedError
def isListeningConnection(self):
return False
class ClientConnection(Connection):
"""A connection from this node to a remote node."""
def __init__(self, event_manager, handler, addr, connector_handler, **kw):
self.connecting = True
Connection.__init__(self, event_manager, handler, addr = addr,
......@@ -460,16 +468,20 @@ class ClientConnection(Connection):
else:
Connection.writable(self)
def isServerConnection(self):
return False
def isClientConnection(self):
return True
class ServerConnection(Connection):
"""A connection from a remote node to this node."""
def isServerConnection(self):
return True
class MTClientConnection(ClientConnection):
"""A Multithread-safe version of ClientConnection."""
def __init__(self, *args, **kwargs):
# _lock is only here for lock debugging purposes. Do not use.
self._lock = lock = RLock()
......@@ -528,8 +540,10 @@ class MTClientConnection(ClientConnection):
finally:
self.release()
class MTServerConnection(ServerConnection):
"""A Multithread-safe version of ServerConnection."""
def __init__(self, *args, **kwargs):
# _lock is only here for lock debugging purposes. Do not use.
self._lock = lock = RLock()
......
......@@ -197,7 +197,7 @@ class Application(object):
self.primary = True
logging.debug('I am the primary, so sending an announcement')
for conn in em.getConnectionList():
if isinstance(conn, ClientConnection):
if conn.isClientConnection():
conn.notify(protocol.announcePrimaryMaster())
conn.abort()
closed = False
......@@ -206,12 +206,12 @@ class Application(object):
em.poll(1)
closed = True
for conn in em.getConnectionList():
if isinstance(conn, ClientConnection):
if conn.isClientConnection():
closed = False
break
if t + 10 < time():
for conn in em.getConnectionList():
if isinstance(conn, ClientConnection):
if conn.isClientConnection():
conn.close()
closed = True
else:
......@@ -227,15 +227,13 @@ class Application(object):
primary = self.primary_master_node
addr = primary.getServer()
for conn in em.getConnectionList():
if isinstance(conn, ServerConnection) \
or isinstance(conn, ClientConnection) \
if conn.isServerConnection() or conn.isClientConnection() \
and addr != conn.getAddress():
conn.close()
# But if there is no such connection, something wrong happened.
for conn in em.getConnectionList():
if isinstance(conn, ClientConnection) \
and addr == conn.getAddress():
if conn.isClientConnection() and addr == conn.getAddress():
break
else:
raise ElectionFailure, 'no connection remains to the primary'
......@@ -246,7 +244,7 @@ class Application(object):
# Ask all connected nodes to reelect a single primary master.
for conn in em.getConnectionList():
if isinstance(conn, ClientConnection):
if conn.isClientConnection():
conn.notify(protocol.reelectPrimaryMaster())
conn.abort()
......@@ -263,7 +261,7 @@ class Application(object):
closed = True
for conn in em.getConnectionList():
if isinstance(conn, ClientConnection):
if conn.isClientConnection():
# Still not closed.
closed = False
break
......
......@@ -171,7 +171,7 @@ class Application(object):
# First of all, make sure that I have no connection.
for conn in self.em.getConnectionList():
if not isinstance(conn, ListeningConnection):
if not conn.isListeningConnection():
conn.close()
# search, find, connect and identify to the primary master
......
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