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
|
#
# 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.
#
require "thread"
require "qpid/queue"
require "qpid/connection08"
require "qpid/fields"
module Qpid08
Queue = Qpid::Queue
class Peer
def initialize(conn, delegate)
@conn = conn
@delegate = delegate
@outgoing = Queue.new()
@work = Queue.new()
@channels = {}
@mutex = Mutex.new()
end
def channel(id)
@mutex.synchronize do
ch = @channels[id]
if ch.nil?
ch = Channel.new(id, self, @outgoing, @conn.spec)
@channels[id] = ch
end
return ch
end
end
def channel_delete(id)
@channels.delete(id)
end
def start()
spawn(:writer)
spawn(:reader)
spawn(:worker)
end
def close()
@mutex.synchronize do
@channels.each_value do |ch|
ch.close()
end
@outgoing.close()
@work.close()
end
end
private
def spawn(method, *args)
Thread.new do
begin
send(method, *args)
# is this the standard way to catch any exception?
rescue Closed => e
puts "#{method} #{e}"
rescue Object => e
print e
e.backtrace.each do |line|
print "\n ", line
end
print "\n"
end
end
end
def reader()
while true
frame = @conn.read()
ch = channel(frame.channel)
ch.dispatch(frame, @work)
end
end
def writer()
while true
@conn.write(@outgoing.get())
end
end
def worker()
while true
dispatch(@work.get())
end
end
def dispatch(queue)
frame = queue.get()
ch = channel(frame.channel)
payload = frame.payload
if payload.method.content?
content = Qpid08::read_content(queue)
else
content = nil
end
message = Message.new(payload.method, payload.args, content)
@delegate.dispatch(ch, message)
end
end
class Channel
def initialize(id, peer, outgoing, spec)
@id = id
@peer = peer
@outgoing = outgoing
@spec = spec
@incoming = Queue.new()
@responses = Queue.new()
@queue = nil
@closed = false
end
attr_reader :id
def closed?; @closed end
def close()
return if closed?
@peer.channel_delete(@id)
@closed = true
@incoming.close()
@responses.close()
end
def dispatch(frame, work)
payload = frame.payload
case payload
when Method
if payload.method.response?
@queue = @responses
else
@queue = @incoming
work << @incoming
end
end
@queue << frame
end
def method_missing(name, *args)
method = @spec.find_method(name)
if method.nil?
raise NoMethodError.new("undefined method '#{name}' for #{self}:#{self.class}")
end
if args.size == 1 and args[0].instance_of? Hash
kwargs = args[0]
invoke_args = method.fields.map do |f|
kwargs[f.name]
end
content = kwargs[:content]
else
invoke_args = []
method.fields.each do |f|
if args.any?
invoke_args << args.shift()
else
invoke_args << f.default
end
end
if method.content? and args.any?
content = args.shift()
else
content = nil
end
if args.any? then raise ArgumentError.new("#{args.size} extr arguments") end
end
return invoke(method, invoke_args, content)
end
def invoke(method, args, content = nil)
raise Closed() if closed?
frame = Frame.new(@id, Method.new(method, args))
@outgoing << frame
if method.content?
content = Content.new() if content.nil?
write_content(method.parent, content, @outgoing)
end
nowait = false
f = method.fields[:"nowait"]
nowait = args[method.fields.index(f)] unless f.nil?
unless nowait or method.responses.empty?
resp = @responses.get().payload
if resp.method.content?
content = read_content(@responses)
else
content = nil
end
if method.responses.include? resp.method
return Message.new(resp.method, resp.args, content)
else
# XXX: ValueError doesn't actually exist
raise ValueError.new(resp)
end
end
end
def write_content(klass, content, queue)
size = content.size
header = Frame.new(@id, Header.new(klass, content.weight, size, content.headers))
queue << header
content.children.each {|child| write_content(klass, child, queue)}
queue << Frame.new(@id, Body.new(content.body)) if size > 0
end
end
def Qpid08.read_content(queue)
frame = queue.get()
header = frame.payload
children = []
1.upto(header.weight) { children << read_content(queue) }
size = header.size
read = 0
buf = ""
while read < size
body = queue.get()
content = body.payload.content
buf << content
read += content.size
end
buf.freeze()
return Content.new(header.properties.clone(), buf, children)
end
class Content
def initialize(headers = {}, body = "", children = [])
@headers = headers
@body = body
@children = children
end
attr_reader :headers, :body, :children
def size; body.size end
def weight; children.size end
def [](key); @headers[key] end
def []=(key, value); @headers[key] = value end
end
class Message
fields(:method, :args, :content)
alias fields args
def method_missing(name)
return args[@method.fields[name].id]
end
def inspect()
"#{method.qname}(#{args.join(", ")})"
end
end
module Delegate
def dispatch(ch, msg)
send(msg.method.qname, ch, msg)
end
end
end
|