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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
#
# 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.
#
module Qpid
def self.struct(type, *args)
# FIXME: This is fragile; the last arg could be a hash,
# without being hte keywords
kwargs = {}
kwargs = args.pop if args.any? && args[-1].is_a?(Hash)
if args.size > type.fields.size
raise TypeError,
"%s() takes at most %d arguments (%d given)" %
[type.name, type.fields.size, args.size]
end
attrs = type.fields.inject({}) do |attrs, field|
if args.any?
attrs[field.name] = args.shift
if kwargs.key?(field.name)
raise TypeError,
"%s() got multiple values for keyword argument '%s'" %
[type.name, field.name]
end
elsif kwargs.key?(field.name)
attrs[field.name] = kwargs.delete(field.name)
else
attrs[field.name] = field.default
end
attrs
end
unless kwargs.empty?
unexpected = kwargs.keys[0]
raise TypeError,
"%s() got an unexpected keyword argument '%s'" %
[type.name, unexpected]
end
attrs[:st_type] = type
attrs[:id] = nil
name = "Qpid_" + type.name.to_s.capitalize
unless ::Struct.const_defined?(name)
vars = type.fields.collect { |f| f.name } << :st_type << :id
::Struct.new(name, *vars)
end
st = ::Struct.const_get(name)
result = st.new
attrs.each { |k, v| result[k] = v }
return result
end
class Message
attr_accessor :headers, :body, :id
def initialize(*args)
@body = nil
@headers = nil
@body = args.pop unless args.empty?
@headers = args unless args.empty?
@id = nil
end
def has(name)
return ! get(name).nil?
end
def get(name)
if @headers
name = name.to_sym
@headers.find { |h| h.st_type.name == name }
end
end
def set(header)
@headers ||= []
if h = @headers.find { |h| h.st_type == header.st_type }
ind = @headers.index(h)
@headers[ind] = header
else
@headers << header
end
end
def clear(name)
if @headers
name = name.to_sym
@headers.delete_if { |h| h.st_type.name == name }
end
end
# FIXME: Not sure what to do here
# Ruby doesn't have a notion of a evaluable string representation
# def __repr__(self):
# args = []
# if self.headers:
# args.extend(map(repr, self.headers))
# if self.body:
# args.append(repr(self.body))
# if self.id is not None:
# args.append("id=%s" % self.id)
# return "Message(%s)" % ", ".join(args)
# end
end
class ::Object
def to_serial
Qpid::Serial.new(self)
end
end
class Serial
include Comparable
attr_accessor :value
def initialize(value)
@value = value & 0xFFFFFFFF
end
def hash
@value.hash
end
def to_serial
self
end
def eql?(other)
other = other.to_serial
value.eql?(other.value)
end
def <=>(other)
return 1 if other.nil?
other = other.to_serial
delta = (value - other.value) & 0xFFFFFFFF
neg = delta & 0x80000000
mag = delta & 0x7FFFFFFF
return (neg>0) ? -mag : mag
end
def +(other)
result = other.to_serial
result.value += value
return result
end
def -(other)
result = other.to_serial
result.value = value - result.value
return result
end
def succ
Serial.new(value + 1)
end
# FIXME: Not sure what to do here
# Ruby doesn't have a notion of a evaluable string representation
# def __repr__(self):
# return "serial(%s)" % self.value
# end
def to_s
value.to_s
end
end
# The Python class datatypes.Range is emulated by the standard
# Range class with a few additions
class ::Range
alias :lower :begin
alias :upper :end
def touches(r)
# XXX: are we doing more checks than we need?
return (r.include?(lower - 1) ||
r.include?(upper + 1) ||
include?(r.lower - 1) ||
include?(r.upper + 1) ||
r.include?(lower) ||
r.include?(upper) ||
include?(r.lower) ||
include?(r.upper))
end
def span(r)
Range.new([lower, r.lower].min, [upper, r.upper].max)
end
def intersect(r)
l = [lower, r.lower].max
u = [upper, r.upper].min
return l > u ? nil : Range.new(l, u)
end
end
class RangedSet
include Enumerable
attr_accessor :ranges
def initialize(*args)
@ranges = []
args.each { |n| add(n) }
end
def each(&block)
ranges.each { |r| yield(r) }
end
def include?(n)
if (n.is_a?(Range))
super(n)
else
ranges.find { |r| r.include?(n) }
end
end
def add_range(range)
ranges.delete_if do |r|
if range.touches(r)
range = range.span(r)
true
else
false
end
end
ranges << range
end
def add(lower, upper = nil)
upper = lower if upper.nil?
add_range(Range.new(lower, upper))
end
def to_s
repr = ranges.sort { |a,b| b.lower <=> a.lower }.
map { |r| r.to_s }.join(",")
"<RangedSet: {#{repr}}"
end
end
class Future
def initialize(initial=nil, exception=Exception)
@value = initial
@error = nil
@set = Util::Event.new
@exception = exception
end
def error(error)
@error = error
@set.set
end
def set(value)
@value = value
@set.set
end
def get(timeout=nil)
@set.wait(timeout)
unless @error.nil?
raise @exception.new(@error)
end
@value
end
end
class UUID
include Comparable
attr_accessor :bytes
def initialize(bytes)
@bytes = bytes
end
def <=>(other)
if other.respond_to?(:bytes)
return bytes <=> other.bytes
else
raise NotImplementedError
end
end
def to_s
UUID::format(bytes)
end
# FIXME: Not sure what to do here
# Ruby doesn't have a notion of a evaluable string representation
# def __repr__(self):
# return "UUID(%r)" % str(self)
# end
def self.random_uuid
bytes = (1..16).collect { |i| rand(256) }
# From RFC4122, the version bits are set to 0100
bytes[7] &= 0x0F
bytes[7] |= 0x40
# From RFC4122, the top two bits of byte 8 get set to 01
bytes[8] &= 0x3F
bytes[8] |= 0x80
return bytes.pack("C16")
end
def self.uuid4
UUID.new(random_uuid)
end
def self.format(s)
# Python format !LHHHHL
# big-endian, ulong, ushort x 4, ulong
"%08x-%04x-%04x-%04x-%04x%08x" % s.unpack("NnnnnN")
end
end
end
|