lib/dripdrop/node.rb in dripdrop-0.9.3 vs lib/dripdrop/node.rb in dripdrop-0.9.4
- old
+ new
@@ -33,33 +33,14 @@
# Starts the reactors and runs the block passed to initialize.
# This is non-blocking.
def start
@thread = Thread.new do
EM.error_handler {|e| self.error_handler e}
- EM.run do
- if @block
- self.instance_eval(&@block)
- elsif self.respond_to?(:action)
- self.action
- else
- raise "Could not start, no block or action specified"
- end
- end
+ EM.run { action }
end
end
- # If the reactor has started, this blocks until the thread
- # running the reactor joins. This should block forever
- # unless +stop+ is called.
- def join
- if @thread
- @thread.join
- else
- raise "Can't join on a node that isn't yet started"
- end
- end
-
# Blocking version of start, equivalent to +start+ then +join+
def start!
self.start
self.join
end
@@ -67,10 +48,31 @@
# Stops the reactors. If you were blocked on #join, that will unblock.
def stop
EM.stop
end
+ # When subclassing +DripDrop::Node+ you probably want to define this method
+ # Otherwise it will attempt to run the @block passed into +DripDrop::Node.new+
+ def action
+ if @block
+ self.instance_eval(&@block)
+ else
+ raise "Could not start, no block or specified"
+ end
+ end
+
+ # If the reactor has started, this blocks until the thread
+ # running the reactor joins. This should block forever
+ # unless +stop+ is called.
+ def join
+ if @thread
+ @thread.join
+ else
+ raise "Can't join on a node that isn't yet started"
+ end
+ end
+
# Defines a new route. Routes are the recommended way to instantiate
# handlers. For example:
#
# route :stats_pub, :zmq_publish, 'tcp://127.0.0.1:2200', :bind
# route :stats_sub, :zmq_subscribe, stats_pub.address, :connect
@@ -284,10 +286,10 @@
host_str = addr_uri.host
end
z_addr = "#{addr_uri.scheme}://#{host_str}:#{addr_uri.port.to_i}"
h_opts = handler_opts_given(opts)
- connection = EM::ZeroMQ.create @zctx, sock_type, socket_ctype, address, klass.new
+ connection = EM::ZeroMQ.create @zctx, sock_type, socket_ctype, address, klass.new(h_opts)
handler = connection.handler
handler.connection = connection
handler.post_setup
handler
end