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

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

    @thr = Thread.new { main }
end

Instance Method Details

#destroyObject

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



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

def destroy
    @mutex.synchronize {
        @done = true
        @condVar.signal
    }

    # 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



316
317
318
319
320
# File 'lib/Ice.rb', line 316

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

#mainObject

Dequeue and dispatch signals.



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

def main
    while true
        sig, callback = @mutex.synchronize {
            while @queue.empty? and not @done
                @condVar.wait(@mutex)
            end
            if @done
                return
            end
            @queue.shift
        }
        if callback
            callback.call(sig)
        end
    end
end

#setCallback(callback) ⇒ Object



310
311
312
313
314
# File 'lib/Ice.rb', line 310

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

#signalHandler(sig) ⇒ Object

Private. Only called by the signal handling mechanism.



323
324
325
326
327
328
329
330
331
# File 'lib/Ice.rb', line 323

def signalHandler(sig)
    @mutex.synchronize {
        #
        # The signal AND the current callback are queued together.
        #
        @queue = @queue.push([sig, @callback])
        @condVar.signal
    }
end