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.



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

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.



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/Ice.rb', line 288

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



312
313
314
315
316
# File 'lib/Ice.rb', line 312

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

#mainObject

Read and dispatch signals.



272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/Ice.rb', line 272

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



306
307
308
309
310
# File 'lib/Ice.rb', line 306

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