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,278 @@
1
+ # frozen_string_literal: true
2
+
3
+ # CORBA::Object (a reference: the stub side of every interface) and what
4
+ # generated stubs and skeletons describe operations with.
5
+ #
6
+ # An IDL interface is a Ruby module (Mod::Iface) holding the stub methods;
7
+ # Iface._narrow gives a reference of the class Iface::Stub, which is a
8
+ # CORBA::Object including the module.
9
+ #
10
+ # Licensed under MIT OR Apache-2.0, like the rest of the rustorb core.
11
+
12
+ module Rustorb
13
+ # An operation or attribute accessor: name (on the wire), result
14
+ # (TypeCode, nil for void), params ([[mode, TypeCode]], mode :in, :out or
15
+ # :inout), exceptions (UserException classes), oneway.
16
+ class Operation
17
+ attr_reader :name, :result, :params, :exceptions, :oneway
18
+
19
+ def initialize(name, result: nil, params: [], exceptions: [], oneway: false)
20
+ @name = name
21
+ @result = result
22
+ @params = params
23
+ @exceptions = exceptions
24
+ @oneway = oneway
25
+ @in_params = params.reject { |p| p[0] == :out }
26
+ @out_params = params.reject { |p| p[0] == :in }
27
+ end
28
+
29
+ attr_reader :in_params, :out_params
30
+
31
+ def find_exception(id)
32
+ @exceptions.find { |e| e::ID == id }
33
+ end
34
+
35
+ # What a call returns: nil, the one value, or [result, out values...].
36
+ def returned(values)
37
+ case values.size
38
+ when 0 then nil
39
+ when 1 then values[0]
40
+ else values
41
+ end
42
+ end
43
+ end
44
+
45
+ @interfaces = {}
46
+
47
+ class << self
48
+ # The stub class of an interface module (Iface::Stub).
49
+ def stub_class(mod)
50
+ klass = Class.new(CORBA::Object) { include mod }
51
+ klass.define_singleton_method(:_tc) { mod._tc }
52
+ mod.const_set(:Stub, klass)
53
+ @types_lock.synchronize { @interfaces[mod::ID] = mod }
54
+ klass
55
+ end
56
+
57
+ def find_interface(id)
58
+ @types_lock.synchronize { @interfaces[id] }
59
+ end
60
+ end
61
+
62
+ # What an interface module extends itself with: _narrow and friends.
63
+ module InterfaceModule
64
+ def _narrow(obj)
65
+ return nil if obj.nil?
66
+ raise CORBA::BAD_PARAM.new("#{obj.inspect} is not a CORBA::Object") unless obj.is_a?(CORBA::Object)
67
+ return obj if obj.is_a?(self)
68
+ raise CORBA::BAD_PARAM.new("the object is not a #{self::ID}", 0, CORBA::COMPLETED_NO) unless obj._is_a?(self::ID)
69
+
70
+ self::Stub._from_ref(obj._native_ref)
71
+ end
72
+
73
+ def _unchecked_narrow(obj)
74
+ return nil if obj.nil?
75
+ return obj if obj.is_a?(self)
76
+
77
+ self::Stub._from_ref(obj._native_ref)
78
+ end
79
+
80
+ def _tc
81
+ @_tc ||= CORBA::TypeCode.create_interface_tc(self::ID, name.split("::").last)
82
+ end
83
+ end
84
+ end
85
+
86
+ module CORBA
87
+ class Object
88
+ # ref: a Rustorb::Native::Ref.
89
+ def self._from_ref(ref, tc = nil)
90
+ return nil if ref.nil?
91
+
92
+ klass = self
93
+ if klass == CORBA::Object && tc
94
+ mod = Rustorb.find_interface(tc.unaliased.id)
95
+ klass = mod::Stub if mod
96
+ end
97
+ obj = klass.allocate
98
+ obj.instance_variable_set(:@_ref, ref)
99
+ obj
100
+ end
101
+
102
+ def _native_ref
103
+ @_ref
104
+ end
105
+
106
+ def _is_a?(id)
107
+ @_ref.is_a(id)
108
+ end
109
+
110
+ def _non_existent?
111
+ @_ref.non_existent
112
+ end
113
+ alias _non_existent _non_existent?
114
+
115
+ def _validate_connection
116
+ @_ref.validate_connection
117
+ end
118
+
119
+ def _is_equivalent?(other)
120
+ other.is_a?(CORBA::Object) && @_ref.equivalent(other._native_ref)
121
+ end
122
+ alias _is_equivalent _is_equivalent?
123
+
124
+ def _hash(maximum = 2**31 - 1)
125
+ @_ref.hash_value(maximum)
126
+ end
127
+
128
+ def _repository_id
129
+ @_ref.type_id
130
+ end
131
+
132
+ def _duplicate
133
+ self.class._from_ref(@_ref.dup)
134
+ end
135
+
136
+ def _release; end
137
+
138
+ def _is_nil?
139
+ false
140
+ end
141
+
142
+ # The call timeout of this reference, in seconds (nil: the ORB's).
143
+ def _call_timeout=(seconds)
144
+ @_ref.set_call_timeout(seconds.nil? ? -1 : (seconds * 1_000_000).round)
145
+ end
146
+
147
+ def _call_timeout
148
+ us = @_ref.call_timeout
149
+ us.negative? ? nil : us / 1_000_000.0
150
+ end
151
+
152
+ def ==(other)
153
+ _is_equivalent?(other)
154
+ end
155
+ alias eql? ==
156
+
157
+ def hash
158
+ _hash
159
+ end
160
+
161
+ def inspect
162
+ "#<#{self.class.name || 'CORBA::Object'} #{_repository_id}>"
163
+ end
164
+ alias to_s inspect
165
+
166
+ # Calls op (a Rustorb::Operation) with the in and inout arguments.
167
+ def _invoke(op, args)
168
+ ins = op.in_params
169
+ unless args.size == ins.size
170
+ raise ArgumentError, "#{op.name}: #{args.size} arguments for #{ins.size}"
171
+ end
172
+
173
+ writer = if ins.empty?
174
+ nil
175
+ else
176
+ lambda do |out|
177
+ ins.each_with_index { |p, i| Rustorb::Marshal.write(out, p[1], args[i]) }
178
+ end
179
+ end
180
+ values = []
181
+ reader = if op.oneway || (op.result.nil? && op.out_params.empty?)
182
+ nil
183
+ else
184
+ lambda do |inp|
185
+ values << Rustorb::Marshal.read(inp, op.result) if op.result
186
+ op.out_params.each { |p| values << Rustorb::Marshal.read(inp, p[1]) }
187
+ raise CORBA::MARSHAL.new("bad reply of #{op.name}", 0, CORBA::COMPLETED_YES) if inp.failed?
188
+ end
189
+ end
190
+ user_exc = if op.exceptions.empty?
191
+ nil
192
+ else
193
+ lambda do |id, inp|
194
+ klass = op.find_exception(id)
195
+ next nil unless klass
196
+
197
+ e = Rustorb::Marshal.read(inp, klass._tc)
198
+ raise CORBA::MARSHAL.new("bad #{id}", 0, CORBA::COMPLETED_YES) if inp.failed?
199
+
200
+ e
201
+ end
202
+ end
203
+ @_ref.invoke(op.name, op.oneway, nil, writer, reader, user_exc)
204
+ op.returned(values)
205
+ end
206
+ end
207
+ end
208
+
209
+ module Rustorb
210
+ # What a local interface module extends itself with: _narrow and friends.
211
+ module LocalInterfaceModule
212
+ def _narrow(obj)
213
+ return nil if obj.nil?
214
+ return obj if obj.is_a?(self)
215
+
216
+ raise CORBA::BAD_PARAM.new("the object is not a #{self::ID}", 0, CORBA::COMPLETED_NO)
217
+ end
218
+ alias _unchecked_narrow _narrow
219
+
220
+ def _tc
221
+ @_tc ||= CORBA::TypeCode.create_local_interface_tc(self::ID, name.split("::").last)
222
+ end
223
+ end
224
+ end
225
+
226
+ module CORBA
227
+ # The base of the implementations of local interfaces: a class derives
228
+ # from it and includes the interface's module.
229
+ #
230
+ # class MyHook < CORBA::LocalObject
231
+ # include App::Hook
232
+ # def notify(text) = ...
233
+ # end
234
+ #
235
+ # A local object is used in this process only: sending it in a call, or
236
+ # converting it to a string, is MARSHAL (minor 4).
237
+ class LocalObject < CORBA::Object
238
+ LOCAL_ID = "IDL:omg.org/CORBA/LocalObject:1.0"
239
+
240
+ def _native_ref
241
+ raise CORBA::MARSHAL.new("#{self.class} is a local object", 4, CORBA::COMPLETED_NO)
242
+ end
243
+
244
+ def _ids
245
+ ids = self.class.ancestors.filter_map { |k| k::ID if k.const_defined?(:ID, false) }
246
+ ids + [LOCAL_ID, "IDL:omg.org/CORBA/Object:1.0"]
247
+ end
248
+
249
+ def _is_a?(id) = _ids.include?(id)
250
+ alias _is_a _is_a?
251
+
252
+ def _non_existent? = false
253
+ alias _non_existent _non_existent?
254
+
255
+ def _validate_connection = true
256
+ def _is_equivalent?(other) = equal?(other)
257
+ alias _is_equivalent _is_equivalent?
258
+
259
+ def _hash(maximum = 2**31 - 1) = object_id % (maximum + 1)
260
+ def _repository_id = _ids.first
261
+ def _duplicate = self
262
+
263
+ def _call_timeout=(_seconds)
264
+ raise CORBA::NO_IMPLEMENT.new("a local object has no call timeout", 8, CORBA::COMPLETED_NO)
265
+ end
266
+
267
+ def _call_timeout = nil
268
+ def _request(_operation) = raise(CORBA::NO_IMPLEMENT.new("the DII on a local object", 4, CORBA::COMPLETED_NO))
269
+ alias _create_request _request
270
+
271
+ def ==(other) = equal?(other)
272
+ alias eql? ==
273
+ def hash = object_id.hash
274
+
275
+ def inspect = "#<#{self.class.name || 'CORBA::LocalObject'} #{_repository_id}>"
276
+ alias to_s inspect
277
+ end
278
+ end
@@ -0,0 +1,152 @@
1
+ # frozen_string_literal: true
2
+
3
+ # CORBA.ORB_init and CORBA::ORB.
4
+ #
5
+ # Licensed under MIT OR Apache-2.0, like the rest of the rustorb core.
6
+
7
+ module CORBA
8
+ # ORB_init(args, orb_id): the ORB of orb_id, configured by the -ORB
9
+ # options of args (removed from it, as CORBA says) and the environment.
10
+ # A second call with the same id gives the same ORB (until it is
11
+ # destroyed). An ORB of an id other than "" has its own server
12
+ # resources; initializers (rustorb) are PortableInterceptor::
13
+ # ORBInitializers of that ORB only, run after the registered ones.
14
+ def self.ORB_init(args = [], orb_id = "", initializers: nil)
15
+ @orb_lock ||= Mutex.new
16
+ @orbs ||= {}
17
+ orb_id = orb_id.to_s
18
+ @orb_lock.synchronize do
19
+ orb = @orbs[orb_id]
20
+ if orb && !orb._native.nil? && !orb._destroyed?
21
+ Rustorb.strip_orb_options(args)
22
+ return orb
23
+ end
24
+ argv = [$PROGRAM_NAME] + args.map(&:to_s)
25
+ PortableInterceptor._take_locals
26
+ native, rest = if orb_id.empty? && initializers.nil?
27
+ Rustorb::Native.orb_init(argv)
28
+ else
29
+ Rustorb::Native.orb_init_pi(argv, orb_id, initializers.to_a)
30
+ end
31
+ args.replace(rest.drop(1)) if args.is_a?(Array) && !args.frozen?
32
+ @orbs[orb_id] = ORB.new(native, PortableInterceptor._take_locals)
33
+ end
34
+ end
35
+
36
+ class ORB
37
+ class InvalidName < UserException
38
+ ID = "IDL:omg.org/CORBA/ORB/InvalidName:1.0"
39
+ MEMBERS = [].freeze
40
+ end
41
+
42
+ Rustorb::Native::LOCAL_EXCEPTIONS[13] = ->(_) { InvalidName.new }
43
+
44
+ def initialize(native, locals = {})
45
+ @native = native
46
+ @locals = locals
47
+ @destroyed = false
48
+ end
49
+
50
+ def _destroyed? = @destroyed
51
+
52
+ def _native
53
+ @native
54
+ end
55
+
56
+ def string_to_object(str)
57
+ CORBA::Object._from_ref(@native.string_to_object(str))
58
+ end
59
+
60
+ def object_to_string(obj)
61
+ @native.object_to_string(obj&._native_ref)
62
+ end
63
+
64
+ def resolve_initial_references(name)
65
+ case name
66
+ when "RootPOA" then PortableServer::POA._wrap(@native.root_poa)
67
+ when "POACurrent" then PortableServer::Current.instance
68
+ when "DynAnyFactory" then DynamicAny::DynAnyFactory.instance
69
+ when "PICurrent" then @locals[name] || PortableInterceptor::Current.new(@native.pi_current)
70
+ when "CodecFactory" then @locals[name] || IOP::CodecFactory.new(@native)
71
+ else @locals[name] || CORBA::Object._from_ref(@native.resolve_initial_references(name))
72
+ end
73
+ end
74
+
75
+ def list_initial_services
76
+ (@native.list_initial_services + %w[RootPOA POACurrent DynAnyFactory PICurrent CodecFactory] +
77
+ @locals.keys).uniq
78
+ end
79
+
80
+ def register_initial_reference(name, obj)
81
+ @native.register_initial_reference(name, obj&._native_ref)
82
+ end
83
+
84
+ # Serves requests until shutdown (Ruby threads go on running).
85
+ def run
86
+ PortableServer.ensure_dispatchers
87
+ @native.run
88
+ end
89
+
90
+ def shutdown(wait_for_completion = false)
91
+ @native.shutdown(wait_for_completion)
92
+ end
93
+
94
+ def destroy
95
+ @native.destroy
96
+ @destroyed = true
97
+ end
98
+
99
+ def work_pending
100
+ @native.work_pending
101
+ end
102
+
103
+ def perform_work
104
+ @native.perform_work
105
+ end
106
+
107
+ # The call timeout of every invocation, in seconds (nil for none).
108
+ def call_timeout=(seconds)
109
+ @native.set_call_timeout(seconds.nil? ? -1 : (seconds * 1_000_000).round)
110
+ end
111
+
112
+ def call_timeout
113
+ us = @native.call_timeout
114
+ us.negative? ? nil : us / 1_000_000.0
115
+ end
116
+
117
+ def connect_timeout=(seconds)
118
+ @native.set_connect_timeout(seconds.nil? ? -1 : (seconds * 1_000_000).round)
119
+ end
120
+
121
+ %i[create_string_tc create_wstring_tc create_fixed_tc create_sequence_tc create_array_tc
122
+ create_alias_tc create_interface_tc create_struct_tc create_exception_tc create_union_tc
123
+ create_enum_tc create_recursive_tc create_value_tc create_event_tc create_value_box_tc].each do |m|
124
+ define_method(m) { |*a| CORBA::TypeCode.public_send(m, *a) }
125
+ end
126
+
127
+ def get_primitive_tc(kind)
128
+ CORBA::TypeCode.simple(kind)
129
+ end
130
+ end
131
+ end
132
+
133
+ module Rustorb
134
+ # Removes the -ORB options (each with its value) from args, for an
135
+ # ORB_init that gives an ORB already made.
136
+ def self.strip_orb_options(args)
137
+ return unless args.is_a?(Array) && !args.frozen?
138
+
139
+ out = []
140
+ skip = false
141
+ args.each do |a|
142
+ if skip
143
+ skip = false
144
+ elsif a.to_s.start_with?("-ORB")
145
+ skip = !a.to_s.include?("=")
146
+ else
147
+ out << a
148
+ end
149
+ end
150
+ args.replace(out)
151
+ end
152
+ end