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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
|
# **********************************************************************
#
# Copyright (c) 2003-2009 ZeroC, Inc. All rights reserved.
#
# This copy of Ice is licensed to you under the terms described in the
# ICE_LICENSE file included in this distribution.
#
# **********************************************************************
require 'IceRuby'
require 'thread'
module Ice
#
# Convenience function for locating the directory containing the
# Slice files.
#
def Ice.getSliceDir
#
# Get the parent of the directory containing this file
# (Ice.rb).
#
rbHome = File::join(File::dirname(__FILE__), "..")
#
# For an installation from a source distribution, a binary
# tarball, or a Windows installer, the "slice" directory is a
# sibling of the "rb" directory.
#
dir = File::join(rbHome, "slice")
if File::exists?(dir)
return File::expand_path(dir)
end
#
# In a source distribution, the "slice" directory is one level
# higher.
#
dir = File::join(rbHome, "..", "slice")
if File::exists?(dir)
return File::expand_path(dir)
end
if RUBY_PLATFORM =~ /linux/i
iceVer = Ice::stringVersion
#
# Check the default RPM location.
#
dir = File::join("/", "usr", "share", "Ice-" + iceVer, "slice")
if File::exists?(dir)
return dir
end
end
return nil
end
#
# Exceptions.
#
class Exception < ::StandardError
def ice_name
to_s
end
def inspect
return ::Ice::__stringifyException(self)
end
end
class UserException < Exception
end
class LocalException < Exception
end
#
# Object.
#
T_Object = Ice.__declareClass('::Ice::Object')
T_ObjectPrx = Ice.__declareProxy('::Ice::Object')
module Object_mixin
def ice_isA(id, current=nil)
return ice_ids().include?(id)
end
def ice_ping(current=nil)
end
end
class Object
include Object_mixin
def Object.ice_staticId()
'::Ice::Object'
end
end
T_Object.defineClass(nil, true, nil, [], [])
Object_mixin::ICE_TYPE = T_Object
T_ObjectPrx.defineProxy(ObjectPrx, T_Object)
ObjectPrx::ICE_TYPE = T_ObjectPrx
#
# LocalObject.
#
T_LocalObject = Ice.__declareLocalClass('::Ice::LocalObject')
T_LocalObject.defineClass(nil, true, nil, [], [])
#
# InitializationData.
#
class InitializationData
def initialize(properties=nil, logger=nil)
@properties = properties
@logger = logger
end
attr_accessor :properties, :logger
end
#
# Slice checksum dictionary.
#
SliceChecksums = {}
end
#
# Include certain generated files.
#
require 'Ice/BuiltinSequences.rb'
require 'Ice/Current.rb'
require 'Ice/EndpointTypes.rb'
require 'Ice/LocalException.rb'
require 'Ice/Locator.rb'
require 'Ice/Logger.rb'
require 'Ice/ObjectFactory.rb'
require 'Ice/Process.rb'
require 'Ice/Router.rb'
require 'Ice/Endpoint.rb'
module Ice
#
# Reopen Identity to add the <=> method.
#
class Identity
def <=>(other)
n = self.name <=> other.name
if n == 0
return self.category <=> other.category
else
return n
end
end
end
#
# Note the interface is the same as the C++ CtrlCHandler
# implementation, however, the implementation is different.
#
class CtrlCHandler
def initialize
if @@_self != nil
raise RuntimeError, "Only a single instance of a CtrlCHandler can be instantiated."
end
@@_self = self
# State variables. These are not class static variables.
@condVar = ConditionVariable.new
@mutex = Mutex.new
@queue = Array.new
@done = false
@callback = nil
#
# Setup and install signal handlers
#
if Signal.list.has_key?('HUP')
Signal.trap('HUP') { signalHandler('HUP') }
end
Signal.trap('INT') { signalHandler('INT') }
Signal.trap('TERM') { signalHandler('TERM') }
@thr = Thread.new { main }
end
# Dequeue and dispatch signals.
def main
while true
sig, callback = @mutex.synchronize {
while @queue.empty? and not @done
@condVar.wait(@mutex)
end
if @done
return
end
@queue.shift
}
if callback
callback.call(sig)
end
end
end
# Destroy the object. Wait for the thread to terminate and cleanup
# the internal state.
def destroy
@mutex.synchronize {
@done = true
@condVar.signal
}
# Wait for the thread to terminate
@thr.join
#
# Cleanup any state set by the CtrlCHandler.
#
if Signal.list.has_key?('HUP')
Signal.trap('HUP', 'SIG_DFL')
end
Signal.trap('INT', 'SIG_DFL')
Signal.trap('TERM', 'SIG_DFL')
@@_self = nil
end
def setCallback(callback)
@mutex.synchronize {
@callback = callback
}
end
def getCallback
@mutex.synchronize {
return @callback
}
end
# Private. Only called by the signal handling mechanism.
def signalHandler(sig)
@mutex.synchronize {
#
# The signal AND the current callback are queued together.
#
@queue = @queue.push([sig, @callback])
@condVar.signal
}
end
@@_self = nil
end
#
# Ice::Application.
#
class Application
def initialize(signalPolicy=HandleSignals)
@@_signalPolicy = signalPolicy
end
def main(args, configFile=nil, initData=nil)
if @@_communicator
Ice::getProcessLogger().error($0 + ": only one instance of the Application class can be used")
return false
end
if @@_signalPolicy == HandleSignals
@@_ctrlCHandler = CtrlCHandler.new
end
@@_interrupted = false
@@_appName = $0
status = 0
begin
if initData.nil?
initData = InitializationData.new
end
if configFile
initData.properties = Ice::createProperties
initData.properties.load(configFile)
end
initData.properties = Ice::createProperties(args, initData.properties)
@@_application = self
@@_communicator = Ice::initialize(args, initData)
@@_destroyed = false
#
# Used by destroyOnInterruptCallback.
#
@@_nohup = @@_communicator.getProperties().getPropertyAsInt("Ice.Nohup") > 0
#
# The default is to destroy when a signal is received.
#
if @@_signalPolicy == HandleSignals
Application::destroyOnInterrupt
end
status = run(args)
rescue => ex
Ice::getProcessLogger().error($!.inspect + "\n" + ex.backtrace.join("\n"))
status = 1
end
#
# Don't want any new interrupt and at this point (post-run),
# it would not make sense to release a held signal to run
# shutdown or destroy.
#
if @@_signalPolicy == HandleSignals
Application::ignoreInterrupt
end
@@_mutex.synchronize {
while @@_callbackInProgress
@@_condVar.wait(@@_mutex)
end
if @@_destroyed
@@_communicator = nil
else
@@_destroyed = true
end
#
# And _communicator != 0, meaning will be destroyed
# next, _destroyed = true also ensures that any
# remaining callback won't do anything
#
@@_application = nil
}
if @@_communicator
begin
@@_communicator.destroy()
rescue => ex
Ice::getProcessLogger().error($!.inspect + "\n" + ex.backtrace.join("\n"))
status = 1
end
@@_communicator = nil
end
if @@_signalPolicy == HandleSignals
@@_ctrlCHandler.destroy()
@@_ctrlCHandler = nil
end
return status
end
def interruptCallback(sig)
end
def Application.appName
@@_appName
end
def Application.communicator
@@_communicator
end
def Application.destroyOnInterrupt
if @@_signalPolicy == HandleSignals
@@_mutex.synchronize {
if @@_ctrlCHandler.getCallback == @@_holdInterruptCallbackProc
@@_released = true
@@_condVar.signal
end
@@_ctrlCHandler.setCallback(@@_destroyOnInterruptCallbackProc)
}
else
Ice::getProcessLogger().error(@@_appName + ": warning: interrupt method called on Application configured to not handle interrupts.")
end
end
# No support for this since no server side in Ice for ruby.
#def Application.shutdownOnInterrupt
#end
def Application.ignoreInterrupt
if @@_signalPolicy == HandleSignals
@@_mutex.synchronize {
if @@_ctrlCHandler.getCallback == @@_holdInterruptCallbackProc
@@_released = true
@@_condVar.signal
end
@@_ctrlCHandler.setCallback(nil)
}
else
Ice::getProcessLogger().error(@@_appName + ": warning: interrupt method called on Application configured to not handle interrupts.")
end
end
def Application.callbackOnInterrupt()
if @@_signalPolicy == HandleSignals
@@_mutex.synchronize {
if @@_ctrlCHandler.getCallback == @@_holdInterruptCallbackProc
@@_released = true
@@_condVar.signal
end
@@_ctrlCHandler.setCallback(@@_callbackOnInterruptCallbackProc)
}
else
Ice::getProcessLogger().error(@@_appName + ": warning: interrupt method called on Application configured to not handle interrupts.")
end
end
def Application.holdInterrupt
if @@_signalPolicy == HandleSignals
@@_mutex.synchronize {
if @@_ctrlCHandler.getCallback != @@_holdInterruptCallbackProc
@@_previousCallback = @@_ctrlCHandler.getCallback
@@_released = false
@@_ctrlCHandler.setCallback(@@_holdInterruptCallbackProc)
end
# else, we were already holding signals
}
else
Ice::getProcessLogger().error(@@_appName + ": warning: interrupt method called on Application configured to not handle interrupts.")
end
end
def Application.releaseInterrupt
if @@_signalPolicy == HandleSignals
@@_mutex.synchronize {
if @@_ctrlCHandler.getCallback == @@_holdInterruptCallbackProc
#
# Note that it's very possible no signal is held;
# in this case the callback is just replaced and
# setting _released to true and signalling _condVar
# do no harm.
#
@@_released = true
@@_ctrlCHandler.setCallback(@@_previousCallback)
@@_condVar.signal
end
# Else nothing to release.
}
else
Ice::getProcessLogger().error(@@_appName + ": warning: interrupt method called on Application configured to not handle interrupts.")
end
end
def Application.interrupted
@@_mutex.synchronize {
return @@_interrupted
}
end
def Application.holdInterruptCallback(sig)
callback = @@_mutex.synchronize {
while not @@_released
@@_condVar.wait(@@_mutex)
end
if @@_destroyed
return
end
@@_ctrlCHandler.getCallback
}
#
# Use the current callback to process this (old) signal.
#
if callback
callback.call(sig)
end
end
def Application.destroyOnInterruptCallback(sig)
@@_mutex.synchronize {
if @@_destroyed or @@_nohup and sig == 'HUP'
return
end
@@_callbackInProcess = true
@@_interrupted = true
@@_destroyed = true
}
begin
@@_communicator.destroy()
rescue => ex
Ice::getProcessLogger().error($!.inspect + "\n" + @@_appName + " (while destroying in response to signal " + sig + "):\n" + ex.backtrace.join("\n"))
end
@@_mutex.synchronize {
@@_callbackInProcess = false
@@_condVar.signal
}
end
def Application.callbackOnInterruptCallback(sig)
# For SIGHUP the user callback is always called. It can
# decide what to do.
@@_mutex.synchronize {
if @@_destroyed
#
# Being destroyed by main thread.
#
return
end
@@_interrupted = true
@@_callbackInProcess = true
}
begin
@@_application.interruptCallback(sig)
rescue => ex
Ice::getProcessLogger().error($!.inspect + "\n" + @@_appName + " (while interrupting in response to signal " + sig + "):\n" + ex.backtrace.join("\n"))
end
@@_mutex.synchronize {
@@_callbackInProcess = false
@@_condVar.signal
}
end
HandleSignals = 0
NoSignalHandling = 1
@@_appName = nil
@@_communicator = nil
@@_application = nil
@@_ctrlCHandler = nil
@@_previousCallback = nil
@@_interrupted = false
@@_released = false
@@_destroyed = false
@@_callbackInProgress = false
@@_condVar = ConditionVariable.new
@@_mutex = Mutex.new
@@_holdInterruptCallbackProc = Proc.new { |sig| Application::holdInterruptCallback(sig) }
@@_destroyOnInterruptCallbackProc = Proc.new { |sig| Application::destroyOnInterruptCallback(sig) }
@@_callbackOnInterruptCallbackProc = Proc.new { |sig| Application::callbackOnInterruptCallback(sig) }
@@_signalPolicy = HandleSignals
end
#
# Proxy comparison functions.
#
def Ice.proxyIdentityCompare(lhs, rhs)
if (lhs && !lhs.is_a?(ObjectPrx)) || (rhs && !rhs.is_a?(ObjectPrx))
raise TypeError, 'argument is not a proxy'
end
if lhs.nil? && rhs.nil?
return 0
elsif lhs.nil? && rhs
return -1
elsif lhs && rhs.nil?
return 1
else
return lhs.ice_getIdentity() <=> rhs.ice_getIdentity()
end
end
def Ice.proxyIdentityEqual(lhs, rhs)
return proxyIdentityCompare(lhs, rhs) == 0
end
def Ice.proxyIdentityAndFacetCompare(lhs, rhs)
n = proxyIdentityCompare(lhs, rhs)
if n == 0 && lhs && rhs
n = lhs.ice_getFacet() <=> rhs.ice_getFacet()
end
return n
end
def Ice.proxyIdentityAndFacetEqual(lhs, rhs)
return proxyIdentityAndFacetCompare(lhs, rhs) == 0
end
end
Ice::Object_mixin::OP_ice_isA = ::Ice::__defineOperation('ice_isA', ::Ice::OperationMode::Idempotent, ::Ice::OperationMode::Nonmutating, false, [::Ice::T_string], [], ::Ice::T_bool, [])
Ice::Object_mixin::OP_ice_ping = ::Ice::__defineOperation('ice_ping', ::Ice::OperationMode::Idempotent, ::Ice::OperationMode::Nonmutating, false, [], [], nil, [])
Ice::Object_mixin::OP_ice_ids = ::Ice::__defineOperation('ice_ids', ::Ice::OperationMode::Idempotent, ::Ice::OperationMode::Nonmutating, false, [], [], ::Ice::T_StringSeq, [])
Ice::Object_mixin::OP_ice_id = ::Ice::__defineOperation('ice_id', ::Ice::OperationMode::Idempotent, ::Ice::OperationMode::Nonmutating, false, [], [], ::Ice::T_string, [])
|