Commit 8c2c8e00 authored by Yoshinori Okuji's avatar Yoshinori Okuji

Reoreder initialization sequences so that connection objects are not...

Reoreder initialization sequences so that connection objects are not registered to an event manager until they are initialized.

git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@216 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent 2107a5ff
...@@ -82,15 +82,15 @@ class ListeningConnection(BaseConnection): ...@@ -82,15 +82,15 @@ class ListeningConnection(BaseConnection):
class Connection(BaseConnection): class Connection(BaseConnection):
"""A connection.""" """A connection."""
def __init__(self, event_manager, handler, s = None, addr = None): def __init__(self, event_manager, handler, s = None, addr = None):
BaseConnection.__init__(self, event_manager, handler, s = s, addr = addr)
if s is not None:
event_manager.addReader(self)
self.read_buf = [] self.read_buf = []
self.write_buf = [] self.write_buf = []
self.cur_id = 0 self.cur_id = 0
self.event_dict = {} self.event_dict = {}
self.aborted = False self.aborted = False
self.uuid = None self.uuid = None
BaseConnection.__init__(self, event_manager, handler, s = s, addr = addr)
if s is not None:
event_manager.addReader(self)
def getUUID(self): def getUUID(self):
return self.uuid return self.uuid
...@@ -271,8 +271,8 @@ class Connection(BaseConnection): ...@@ -271,8 +271,8 @@ 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 = None, **kw): def __init__(self, event_manager, handler, addr = None, **kw):
Connection.__init__(self, event_manager, handler, addr = addr)
self.connecting = False self.connecting = False
Connection.__init__(self, event_manager, handler, addr = addr)
handler.connectionStarted(self) handler.connectionStarted(self)
try: try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
...@@ -316,13 +316,17 @@ class ServerConnection(Connection): ...@@ -316,13 +316,17 @@ class ServerConnection(Connection):
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):
super(MTClientConnection, self).__init__(*args, **kwargs)
lock = RLock() lock = RLock()
self.acquire = lock.acquire self.acquire = lock.acquire
self.release = lock.release self.release = lock.release
super(MTClientConnection, self).__init__(*args, **kwargs)
def lock(self, blocking = 1): def lock(self, blocking = 1):
return self.acquire(blocking = blocking) try:
return self.acquire(blocking = blocking)
except:
import pdb
pdb.set_trace()
def unlock(self): def unlock(self):
self.release() self.release()
...@@ -330,10 +334,10 @@ class MTClientConnection(ClientConnection): ...@@ -330,10 +334,10 @@ class MTClientConnection(ClientConnection):
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):
super(MTClientConnection, self).__init__(*args, **kwargs)
lock = RLock() lock = RLock()
self.acquire = lock.acquire self.acquire = lock.acquire
self.release = lock.release self.release = lock.release
super(MTServerConnection, self).__init__(*args, **kwargs)
def lock(self, blocking = 1): def lock(self, blocking = 1):
return self.acquire(blocking = blocking) return self.acquire(blocking = blocking)
......
...@@ -214,28 +214,40 @@ class EpollEventManager(object): ...@@ -214,28 +214,40 @@ class EpollEventManager(object):
pass pass
def addReader(self, conn): def addReader(self, conn):
fd = conn.getSocket().fileno() try:
if fd not in self.reader_set: fd = conn.getSocket().fileno()
self.reader_set.add(fd) if fd not in self.reader_set:
self.epoll.modify(fd, 1, fd in self.writer_set) self.reader_set.add(fd)
self.epoll.modify(fd, 1, fd in self.writer_set)
except AttributeError:
pass
def removeReader(self, conn): def removeReader(self, conn):
fd = conn.getSocket().fileno() try:
if fd in self.reader_set: fd = conn.getSocket().fileno()
self.reader_set.remove(fd) if fd in self.reader_set:
self.epoll.modify(fd, 0, fd in self.writer_set) self.reader_set.remove(fd)
self.epoll.modify(fd, 0, fd in self.writer_set)
except AttributeError:
pass
def addWriter(self, conn): def addWriter(self, conn):
fd = conn.getSocket().fileno() try:
if fd not in self.writer_set: fd = conn.getSocket().fileno()
self.writer_set.add(fd) if fd not in self.writer_set:
self.epoll.modify(fd, fd in self.reader_set, 1) self.writer_set.add(fd)
self.epoll.modify(fd, fd in self.reader_set, 1)
except AttributeError:
pass
def removeWriter(self, conn): def removeWriter(self, conn):
fd = conn.getSocket().fileno() try:
if fd in self.writer_set: fd = conn.getSocket().fileno()
self.writer_set.remove(fd) if fd in self.writer_set:
self.epoll.modify(fd, fd in self.reader_set, 0) self.writer_set.remove(fd)
self.epoll.modify(fd, fd in self.reader_set, 0)
except AttributeError:
pass
# Default to EpollEventManager. # Default to EpollEventManager.
EventManager = EpollEventManager EventManager = EpollEventManager
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