# Coverage for server/parts/serve.py : 100%

## 26 statements 26 run 0 missing 4 excluded 0 partial

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1# -*- coding: utf-8 -*-

2 from __future__ import annotations

3

4# -- stdlib --

5 from typing import Any, TYPE_CHECKING

6 import logging

7 import urllib.parse

8

9# -- third party --

10 from gevent.server import StreamServer

11 import gevent

12

13# -- own --

14# -- typing --

15 if TYPE_CHECKING:

16 from server.core import Core # noqa: F401

17

18

19# -- code --

20 log = logging.getLogger('Interconnect')

21

22

23 class Serve(object):

24 def __init__(self, core: Core):

25 self.core = core

26 core.tasks['serve/accept'] = self.accept

27

28 def __repr__(self) -> str:

29 return self.__class__.__name__

30

31 def accept(self) -> None:

32 core = self.core

33 a = urllib.parse.urlparse(core.options.listen)

34 assert a.scheme == 'tcp'

35 server = StreamServer((a.hostname, a.port), self._serve, None)

36 server.serve_forever()

37

38 # ----- Methods -----

39 def _serve(self, sock: Any, addr: Any) -> None:

40 core = self.core

41

42 from endpoint import Endpoint

43 from server.endpoint import Client

44

45 ep = Endpoint(sock, addr)

46 cli = Client(core, ep)

47

48 gr = gevent.getcurrent()

49 gr.gr_name = repr(cli)

50

51 cli.serve()