Class: Uc::Mqueue

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, max_msg: 15, msg_size: 70) ⇒ Mqueue

Returns a new instance of Mqueue.



7
8
9
10
11
# File 'lib/uc/mqueue.rb', line 7

def initialize(name, max_msg: 15, msg_size: 70)
  @name = name
  @max_msg = max_msg
  @msg_size = msg_size
end

Instance Attribute Details

#max_msgObject (readonly)

Returns the value of attribute max_msg.



5
6
7
# File 'lib/uc/mqueue.rb', line 5

def max_msg
  @max_msg
end

#msg_sizeObject (readonly)

Returns the value of attribute msg_size.



5
6
7
# File 'lib/uc/mqueue.rb', line 5

def msg_size
  @msg_size
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/uc/mqueue.rb', line 5

def name
  @name
end

Instance Method Details

#attrObject



28
29
30
# File 'lib/uc/mqueue.rb', line 28

def attr
  ::POSIX_MQ::Attr.new(0,max_msg,msg_size)
end

#clearObject



79
80
81
82
83
84
85
# File 'lib/uc/mqueue.rb', line 79

def clear
  nb_reader do |mq|
    loop { mq.receive }
  end
rescue Errno::EAGAIN
  return
end

#createObject



13
14
15
# File 'lib/uc/mqueue.rb', line 13

def create
  ::POSIX_MQ.new("/#{name}", :rw, 0700, attr)
end

#destroyObject



22
23
24
25
26
# File 'lib/uc/mqueue.rb', line 22

def destroy
  ::POSIX_MQ.unlink("/#{name}")
rescue
  return false
end

#nb_reader(&block) ⇒ Object



48
49
50
# File 'lib/uc/mqueue.rb', line 48

def nb_reader(&block)
  new_mq(:r, true, &block)
end

#nb_writer(&block) ⇒ Object



56
57
58
# File 'lib/uc/mqueue.rb', line 56

def nb_writer(&block)
  new_mq(IO::WRONLY, true, &block)
end

#new_mq(io_mode, nonblock, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/uc/mqueue.rb', line 32

def new_mq(io_mode, nonblock, &block)
  mq = ::POSIX_MQ.new("/#{name}", io_mode)
  mq.nonblock = true if nonblock
  return mq if not block_given?
   
  begin
      yield mq
  ensure
      mq.close if mq
  end
end

#reader(&block) ⇒ Object



44
45
46
# File 'lib/uc/mqueue.rb', line 44

def reader(&block)
  new_mq(:r, false, &block)
end

#recreateObject



17
18
19
20
# File 'lib/uc/mqueue.rb', line 17

def recreate
  destroy
  create
end

#wait(event, timeout, output: false) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/uc/mqueue.rb', line 60

def wait(event, timeout, output: false)
  event = event.to_s
  message = ""
  reader do |r|
    while message != event do
      r.receive(message, timeout)
      puts "> #{message}" if output
    end
  end
  return message
end

#watch(event, timeout: 30, recreate: true, &block) ⇒ Object



72
73
74
75
76
77
# File 'lib/uc/mqueue.rb', line 72

def watch(event, timeout: 30, recreate: true, &block)
  self.recreate if recreate 
  clear
  yield
  wait(event, timeout, output: true)
end

#writer(&block) ⇒ Object



52
53
54
# File 'lib/uc/mqueue.rb', line 52

def writer(&block)
  new_mq(IO::WRONLY, false, &block)
end