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