Class: MessageChannel::Druby
- Inherits:
-
Object
- Object
- MessageChannel::Druby
- Defined in:
- lib/message_channel/druby.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
-
#initialize(host: nil, port: nil) ⇒ Druby
constructor
A new instance of Druby.
- #listen(*patterns, timeout: nil, &block) ⇒ Object
- #listen_each(*patterns, &block) ⇒ Object
- #listen_once(*patterns) ⇒ Object
- #notify(topic, **items) ⇒ Object
- #unlisten(*patterns) ⇒ Object
Constructor Details
#initialize(host: nil, port: nil) ⇒ Druby
Returns a new instance of Druby.
111 112 113 114 115 116 |
# File 'lib/message_channel/druby.rb', line 111 def initialize( host: nil, port: nil ) @host = host || "127.0.0.1" @port = ( port || 8787 ).to_i @agent = Agent.new( host: @host, port: @port ) @threads = {} end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
109 110 111 |
# File 'lib/message_channel/druby.rb', line 109 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
109 110 111 |
# File 'lib/message_channel/druby.rb', line 109 def port @port end |
Instance Method Details
#listen(*patterns, timeout: nil, &block) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/message_channel/druby.rb', line 159 def listen( *patterns, timeout: nil, &block ) if block_given? listen_each( *patterns ) do |topic, items| block.call( topic, items ) end return nil end if timeout.nil? || ( timeout.is_a?( Numeric ) && timeout >= 0 ) begin Timeout.timeout( timeout ) do listen_once( *patterns ) end rescue Timeout::Error return nil end else raise ArgumentError, "timeout: %s" % timeout end end |
#listen_each(*patterns, &block) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/message_channel/druby.rb', line 144 def listen_each( *patterns, &block ) patterns.each do |pattern| @threads[pattern] = Thread.start( pattern ) do |pttrn| begin @agent.listen_each( pttrn ) do |topic, | items = JSON.parse( , symbolize_names: true ) block.call( topic, items ) end rescue => error nil end end end end |
#listen_once(*patterns) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/message_channel/druby.rb', line 118 def listen_once( *patterns ) queue = Queue.new threads = {} patterns.each do |pattern| threads[pattern] = Thread.start( pattern ) do |pttrn| agent = Agent.new begin topic, = * agent.listen_once( pttrn ) items = JSON.parse( , symbolize_names: true ) queue.push( [topic, items] ) rescue => error nil end end end topic, items = queue.pop patterns.each do |pattern| threads[pattern].kill rescue nil threads.delete( pattern ) rescue nil end [topic, items] rescue nil end |
#notify(topic, **items) ⇒ Object
186 187 188 |
# File 'lib/message_channel/druby.rb', line 186 def notify( topic, **items ) @agent.notify( topic, items.to_json ) end |
#unlisten(*patterns) ⇒ Object
179 180 181 182 183 184 |
# File 'lib/message_channel/druby.rb', line 179 def unlisten( *patterns ) patterns.each do |pattern| @agent.unlisten( pattern ) @threads.delete( pattern ) end end |