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.



366
367
368
# File 'lib/Ice.rb', line 366

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

Class Method Details

.appNameObject



458
459
460
# File 'lib/Ice.rb', line 458

def Application.appName
    @@_appName
end

.callbackOnInterruptObject



498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/Ice.rb', line 498

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



591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
# File 'lib/Ice.rb', line 591

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



462
463
464
# File 'lib/Ice.rb', line 462

def Application.communicator
    @@_communicator
end

.destroyOnInterruptObject



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

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



573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
# File 'lib/Ice.rb', line 573

def Application.destroyOnInterruptCallback(sig)
    @@_mutex.synchronize {
        if @@_destroyed or @@_nohup and sig == 'HUP'
            return
        end
        @@_callbackInProcess = true
        @@_interrupted = true
        @@_destroyed = true
    }

    @@_communicator.destroy()

    @@_mutex.synchronize {
        @@_callbackInProcess = false
        @@_condVar.signal
    }
end

.holdInterruptObject



512
513
514
515
516
517
518
519
520
521
522
523
524
525
# File 'lib/Ice.rb', line 512

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



554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
# File 'lib/Ice.rb', line 554

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



484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/Ice.rb', line 484

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



548
549
550
551
552
# File 'lib/Ice.rb', line 548

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

.releaseInterruptObject



527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/Ice.rb', line 527

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



455
456
# File 'lib/Ice.rb', line 455

def interruptCallback(sig)
end

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



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

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
        @@_communicator.destroy()
        @@_communicator = nil
    end

    if @@_signalPolicy == HandleSignals
        @@_ctrlCHandler.destroy()
        @@_ctrlCHandler = nil
    end

    return status
end