-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.py
293 lines (240 loc) · 8.33 KB
/
session.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
from link import Sender, Receiver
from protocol import Begin, End, Flow
from util import RangeSet, Constant
SLIDING = Constant("SLIDING")
FIXED = Constant("FIXED")
class SessionError(Exception):
pass
class Session:
def __init__(self, factory, properties=None):
self.factory = factory
self.properties = properties
self.remote_channel = None
self.channel = None
self.max_frame_size = None
self.begin_sent = False
self.begin_rcvd = False
self.end_sent = False
self.end_rcvd = False
self.incoming = Incoming()
self.outgoing = Outgoing()
self.next_receiver_id = None
self.roles = {Sender.role: self.outgoing,
Receiver.role: self.incoming}
# link name -> link endpoint
self.links = {}
# handle -> link endpoint
self.handles = {}
self.output = []
def write(self, body):
self.dispatch(body)
# XXX: dup of read in framing
def read(self, n=None):
result = self.output[:n]
del self.output[:n]
return result
def dispatch(self, body):
getattr(self, "do_%s" % body.NAME, self.unhandled)(body)
def unhandled(self, body):
link = self.handles[body.handle]
link.write(body)
def post_frame(self, body):
assert self.begin_sent and not self.end_sent
self.output.append(body)
def beginning(self):
return self.begin_rcvd and not self.begin_sent
def ending(self):
return self.end_rcvd and not self.end_sent
def begin(self):
if self.begin_sent:
raise SessionError("already begun")
self.begin_sent = True
self.post_frame(Begin(remote_channel = self.remote_channel,
next_outgoing_id = self.outgoing.unsettled_hwm + 1,
incoming_window = self.incoming.window,
outgoing_window = 65536, # this should NOT be self.outgoing.window
handle_max = 2147483647,
properties = self.properties))
def do_begin(self, begin):
self.begin_rcvd = True
self.incoming.transfer_count = begin.next_outgoing_id - 1
self.outgoing.max_id = self.outgoing.transfer_count + begin.incoming_window - 1
def end(self, error=None):
if self.end_sent:
raise SessionError("already ended")
# process any outstanding work before ending
self.tick()
self.post_frame(End())
self.end_sent = True
def do_end(self, det):
self.end_rcvd = True
def add(self, link):
link.session = self
self.links[link.name] = link
def allocate_handle(self):
return max([-1] + [l.handle for l in self.links.values()]) + 1
def remove(self, link):
# process any outstanding work before removing
self.tick()
if link.handle is not None:
raise SessionError("link is attached")
if link.name in self.links and self.links[link.name] == link:
del self.links[link.name]
link.session = None
else:
raise SessionError("no such link")
def do_attach(self, attach):
if attach.handle in self.handles:
raise SessionError("double attach")
if self.links.has_key(attach.name):
link = self.links[attach.name]
else:
link = self.factory(attach)
self.add(link)
self.handles[attach.handle] = link
link.write(attach)
def do_detach(self, detach):
if detach.handle not in self.handles:
raise SessionError("double detach")
link = self.handles.pop(detach.handle)
link.write(detach)
def do_disposition(self, disp):
role = self.roles[not disp.role]
start = max(role.unsettled_lwm, disp.first)
if disp.last is None:
last = start + 1
else:
last = disp.last + 1
for id in range(start, last):
delivery = role.get_delivery(id)
if delivery:
link, tag = delivery
link.do_disposition(tag, disp.state, disp.settled)
def do_flow(self, flow):
if flow.next_incoming_id is None:
start = Outgoing.initial_transfer
else:
start = flow.next_incoming_id
self.outgoing.window = start + flow.incoming_window - self.outgoing.unsettled_hwm - 1
if flow.handle is not None:
link = self.handles[flow.handle]
link.write(flow)
def incoming_window(self):
return self.incoming.window
def set_incoming_window(self, window, policy=SLIDING):
old = self.incoming.window
self.incoming.window = window
self.incoming.policy = policy
if window != old and self.begin_sent and not self.end_sent:
if self.incoming.unsettled_hwm is None:
next = None
else:
next = self.incoming.unsettled_hwm + 1
self.post_frame(Flow(next_outgoing_id = self.outgoing.unsettled_hwm + 1,
outgoing_window = self.outgoing.window,
next_incoming_id = next,
incoming_window = self.incoming.window))
def tick(self):
for link in self.links.values():
link.tick()
def update_next_receiver(self, xfr):
if xfr.delivery_id == self.next_receiver_id:
self.next_receiver_id += 1
def next_receiver(self):
if self.next_receiver_id is None or self.next_receiver_id > self.incoming.unsettled_hwm:
return None
while True:
d = self.incoming.get_delivery(self.next_receiver_id)
if d:
return d[0]
elif self.next_receiver_id < self.unsettled_hwm:
self.next_receiver_id += 1
else:
return None
class DeliveryMap:
def __init__(self):
# (link, delivery_tag) -> delivery_id
self.aliases = {}
# (delivery_id - unsettled_lwm) -> (link, delivery_tag)
self.deliveries = []
# lowest unsettled delivery_id
self.unsettled_lwm = None
# highest unsettled delivery_id
self.unsettled_hwm = None
self.transfer_count = None
self.window = None
self.policy = SLIDING
self.init()
def capacity(self):
return self.window
def append(self, link, transfer):
self.window -= 1
delivery = (link, transfer.delivery_tag)
if delivery in self.aliases:
# XXX
transfer.delivery_id = self.unsettled_hwm
else:
self.mark(transfer)
self.aliases[delivery] = transfer.delivery_id
self.deliveries.append(delivery)
self.transfer_count += 1
def settle(self, link, delivery_tag):
delivery = (link, delivery_tag)
id = self.aliases.pop(delivery)
idx = id - self.unsettled_lwm
assert self.deliveries[idx] == delivery
self.deliveries[idx] = None
while self.deliveries:
if self.deliveries[0] is None:
self.deliveries.pop(0)
self.unsettled_lwm += 1
else:
break
def get_delivery(self, delivery_id):
return self.deliveries[delivery_id - self.unsettled_lwm]
def __repr__(self):
return "%s(%r, %r, %s, %s)" % (self.__class__, self.aliases, self.deliveries,
self.unsettled_lwm, self.unsettled_hwm)
class Incoming(DeliveryMap):
def init(self):
self.window = 65536
def append(self, link, transfer):
DeliveryMap.append(self, link, transfer)
if self.policy is SLIDING:
self.window += 1
def mark(self, transfer):
if self.unsettled_lwm is None:
self.unsettled_lwm = transfer.delivery_id
else:
assert transfer.delivery_id == self.unsettled_hwm + 1
self.unsettled_hwm = transfer.delivery_id
class Outgoing(DeliveryMap):
initial_delivery = 1
initial_transfer = 1
def init(self):
self.unsettled_lwm = self.initial_delivery
self.unsettled_hwm = self.initial_delivery - 1
self.transfer_count = self.initial_transfer
self.window = 0
def mark(self, transfer):
assert transfer.delivery_id is None
self.unsettled_hwm += 1
transfer.delivery_id = self.unsettled_hwm