Module: EventMachine

Defined in:
lib/em/deferrable.rb,
lib/foxbat.rb,
lib/em/timer.rb,
lib/eventmachine.rb,
lib/em/connection.rb,
lib/em/periodic_timer.rb

Overview

Author

Francis Cianfrocca (gmail: blackhedd)

Homepage

rubyeventmachine.com

Date

16 Jul 2006

See EventMachine and EventMachine::Connection for documentation and usage examples.


Copyright © 2006-07 by Francis Cianfrocca. All Rights Reserved. Gmail: blackhedd

This program is free software; you can redistribute it and/or modify it under the terms of either: 1) the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version; or 2) Ruby’s License.

See the file COPYING for complete licensing information.


Defined Under Namespace

Modules: Deferrable Classes: Connection, DefaultDeferrable, PeriodicTimer, Timer

Class Method Summary collapse

Class Method Details

.add_timer(*args, &block) ⇒ Object



38
39
40
41
42
43
# File 'lib/eventmachine.rb', line 38

def self.add_timer(*args, &block)
  timeout = args.shift
  callable = args.shift || block
  task = lambda { |t| callable.call }
  @@timer.newTimeout(task, timeout, TimeUnit::SECONDS)
end

.connect(host, port = nil, handler = nil, *args, &block) ⇒ Object



19
20
21
22
# File 'lib/eventmachine.rb', line 19

def self.connect(host, port=nil, handler=nil, *args, &block)
  c = Foxbat::Client.new(host, port, handler, args.first ||  {}, &block)
  c.start(@@threadpool)
end

.connection_countObject



59
60
61
# File 'lib/eventmachine.rb', line 59

def self.connection_count
  @@servers.map { |s| s.connection_count }.reduce(:+)
end

.defer(op, callback) ⇒ Object



51
52
53
# File 'lib/eventmachine.rb', line 51

def self.defer(op, callback)
  Foxbat::Future.schedule(op, callback, @@threadpool)
end

.epollObject

We’re on the JVM- this does nothing!



25
# File 'lib/eventmachine.rb', line 25

def self.epoll; end

.kqueueObject



26
# File 'lib/eventmachine.rb', line 26

def self.kqueue; end

.reactor_running?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/eventmachine.rb', line 55

def self.reactor_running?
  @alive
end

.run(blk = nil, tail = nil, &block) ⇒ Object



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

def self.run(blk=nil, tail=nil, &block)
  @alive = true
  @@threadpool = Executors.newCachedThreadPool
  @@timer = HashedWheelTimer.new

  block.call

  @@threadpool.awaitTermination(Long::MAX_VALUE, TimeUnit::SECONDS)
end

.start_server(host, port = nil, handler = nil, *args, &block) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/eventmachine.rb', line 10

def self.start_server(host, port=nil, handler=nil, *args, &block)
  s = Foxbat::Server.new(host, port, handler, args.first || {}, &block)

  @@servers ||= []
  @@servers << s

  s.start(@@threadpool)
end

.stopObject



45
46
47
48
49
# File 'lib/eventmachine.rb', line 45

def self.stop
  @@servers.each { |s| s.stop }
  @@threadpool.shutdown
  @@timer.stop
end