Class: ThreadComm

Inherits:
Thread
  • Object
show all
Defined in:
lib/nub/thread_comm.rb

Overview

Thread with communication queues for simple messaging

Instance Method Summary collapse

Constructor Details

#initializeThreadComm

Returns a new instance of ThreadComm.



29
30
31
32
33
34
35
36
# File 'lib/nub/thread_comm.rb', line 29

def initialize
  @comm_in = Queue.new
  @comm_out = Queue.new

  # Proc.new will return the block given to this method
  # pass it along to thread .new with arguments
  super(@comm_in, @comm_out, &Proc.new)
end

Instance Method Details

#empty?Boolean

Check if the message queue is empty

Returns:

  • (Boolean)


39
40
41
# File 'lib/nub/thread_comm.rb', line 39

def empty?
  return @comm_out.empty?
end

#popObject

Pop a message off the thread’s outbound queue or block



44
45
46
47
48
# File 'lib/nub/thread_comm.rb', line 44

def pop
  msg = @comm_out.pop
  return msg if msg.is_a?(ThreadMsg)
  return ThreadMsg.new(msg)
end

#push(*args) ⇒ Object

Push the given message onto the threads inbound queue

Parameters:

  • msg (ThreadMsg)

    message to the thread

  • cmd (String)

    message command to the thread

  • value (String)

    message value to the thread



54
55
56
57
58
59
60
61
62
# File 'lib/nub/thread_comm.rb', line 54

def push(*args)
  if args.first.is_a?(ThreadMsg)
    @comm_in << args.first
  elsif args.size == 1
    @comm_in << ThreadMsg.new(args.first)
  else
    @comm_in << ThreadMsg.new(args.first, args.last)
  end
end