Class: Ice::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/Ice.rb

Overview

Ice::Application.

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(signalPolicy = HandleSignals) ⇒ Application

Returns a new instance of Application.



326
327
328
# File 'lib/Ice.rb', line 326

def initialize(signalPolicy=HandleSignals)
    @@_signalPolicy = signalPolicy
end

Class Method Details

.appNameObject



424
425
426
# File 'lib/Ice.rb', line 424

def Application.appName
    @@_appName
end

.callbackOnInterruptObject



464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/Ice.rb', line 464

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

.callbackOnInterruptCallback(sig) ⇒ Object



560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# File 'lib/Ice.rb', line 560

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

.communicatorObject



428
429
430
# File 'lib/Ice.rb', line 428

def Application.communicator
    @@_communicator
end

.destroyOnInterruptObject



432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/Ice.rb', line 432

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

.destroyOnInterruptCallback(sig) ⇒ Object



539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'lib/Ice.rb', line 539

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

.holdInterruptObject



478
479
480
481
482
483
484
485
486
487
488
489
490
491
# File 'lib/Ice.rb', line 478

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

.holdInterruptCallback(sig) ⇒ Object



520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'lib/Ice.rb', line 520

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

.ignoreInterruptObject

No support for this since no server side in Ice for ruby. def Application.shutdownOnInterrupt end



450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/Ice.rb', line 450

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

.interruptedObject



514
515
516
517
518
# File 'lib/Ice.rb', line 514

def Application.interrupted
    @@_mutex.synchronize {
        return @@_interrupted
    }
end

.releaseInterruptObject



493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
# File 'lib/Ice.rb', line 493

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

Instance Method Details

#interruptCallback(sig) ⇒ Object



421
422
# File 'lib/Ice.rb', line 421

def interruptCallback(sig)
end

#main(args, configFile = nil, initData = nil) ⇒ Object



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
# File 'lib/Ice.rb', line 330

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)
        @@_appName = initData.properties.getPropertyWithDefault("Ice.ProgramName", @@_appName)
        @@_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