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.



339
340
341
# File 'lib/Ice.rb', line 339

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

Class Method Details

.appNameObject



437
438
439
# File 'lib/Ice.rb', line 437

def Application.appName
    @@_appName
end

.callbackOnInterruptObject



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

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



573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# File 'lib/Ice.rb', line 573

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



441
442
443
# File 'lib/Ice.rb', line 441

def Application.communicator
    @@_communicator
end

.destroyOnInterruptObject



445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'lib/Ice.rb', line 445

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



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

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



491
492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/Ice.rb', line 491

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



533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
# File 'lib/Ice.rb', line 533

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



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

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



527
528
529
530
531
# File 'lib/Ice.rb', line 527

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

.releaseInterruptObject



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

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



434
435
# File 'lib/Ice.rb', line 434

def interruptCallback(sig)
end

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



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

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