Permalink
Browse files

- Add EventMachine::Timer support

  • Loading branch information...
1 parent c017ab2 commit 85fa5277ca83e136a4d2f3489e5f74bb72b97fe9 @jordansissel committed Apr 11, 2011
Showing with 47 additions and 2 deletions.
  1. +8 −2 lib/netty-eventmachine/em_api.rb
  2. +14 −0 lib/netty-eventmachine/netty/timertask.rb
  3. +25 −0 lib/netty-eventmachine/timer.rb
@@ -3,6 +3,7 @@
require "java"
require "netty-eventmachine/connection"
+require "netty-eventmachine/timer"
module EventMachine
java_import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory
@@ -34,7 +35,12 @@ def self.start_server(address, port=nil, handlerclass=nil, *args, &block)
public # EventMachine API, run
def self.run(&block)
- # Nothing to do
block.call
- end
+ # TODO(sissel): we should block until netty exits.
+ end # def run
+
+ public # EventMachine API add_timer
+ def self.add_timer(delay, &block)
+ return EventMachine::Timer.new(delay, &block)
+ end # def add_timer
end
@@ -0,0 +1,14 @@
+require "netty-eventmachine/namespace"
+require "java"
+
+class EventMachine::Netty::TimerTask
+ include org.jboss.netty.util.TimerTask
+
+ def initialize(&block)
+ @block = block
+ end
+
+ def run(timeout)
+ @block.call
+ end
+end
@@ -0,0 +1,25 @@
+require "netty-eventmachine/namespace"
+require "netty-eventmachine/netty/timertask"
+require "java"
+
+class EventMachine::Timer
+ public
+ def initialize(delay, &block)
+ # Only want one HashedWheelTimer instance per application.
+ # TODO(sissel): Make the tick interval configurable? Default is 100ms
+ # TODO(sissel): Tunable size of wheel? (Number of schedulable tasks)
+ @@timer ||= org.jboss.netty.util.HashedWheelTimer.new
+ @timertask = EventMachine::Netty::TimerTask.new(&block)
+
+ # Convert to ms
+ delay_ms = delay * 1000
+ @timeout = @@timer.newTimeout(@timertask, delay_ms,
+ java.util.concurrent.TimeUnit::MILLISECONDS)
+ end # def initialize
+
+ # EventMachine::Timer#cancel
+ public
+ def cancel
+ @timeout.cancel
+ end # def cancel
+end

0 comments on commit 85fa527

Please sign in to comment.