12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- from typing import TYPE_CHECKING , Optional
15
+ from abc import ABC
16
16
17
17
from twisted .internet import protocol
18
18
from twisted .internet .interfaces import IAddress
22
22
from hathor .p2p .peer import PrivatePeer
23
23
from hathor .p2p .protocol import HathorLineReceiver
24
24
25
- if TYPE_CHECKING :
26
- from hathor .manager import HathorManager # noqa: F401
27
25
28
- MyServerProtocol = HathorLineReceiver
29
- MyClientProtocol = HathorLineReceiver
30
-
31
-
32
- class HathorServerFactory (protocol .ServerFactory ):
33
- """ HathorServerFactory is used to generate HathorProtocol objects when a new connection arrives.
34
- """
35
-
36
- manager : Optional [ConnectionsManager ]
37
- protocol : type [MyServerProtocol ] = MyServerProtocol
26
+ class _HathorLineReceiverFactory (ABC , protocol .Factory ):
27
+ inbound : bool
38
28
39
29
def __init__ (
40
30
self ,
41
- network : str ,
42
31
my_peer : PrivatePeer ,
43
32
p2p_manager : ConnectionsManager ,
44
33
* ,
@@ -47,56 +36,29 @@ def __init__(
47
36
):
48
37
super ().__init__ ()
49
38
self ._settings = settings
50
- self .network = network
51
39
self .my_peer = my_peer
52
40
self .p2p_manager = p2p_manager
53
41
self .use_ssl = use_ssl
54
42
55
- def buildProtocol (self , addr : IAddress ) -> MyServerProtocol :
56
- assert self .protocol is not None
57
- p = self .protocol (
58
- network = self .network ,
43
+ def buildProtocol (self , addr : IAddress ) -> HathorLineReceiver :
44
+ p = HathorLineReceiver (
59
45
my_peer = self .my_peer ,
60
46
p2p_manager = self .p2p_manager ,
61
47
use_ssl = self .use_ssl ,
62
- inbound = True ,
48
+ inbound = self . inbound ,
63
49
settings = self ._settings
64
50
)
65
51
p .factory = self
66
52
return p
67
53
68
54
69
- class HathorClientFactory ( protocol .ClientFactory ):
70
- """ HathorClientFactory is used to generate HathorProtocol objects when we connected to another peer .
55
+ class HathorServerFactory ( _HathorLineReceiverFactory , protocol .ServerFactory ):
56
+ """ HathorServerFactory is used to generate HathorProtocol objects when a new connection arrives .
71
57
"""
58
+ inbound = True
72
59
73
- protocol : type [MyClientProtocol ] = MyClientProtocol
74
-
75
- def __init__ (
76
- self ,
77
- network : str ,
78
- my_peer : PrivatePeer ,
79
- p2p_manager : ConnectionsManager ,
80
- * ,
81
- settings : HathorSettings ,
82
- use_ssl : bool ,
83
- ):
84
- super ().__init__ ()
85
- self ._settings = settings
86
- self .network = network
87
- self .my_peer = my_peer
88
- self .p2p_manager = p2p_manager
89
- self .use_ssl = use_ssl
90
60
91
- def buildProtocol (self , addr : IAddress ) -> MyClientProtocol :
92
- assert self .protocol is not None
93
- p = self .protocol (
94
- network = self .network ,
95
- my_peer = self .my_peer ,
96
- p2p_manager = self .p2p_manager ,
97
- use_ssl = self .use_ssl ,
98
- inbound = False ,
99
- settings = self ._settings
100
- )
101
- p .factory = self
102
- return p
61
+ class HathorClientFactory (_HathorLineReceiverFactory , protocol .ClientFactory ):
62
+ """ HathorClientFactory is used to generate HathorProtocol objects when we connected to another peer.
63
+ """
64
+ inbound = False
0 commit comments