-
Notifications
You must be signed in to change notification settings - Fork 1
/
channel.py
209 lines (165 loc) · 6.23 KB
/
channel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import threading
from twisted.web.server import NOT_DONE_YET
from twisted.web.resource import NoResource
from twisted.internet.error import AlreadyCalled, AlreadyCancelled
from helpers import error, success, closed
from response import Data, Ok, Timeout, Closed
_PEER_DISCONNECTED = error(1, 'peer disconnected whilst writing data')
_TIMEOUT = error(2, 'request timed out')
_CHANNEL_CLOSED = error(3, 'channel closed unexpectedly')
class Channel(object):
def __init__(self, name, reactor, timeout_delay):
self.name = name
self.state = Standby(self)
self.lock = threading.Lock()
self._timeout_delay = timeout_delay
self._reactor = reactor
self._timeout_call = None
def write(self, request, data):
with self.lock:
return self.state.write(request, data)
def read(self, request):
with self.lock:
return self.state.read(request)
def disconnect(self, err):
with self.lock:
return self.state.disconnect(err)
def timeout(self):
self.cancel_delayed_timeout()
with self.lock:
self.state.timeout()
def delayed_timeout(self):
if self._timeout_delay > 0:
try:
self._timeout_call.reset(self._timeout_delay)
except (AttributeError, AlreadyCancelled, AlreadyCalled):
# self._timeout_call not yet set or already called/cancelled
self._timeout_call = self._reactor.callLater(
self._timeout_delay, self.timeout)
else:
# Call timeout immediately
self.timeout()
def cancel_delayed_timeout(self):
try:
self._timeout_call.cancel()
except (AttributeError, AlreadyCancelled, AlreadyCalled):
# self._timeout_call not yet set or already called/cancelled
# That's fine, no action needed
pass
def close(self):
self.cancel_delayed_timeout()
with self.lock:
self.state.close()
def __str__(self):
return "Channel '{}' with state: {}".format(self.name, self.state)
class ChannelState(object):
def __init__(self, channel):
self._channel = channel
class Standby(ChannelState):
def __init__(self, channel):
super(Standby, self).__init__(channel)
self._channel.cancel_delayed_timeout()
def write(self, request, data):
self._channel.state = WriteWaiting(self._channel, request, data)
return NOT_DONE_YET
def read(self, request):
self._channel.state = ReadWaiting(self._channel, request)
return NOT_DONE_YET
def disconnect(self, err):
pass
def timeout(self):
pass
def close(self):
pass
def __str__(self):
return 'Standby'
class WriteWaiting(ChannelState):
def __init__(self, channel, write_request, data):
super(WriteWaiting, self).__init__(channel)
# Keep a reference to the writing request
self._write_request = write_request
write_request.notifyFinish().addErrback(self._channel.disconnect)
# Store the written data
self._data = data
# Start the timeout
self._channel.delayed_timeout()
def __str__(self):
return "WriteWaiting '{}'".format(self._data)
def write(self, request, data):
# No state change
return NoResource().render(request)
def read(self, read_request):
data = self._data
# Write to reader
read_request.write(Data(data).render(read_request))
try:
self._write_request.write(Ok().render(self._write_request))
self._write_request.finish()
except RuntimeError:
# Waiting write request disconnected
# Nothing to be done
print("Runtime error!")
self._channel.state = Standby(self._channel)
return ''
def disconnect(self, err):
print('Waiting write request closed')
self._channel.state = Standby(self._channel)
def timeout(self):
print('Timing out waiting write request')
try:
self._write_request.write(Timeout().render(self._write_request))
self._write_request.finish()
except RuntimeError:
# The waiting read request was closed remotely
pass
finally:
self._channel.state = Standby(self._channel)
def close(self):
try:
self._write_request.write(Closed().render(self._write_request))
self._write_request.finish()
except RuntimeError:
# The waiting write request was closed remotely
pass
class ReadWaiting(ChannelState):
def __init__(self, channel, read_request):
super(ReadWaiting, self).__init__(channel)
self._read_request = read_request
read_request.notifyFinish().addErrback(self._channel.disconnect)
self._channel.delayed_timeout()
def __str__(self):
return 'ReadWaiting'
def write(self, write_request, data):
try:
self._read_request.write(Data(data).render(self._read_request))
self._read_request.finish()
except RuntimeError:
# The waiting read request was closed remotely
return _PEER_DISCONNECTED
else:
return Ok().render(write_request)
finally:
self._channel.state = Standby(self._channel)
def read(self, request):
# No state change
return NoResource().render(request)
def disconnect(self, err):
print('Waiting read request closed remotely')
self._channel.state = Standby(self._channel)
def timeout(self):
print('Timing out waiting read request')
try:
self._read_request.write(Timeout().render(self._read_request))
self._read_request.finish()
except RuntimeError:
# The waiting read request was closed remotely
pass
finally:
self._channel.state = Standby(self._channel)
def close(self):
try:
self._read_request.write(Closed().render(self._read_request))
self._read_request.finish()
except RuntimeError:
# The waiting read request was closed remotely
pass