Commit a20c1279 authored by Vincent Pelletier's avatar Vincent Pelletier

Reindent to 4 spaces.


git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@894 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 4fcd66f7
...@@ -25,137 +25,141 @@ import logging ...@@ -25,137 +25,141 @@ import logging
connector_registry = {} connector_registry = {}
def registerConnectorHandler(connector_handler): def registerConnectorHandler(connector_handler):
connector_registry[connector_handler.__name__] = connector_handler connector_registry[connector_handler.__name__] = connector_handler
def getConnectorHandler(connector): def getConnectorHandler(connector):
if isinstance(connector, basestring): if isinstance(connector, basestring):
connector_handler = connector_registry.get(connector) connector_handler = connector_registry.get(connector)
else: else:
# Allow to directly provide a handler class without requiring to register # Allow to directly provide a handler class without requiring to
# it first. # register it first.
connector_handler = connector connector_handler = connector
return connector_handler return connector_handler
class SocketConnector: class SocketConnector:
""" This class is a wrapper for a socket """ """ This class is a wrapper for a socket """
is_listening = False is_listening = False
remote_addr = None remote_addr = None
is_closed = None is_closed = None
def __init__(self, s=None, accepted_from=None): def __init__(self, s=None, accepted_from=None):
self.accepted_from = accepted_from self.accepted_from = accepted_from
if accepted_from is not None: if accepted_from is not None:
self.remote_addr = accepted_from self.remote_addr = accepted_from
self.is_listening = False self.is_listening = False
self.is_closed = False self.is_closed = False
if s is None: if s is None:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
else: else:
self.socket = s self.socket = s
def makeClientConnection(self, addr): def makeClientConnection(self, addr):
self.is_closed = False self.is_closed = False
self.remote_addr = addr self.remote_addr = addr
try: try:
try: try:
self.socket.setblocking(0) self.socket.setblocking(0)
self.socket.connect(addr) self.socket.connect(addr)
except socket.error, (err, errmsg): except socket.error, (err, errmsg):
if err == errno.EINPROGRESS: if err == errno.EINPROGRESS:
raise ConnectorInProgressException raise ConnectorInProgressException
if err == errno.ECONNREFUSED: if err == errno.ECONNREFUSED:
raise ConnectorConnectionRefusedException raise ConnectorConnectionRefusedException
raise ConnectorException, 'makeClientConnection failed: %s:%s' % (err, errmsg) raise ConnectorException, 'makeClientConnection failed: ' \
finally: '%s:%s' % (err, errmsg)
logging.debug('%r connecting to %r', self.socket.getsockname(), addr) finally:
logging.debug('%r connecting to %r', self.socket.getsockname(),
def makeListeningConnection(self, addr): addr)
self.is_closed = False
self.is_listening = True def makeListeningConnection(self, addr):
try: self.is_closed = False
self.socket.setblocking(0) self.is_listening = True
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) try:
self.socket.bind(addr) self.socket.setblocking(0)
self.socket.listen(5) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
except socket.error, (err, errmsg): self.socket.bind(addr)
self.socket.close() self.socket.listen(5)
raise ConnectorException, 'makeListeningConnection failed: %s:%s' % (err, errmsg) except socket.error, (err, errmsg):
self.socket.close()
def getError(self): raise ConnectorException, 'makeListeningConnection failed: %s:%s' % \
return self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR) (err, errmsg)
def getDescriptor(self): def getError(self):
# this descriptor must only be used by the event manager, where it guarantee return self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
# unicity only while the connector is opened and registered in epoll
return self.socket.fileno() def getDescriptor(self):
# this descriptor must only be used by the event manager, where it guarantee
def getNewConnection(self): # unicity only while the connector is opened and registered in epoll
try: return self.socket.fileno()
new_s, addr = self.socket.accept()
new_s = SocketConnector(new_s, accepted_from=addr) def getNewConnection(self):
return new_s, addr try:
except socket.error, (err, errmsg): new_s, addr = self.socket.accept()
if err == errno.EAGAIN: new_s = SocketConnector(new_s, accepted_from=addr)
raise ConnectorTryAgainException return new_s, addr
raise ConnectorException, 'getNewConnection failed: %s:%s' % (err, errmsg) except socket.error, (err, errmsg):
if err == errno.EAGAIN:
def shutdown(self): raise ConnectorTryAgainException
# This may fail if the socket is not connected. raise ConnectorException, 'getNewConnection failed: %s:%s' % \
try: (err, errmsg)
self.socket.shutdown(socket.SHUT_RDWR)
except socket.error: def shutdown(self):
pass # This may fail if the socket is not connected.
try:
def receive(self): self.socket.shutdown(socket.SHUT_RDWR)
try: except socket.error:
return self.socket.recv(4096) pass
except socket.error, (err, errmsg):
if err == errno.EAGAIN: def receive(self):
raise ConnectorTryAgainException try:
if err == errno.ECONNREFUSED: return self.socket.recv(4096)
raise ConnectorConnectionRefusedException except socket.error, (err, errmsg):
if err == errno.ECONNRESET: if err == errno.EAGAIN:
raise ConnectorConnectionClosedException raise ConnectorTryAgainException
raise ConnectorException, 'receive failed: %s:%s' % (err, errmsg) if err == errno.ECONNREFUSED:
raise ConnectorConnectionRefusedException
def send(self, msg): if err == errno.ECONNRESET:
try: raise ConnectorConnectionClosedException
return self.socket.send(msg) raise ConnectorException, 'receive failed: %s:%s' % (err, errmsg)
except socket.error, (err, errmsg):
if err == errno.EAGAIN: def send(self, msg):
raise ConnectorTryAgainException try:
if err == errno.ECONNRESET: return self.socket.send(msg)
raise ConnectorConnectionClosedException except socket.error, (err, errmsg):
raise ConnectorException, 'send failed: %s:%s' % (err, errmsg) if err == errno.EAGAIN:
raise ConnectorTryAgainException
def close(self): if err == errno.ECONNRESET:
self.is_closed = True raise ConnectorConnectionClosedException
return self.socket.close() raise ConnectorException, 'send failed: %s:%s' % (err, errmsg)
def __repr__(self): def close(self):
try: self.is_closed = True
fileno = str(self.socket.fileno()) return self.socket.close()
except socket.error, (err, errmsg):
fileno = '?' def __repr__(self):
result = '<%s at 0x%x fileno %s %s>' % (self.__class__.__name__, id(self), try:
fileno, self.socket.getsockname()) fileno = str(self.socket.fileno())
if self.is_closed is None: except socket.error, (err, errmsg):
result += 'never opened' fileno = '?'
else: result = '<%s at 0x%x fileno %s %s>' % (self.__class__.__name__,
if self.is_closed: id(self), fileno, self.socket.getsockname())
result += 'closed ' if self.is_closed is None:
else: result += 'never opened'
result += 'opened '
if self.is_listening:
result += 'listening'
else:
if self.accepted_from is None:
result += 'to'
else: else:
result += 'from' if self.is_closed:
result += ' %s' % (self.remote_addr, ) result += 'closed '
return result + '>' else:
result += 'opened '
if self.is_listening:
result += 'listening'
else:
if self.accepted_from is None:
result += 'to'
else:
result += 'from'
result += ' %s' % (self.remote_addr, )
return result + '>'
registerConnectorHandler(SocketConnector) registerConnectorHandler(SocketConnector)
......
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