rustorb 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,405 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bigdecimal"
4
+
5
+ # The TypeCode-driven writer and reader: every value (an argument, a
6
+ # result, the value of an any) goes through write / read with its
7
+ # TypeCode. A value that does not fit its type is CORBA::MARSHAL
8
+ # (COMPLETED_NO), as RCLM says.
9
+ #
10
+ # Licensed under MIT OR Apache-2.0, like the rest of the rustorb core.
11
+
12
+ module Rustorb
13
+ module Marshal
14
+ # rorb_out_write_array kinds and pack formats (host representation).
15
+ PRIM = {
16
+ CORBA::TK_SHORT => [1, "s*", -(2**15), 2**15 - 1],
17
+ CORBA::TK_USHORT => [2, "S*", 0, 2**16 - 1],
18
+ CORBA::TK_LONG => [3, "l*", -(2**31), 2**31 - 1],
19
+ CORBA::TK_ULONG => [4, "L*", 0, 2**32 - 1],
20
+ CORBA::TK_LONGLONG => [5, "q*", -(2**63), 2**63 - 1],
21
+ CORBA::TK_ULONGLONG => [6, "Q*", 0, 2**64 - 1],
22
+ CORBA::TK_FLOAT => [7, "f*", nil, nil],
23
+ CORBA::TK_DOUBLE => [8, "d*", nil, nil],
24
+ CORBA::TK_OCTET => [0, "C*", 0, 255]
25
+ }.freeze
26
+
27
+ MIN_SIZE = {
28
+ CORBA::TK_SHORT => 2, CORBA::TK_USHORT => 2, CORBA::TK_WCHAR => 2, CORBA::TK_LONG => 4,
29
+ CORBA::TK_ULONG => 4, CORBA::TK_FLOAT => 4, CORBA::TK_ENUM => 4, CORBA::TK_STRING => 4,
30
+ CORBA::TK_WSTRING => 4, CORBA::TK_SEQUENCE => 4, CORBA::TK_LONGLONG => 8,
31
+ CORBA::TK_ULONGLONG => 8, CORBA::TK_DOUBLE => 8, CORBA::TK_LONGDOUBLE => 16,
32
+ CORBA::TK_OBJREF => 4, CORBA::TK_TYPECODE => 4, CORBA::TK_ANY => 4
33
+ }.freeze
34
+
35
+ MASK64 = 2**64 - 1
36
+
37
+ module_function
38
+
39
+ def bad(why)
40
+ raise CORBA::MARSHAL.new(why, 0, CORBA::COMPLETED_NO)
41
+ end
42
+
43
+ def int(v, lo, hi, what)
44
+ bad("#{what} expected, got #{v.inspect}") unless v.is_a?(Integer)
45
+ bad("#{v} out of range for #{what}") if v < lo || v > hi
46
+ v
47
+ end
48
+
49
+ def float(v)
50
+ bad("a number expected, got #{v.inspect}") unless v.is_a?(Numeric)
51
+ v.to_f
52
+ end
53
+
54
+ def char_code(v, max, what)
55
+ code = case v
56
+ when Integer then v
57
+ when String
58
+ bad("one character expected, got #{v.inspect}") unless v.length == 1
59
+ v.ord
60
+ else bad("#{what} expected, got #{v.inspect}")
61
+ end
62
+ bad("#{v.inspect} is not a #{what}") if code.negative? || code > max
63
+ code
64
+ end
65
+
66
+ # A wstring as UTF-16LE code units.
67
+ def wstring_units(v)
68
+ case v
69
+ when String then v.encode(Encoding::UTF_16LE).b
70
+ when Array
71
+ v.each { |c| int(c, 0, 0xFFFF, "a UTF-16 code unit") }
72
+ v.pack("v*")
73
+ else bad("a wstring (Array of Integer or String) expected, got #{v.inspect}")
74
+ end
75
+ end
76
+
77
+ # The union label value as rorb_tc_union takes it.
78
+ def label_bits(label, disc)
79
+ case disc.unaliased.kind
80
+ when CORBA::TK_BOOLEAN then label ? 1 : 0
81
+ when CORBA::TK_CHAR, CORBA::TK_WCHAR then label.is_a?(String) ? label.ord : label
82
+ else label & MASK64
83
+ end
84
+ end
85
+
86
+ def write(out, tc, v)
87
+ tc = tc.resolve
88
+ return write_extra(out, tc, v) if tc._extra
89
+
90
+ case tc.kind
91
+ when CORBA::TK_NULL, CORBA::TK_VOID then nil
92
+ when CORBA::TK_SHORT then out.write_short(int(v, -(2**15), 2**15 - 1, "short"))
93
+ when CORBA::TK_LONG then out.write_long(int(v, -(2**31), 2**31 - 1, "long"))
94
+ when CORBA::TK_USHORT then out.write_ushort(int(v, 0, 2**16 - 1, "unsigned short"))
95
+ when CORBA::TK_ULONG then out.write_ulong(int(v, 0, 2**32 - 1, "unsigned long"))
96
+ when CORBA::TK_LONGLONG then out.write_longlong(int(v, -(2**63), 2**63 - 1, "long long"))
97
+ when CORBA::TK_ULONGLONG then out.write_ulonglong(int(v, 0, MASK64, "unsigned long long"))
98
+ when CORBA::TK_FLOAT then out.write_float(float(v))
99
+ when CORBA::TK_DOUBLE then out.write_double(float(v))
100
+ when CORBA::TK_BOOLEAN
101
+ bad("true or false expected, got #{v.inspect}") unless [true, false].include?(v)
102
+ out.write_boolean(v)
103
+ when CORBA::TK_CHAR then out.write_char(char_code(v, 255, "char"))
104
+ when CORBA::TK_WCHAR then out.write_wchar(char_code(v, 0xFFFF, "wchar"))
105
+ when CORBA::TK_OCTET then out.write_octet(int(v, 0, 255, "octet"))
106
+ when CORBA::TK_STRING
107
+ bad("a String expected, got #{v.inspect}") unless v.is_a?(String)
108
+ bound = tc.length
109
+ bad("string longer than its bound #{bound}") if bound.positive? && v.length > bound
110
+ out.write_string(v)
111
+ when CORBA::TK_WSTRING
112
+ units = wstring_units(v)
113
+ bound = tc.length
114
+ bad("wstring longer than its bound #{bound}") if bound.positive? && units.bytesize / 2 > bound
115
+ out.write_wstring(units)
116
+ when CORBA::TK_ENUM
117
+ count = tc._enumerators.size
118
+ out.write_ulong(int(v, 0, count - 1, "enumerator of #{tc.name}"))
119
+ when CORBA::TK_STRUCT, CORBA::TK_EXCEPT
120
+ members = tc._struct_members
121
+ bad("#{tc.name} expected, got #{v.inspect}") unless v.respond_to?(:_values)
122
+ values = v._values
123
+ check_ranges(v.class, values) if v.class.const_defined?(:RANGES) && !v.class::RANGES.empty?
124
+ members.each_with_index { |m, i| write(out, m[1], values[i]) }
125
+ when CORBA::TK_UNION then write_union(out, tc, v)
126
+ when CORBA::TK_SEQUENCE
127
+ bound = tc.length
128
+ n = v.is_a?(String) ? v.bytesize : (v.respond_to?(:size) ? v.size : bad("an Array expected, got #{v.inspect}"))
129
+ bad("sequence longer than its bound #{bound}") if bound.positive? && n > bound
130
+ out.write_ulong(n)
131
+ write_elements(out, tc.content_type, v)
132
+ when CORBA::TK_ARRAY
133
+ n = v.is_a?(String) ? v.bytesize : (v.respond_to?(:size) ? v.size : bad("an Array expected, got #{v.inspect}"))
134
+ bad("array of #{tc.length} elements expected, got #{n}") unless n == tc.length
135
+ write_elements(out, tc.content_type, v)
136
+ when CORBA::TK_ALIAS then write(out, tc.content_type, v)
137
+ when CORBA::TK_LOCAL_INTERFACE
138
+ raise CORBA::MARSHAL.new("#{tc.id} is a local interface", 4, CORBA::COMPLETED_NO)
139
+ when CORBA::TK_VALUE, CORBA::TK_EVENT then Rustorb::Values.write(out, v)
140
+ when CORBA::TK_VALUE_BOX then Rustorb::Values.write_box(out, tc, v)
141
+ when CORBA::TK_ABSTRACT_INTERFACE then Rustorb::Values.write_abstract(out, v)
142
+ when CORBA::TK_OBJREF
143
+ bad("an object reference expected, got #{v.inspect}") unless v.nil? || v.is_a?(CORBA::Object)
144
+ out.write_object(v&._native_ref)
145
+ when CORBA::TK_TYPECODE
146
+ bad("a TypeCode expected, got #{v.inspect}") unless v.is_a?(CORBA::TypeCode)
147
+ out.write_typecode(v.resolve.native)
148
+ when CORBA::TK_ANY
149
+ any = v.is_a?(CORBA::Any) ? v : CORBA::Any.to_any(v)
150
+ out.write_any(any._native)
151
+ when CORBA::TK_LONGDOUBLE
152
+ ld = v.is_a?(CORBA::LongDouble) ? v : CORBA::LongDouble.new(float(v))
153
+ out.write_longdouble(ld.hi, ld.lo)
154
+ when CORBA::TK_FIXED then write_fixed(out, tc, v)
155
+ else
156
+ raise CORBA::NO_IMPLEMENT.new("TypeCode kind #{tc.kind} is not supported by rustorb's Ruby binding yet")
157
+ end
158
+ end
159
+
160
+ # IDL 4 int8 and bitsets (rustorb), sent as integers.
161
+ def write_extra(out, tc, v)
162
+ extra = tc._extra
163
+ if extra == :int8
164
+ out.write_octet(int(v, -128, 127, "int8") & 0xFF)
165
+ else
166
+ bad("#{extra} expected, got #{v.inspect}") unless v.is_a?(extra)
167
+ write(out, CORBA::TypeCode.simple(tc.kind), v._bits)
168
+ end
169
+ end
170
+
171
+ def read_extra(inp, tc)
172
+ extra = tc._extra
173
+ if extra == :int8
174
+ v = inp.read_octet
175
+ v >= 128 ? v - 256 : v
176
+ else
177
+ extra._from_bits(read(inp, CORBA::TypeCode.simple(tc.kind)))
178
+ end
179
+ end
180
+
181
+ # @range / @min / @max of a struct's members: BAD_PARAM out of them.
182
+ def check_ranges(klass, values)
183
+ klass::RANGES.each do |m, (lo, hi)|
184
+ v = values[klass::MEMBERS.index(m)]
185
+ next unless v.is_a?(Numeric)
186
+ next unless (lo && v < lo) || (hi && v > hi)
187
+
188
+ raise CORBA::BAD_PARAM.new("#{klass.name}##{m} #{v} is out of its @range", 0, CORBA::COMPLETED_NO)
189
+ end
190
+ end
191
+
192
+ # The members of a struct as read: those of the generated class when
193
+ # there is one (a TypeCode from the wire, as in an any, does not say
194
+ # which octets are int8 or which integers bitsets).
195
+ def struct_members(tc, klass)
196
+ members = tc._struct_members
197
+ return members unless klass.respond_to?(:_tc)
198
+
199
+ own = klass._tc.resolve
200
+ own.kind == tc.kind && own._struct_members.size == members.size ? own._struct_members : members
201
+ end
202
+
203
+ def in_ranges?(klass, values)
204
+ klass::RANGES.all? do |m, (lo, hi)|
205
+ v = values[klass::MEMBERS.index(m)]
206
+ !v.is_a?(Numeric) || !((lo && v < lo) || (hi && v > hi))
207
+ end
208
+ end
209
+
210
+ def write_elements(out, content, v)
211
+ c = content.unaliased
212
+ if c._extra == :int8 && v.is_a?(Array)
213
+ v.each { |e| int(e, -128, 127, "int8") }
214
+ out.write_array(0, v.pack("c*"))
215
+ return
216
+ end
217
+ prim = !c._extra && PRIM[c.kind]
218
+ if prim
219
+ if c.kind == CORBA::TK_OCTET && v.is_a?(String)
220
+ out.write_array(0, v.b)
221
+ return
222
+ end
223
+ bad("an Array expected, got #{v.inspect}") unless v.is_a?(Array)
224
+ if prim[2]
225
+ v.each { |e| int(e, prim[2], prim[3], c.kind == CORBA::TK_OCTET ? "octet" : "integer") }
226
+ else
227
+ v.each { |e| float(e) }
228
+ end
229
+ out.write_array(prim[0], v.pack(prim[1]))
230
+ elsif c.kind == CORBA::TK_CHAR && v.is_a?(String)
231
+ v.each_char { |ch| write(out, c, ch) }
232
+ else
233
+ bad("an Array expected, got #{v.inspect}") unless v.respond_to?(:each)
234
+ v.each { |e| write(out, content, e) }
235
+ end
236
+ end
237
+
238
+ def union_member(tc, disc)
239
+ default = nil
240
+ tc._union_members.each do |m|
241
+ if m[0] == :default
242
+ default = m
243
+ elsif m[0] == disc
244
+ return m
245
+ end
246
+ end
247
+ default
248
+ end
249
+
250
+ def write_union(out, tc, v)
251
+ bad("#{tc.name} expected, got #{v.inspect}") unless v.respond_to?(:_disc)
252
+ disc_tc = tc.discriminator_type
253
+ disc = v._disc
254
+ write(out, disc_tc, disc)
255
+ m = union_member(tc, disc)
256
+ write(out, m[2], v._value) if m
257
+ end
258
+
259
+ def write_fixed(out, tc, v)
260
+ digits = tc.fixed_digits
261
+ scale = tc.fixed_scale
262
+ d = case v
263
+ when BigDecimal then v
264
+ when Integer then BigDecimal(v)
265
+ when String, Float then BigDecimal(v.to_s)
266
+ else bad("a BigDecimal expected, got #{v.inspect}")
267
+ end
268
+ bad("#{v} is not finite") unless d.finite?
269
+ unscaled = d * (BigDecimal(10)**scale)
270
+ bad("#{d.to_s('F')} has more than #{scale} decimal places") unless unscaled.frac.zero?
271
+ text = unscaled.abs.to_i.to_s
272
+ bad("#{d.to_s('F')} does not fit fixed<#{digits},#{scale}>") if text.size > digits
273
+ text = text.rjust(digits, "0")
274
+ out.write_fixed(text.bytes.map { |b| b - 48 }.pack("C*"), d.negative?)
275
+ end
276
+
277
+ def min_size(tc)
278
+ MIN_SIZE[tc.unaliased.kind] || 1
279
+ end
280
+
281
+ def read(inp, tc)
282
+ tc = tc.resolve
283
+ return read_extra(inp, tc) if tc._extra
284
+
285
+ case tc.kind
286
+ when CORBA::TK_NULL, CORBA::TK_VOID then nil
287
+ when CORBA::TK_SHORT then inp.read_short
288
+ when CORBA::TK_LONG then inp.read_long
289
+ when CORBA::TK_USHORT then inp.read_ushort
290
+ when CORBA::TK_ULONG then inp.read_ulong
291
+ when CORBA::TK_LONGLONG then inp.read_longlong
292
+ when CORBA::TK_ULONGLONG then inp.read_ulonglong
293
+ when CORBA::TK_FLOAT then inp.read_float
294
+ when CORBA::TK_DOUBLE then inp.read_double
295
+ when CORBA::TK_BOOLEAN then inp.read_boolean
296
+ when CORBA::TK_CHAR then inp.read_char.chr(Encoding::UTF_8)
297
+ when CORBA::TK_WCHAR then inp.read_wchar
298
+ when CORBA::TK_OCTET then inp.read_octet
299
+ when CORBA::TK_STRING
300
+ s = inp.read_string
301
+ inp.fail if tc.length.positive? && s.length > tc.length
302
+ s
303
+ when CORBA::TK_WSTRING
304
+ units = inp.read_wstring.unpack("v*")
305
+ inp.fail if tc.length.positive? && units.size > tc.length
306
+ units
307
+ when CORBA::TK_ENUM
308
+ v = inp.read_ulong
309
+ inp.fail if v >= tc._enumerators.size
310
+ v
311
+ when CORBA::TK_STRUCT, CORBA::TK_EXCEPT
312
+ klass = Rustorb.find_type(tc.id)
313
+ values = struct_members(tc, klass).map { |m| read(inp, m[1]) }
314
+ inp.fail if klass && klass.const_defined?(:RANGES) && !klass::RANGES.empty? && !in_ranges?(klass, values)
315
+ klass ? klass.new(*values) : GenericStruct.new(tc, values)
316
+ when CORBA::TK_UNION
317
+ disc = read(inp, tc.discriminator_type)
318
+ m = union_member(tc, disc)
319
+ value = m ? read(inp, m[2]) : nil
320
+ klass = Rustorb.find_type(tc.id) || GenericUnion
321
+ u = klass == GenericUnion ? GenericUnion.new(tc) : klass.new
322
+ u._set(disc, value)
323
+ u
324
+ when CORBA::TK_SEQUENCE
325
+ content = tc.content_type
326
+ n = inp.read_length(min_size(content))
327
+ inp.fail if tc.length.positive? && n > tc.length
328
+ read_elements(inp, content, n)
329
+ when CORBA::TK_ARRAY then read_elements(inp, tc.content_type, tc.length)
330
+ when CORBA::TK_ALIAS then read(inp, tc.content_type)
331
+ when CORBA::TK_OBJREF then CORBA::Object._from_ref(inp.read_object, tc)
332
+ when CORBA::TK_VALUE, CORBA::TK_EVENT then Rustorb::Values.read(inp, tc.id)
333
+ when CORBA::TK_VALUE_BOX then Rustorb::Values.read_box(inp, tc)
334
+ when CORBA::TK_ABSTRACT_INTERFACE then Rustorb::Values.read_abstract(inp, tc)
335
+ when CORBA::TK_LOCAL_INTERFACE
336
+ raise CORBA::MARSHAL.new("#{tc.id} is a local interface", 4, CORBA::COMPLETED_NO)
337
+ when CORBA::TK_TYPECODE then CORBA::TypeCode._wrap(inp.read_typecode)
338
+ when CORBA::TK_ANY
339
+ native = inp.read_any
340
+ native && CORBA::Any._from_native(native)
341
+ when CORBA::TK_LONGDOUBLE
342
+ hi, lo = inp.read_longdouble
343
+ CORBA::LongDouble._from_bits(hi, lo)
344
+ when CORBA::TK_FIXED
345
+ digits, negative = inp.read_fixed(tc.fixed_digits)
346
+ text = digits.bytes.map { |b| (b + 48).chr }.join
347
+ d = BigDecimal(text.empty? ? "0" : text) / (BigDecimal(10)**tc.fixed_scale)
348
+ negative ? -d : d
349
+ else
350
+ raise CORBA::NO_IMPLEMENT.new("TypeCode kind #{tc.kind} is not supported by rustorb's Ruby binding yet")
351
+ end
352
+ end
353
+
354
+ def read_elements(inp, content, n)
355
+ c = content.unaliased
356
+ return inp.read_array(0, n).unpack("c*") if c._extra == :int8
357
+
358
+ prim = !c._extra && PRIM[c.kind]
359
+ return inp.read_array(prim[0], n).unpack(prim[1]) if prim
360
+
361
+ Array.new(n) { read(inp, content) }
362
+ end
363
+ end
364
+
365
+ # A struct or exception value of a type without a Ruby class here (in an
366
+ # any from elsewhere).
367
+ class GenericStruct
368
+ attr_reader :_tc, :_values
369
+
370
+ def initialize(tc, values)
371
+ @_tc = tc
372
+ @_values = values
373
+ end
374
+
375
+ def [](name)
376
+ i = @_tc._struct_members.index { |m| m[0] == name.to_s }
377
+ i && @_values[i]
378
+ end
379
+
380
+ def ==(other)
381
+ other.is_a?(GenericStruct) && other._tc.equal?(@_tc) && other._values == @_values
382
+ end
383
+
384
+ def inspect
385
+ "#<#{@_tc.name} #{@_tc._struct_members.map(&:first).zip(@_values).map { |n, v| "#{n}=#{v.inspect}" }.join(' ')}>"
386
+ end
387
+ end
388
+
389
+ class GenericUnion
390
+ attr_reader :_tc, :_disc, :_value
391
+
392
+ def initialize(tc)
393
+ @_tc = tc
394
+ end
395
+
396
+ def _set(disc, value)
397
+ @_disc = disc
398
+ @_value = value
399
+ end
400
+
401
+ def ==(other)
402
+ other.is_a?(GenericUnion) && other._disc == @_disc && other._value == @_value
403
+ end
404
+ end
405
+ end
@@ -0,0 +1,207 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Callback AMI (docs/ami.md): Messaging::ReplyHandler, the base of the
4
+ # AMI_<I>Handler interfaces rustorb-idl -bruby -Wbami writes,
5
+ # Messaging::ExceptionHolder, and the sendc_ requests of the stubs
6
+ # (CORBA::Object#_sendc, on rorb_object_sendc).
7
+ #
8
+ # Licensed under MIT OR Apache-2.0, like the rest of the rustorb core.
9
+
10
+ module Messaging
11
+ module ReplyHandler
12
+ ID = "IDL:omg.org/Messaging/ReplyHandler:1.0"
13
+ extend ::Rustorb::InterfaceModule
14
+
15
+ def self._tc
16
+ @_tc ||= ::CORBA::TypeCode.create_interface_tc(ID, "ReplyHandler")
17
+ end
18
+
19
+ def self._ops = {}.freeze
20
+
21
+ ::Rustorb.stub_class(self)
22
+ end
23
+
24
+ # The exception a handler's <op>_excep gets. raise_exception raises it;
25
+ # raise_exception_with_list(tcs) raises a user exception only when its
26
+ # TypeCode is in tcs (else UNKNOWN). The state (is_system_exception,
27
+ # byte_order, marshaled_exception: the exception in CDR, its repository id
28
+ # first) is what is sent, so a handler in another process gets it too.
29
+ class ExceptionHolder
30
+ include ::CORBA::ValueBase
31
+ ID = "IDL:omg.org/Messaging/ExceptionHolder:1.0"
32
+ TRUNCATABLE_IDS = [ID].freeze
33
+ MEMBERS = %w[is_system_exception byte_order marshaled_exception].freeze
34
+ attr_accessor :is_system_exception, :byte_order, :marshaled_exception
35
+
36
+ def self._tc
37
+ ::Rustorb.lazy_tc(self, ID) do
38
+ ::CORBA::TypeCode.create_value_tc(ID, "ExceptionHolder", ::CORBA::VM_NONE, nil, [
39
+ ["is_system_exception", ::CORBA._tc_boolean, ::CORBA::PRIVATE_MEMBER],
40
+ ["byte_order", ::CORBA._tc_boolean, ::CORBA::PRIVATE_MEMBER],
41
+ ["marshaled_exception", ::CORBA::TypeCode.create_sequence_tc(0, ::CORBA._tc_octet),
42
+ ::CORBA::PRIVATE_MEMBER]
43
+ ])
44
+ end
45
+ end
46
+
47
+ UNKNOWN_MINOR = 0x4f4d0001
48
+ LITTLE = [1].pack("s") == [1].pack("s<")
49
+
50
+ # A holder of the exception e (a CORBA::SystemException, or a user
51
+ # exception of the class klass).
52
+ def self._of(e, klass = nil)
53
+ h = new
54
+ h.is_system_exception = e.is_a?(::CORBA::SystemException)
55
+ h.byte_order = LITTLE
56
+ h.marshaled_exception = ::Rustorb::Native.cdr_encode(LITTLE, lambda do |out|
57
+ out.write_string(e._id)
58
+ if h.is_system_exception
59
+ out.write_ulong(e.minor)
60
+ out.write_ulong(e.completed)
61
+ else
62
+ ::Rustorb::Marshal.write(out, klass._tc, e)
63
+ end
64
+ end)
65
+ h.instance_variable_set(:@__rustorb_exception, e)
66
+ h
67
+ end
68
+
69
+ def raise_exception
70
+ e = @__rustorb_exception || _decode(nil)
71
+ raise e
72
+ end
73
+
74
+ def raise_exception_with_list(exc_list)
75
+ e = @__rustorb_exception
76
+ if e.is_a?(::CORBA::UserException) && exc_list.none? { |tc| tc.id == e._id }
77
+ e = ::CORBA::UNKNOWN.new("user exception #{e._id}", UNKNOWN_MINOR, ::CORBA::COMPLETED_YES)
78
+ end
79
+ raise(e || _decode(exc_list))
80
+ end
81
+
82
+ private
83
+
84
+ # The exception of the state; user exceptions by the registered classes
85
+ # (or the TypeCodes of list, when given).
86
+ def _decode(list)
87
+ bytes = marshaled_exception.is_a?(Array) ? marshaled_exception.pack("C*") : marshaled_exception.to_s
88
+ ::Rustorb::Native.cdr_decode(bytes, byte_order, lambda do |inp|
89
+ id = inp.read_string
90
+ if is_system_exception
91
+ minor = inp.read_ulong
92
+ completed = inp.read_ulong
93
+ raise ::CORBA::MARSHAL.new("bad marshaled_exception", 0, ::CORBA::COMPLETED_NO) if inp.failed?
94
+
95
+ next ::Rustorb::Native.system_exception(id, minor, completed)
96
+ end
97
+ klass = ::Rustorb.find_type(id)
98
+ tc = if list
99
+ list.find { |t| t.id == id }
100
+ elsif klass.is_a?(Class) && klass <= ::CORBA::UserException
101
+ klass._tc
102
+ end
103
+ next ::CORBA::UNKNOWN.new("user exception #{id}", UNKNOWN_MINOR, ::CORBA::COMPLETED_YES) unless tc
104
+
105
+ e = ::Rustorb::Marshal.read(inp, tc)
106
+ raise ::CORBA::MARSHAL.new("bad marshaled_exception", 0, ::CORBA::COMPLETED_NO) if inp.failed?
107
+
108
+ e.is_a?(::Exception) ? e : ::CORBA::UNKNOWN.new("user exception #{id}", UNKNOWN_MINOR, ::CORBA::COMPLETED_YES)
109
+ end)
110
+ end
111
+ end
112
+ ::Rustorb::Values.register(ExceptionHolder, default: true)
113
+ end
114
+
115
+ module POA
116
+ module Messaging
117
+ class ReplyHandler < ::PortableServer::Servant
118
+ IDS = [::Messaging::ReplyHandler::ID].freeze
119
+
120
+ def self._operations = {}.freeze
121
+ end
122
+ end
123
+ end
124
+
125
+ module CORBA
126
+ class Object
127
+ # A sendc_ request (generated code): the in and inout values of op are
128
+ # copied now; the reply goes to handler.<reply>(result, inout and out
129
+ # values) or handler.<excep>(Messaging::ExceptionHolder), called on a
130
+ # thread of the ORB's reply pool. A nil handler drops the reply. What
131
+ # the handler raises is dropped.
132
+ def _sendc(op, reply, excep, handler, args)
133
+ ins = op.in_params
134
+ raise ArgumentError, "#{op.name}: #{args.size} arguments for #{ins.size}" unless args.size == ins.size
135
+ if !handler.nil? && !handler.is_a?(CORBA::Object)
136
+ raise CORBA::BAD_PARAM.new("the handler is not an object reference", 0, CORBA::COMPLETED_NO)
137
+ end
138
+
139
+ anys = ins.each_with_index.map do |p, i|
140
+ tc = p[1]
141
+ Rustorb::Native::Any.insert(tc.resolve.native, ->(out) { Rustorb::Marshal.write(out, tc, args[i]) })
142
+ end
143
+ values = nil
144
+ failure = nil
145
+ user = nil
146
+ reader = if op.result.nil? && op.out_params.empty?
147
+ nil
148
+ else
149
+ lambda do |inp|
150
+ v = []
151
+ v << Rustorb::Marshal.read(inp, op.result) if op.result
152
+ op.out_params.each { |p| v << Rustorb::Marshal.read(inp, p[1]) }
153
+ raise CORBA::MARSHAL.new("bad reply of #{op.name}", 0, CORBA::COMPLETED_YES) if inp.failed?
154
+
155
+ values = v
156
+ rescue ::Exception => e # rubocop:disable Lint/RescueException - kept for the handler
157
+ failure = e
158
+ raise
159
+ end
160
+ end
161
+ user_exc = if op.exceptions.empty?
162
+ nil
163
+ else
164
+ lambda do |id, inp|
165
+ klass = op.find_exception(id)
166
+ next nil unless klass
167
+
168
+ e = Rustorb::Marshal.read(inp, klass._tc)
169
+ raise CORBA::MARSHAL.new("bad #{id}", 0, CORBA::COMPLETED_YES) if inp.failed?
170
+
171
+ user = [e, klass]
172
+ e
173
+ rescue ::Exception => e # rubocop:disable Lint/RescueException - kept for the handler
174
+ failure = e
175
+ raise
176
+ end
177
+ end
178
+ # status: RORB_OK (0), RORB_SYSTEM_EXCEPTION (1, sys is [id, minor,
179
+ # completed]), RORB_USER_EXCEPTION (2) or RORB_CALLBACK_FAILED (4).
180
+ done = lambda do |status, sys|
181
+ next if handler.nil?
182
+
183
+ begin
184
+ case status
185
+ when 0 then handler.public_send(reply, *(values || []))
186
+ when 2 then handler.public_send(excep, Messaging::ExceptionHolder._of(*user))
187
+ else
188
+ e = if sys
189
+ Rustorb::Native.system_exception(*sys)
190
+ elsif failure.is_a?(CORBA::SystemException)
191
+ failure
192
+ else
193
+ CORBA::UNKNOWN.new(failure&.message.to_s, 0, CORBA::COMPLETED_MAYBE)
194
+ end
195
+ handler.public_send(excep, Messaging::ExceptionHolder._of(e))
196
+ end
197
+ rescue ::Exception => e # rubocop:disable Lint/RescueException - nowhere to report it
198
+ raise if e.is_a?(SystemExit) || e.is_a?(Interrupt)
199
+
200
+ warn "rustorb: the AMI handler of #{op.name} raised #{e.class}: #{e.message}" if $VERBOSE
201
+ end
202
+ end
203
+ @_ref.sendc(op.name, anys, nil, reader, user_exc, done)
204
+ nil
205
+ end
206
+ end
207
+ end