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.
data/lib/rustorb/pi.rb ADDED
@@ -0,0 +1,526 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Portable Interceptors and IOP::Codec (docs/portable-interceptors.md), on
4
+ # the PI part of the C ABI.
5
+ #
6
+ # An interceptor is an object whose class includes
7
+ # PortableInterceptor::ClientRequestInterceptor, ServerRequestInterceptor
8
+ # or IORInterceptor (one class may include several); it is added by an
9
+ # ORBInitializer, registered before CORBA.ORB_init:
10
+ #
11
+ # class Tracer
12
+ # include PortableInterceptor::ClientRequestInterceptor
13
+ # def name = "tracer"
14
+ # def send_request(ri)
15
+ # ri.add_request_service_context(IOP::ServiceContext.new(0x52555354, "..."), false)
16
+ # end
17
+ # end
18
+ #
19
+ # class Init
20
+ # include PortableInterceptor::ORBInitializer
21
+ # def pre_init(info) = info.add_client_request_interceptor(Tracer.new)
22
+ # end
23
+ #
24
+ # PortableInterceptor.register_orb_initializer(Init.new)
25
+ # orb = CORBA.ORB_init(ARGV)
26
+ #
27
+ # The points run with the GVL on one of the extension's dispatcher threads
28
+ # while the thread of the call (client) or of the upcall (server) waits. An
29
+ # info object is valid only during its point (BAD_INV_ORDER after). A point
30
+ # raises PortableInterceptor::ForwardRequest or a CORBA::SystemException;
31
+ # anything else it raises is UNKNOWN.
32
+ #
33
+ # Licensed under MIT OR Apache-2.0, like the rest of the rustorb core.
34
+
35
+ module IOP
36
+ TAG_INTERNET_IOP = 0
37
+ TAG_MULTIPLE_COMPONENTS = 1
38
+
39
+ TAG_ORB_TYPE = 0
40
+ TAG_CODE_SETS = 1
41
+ TAG_POLICIES = 2
42
+ TAG_ALTERNATE_IIOP_ADDRESS = 3
43
+ TAG_COMPLETE_OBJECT_KEY = 5
44
+ TAG_ENDPOINT_ID_POSITION = 6
45
+ TAG_LOCATION_POLICY = 12
46
+ TAG_ASSOCIATION_OPTIONS = 13
47
+ TAG_SEC_NAME = 14
48
+ TAG_SSL_SEC_TRANS = 20
49
+ TAG_JAVA_CODEBASE = 25
50
+ TAG_CSI_SEC_MECH_LIST = 33
51
+ TAG_NULL_TAG = 34
52
+ TAG_TLS_SEC_TRANS = 36
53
+
54
+ TransactionService = 0
55
+ CodeSets = 1
56
+ ChainBypassCheck = 2
57
+ ChainBypassInfo = 3
58
+ LogicalThreadId = 4
59
+ BI_DIR_IIOP = 5
60
+ SendingContextRunTime = 6
61
+ INVOCATION_POLICIES = 7
62
+ FORWARDED_IDENTITY = 8
63
+ UnknownExceptionInfo = 9
64
+ RTCorbaPriority = 10
65
+ RTCorbaPriorityRange = 11
66
+ FT_GROUP_VERSION = 12
67
+ FT_REQUEST = 13
68
+ ExceptionDetailMessage = 14
69
+
70
+ ENCODING_CDR_ENCAPS = 0
71
+
72
+ def self._struct(name, members)
73
+ klass = Class.new(::Rustorb::Struct)
74
+ const_set(name, klass)
75
+ id = "IDL:omg.org/IOP/#{name}:1.0"
76
+ klass.const_set(:ID, id)
77
+ klass.const_set(:MEMBERS, members.map(&:first).freeze)
78
+ members.each { |m, _| klass.attr_accessor(m) }
79
+ klass.define_singleton_method(:_tc) do
80
+ @_tc ||= ::CORBA::TypeCode.create_struct_tc(id, name, members.map { |m, tc| [m, tc.call] })
81
+ end
82
+ klass
83
+ end
84
+ private_class_method :_struct
85
+
86
+ octets = -> { ::CORBA::TypeCode.create_sequence_tc(0, ::CORBA._tc_octet) }
87
+ _struct("TaggedProfile", [["tag", -> { ::CORBA._tc_ulong }], ["profile_data", octets]])
88
+ _struct("TaggedComponent", [["tag", -> { ::CORBA._tc_ulong }], ["component_data", octets]])
89
+ _struct("ServiceContext", [["context_id", -> { ::CORBA._tc_ulong }], ["context_data", octets]])
90
+ _struct("Encoding", [["format", -> { ::CORBA._tc_short }], ["major_version", -> { ::CORBA._tc_octet }],
91
+ ["minor_version", -> { ::CORBA._tc_octet }]])
92
+
93
+ # Octets (a String or an Array of Integers) as a binary String.
94
+ def self._octets(data)
95
+ data.is_a?(Array) ? data.pack("C*") : data.to_s.b
96
+ end
97
+
98
+ # IOP::Codec: CDR encapsulations of anys (encode / decode) and of values
99
+ # alone (encode_value / decode_value). Octets are binary Strings.
100
+ class Codec < ::CORBA::LocalObject
101
+ ID = "IDL:omg.org/IOP/Codec:1.0"
102
+ ::PortableServer.local_exception(self, "InvalidTypeForEncoding", 24)
103
+ ::PortableServer.local_exception(self, "FormatMismatch", 25)
104
+ ::PortableServer.local_exception(self, "TypeMismatch", 26)
105
+
106
+ def initialize(native)
107
+ super()
108
+ @native = native
109
+ end
110
+
111
+ def encode(data) = @native.encode(data._native, false)
112
+ def encode_value(data) = @native.encode(data._native, true)
113
+ def decode(data) = ::CORBA::Any._from_native(@native.decode(IOP._octets(data), nil))
114
+
115
+ def decode_value(data, tc)
116
+ ::CORBA::Any._from_native(@native.decode(IOP._octets(data), tc.resolve.native))
117
+ end
118
+ end
119
+
120
+ # IOP::CodecFactory (orb.resolve_initial_references("CodecFactory"), or
121
+ # ORBInitInfo#codec_factory).
122
+ class CodecFactory < ::CORBA::LocalObject
123
+ ID = "IDL:omg.org/IOP/CodecFactory:1.0"
124
+ ::PortableServer.local_exception(self, "UnknownEncoding", 23)
125
+
126
+ # source: a Native::ORB or a Native::ORBInitInfo.
127
+ def initialize(source)
128
+ super()
129
+ @source = source
130
+ end
131
+
132
+ def create_codec(enc)
133
+ Codec.new(@source.create_codec(enc.format, enc.major_version, enc.minor_version))
134
+ end
135
+ end
136
+ end
137
+
138
+ module Dynamic
139
+ # Dynamic::Parameter: argument (a CORBA::Any) and mode
140
+ # (CORBA::PARAM_IN, PARAM_OUT or PARAM_INOUT).
141
+ class Parameter < ::Rustorb::Struct
142
+ ID = "IDL:omg.org/Dynamic/Parameter:1.0"
143
+ MEMBERS = %w[argument mode].freeze
144
+ attr_accessor :argument, :mode
145
+ end
146
+ end
147
+
148
+ module CORBA
149
+ PARAM_IN = 0
150
+ PARAM_OUT = 1
151
+ PARAM_INOUT = 2
152
+ end
153
+
154
+ module PortableInterceptor
155
+ # ReplyStatus
156
+ SUCCESSFUL = 0
157
+ SYSTEM_EXCEPTION = 1
158
+ USER_EXCEPTION = 2
159
+ LOCATION_FORWARD = 3
160
+ TRANSPORT_RETRY = 4
161
+ UNKNOWN = 5
162
+
163
+ # AdapterState
164
+ HOLDING = 0
165
+ ACTIVE = 1
166
+ DISCARDING = 2
167
+ INACTIVE = 3
168
+ NON_EXISTENT = 4
169
+
170
+ # Raised by a point to send the request to forward instead.
171
+ class ForwardRequest < ::CORBA::UserException
172
+ ID = "IDL:omg.org/PortableInterceptor/ForwardRequest:1.0"
173
+ MEMBERS = %w[forward].freeze
174
+ attr_accessor :forward
175
+ end
176
+
177
+ ::PortableServer.local_exception(self, "InvalidSlot", 21)
178
+
179
+ # --- the interceptors ---
180
+
181
+ # name is "" (an anonymous interceptor) unless the class defines it.
182
+ module Interceptor
183
+ ID = "IDL:omg.org/PortableInterceptor/Interceptor:1.0"
184
+
185
+ def name = ""
186
+ def destroy; end
187
+ end
188
+
189
+ module ClientRequestInterceptor
190
+ include Interceptor
191
+ ID = "IDL:omg.org/PortableInterceptor/ClientRequestInterceptor:1.0"
192
+
193
+ def send_request(_ri); end
194
+ def send_poll(_ri); end
195
+ def receive_reply(_ri); end
196
+ def receive_exception(_ri); end
197
+ def receive_other(_ri); end
198
+ end
199
+
200
+ module ServerRequestInterceptor
201
+ include Interceptor
202
+ ID = "IDL:omg.org/PortableInterceptor/ServerRequestInterceptor:1.0"
203
+
204
+ def receive_request_service_contexts(_ri); end
205
+ def receive_request(_ri); end
206
+ def send_reply(_ri); end
207
+ def send_exception(_ri); end
208
+ def send_other(_ri); end
209
+ end
210
+
211
+ module IORInterceptor
212
+ include Interceptor
213
+ ID = "IDL:omg.org/PortableInterceptor/IORInterceptor:1.0"
214
+
215
+ def establish_components(_info); end
216
+ end
217
+
218
+ module ORBInitializer
219
+ ID = "IDL:omg.org/PortableInterceptor/ORBInitializer:1.0"
220
+
221
+ def pre_init(_info); end
222
+ def post_init(_info); end
223
+ end
224
+
225
+ # For every ORB that CORBA.ORB_init creates after it in the process.
226
+ def self.register_orb_initializer(init)
227
+ Rustorb::Native.register_orb_initializer(init)
228
+ nil
229
+ end
230
+
231
+ def self._policy(type, raw)
232
+ klass = if type.between?(::PortableServer::THREAD_POLICY_ID, ::PortableServer::REQUEST_PROCESSING_POLICY_ID)
233
+ ::PortableServer::POLICY_CLASSES[type - ::PortableServer::THREAD_POLICY_ID]
234
+ else
235
+ ::PortableServer::Policy
236
+ end
237
+ klass.new(type, raw)
238
+ end
239
+
240
+ def self._ref(native) = native && ::CORBA::Object._from_ref(native)
241
+
242
+ # --- the info objects ---
243
+
244
+ # What ClientRequestInfo and ServerRequestInfo share.
245
+ class RequestInfo < ::CORBA::LocalObject
246
+ ID = "IDL:omg.org/PortableInterceptor/RequestInfo:1.0"
247
+
248
+ def initialize(native)
249
+ super()
250
+ @native = native
251
+ end
252
+
253
+ def request_id = @native.request_id
254
+ def operation = @native.operation
255
+ def response_expected = @native.response_expected
256
+ def sync_scope = @native.sync_scope
257
+ def reply_status = @native.reply_status
258
+ def forward_reference = PortableInterceptor._ref(@native.forward_reference)
259
+ def get_slot(id) = ::CORBA::Any._from_native(@native.get_slot(id))
260
+
261
+ def arguments
262
+ @native.arguments.map { |a, m| ::Dynamic::Parameter.new(::CORBA::Any._from_native(a), m) }
263
+ end
264
+
265
+ def exceptions = @native.exceptions.map { |tc| ::CORBA::TypeCode._wrap(tc) }
266
+ def contexts = []
267
+ def operation_context = []
268
+
269
+ def result
270
+ r = @native.result
271
+ r && ::CORBA::Any._from_native(r)
272
+ end
273
+
274
+ def get_request_service_context(id)
275
+ ::IOP::ServiceContext.new(id, @native.get_request_service_context(id))
276
+ end
277
+
278
+ def get_reply_service_context(id)
279
+ ::IOP::ServiceContext.new(id, @native.get_reply_service_context(id))
280
+ end
281
+ end
282
+
283
+ class ClientRequestInfo < RequestInfo
284
+ ID = "IDL:omg.org/PortableInterceptor/ClientRequestInfo:1.0"
285
+
286
+ def target = PortableInterceptor._ref(@native.target)
287
+ def effective_target = PortableInterceptor._ref(@native.effective_target)
288
+ def effective_profile = ::IOP::TaggedProfile.new(*@native.effective_profile)
289
+ def received_exception = ::CORBA::Any._from_native(@native.received_exception)
290
+ def received_exception_id = @native.received_exception_id
291
+
292
+ def get_effective_component(id)
293
+ ::IOP::TaggedComponent.new(id, @native.get_effective_component(id))
294
+ end
295
+
296
+ def get_effective_components(id)
297
+ @native.get_effective_components(id).map { |t, d| ::IOP::TaggedComponent.new(t, d) }
298
+ end
299
+
300
+ # The policy of the type in effect, or nil.
301
+ def get_request_policy(type)
302
+ raw = @native.get_request_policy(type)
303
+ raw && PortableInterceptor._policy(type, raw)
304
+ end
305
+
306
+ def add_request_service_context(service_context, replace)
307
+ @native.add_request_service_context(service_context.context_id,
308
+ ::IOP._octets(service_context.context_data), replace)
309
+ nil
310
+ end
311
+ end
312
+
313
+ class ServerRequestInfo < RequestInfo
314
+ ID = "IDL:omg.org/PortableInterceptor/ServerRequestInfo:1.0"
315
+
316
+ def sending_exception = ::CORBA::Any._from_native(@native.sending_exception)
317
+ def sending_exception_id = @native.sending_exception_id
318
+ def r_object_id = @native.object_id
319
+ def adapter_id = @native.adapter_id
320
+ def adapter_name = @native.adapter_name
321
+ def orb_id = @native.orb_id
322
+ def server_id = @native.server_id
323
+ def target_most_derived_interface = @native.target_most_derived_interface
324
+ def target_is_a(id) = @native.target_is_a(id)
325
+ def get_server_policy(type) = PortableInterceptor._policy(type, @native.get_server_policy(type))
326
+
327
+ def set_slot(id, data)
328
+ @native.set_slot(id, data._native)
329
+ nil
330
+ end
331
+
332
+ def add_reply_service_context(service_context, replace)
333
+ @native.add_reply_service_context(service_context.context_id,
334
+ ::IOP._octets(service_context.context_data), replace)
335
+ nil
336
+ end
337
+ end
338
+
339
+ class IORInfo < ::CORBA::LocalObject
340
+ ID = "IDL:omg.org/PortableInterceptor/IORInfo:1.0"
341
+
342
+ def initialize(native)
343
+ super()
344
+ @native = native
345
+ end
346
+
347
+ def get_effective_policy(type) = PortableInterceptor._policy(type, @native.get_effective_policy(type))
348
+
349
+ # The names of the POAs from the RootPOA down (rustorb).
350
+ def adapter_name = @native.adapter_name
351
+
352
+ def add_ior_component(a_component)
353
+ @native.add_ior_component(a_component.tag, ::IOP._octets(a_component.component_data))
354
+ nil
355
+ end
356
+
357
+ def add_ior_component_to_profile(a_component, profile_id)
358
+ @native.add_ior_component_to_profile(a_component.tag, ::IOP._octets(a_component.component_data),
359
+ profile_id)
360
+ nil
361
+ end
362
+ end
363
+
364
+ # PICurrent (orb.resolve_initial_references("PICurrent")): the slots of
365
+ # ORBInitInfo#allocate_slot_id, as the calling thread sees them (in an
366
+ # upcall, those the server interceptors set).
367
+ class Current < ::CORBA::LocalObject
368
+ ID = "IDL:omg.org/PortableInterceptor/Current:1.0"
369
+
370
+ def initialize(native)
371
+ super()
372
+ @native = native
373
+ end
374
+
375
+ def get_slot(id) = ::CORBA::Any._from_native(@native.get_slot(id))
376
+
377
+ def set_slot(id, data)
378
+ @native.set_slot(id, data._native)
379
+ nil
380
+ end
381
+ end
382
+
383
+ class ORBInitInfo < ::CORBA::LocalObject
384
+ ID = "IDL:omg.org/PortableInterceptor/ORBInitInfo:1.0"
385
+
386
+ class DuplicateName < ::CORBA::UserException
387
+ ID = "IDL:omg.org/PortableInterceptor/ORBInitInfo/DuplicateName:1.0"
388
+ MEMBERS = %w[name].freeze
389
+ attr_accessor :name
390
+ end
391
+
392
+ class InvalidName < ::CORBA::UserException
393
+ ID = "IDL:omg.org/PortableInterceptor/ORBInitInfo/InvalidName:1.0"
394
+ MEMBERS = [].freeze
395
+ end
396
+
397
+ def initialize(native)
398
+ super()
399
+ @native = native
400
+ end
401
+
402
+ def arguments = @native.arguments
403
+ def orb_id = @native.orb_id
404
+ def codec_factory = ::IOP::CodecFactory.new(@native)
405
+ def allocate_slot_id = @native.allocate_slot_id
406
+
407
+ # A local object is kept for the ORB being made (CORBA.ORB_init).
408
+ def register_initial_reference(id, obj)
409
+ raise InvalidName if id.to_s.empty?
410
+
411
+ if obj.is_a?(::CORBA::LocalObject)
412
+ raise InvalidName if PortableInterceptor._locals.key?(id)
413
+
414
+ PortableInterceptor._locals[id] = obj
415
+ return nil
416
+ end
417
+ _named { @native.register_initial_reference(id, obj&._native_ref) }
418
+ nil
419
+ end
420
+
421
+ # Only in post_init (BAD_INV_ORDER in pre_init).
422
+ def resolve_initial_references(id)
423
+ local = PortableInterceptor._locals[id]
424
+ if local || %w[PICurrent CodecFactory].include?(id)
425
+ # The core raises BAD_INV_ORDER in pre_init (and InvalidName for "").
426
+ begin
427
+ @native.resolve_initial_references("")
428
+ rescue ::CORBA::ORB::InvalidName
429
+ nil
430
+ end
431
+ return local if local
432
+ return Current.new(@native.pi_current) if id == "PICurrent"
433
+
434
+ return codec_factory
435
+ end
436
+ _named { PortableInterceptor._ref(@native.resolve_initial_references(id)) }
437
+ end
438
+
439
+ def add_client_request_interceptor(interceptor)
440
+ _add(:add_client_request_interceptor, ClientRequestInterceptor, interceptor)
441
+ end
442
+
443
+ def add_server_request_interceptor(interceptor)
444
+ _add(:add_server_request_interceptor, ServerRequestInterceptor, interceptor)
445
+ end
446
+
447
+ def add_ior_interceptor(interceptor)
448
+ _add(:add_ior_interceptor, IORInterceptor, interceptor)
449
+ end
450
+
451
+ private
452
+
453
+ def _named
454
+ yield
455
+ rescue ::CORBA::ORB::InvalidName
456
+ raise InvalidName
457
+ end
458
+
459
+ def _add(method, kind, interceptor)
460
+ unless interceptor.is_a?(kind)
461
+ raise ::CORBA::BAD_PARAM.new("#{interceptor.class} does not include #{kind}", 0, ::CORBA::COMPLETED_NO)
462
+ end
463
+
464
+ name = interceptor.name.to_s
465
+ begin
466
+ @native.public_send(method, name, interceptor)
467
+ rescue DuplicateName
468
+ raise DuplicateName.new(name)
469
+ end
470
+ nil
471
+ end
472
+ end
473
+ ::Rustorb::Native::LOCAL_EXCEPTIONS[22] = ->(_) { ORBInitInfo::DuplicateName.new("") }
474
+
475
+ # The local initial references ORBInitInfo#register_initial_reference
476
+ # keeps, for the ORB being made.
477
+ def self._locals
478
+ Thread.current[:__rustorb_pi_locals] ||= {}
479
+ end
480
+
481
+ def self._take_locals
482
+ locals = _locals
483
+ Thread.current[:__rustorb_pi_locals] = nil
484
+ locals
485
+ end
486
+ end
487
+
488
+ module Rustorb
489
+ # What the extension calls (rustorb_ext.c, the PI section).
490
+ module PI
491
+ INFO = {
492
+ Rustorb::Native::ClientRequestInfo => PortableInterceptor::ClientRequestInfo,
493
+ Rustorb::Native::ServerRequestInfo => PortableInterceptor::ServerRequestInfo,
494
+ Rustorb::Native::IORInfo => PortableInterceptor::IORInfo,
495
+ Rustorb::Native::ORBInitInfo => PortableInterceptor::ORBInitInfo
496
+ }.freeze
497
+ UNKNOWN_ID = "IDL:omg.org/CORBA/UNKNOWN:1.0"
498
+
499
+ # Runs obj.point(info); nil, [:forward, ref] or [:system, id, minor,
500
+ # completed].
501
+ def self.point(obj, point, native)
502
+ obj.public_send(point, INFO.fetch(native.class).new(native))
503
+ nil
504
+ rescue PortableInterceptor::ForwardRequest => e
505
+ ref = e.forward
506
+ return [:system, "IDL:omg.org/CORBA/BAD_PARAM:1.0", 0, CORBA::COMPLETED_NO] unless ref.is_a?(CORBA::Object)
507
+
508
+ begin
509
+ [:forward, ref._native_ref]
510
+ rescue CORBA::SystemException
511
+ [:system, "IDL:omg.org/CORBA/BAD_PARAM:1.0", 0, CORBA::COMPLETED_NO]
512
+ end
513
+ rescue CORBA::SystemException => e
514
+ [:system, e._id, e.minor, e.completed]
515
+ rescue ::Exception => e # rubocop:disable Lint/RescueException - every error is UNKNOWN
516
+ raise if e.is_a?(SystemExit) || e.is_a?(Interrupt) || e.is_a?(NoMemoryError)
517
+
518
+ warn "rustorb: interceptor #{point} raised #{e.class}: #{e.message}" if $VERBOSE
519
+ [:system, UNKNOWN_ID, 0, CORBA::COMPLETED_MAYBE]
520
+ end
521
+
522
+ def self.destroy_interceptor(obj)
523
+ obj.destroy
524
+ end
525
+ end
526
+ end