Commit 215250bb authored by Grégory Wisniewski's avatar Grégory Wisniewski

Remove 'connector_handler' parameter/attribute from Connection.

Merge both into 'connector' only to simplify API.

git-svn-id: https://svn.erp5.org/repos/neo/trunk@1884 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 7d0a1e25
...@@ -84,8 +84,8 @@ class Application(object): ...@@ -84,8 +84,8 @@ class Application(object):
# Make a listening port. # Make a listening port.
handler = AdminEventHandler(self) handler = AdminEventHandler(self)
ListeningConnection(self.em, handler, addr = self.server, ListeningConnection(self.em, handler, addr=self.server,
connector_handler = self.connector_handler) connector=self.connector_handler())
# Connect to a primary master node, verify data, and # Connect to a primary master node, verify data, and
# start the operation. This cycle will be executed permentnly, # start the operation. This cycle will be executed permentnly,
......
...@@ -146,7 +146,7 @@ class BootstrapManager(EventHandler): ...@@ -146,7 +146,7 @@ class BootstrapManager(EventHandler):
if conn is None: if conn is None:
# open the connection # open the connection
addr = self.current.getAddress() addr = self.current.getAddress()
conn = ClientConnection(em, self, addr, connector_handler) conn = ClientConnection(em, self, addr, connector_handler())
# still processing # still processing
em.poll(1) em.poll(1)
node = nm.getByUUID(conn.getUUID()) node = nm.getByUUID(conn.getUUID())
......
...@@ -303,7 +303,7 @@ class Application(object): ...@@ -303,7 +303,7 @@ class Application(object):
# Connect to master # Connect to master
conn = MTClientConnection(self.em, self.notifications_handler, conn = MTClientConnection(self.em, self.notifications_handler,
addr=self.trying_master_node.getAddress(), addr=self.trying_master_node.getAddress(),
connector_handler=self.connector_handler, connector=self.connector_handler(),
dispatcher=self.dispatcher) dispatcher=self.dispatcher)
# Query for primary master node # Query for primary master node
conn.lock() conn.lock()
......
...@@ -49,9 +49,9 @@ class ConnectionPool(object): ...@@ -49,9 +49,9 @@ class ConnectionPool(object):
logging.debug('trying to connect to %s - %s', node, node.getState()) logging.debug('trying to connect to %s - %s', node, node.getState())
app.setNodeReady() app.setNodeReady()
conn = MTClientConnection(app.em, app.storage_event_handler, addr, conn = MTClientConnection(app.em, app.storage_event_handler, addr,
connector_handler=app.connector_handler, connector=app.connector_handler(), dispatcher=app.dispatcher)
dispatcher=app.dispatcher)
conn.lock() conn.lock()
try: try:
if conn.getConnector() is None: if conn.getConnector() is None:
# This happens, if a connection could not be established. # This happens, if a connection could not be established.
......
...@@ -123,17 +123,13 @@ class HandlerSwitcher(object): ...@@ -123,17 +123,13 @@ class HandlerSwitcher(object):
class BaseConnection(object): class BaseConnection(object):
"""A base connection.""" """A base connection."""
def __init__(self, event_manager, handler, connector = None, def __init__(self, event_manager, handler, connector, addr=None):
addr = None, connector_handler = None): assert connector is not None, "Need a low-level connector"
self.em = event_manager self.em = event_manager
self.connector = connector self.connector = connector
self.addr = addr self.addr = addr
self._handlers = HandlerSwitcher(self, handler) self._handlers = HandlerSwitcher(self, handler)
if connector is not None: event_manager.register(self)
self.connector_handler = connector.__class__
event_manager.register(self)
else:
self.connector_handler = connector_handler
def lock(self): def lock(self):
return 1 return 1
...@@ -144,13 +140,6 @@ class BaseConnection(object): ...@@ -144,13 +140,6 @@ class BaseConnection(object):
def getConnector(self): def getConnector(self):
return self.connector return self.connector
def setConnector(self, connector):
if self.connector is not None:
raise RuntimeError, 'cannot overwrite a connector in a connection'
if connector is not None:
self.connector = connector
self.em.register(self)
def getAddress(self): def getAddress(self):
return self.addr return self.addr
...@@ -162,8 +151,8 @@ class BaseConnection(object): ...@@ -162,8 +151,8 @@ class BaseConnection(object):
def close(self): def close(self):
"""Close the connection.""" """Close the connection."""
em = self.em
if self.connector is not None: if self.connector is not None:
em = self.em
em.removeReader(self) em.removeReader(self)
em.removeWriter(self) em.removeWriter(self)
em.unregister(self) em.unregister(self)
...@@ -212,14 +201,11 @@ attributeTracker.track(BaseConnection) ...@@ -212,14 +201,11 @@ attributeTracker.track(BaseConnection)
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, **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,
addr = addr, addr=addr, connector=connector)
connector_handler = connector_handler) self.connector.makeListeningConnection(addr)
connector = connector_handler()
connector.makeListeningConnection(addr)
self.setConnector(connector)
self.em.addReader(self) self.em.addReader(self)
def readable(self): def readable(self):
...@@ -228,7 +214,7 @@ class ListeningConnection(BaseConnection): ...@@ -228,7 +214,7 @@ class ListeningConnection(BaseConnection):
logging.debug('accepted a connection from %s:%d', *addr) logging.debug('accepted a connection from %s:%d', *addr)
handler = self.getHandler() handler = self.getHandler()
new_conn = ServerConnection(self.getEventManager(), handler, new_conn = ServerConnection(self.getEventManager(), handler,
connector=new_s, addr=addr) connector=new_s, addr=addr)
handler.connectionAccepted(new_conn) handler.connectionAccepted(new_conn)
except ConnectorTryAgainException: except ConnectorTryAgainException:
pass pass
...@@ -243,9 +229,9 @@ class ListeningConnection(BaseConnection): ...@@ -243,9 +229,9 @@ class ListeningConnection(BaseConnection):
class Connection(BaseConnection): class Connection(BaseConnection):
"""A connection.""" """A connection."""
def __init__(self, event_manager, handler, def __init__(self, event_manager, handler, connector, addr=None):
connector = None, addr = None, BaseConnection.__init__(self, event_manager, handler,
connector_handler = None): connector=connector, addr=addr)
self.read_buf = [] self.read_buf = []
self.write_buf = [] self.write_buf = []
self.cur_id = 0 self.cur_id = 0
...@@ -255,11 +241,7 @@ class Connection(BaseConnection): ...@@ -255,11 +241,7 @@ class Connection(BaseConnection):
self.uuid = None self.uuid = None
self._queue = [] self._queue = []
self._on_close = None self._on_close = None
BaseConnection.__init__(self, event_manager, handler, event_manager.addReader(self)
connector = connector, addr = addr,
connector_handler = connector_handler)
if connector is not None:
event_manager.addReader(self)
def setOnClose(self, callback): def setOnClose(self, callback):
assert self._on_close is None assert self._on_close is None
...@@ -522,22 +504,19 @@ class Connection(BaseConnection): ...@@ -522,22 +504,19 @@ class Connection(BaseConnection):
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, **kw):
self.connecting = True self.connecting = True
Connection.__init__(self, event_manager, handler, addr = addr, Connection.__init__(self, event_manager, handler, addr=addr,
connector_handler = connector_handler) connector=connector)
handler.connectionStarted(self) handler.connectionStarted(self)
try: try:
connector = connector_handler()
self.setConnector(connector)
try: try:
connector.makeClientConnection(addr) self.connector.makeClientConnection(addr)
except ConnectorInProgressException: except ConnectorInProgressException:
event_manager.addWriter(self) event_manager.addWriter(self)
else: else:
self.connecting = False self.connecting = False
self.getHandler().connectionCompleted(self) self.getHandler().connectionCompleted(self)
event_manager.addReader(self)
except ConnectorConnectionRefusedException: except ConnectorConnectionRefusedException:
self._closure(was_connected=True) self._closure(was_connected=True)
except ConnectorException: except ConnectorException:
......
...@@ -96,7 +96,7 @@ class Application(object): ...@@ -96,7 +96,7 @@ class Application(object):
# Make a listening port. # Make a listening port.
self.listening_conn = ListeningConnection(self.em, None, self.listening_conn = ListeningConnection(self.em, None,
addr = self.server, connector_handler = self.connector_handler) addr=self.server, connector=self.connector_handler())
# Start a normal operation. # Start a normal operation.
while True: while True:
...@@ -189,7 +189,7 @@ class Application(object): ...@@ -189,7 +189,7 @@ class Application(object):
self.em.getClientList()] self.em.getClientList()]
if addr not in current_connections: if addr not in current_connections:
ClientConnection(self.em, client_handler, addr=addr, ClientConnection(self.em, client_handler, addr=addr,
connector_handler=self.connector_handler) connector=self.connector_handler())
self.em.poll(1) self.em.poll(1)
if len(self.unconnected_master_node_set | if len(self.unconnected_master_node_set |
...@@ -414,7 +414,7 @@ class Application(object): ...@@ -414,7 +414,7 @@ class Application(object):
if not connected_to_master: if not connected_to_master:
ClientConnection(self.em, primary_handler, addr=addr, ClientConnection(self.em, primary_handler, addr=addr,
connector_handler=self.connector_handler) connector=self.connector_handler())
# and another for the future incoming connections # and another for the future incoming connections
handler = identification.IdentificationHandler(self) handler = identification.IdentificationHandler(self)
......
...@@ -38,9 +38,8 @@ class NeoCTL(object): ...@@ -38,9 +38,8 @@ class NeoCTL(object):
def __getConnection(self): def __getConnection(self):
if not self.connected: if not self.connected:
self.connection = ClientConnection( self.connection = ClientConnection(self.em, self.handler,
self.em, self.handler, addr=self.server, addr=self.server, connector=self.connector_handler())
connector_handler=self.connector_handler)
while not self.connected and self.connection is not None: while not self.connected and self.connection is not None:
self.em.poll(0) self.em.poll(0)
if self.connection is None: if self.connection is None:
......
...@@ -133,7 +133,7 @@ class Application(object): ...@@ -133,7 +133,7 @@ class Application(object):
# Make a listening port # Make a listening port
handler = identification.IdentificationHandler(self) handler = identification.IdentificationHandler(self)
self.listening_conn = ListeningConnection(self.em, handler, self.listening_conn = ListeningConnection(self.em, handler,
addr=self.server, connector_handler=self.connector_handler) addr=self.server, connector=self.connector_handler())
# Connect to a primary master node, verify data, and # Connect to a primary master node, verify data, and
# start the operation. This cycle will be executed permentnly, # start the operation. This cycle will be executed permentnly,
......
...@@ -174,7 +174,7 @@ class Replicator(object): ...@@ -174,7 +174,7 @@ class Replicator(object):
if self.current_connection is None: if self.current_connection is None:
handler = replication.ReplicationHandler(app) handler = replication.ReplicationHandler(app)
self.current_connection = ClientConnection(app.em, handler, self.current_connection = ClientConnection(app.em, handler,
addr = addr, connector_handler = app.connector_handler) addr=addr, connector=app.connector_handler())
p = Packets.RequestIdentification(NodeTypes.STORAGE, p = Packets.RequestIdentification(NodeTypes.STORAGE,
app.uuid, app.server, app.name) app.uuid, app.server, app.name)
self.current_connection.ask(p) self.current_connection.ask(p)
......
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