Class: Ice::CtrlCHandler

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

Overview

Note the interface is the same as the C++ CtrlCHandler implementation, however, the implementation is different.

Constant Summary collapse

@@_self =
nil

Instance Method Summary collapse

Constructor Details

#initializeCtrlCHandler

Returns a new instance of CtrlCHandler.



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

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
    @callback = nil

    @read, @write = IO.pipe

    #
    # Setup and install signal handlers
    #
    if Signal.list.has_key?('HUP')
        Signal.trap('HUP') { @write.puts 'HUP' }
    end
    Signal.trap('INT') { @write.puts 'INT' }
    Signal.trap('TERM') { @write.puts 'TERM' }

    @thr = Thread.new { main }
end

Instance Method Details

#destroyObject

Destroy the object. Wait for the thread to terminate and cleanup the internal state.



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/Ice.rb', line 329

def destroy
    @write.puts 'DONE'
    @write.close()

    # 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

#getCallbackObject



353
354
355
356
357
# File 'lib/Ice.rb', line 353

def getCallback
    @mutex.synchronize {
        return @callback
    }
end

#mainObject

Read and dispatch signals.



313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/Ice.rb', line 313

def main
    while rs = IO.select([@read])
        signal = rs.first[0].gets.strip
        if signal == 'DONE'
            @read.close()
            break
        end
        callback = @callback
        if callback
            callback.call(signal)
        end
    end
end

#setCallback(callback) ⇒ Object



347
348
349
350
351
# File 'lib/Ice.rb', line 347

def setCallback(callback)
    @mutex.synchronize {
        @callback = callback
    }
end