Commit b28dbac8 authored by Victor Stinner's avatar Victor Stinner

asyncio, Tulip issue 180: Make Server attributes and methods private

- loop, waiters and active_count attributes are now private
- attach(), detach() and wakeup() methods are now private

The sockets attribute remains public.
parent 7aee3ecd
...@@ -89,43 +89,44 @@ def _raise_stop_error(*args): ...@@ -89,43 +89,44 @@ def _raise_stop_error(*args):
class Server(events.AbstractServer): class Server(events.AbstractServer):
def __init__(self, loop, sockets): def __init__(self, loop, sockets):
self.loop = loop self._loop = loop
self.sockets = sockets self.sockets = sockets
self.active_count = 0 self._active_count = 0
self.waiters = [] self._waiters = []
def attach(self, transport): def _attach(self):
assert self.sockets is not None assert self.sockets is not None
self.active_count += 1 self._active_count += 1
def detach(self, transport): def _detach(self):
assert self.active_count > 0 assert self._active_count > 0
self.active_count -= 1 self._active_count -= 1
if self.active_count == 0 and self.sockets is None: if self._active_count == 0 and self.sockets is None:
self._wakeup() self._wakeup()
def close(self): def close(self):
sockets = self.sockets sockets = self.sockets
if sockets is not None: if sockets is None:
self.sockets = None return
for sock in sockets: self.sockets = None
self.loop._stop_serving(sock) for sock in sockets:
if self.active_count == 0: self._loop._stop_serving(sock)
self._wakeup() if self._active_count == 0:
self._wakeup()
def _wakeup(self): def _wakeup(self):
waiters = self.waiters waiters = self._waiters
self.waiters = None self._waiters = None
for waiter in waiters: for waiter in waiters:
if not waiter.done(): if not waiter.done():
waiter.set_result(waiter) waiter.set_result(waiter)
@coroutine @coroutine
def wait_closed(self): def wait_closed(self):
if self.sockets is None or self.waiters is None: if self.sockets is None or self._waiters is None:
return return
waiter = futures.Future(loop=self.loop) waiter = futures.Future(loop=self._loop)
self.waiters.append(waiter) self._waiters.append(waiter)
yield from waiter yield from waiter
......
...@@ -35,7 +35,7 @@ class _ProactorBasePipeTransport(transports._FlowControlMixin, ...@@ -35,7 +35,7 @@ class _ProactorBasePipeTransport(transports._FlowControlMixin,
self._closing = False # Set when close() called. self._closing = False # Set when close() called.
self._eof_written = False self._eof_written = False
if self._server is not None: if self._server is not None:
self._server.attach(self) self._server._attach()
self._loop.call_soon(self._protocol.connection_made, self) self._loop.call_soon(self._protocol.connection_made, self)
if waiter is not None: if waiter is not None:
# wait until protocol.connection_made() has been called # wait until protocol.connection_made() has been called
...@@ -91,7 +91,7 @@ class _ProactorBasePipeTransport(transports._FlowControlMixin, ...@@ -91,7 +91,7 @@ class _ProactorBasePipeTransport(transports._FlowControlMixin,
self._sock.close() self._sock.close()
server = self._server server = self._server
if server is not None: if server is not None:
server.detach(self) server._detach()
self._server = None self._server = None
def get_write_buffer_size(self): def get_write_buffer_size(self):
......
...@@ -417,7 +417,7 @@ class _SelectorTransport(transports._FlowControlMixin, ...@@ -417,7 +417,7 @@ class _SelectorTransport(transports._FlowControlMixin,
self._conn_lost = 0 # Set when call to connection_lost scheduled. self._conn_lost = 0 # Set when call to connection_lost scheduled.
self._closing = False # Set when close() called. self._closing = False # Set when close() called.
if self._server is not None: if self._server is not None:
self._server.attach(self) self._server._attach()
def abort(self): def abort(self):
self._force_close(None) self._force_close(None)
...@@ -464,7 +464,7 @@ class _SelectorTransport(transports._FlowControlMixin, ...@@ -464,7 +464,7 @@ class _SelectorTransport(transports._FlowControlMixin,
self._loop = None self._loop = None
server = self._server server = self._server
if server is not None: if server is not None:
server.detach(self) server._detach()
self._server = None self._server = None
def get_write_buffer_size(self): def get_write_buffer_size(self):
......
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