Module: EventMachine

Defined in:
lib/em/future.rb,
lib/em/streamer.rb,
lib/em/eventable.rb,
lib/em/spawnable.rb,
lib/eventmachine.rb,
lib/em/deferrable.rb,
lib/evma/callback.rb,
lib/jeventmachine.rb,
lib/pr_eventmachine.rb,
lib/pr_eventmachine.rb,
lib/pr_eventmachine.rb,
lib/pr_eventmachine.rb,
lib/pr_eventmachine.rb,
lib/pr_eventmachine.rb,
lib/pr_eventmachine.rb,
lib/pr_eventmachine.rb,
lib/pr_eventmachine.rb,
lib/pr_eventmachine.rb,
lib/pr_eventmachine.rb,
lib/pr_eventmachine.rb,
lib/pr_eventmachine.rb,
lib/pr_eventmachine.rb,
lib/pr_eventmachine.rb,
lib/pr_eventmachine.rb,
lib/protocols/stomp.rb,
lib/protocols/tcptest.rb,
lib/protocols/httpcli2.rb,
lib/protocols/saslauth.rb,
lib/protocols/linetext2.rb,
lib/eventmachine_version.rb,
lib/protocols/httpclient.rb,
lib/protocols/smtpclient.rb,
lib/protocols/smtpserver.rb,
lib/protocols/line_and_text.rb,
lib/protocols/header_and_content.rb,
ext/rubymain.cpp

Overview

$Id: eventmachine_version.rb 608 2007-12-09 21:04:39Z blackhedd $

Author

Francis Cianfrocca (gmail: blackhedd)

Homepage

rubyeventmachine.com

Date

8 Apr 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, Eventable, Protocols, UuidGenerator Classes: Connection, DatagramObject, DefaultDeferrable, EM, Error, EvmaKeyboard, EvmaTCPClient, EvmaTCPServer, EvmaUDPSocket, EvmaUNIXClient, EvmaUNIXServer, FileStreamer, LoopbreakReader, PeriodicTimer, Reactor, Selectable, SpawnedProcess, StreamObject, Timer, YieldBlockFromSpawnedProcess

Constant Summary collapse

P =
EventMachine::Protocols
TimerFired =

TODO: These event numbers are defined in way too many places. DRY them up.

INT2NUM(100)
ConnectionData =
INT2NUM(101)
ConnectionUnbound =
INT2NUM(102)
ConnectionAccepted =
INT2NUM(103)
ConnectionCompleted =
INT2NUM(104)
LoopbreakSignalled =
INT2NUM(105)
VERSION =
"0.10.0"

Class Method Summary collapse

Class Method Details

._open_file_for_writing(filename, handler = nil) ⇒ Object



1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
# File 'lib/eventmachine.rb', line 1144

def _open_file_for_writing filename, handler=nil
  klass = if (handler and handler.is_a?(Class))
    handler
  else
    Class.new( Connection ) {handler and include handler}
  end

  s = _write_file filename
  c = klass.new s
  @conns[s] = c
  block_given? and yield c
  c
end

.add_oneshot_timer(interval) ⇒ Object

#add_oneshot_timer – Changed 04Oct06: intervals from the caller are now in milliseconds, but our native-ruby processor still wants them in seconds.



58
59
60
# File 'lib/pr_eventmachine.rb', line 58

def self.add_oneshot_timer interval
	@em.installOneshotTimer interval
end

.add_periodic_timer(*args, &block) ⇒ Object

EventMachine#add_periodic_timer adds a periodic timer to the event loop. It takes the same parameters as the one-shot timer method, EventMachine#add_timer. This method schedules execution of the given block repeatedly, at intervals of time at least as great as the number of seconds given in the first parameter to the call.

Usage example

The following sample program will write a dollar-sign to stderr every five seconds. (Of course if the program defined network clients and/or servers, they would be doing their work while the periodic timer is counting off.)

EventMachine::run {
  EventMachine::add_periodic_timer( 5 ) { $stderr.write "$" }
}


323
324
325
326
327
328
329
330
331
332
333
# File 'lib/eventmachine.rb', line 323

def EventMachine::add_periodic_timer *args, &block
  interval = args.shift
  code = args.shift || block
  if code
    block_1 = proc {
      code.call
      EventMachine::add_periodic_timer interval, code
    }
    add_timer interval, block_1
  end
end

.add_timer(*args, &block) ⇒ Object

EventMachine#add_timer adds a one-shot timer to the event loop. Call it with one or two parameters. The first parameters is a delay-time expressed in seconds (not milliseconds). The second parameter, if present, must be a proc object. If a proc object is not given, then you can also simply pass a block to the method call.

EventMachine#add_timer may be called from the block passed to EventMachine#run or from any callback method. It schedules execution of the proc or block passed to add_timer, after the passage of an interval of time equal to at least the number of seconds specified in the first parameter to the call.

EventMachine#add_timer is a non-blocking call. Callbacks can and will be called during the interval of time that the timer is in effect. There is no built-in limit to the number of timers that can be outstanding at any given time.

Usage example

This example shows how easy timers are to use. Observe that two timers are initiated simultaneously. Also, notice that the event loop will continue to run even after the second timer event is processed, since there was no call to EventMachine#stop_event_loop. There will be no activity, of course, since no network clients or servers are defined. Stop the program with Ctrl-C.

require 'rubygems'
require 'eventmachine'

EventMachine::run {
  puts "Starting the run now: #{Time.now}"
  EventMachine::add_timer 5, proc { puts "Executing timer event: #{Time.now}" }
  EventMachine::add_timer( 10 ) { puts "Executing timer event: #{Time.now}" }
}

– Changed 04Oct06: We now pass the interval as an integer number of milliseconds.



296
297
298
299
300
301
302
303
304
305
# File 'lib/eventmachine.rb', line 296

def EventMachine::add_timer *args, &block
  interval = args.shift
  code = args.shift || block
  if code
    # check too many timers!
    s = add_oneshot_timer((interval * 1000).to_i)
    @timers[s] = code
    s
  end
end

.close_connection(target, after_writing) ⇒ Object

#close_connection The extension version does NOT raise any kind of an error if an attempt is made to close a non-existent connection. Not sure whether we should. For now, we’ll raise an error here in that case.



92
93
94
# File 'lib/pr_eventmachine.rb', line 92

def self.close_connection sig, after_writing
	@em.closeConnection sig, after_writing
end

.connect(server, port, handler = nil, *args) ⇒ Object

EventMachine#connect initiates a TCP connection to a remote server and sets up event-handling for the connection. You can call EventMachine#connect in the block supplied to EventMachine#run or in any callback method.

EventMachine#connect takes the IP address (or hostname) and port of the remote server you want to connect to. It also takes an optional handler Module which you must define, that contains the callbacks that will be invoked by the event loop on behalf of the connection.

See the description of EventMachine#start_server for a discussion of the handler Module. All of the details given in that description apply for connections created with EventMachine#connect.

Usage Example

Here’s a program which connects to a web server, sends a naive request, parses the HTTP header of the response, and then (antisocially) ends the event loop, which automatically drops the connection (and incidentally calls the connection’s unbind method).

require 'rubygems'
require 'eventmachine'

module DumbHttpClient

  def post_init
    send_data "GET / HTTP/1.1\r\nHost: _\r\n\r\n"
    @data = ""
  end

  def receive_data data
    @data << data
    if  @data =~ /[\n][\r]*[\n]/m
      puts "RECEIVED HTTP HEADER:"
      $`.each {|line| puts ">>> #{line}" }

      puts "Now we'll terminate the loop, which will also close the connection"
      EventMachine::stop_event_loop
    end
  end

  def unbind
    puts "A connection has terminated"
  end

end # DumbHttpClient

EventMachine::run {
  EventMachine::connect "www.bayshorenetworks.com", 80, DumbHttpClient
}
puts "The event loop has ended"

There are times when it’s more convenient to define a protocol handler as a Class rather than a Module. Here’s how to do this:

class MyProtocolHandler < EventMachine::Connection
  def initialize *args
    super
    # whatever else you want to do here
  end

  #.......your other class code
end # class MyProtocolHandler

If you do this, then an instance of your class will be instantiated to handle every network connection created by your code or accepted by servers that you create. If you redefine #post_init in your protocol-handler class, your #post_init method will be called inside the call to #super that you will make in your #initialize method (if you provide one).

– EventMachine::connect initiates a TCP connection to a remote server and sets up event-handling for the connection. It internally creates an object that should not be handled by the caller. HOWEVER, it’s often convenient to get the object to set up interfacing to other objects in the system. We return the newly-created anonymous-class object to the caller. It’s expected that a considerable amount of code will depend on this behavior, so don’t change it.

Ok, added support for a user-defined block, 13Apr06. This leads us to an interesting choice because of the presence of the post_init call, which happens in the initialize method of the new object. We call the user’s block and pass the new object to it. This is a great way to do protocol-specific initiation. It happens AFTER post_init has been called on the object, which I certainly hope is the right choice. Don’t change this lightly, because accepted connections are different from connected ones and we don’t want to have them behave differently with respect to post_init if at all possible.



629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
# File 'lib/eventmachine.rb', line 629

def EventMachine::connect server, port, handler=nil, *args
  klass = if (handler and handler.is_a?(Class))
    handler
  else
    Class.new( Connection ) {handler and include handler}
  end

  arity = klass.instance_method(:initialize).arity
  expected = arity >= 0 ? arity : -(arity + 1)
  if (arity >= 0 and args.size != expected) or (arity < 0 and args.size < expected)
    raise ArgumentError, "wrong number of arguments for #{klass}#initialize (#{args.size} for #{expected})"
  end

  s = connect_server server, port
  c = klass.new s, *args
  @conns[s] = c
  block_given? and yield c
  c
end

.connect_server(host, port) ⇒ Object

#connect_server. Return a connection descriptor to the caller. TODO, what do we return here if we can’t connect?



78
79
80
# File 'lib/pr_eventmachine.rb', line 78

def self.connect_server server, port
	@em.connectTcpServer server, port
end

.connect_unix_domain(socketname, handler = nil, *args) ⇒ Object

Make a connection to a Unix-domain socket. This is not implemented on Windows platforms. The parameter socketname is a String which identifies the Unix-domain socket you want to connect to. socketname is the name of a file on your local system, and in most cases is a fully-qualified path name. Make sure that your process has enough local permissions to open the Unix-domain socket. See also the documentation for #connect_server. This method behaves like #connect_server in all respects except for the fact that it connects to a local Unix-domain socket rather than a TCP socket. NOTE: this functionality will soon be subsumed into the #connect method. This method will still be supported as an alias. – For making connections to Unix-domain sockets. Eventually this has to get properly documented and unified with the TCP-connect methods. Note how nearly identical this is to EventMachine#connect



689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
# File 'lib/eventmachine.rb', line 689

def EventMachine::connect_unix_domain socketname, handler=nil, *args
	klass = if (handler and handler.is_a?(Class))
		handler
	else
		Class.new( Connection ) {handler and include handler}
	end

   arity = klass.instance_method(:initialize).arity
   expected = arity >= 0 ? arity : -(arity + 1)
   if (arity >= 0 and args.size != expected) or (arity < 0 and args.size < expected)
     raise ArgumentError, "wrong number of arguments for #{klass}#initialize (#{args.size} for #{expected})"
   end

	s = connect_unix_server socketname
	c = klass.new s, *args
	@conns[s] = c
	block_given? and yield c
	c
end

.connect_unix_server(chain) ⇒ Object

#connect_unix_server



116
117
118
# File 'lib/pr_eventmachine.rb', line 116

def connect_unix_server chain
  EvmaUNIXClient.connect(chain).uuid
end

.defer(op, callback = nil) ⇒ Object

#defer is for integrating blocking operations into EventMachine’s control flow. Call #defer with one or two blocks, as shown below (the second block is optional):

operation = proc {
  # perform a long-running operation here, such as a database query.
  "result" # as usual, the last expression evaluated in the block will be the return value.
}
callback = proc {|result|
  # do something with result here, such as send it back to a network client.
}

EventMachine.defer( operation, callback )

The action of #defer is to take the block specified in the first parameter (the “operation”) and schedule it for asynchronous execution on an internal thread pool maintained by EventMachine. When the operation completes, it will pass the result computed by the block (if any) back to the EventMachine reactor. Then, EventMachine calls the block specified in the second parameter to #defer (the “callback”), as part of its normal, synchronous event handling loop. The result computed by the operation block is passed as a parameter to the callback. You may omit the callback parameter if you don’t need to execute any code after the operation completes.

Caveats: Note carefully that the code in your deferred operation will be executed on a separate thread from the main EventMachine processing and all other Ruby threads that may exist in your program. Also, multiple deferred operations may be running at once! Therefore, you are responsible for ensuring that your operation code is threadsafe. [Need more explanation and examples.] Don’t write a deferred operation that will block forever. If so, the current implementation will not detect the problem, and the thread will never be returned to the pool. EventMachine limits the number of threads in its pool, so if you do this enough times, your subsequent deferred operations won’t get a chance to run. [We might put in a timer to detect this problem.]

– OBSERVE that #next_tick hacks into this mechanism, so don’t make any changes here without syncing there.

Running with $VERBOSE set to true gives a warning unless all ivars are defined when they appear in rvalues. But we DON’T ever want to initialize @threadqueue unless we need it, because the Ruby threads are so heavyweight. We end up with this bizarre way of initializing @threadqueue because EventMachine is a Module, not a Class, and has no constructor.



899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
# File 'lib/eventmachine.rb', line 899

def self::defer op, callback = nil
	@need_threadqueue ||= 0
	if @need_threadqueue == 0
		@need_threadqueue = 1
		require 'thread'
		@threadqueue = Queue.new
		@resultqueue = Queue.new
		20.times {|ix|
			Thread.new {
				my_ix = ix
				loop {
					op,cback = @threadqueue.pop
					result = op.call
					@resultqueue << [result, cback]
					EventMachine.signal_loopbreak
				}
			}
		}
	end

	@threadqueue << [op,callback]
end

.epollObject

#epoll is a harmless no-op in the pure-Ruby implementation. This is intended to ensure that user code behaves properly across different EM implementations.



152
153
154
155
# File 'lib/pr_eventmachine.rb', line 152

def self.epoll
	# Epoll is a no-op for Java.
	# The latest Java versions run epoll when possible in NIO.
end

.event_callback(target, opcode, data) ⇒ Object



1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
# File 'lib/eventmachine.rb', line 1047

def EventMachine::event_callback conn_binding, opcode, data
	#
	# Added 03Oct07: Any code path that invokes user-written code must
	# wrap itself in a begin/rescue for RuntimeErrors, that calls the
	# user-overridable class method #handle_runtime_error.
	#
	if opcode == ConnectionData
		c = @conns[conn_binding] or raise ConnectionNotBound
		begin
			c.receive_data data
		rescue
			EventMachine.handle_runtime_error
		end
	elsif opcode == ConnectionUnbound
		if c = @conns.delete( conn_binding )
			begin
				c.unbind
			rescue
				EventMachine.handle_runtime_error
			end
		elsif c = @acceptors.delete( conn_binding )
			# no-op
		else
			raise ConnectionNotBound
		end
	elsif opcode == ConnectionAccepted
		accep,args,blk = @acceptors[conn_binding]
		raise NoHandlerForAcceptedConnection unless accep
		c = accep.new data, *args
		@conns[data] = c
		begin
			blk and blk.call(c)
		rescue
			EventMachine.handle_runtime_error
		end
		c # (needed?)
	elsif opcode == TimerFired
		t = @timers.delete( data ) or raise UnknownTimerFired
		begin
			t.call
		rescue
			EventMachine.handle_runtime_error
		end
	elsif opcode == ConnectionCompleted
		c = @conns[conn_binding] or raise ConnectionNotBound
		begin
			c.connection_completed
		rescue
			EventMachine.handle_runtime_error
		end
	elsif opcode == LoopbreakSignalled
		begin
		run_deferred_callbacks
		rescue
			EventMachine.handle_runtime_error
		end
	end
end

.get_outbound_data_size(sig) ⇒ Object

#get_outbound_data_size



181
182
183
184
# File 'lib/pr_eventmachine.rb', line 181

def get_outbound_data_size sig
  r = Reactor.instance.get_selectable( sig ) or raise "unknown get_outbound_data_size target"
  r.get_outbound_data_size
end

.get_peername(sig) ⇒ Object

#get_peername



126
127
128
129
# File 'lib/pr_eventmachine.rb', line 126

def get_peername sig
  selectable = Reactor.instance.get_selectable( sig ) or raise "unknown get_peername target"
  selectable.get_peername
end

.initialize_event_machineObject

#initialize_event_machine



50
51
52
# File 'lib/pr_eventmachine.rb', line 50

def self.initialize_event_machine
	@em = EM.new
end

.library_typeObject

This is mostly useful for automated tests. Return a distinctive symbol so the caller knows whether he’s dealing with an extension or with a pure-Ruby library.



45
46
47
# File 'lib/pr_eventmachine.rb', line 45

def self.library_type
	:java
end

.next_tick(pr = nil, &block) ⇒ Object

core. An advanced technique, this can be useful for improving memory management and/or application responsiveness, especially when scheduling large amounts of data for writing to a network connection. TODO, we need a FAQ entry on this subject.

#next_tick takes either a single argument (which must be a Proc) or a block. And I’m taking suggestions for a better name for this method. – This works by adding to the @resultqueue that’s used for #defer. The general idea is that next_tick is used when we want to give the reactor a chance to let other operations run, either to balance the load out more evenly, or to let outbound network buffers drain, or both. So we probably do NOT want to block, and we probably do NOT want to be spinning any threads. A program that uses next_tick but not #defer shouldn’t suffer the penalty of having Ruby threads running. They’re extremely expensive even if they’re just sleeping.



938
939
940
941
942
943
944
945
946
947
948
949
950
951
# File 'lib/eventmachine.rb', line 938

def self::next_tick pr=nil, &block
	raise "no argument or block given" unless ((pr && pr.respond_to?(:call)) or block)
	(@next_tick_queue ||= []) << ( pr || block )
	EventMachine.signal_loopbreak
=begin
	(@next_tick_procs ||= []) << (pr || block)
	if @next_tick_procs.length == 1
		add_timer(0) {
			@next_tick_procs.each {|t| t.call}
			@next_tick_procs.clear
		}
	end
=end
end

.open_datagram_socket(address, port, handler = nil, *args) ⇒ Object

EventMachine#open_datagram_socket is for support of UDP-based protocols. Its usage is similar to that of EventMachine#start_server. It takes three parameters: an IP address (which must be valid on the machine which executes the method), a port number, and an optional Module name which will handle the data. This method will create a new UDP (datagram) socket and bind it to the address and port that you specify. The normal callbacks (see EventMachine#start_server) will be called as events of interest occur on the newly-created socket, but there are some differences in how they behave.

Connection#receive_data will be called when a datagram packet is received on the socket, but unlike TCP sockets, the message boundaries of the received data will be respected. In other words, if the remote peer sent you a datagram of a particular size, you may rely on Connection#receive_data to give you the exact data in the packet, with the original data length. Also observe that Connection#receive_data may be called with a zero-length data payload, since empty datagrams are permitted in UDP.

Connection#send_data is available with UDP packets as with TCP, but there is an important difference. Because UDP communications are connectionless, there is no implicit recipient for the packets you send. Ordinarily you must specify the recipient for each packet you send. However, EventMachine provides for the typical pattern of receiving a UDP datagram from a remote peer, performing some operation, and then sending one or more packets in response to the same remote peer. To support this model easily, just use Connection#send_data in the code that you supply for Connection:receive_data. EventMachine will provide an implicit return address for any messages sent to Connection#send_data within the context of a Connection#receive_data callback, and your response will automatically go to the correct remote peer. (TODO: Example-code needed!)

Observe that the port number that you supply to EventMachine#open_datagram_socket may be zero. In this case, EventMachine will create a UDP socket that is bound to an ephemeral (not well-known) port. This is not appropriate for servers that must publish a well-known port to which remote peers may send datagrams. But it can be useful for clients that send datagrams to other servers. If you do this, you will receive any responses from the remote servers through the normal Connection#receive_data callback. Observe that you will probably have issues with firewalls blocking the ephemeral port numbers, so this technique is most appropriate for LANs. (TODO: Need an example!)

If you wish to send datagrams to arbitrary remote peers (not necessarily ones that have sent data to which you are responding), then see Connection#send_datagram.

DO NOT call send_data from a datagram socket outside of a #receive_data method. Use #send_datagram. If you do use #send_data outside of a #receive_data method, you’ll get a confusing error because there is no “peer,” as #send_data requires. (Inside of #receive_data, #send_data “fakes” the peer as described above.)

– Replaced the implementation on 01Oct06. Thanks to Tobias Gustafsson for pointing out that this originally did not take a class but only a module.



773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
# File 'lib/eventmachine.rb', line 773

def self::open_datagram_socket address, port, handler=nil, *args
	klass = if (handler and handler.is_a?(Class))
		handler
	else
		Class.new( Connection ) {handler and include handler}
	end

   arity = klass.instance_method(:initialize).arity
   expected = arity >= 0 ? arity : -(arity + 1)
   if (arity >= 0 and args.size != expected) or (arity < 0 and args.size < expected)
     raise ArgumentError, "wrong number of arguments for #{klass}#initialize (#{args.size} for #{expected})"
   end

	s = open_udp_socket address, port
	c = klass.new s, *args
	@conns[s] = c
	block_given? and yield c
	c
end

.open_keyboard(handler = nil) ⇒ Object

(Experimental)



1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
# File 'lib/eventmachine.rb', line 1030

def EventMachine::open_keyboard handler=nil
	klass = if (handler and handler.is_a?(Class))
		handler
	else
		Class.new( Connection ) {handler and include handler}
	end

	s = read_keyboard
	c = klass.new s
	@conns[s] = c
	block_given? and yield c
	c
end

.open_udp_socket(host, port) ⇒ Object

#open_udp_socket



99
100
101
# File 'lib/jeventmachine.rb', line 99

def self.open_udp_socket server, port
	@em.openUdpSocket server, port
end

.popen(cmd, handler = nil) {|c| ... } ⇒ Object

TODO, must document popen. At this moment, it’s only available on Unix. This limitation is expected to go away. – Perhaps misnamed since the underlying function uses socketpair and is full-duplex.

Yields:

  • (c)


995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
# File 'lib/eventmachine.rb', line 995

def self::popen cmd, handler=nil
	klass = if (handler and handler.is_a?(Class))
		handler
	else
		Class.new( Connection ) {handler and include handler}
	end

	w = Shellwords::shellwords( cmd )
	w.unshift( w.first ) if w.first
	s = invoke_popen( w )
	c = klass.new s
	@conns[s] = c
	yield(c) if block_given?
	c
end

.reactor_running?Boolean

Tells you whether the EventMachine reactor loop is currently running. Returns true or false. Useful when writing libraries that want to run event-driven code, but may be running in programs that are already event-driven. In such cases, if EventMachine#reactor_running? returns false, your code can invoke EventMachine#run and run your application code inside the block passed to that method. If EventMachine#reactor_running? returns true, just execute your event-aware code.

This method is necessary because calling EventMachine#run inside of another call to EventMachine#run generates a fatal error.

Returns:

  • (Boolean)


1022
1023
1024
# File 'lib/eventmachine.rb', line 1022

def self::reactor_running?
	(@reactor_running || false)
end

.read_keyboardObject

#read_keyboard



188
189
190
# File 'lib/pr_eventmachine.rb', line 188

def read_keyboard
  EvmaKeyboard.open.uuid
end

.reconnect(server, port, handler) ⇒ Object

– EXPERIMENTAL. DO NOT RELY ON THIS METHOD TO BE HERE IN THIS FORM, OR AT ALL. (03Nov06) Observe, the test for already-connected FAILS if we call a reconnect inside post_init, because we haven’t set up the connection in @conns by that point. RESIST THE TEMPTATION to “fix” this problem by redefining the behavior of post_init.

Changed 22Nov06: if called on an already-connected handler, just return the handler and do nothing more. Originally this condition raised an exception. We may want to change it yet again and call the block, if any.



661
662
663
664
665
666
667
668
669
670
# File 'lib/eventmachine.rb', line 661

def EventMachine::reconnect server, port, handler
	raise "invalid handler" unless handler.respond_to?(:connection_completed)
	#raise "still connected" if @conns.has_key?(handler.signature)
	return handler if @conns.has_key?(handler.signature)
	s = connect_server server, port
	handler.signature = s
	@conns[s] = handler
	block_given? and yield handler
	handler
end

.release_machineObject

release_machine. Probably a no-op.



68
69
70
# File 'lib/pr_eventmachine.rb', line 68

def self.release_machine
	@em = nil
end

.run(&block) ⇒ Object

EventMachine::run initializes and runs an event loop. This method only returns if user-callback code calls stop_event_loop. Use the supplied block to define your clients and servers. The block is called by EventMachine::run immediately after initializing its internal event loop but before running the loop. Therefore this block is the right place to call start_server if you want to accept connections from remote clients.

For programs that are structured as servers, it’s usually appropriate to start an event loop by calling EventMachine::run, and let it run forever. It’s also possible to use EventMachine::run to make a single client-connection to a remote server, process the data flow from that single connection, and then call stop_event_loop to force EventMachine::run to return. Your program will then continue from the point immediately following the call to EventMachine::run.

You can of course do both client and servers simultaneously in the same program. One of the strengths of the event-driven programming model is that the handling of network events on many different connections will be interleaved, and scheduled according to the actual events themselves. This maximizes efficiency.

Server usage example

See the text at the top of this file for an example of an echo server.

Client usage example

See the description of stop_event_loop for an extremely simple client example.

– Obsoleted the use_threads mechanism. 25Nov06: Added the begin/ensure block. We need to be sure that release_machine gets called even if an exception gets thrown within any of the user code that the event loop runs. The best way to see this is to run a unit test with two functions, each of which calls EventMachine#run and each of which throws something inside of #run. Without the ensure, the second test will start without release_machine being called and will immediately throw a C++ runtime error.



216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/eventmachine.rb', line 216

def EventMachine::run &block
	@conns = {}
	@acceptors = {}
	@timers = {}
	begin
 @reactor_running = true
 initialize_event_machine
 block and add_timer 0, block
 run_machine
	ensure
 release_machine
 @reactor_running = false
	end
end

.run_block(&block) ⇒ Object

Sugars a common use case. Will pass the given block to #run, but will terminate the reactor loop and exit the function as soon as the code in the block completes. (Normally, #run keeps running indefinitely, even after the block supplied to it finishes running, until user code calls #stop.)



237
238
239
240
241
242
243
# File 'lib/eventmachine.rb', line 237

def EventMachine::run_block &block
 pr = proc {
  block.call
  EventMachine::stop
 }
 run(&pr)
end

.run_deferred_callbacksObject

– The is the responder for the loopback-signalled event. It can be fired either by code running on a separate thread (EM#defer) or on the main thread (EM#next_tick). It will often happen that a next_tick handler will reschedule itself. We consume a copy of the tick queue so that tick events scheduled by tick events have to wait for the next pass through the reactor core.



827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
# File 'lib/eventmachine.rb', line 827

def self::run_deferred_callbacks # :nodoc:
	until (@resultqueue ||= []).empty?
		result,cback = @resultqueue.pop
		cback.call result if cback
	end

	@next_tick_queue ||= []
	if (l = @next_tick_queue.length) > 0
		l.times {|i| @next_tick_queue[i].call}
		@next_tick_queue.slice!( 0...l )
	end

=begin
	(@next_tick_queue ||= []).length.times {
		cback=@next_tick_queue.pop and cback.call
	}
=end
=begin
	if (@next_tick_queue ||= []) and @next_tick_queue.length > 0
		ary = @next_tick_queue.dup
		@next_tick_queue.clear
		until ary.empty?
			cback=ary.pop and cback.call
		end
	end
=end
end

.run_machineObject

run_machine



63
64
65
# File 'lib/pr_eventmachine.rb', line 63

def self.run_machine
	@em.run
end

.run_without_threads(&block) ⇒ Object

deprecated – EventMachine#run_without_threads is semantically identical to EventMachine#run, but it runs somewhat faster. However, it must not be used in applications that spin Ruby threads.



252
253
254
255
# File 'lib/eventmachine.rb', line 252

def EventMachine::run_without_threads &block
  #EventMachine::run false, &block
  EventMachine::run(&block)
end

.send_data(target, data, datalength) ⇒ Object

#send_data



83
84
85
# File 'lib/pr_eventmachine.rb', line 83

def self.send_data sig, data, length
	@em.sendData sig, data, length
end

.send_datagram(target, data, datalength, host, port) ⇒ Object

#send_datagram. This is currently only for UDP! We need to make it work with unix-domain sockets as well.



138
139
140
# File 'lib/pr_eventmachine.rb', line 138

def self.send_datagram sig, data, length, address, port
	@em.sendDatagram sig, data, length, address, port
end

.send_file_data(sig, filename) ⇒ Object

#send_file_data



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/pr_eventmachine.rb', line 167

def send_file_data sig, filename
  sz = File.size(filename)
  raise "file too large" if sz > 32*1024
  data =
  begin
    File.read filename
  rescue
    ""
  end
  send_data sig, data, data.length
end

.set_comm_inactivity_timeout(sig, tm) ⇒ Object

#set_comm_inactivity_timeout



194
195
196
197
# File 'lib/pr_eventmachine.rb', line 194

def set_comm_inactivity_timeout sig, tm
  r = Reactor.instance.get_selectable( sig ) or raise "unknown set_comm_inactivity_timeout target"
  r.set_inactivity_timeout tm
end

.set_descriptor_table_size(n_descriptors = nil) ⇒ Object

Sets the maximum number of file or socket descriptors that your process may open. You can pass this method an integer specifying the new size of the descriptor table. Returns the new descriptor-table size, which may be less than the number you requested. If you call this method with no arguments, it will simply return the current size of the descriptor table without attempting to change it.

The new limit on open descriptors ONLY applies to sockets and other descriptors that belong to EventMachine. It has NO EFFECT on the number of descriptors you can create in ordinary Ruby code.

Not available on all platforms. Increasing the number of descriptors beyond its default limit usually requires superuser privileges. (See #set_effective_user for a way to drop superuser privileges while your program is running.)



984
985
986
# File 'lib/eventmachine.rb', line 984

def self::set_descriptor_table_size n_descriptors=nil
	EventMachine::set_rlimit_nofile n_descriptors
end

.set_effective_user(username) ⇒ Object

A wrapper over the setuid system call. Particularly useful when opening a network server on a privileged port because you can use this call to drop privileges after opening the port. Also very useful after a call to #set_descriptor_table_size, which generally requires that you start your process with root privileges.

This method has no effective implementation on Windows or in the pure-Ruby implementation of EventMachine. Call #set_effective_user by passing it a string containing the effective name of the user whose privilege-level your process should attain. This method is intended for use in enforcing security requirements, consequently it will throw a fatal error and end your program if it fails.



965
966
967
# File 'lib/eventmachine.rb', line 965

def self::set_effective_user username
	EventMachine::setuid_string username
end

.set_max_timer_count(n) ⇒ Object

#set_max_timer_count is a harmless no-op in pure Ruby, which doesn’t have a built-in limit on the number of available timers.



163
164
# File 'lib/pr_eventmachine.rb', line 163

def set_max_timer_count n
end

.set_max_timers(ct) ⇒ Object

Sets the maximum number of timers and periodic timers that may be outstanding at any given time. You only need to call #set_max_timers if you need more than the default number of timers, which on most platforms is 1000. Call this method before calling EventMachine#run.



815
816
817
# File 'lib/eventmachine.rb', line 815

def self::set_max_timers ct
	set_max_timer_count ct
end

.set_quantum(mills) ⇒ Object

For advanced users. This function sets the default timer granularity, which by default is slightly smaller than 100 milliseconds. Call this function to set a higher or lower granularity. The function affects the behavior of #add_timer and #add_periodic_timer. Most applications will not need to call this function.

The argument is a number of milliseconds. Avoid setting the quantum to very low values because that may reduce performance under some extreme conditions. We recommend that you not set a quantum lower than 10.

You may only call this function while an EventMachine loop is running (that is, after a call to EventMachine#run and before a subsequent call to EventMachine#stop).



806
807
808
# File 'lib/eventmachine.rb', line 806

def self::set_quantum mills
	set_timer_quantum mills.to_i
end

.set_rlimit_nofile(n) ⇒ Object

#set_rlimit_nofile is a no-op in the pure-Ruby implementation. We simply return Ruby’s built-in per-process file-descriptor limit.



96
97
98
# File 'lib/jeventmachine.rb', line 96

def self.set_rlimit_nofile n_descriptors
	# Currently a no-op for Java.
end

.set_timer_quantum(interval) ⇒ Object

#set_timer_quantum in milliseconds. The underlying Reactor function wants a (possibly fractional) number of seconds.



146
147
148
# File 'lib/pr_eventmachine.rb', line 146

def self.set_timer_quantum q
	@em.setTimerQuantum q
end

.signal_loopbreakObject

#signal_loopbreak



121
122
123
# File 'lib/pr_eventmachine.rb', line 121

def self.signal_loopbreak
	@em.signalLoopbreak
end

.spawn(&block) ⇒ Object



72
73
74
75
76
# File 'lib/em/spawnable.rb', line 72

def EventMachine.spawn &block
	s = SpawnedProcess.new
	s.set_receiver block
	s
end

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

EventMachine::start_server initiates a TCP server (socket acceptor) on the specified IP address and port. The IP address must be valid on the machine where the program runs, and the process must be privileged enough to listen on the specified port (on Unix-like systems, superuser privileges are usually required to listen on any port lower than 1024). Only one listener may be running on any given address/port combination. start_server will fail if the given address and port are already listening on the machine, either because of a prior call to start_server or some unrelated process running on the machine. If start_server succeeds, the new network listener becomes active immediately and starts accepting connections from remote peers, and these connections generate callback events that are processed by the code specified in the handler parameter to start_server.

The optional handler which is passed to start_server is the key to EventMachine’s ability to handle particular network protocols. The handler parameter passed to start_server must be a Ruby Module that you must define. When the network server that is started by start_server accepts a new connection, it instantiates a new object of an anonymous class that is inherited from EventMachine::Connection, into which the methods from your handler have been mixed. Your handler module may redefine any of the methods in EventMachine::Connection in order to implement the specific behavior of the network protocol.

Callbacks invoked in response to network events always take place within the execution context of the object derived from EventMachine::Connection extended by your handler module. There is one object per connection, and all of the callbacks invoked for a particular connection take the form of instance methods called against the corresponding EventMachine::Connection object. Therefore, you are free to define whatever instance variables you wish, in order to contain the per-connection state required by the network protocol you are implementing.

start_server is often called inside the block passed to EventMachine::run, but it can be called from any EventMachine callback. start_server will fail unless the EventMachine event loop is currently running (which is why it’s often called in the block suppled to EventMachine::run).

You may call start_server any number of times to start up network listeners on different address/port combinations. The servers will all run simultaneously. More interestingly, each individual call to start_server can specify a different handler module and thus implement a different network protocol from all the others.

Usage example

Here is an example of a server that counts lines of input from the remote peer and sends back the total number of lines received, after each line. Try the example with more than one client connection opened via telnet, and you will see that the line count increments independently on each of the client connections. Also very important to note, is that the handler for the receive_data function, which our handler redefines, may not assume that the data it receives observes any kind of message boundaries. Also, to use this example, be sure to change the server and port parameters to the start_server call to values appropriate for your environment.

require 'rubygems'
require 'eventmachine'

module LineCounter

  MaxLinesPerConnection = 10

  def post_init
    puts "Received a new connection"
    @data_received = ""
    @line_count = 0
  end

  def receive_data data
    @data_received << data
    while @data_received.slice!( /^[^\n]*[\n]/m )
      @line_count += 1
      send_data "received #{@line_count} lines so far\r\n"
      @line_count == MaxLinesPerConnection and close_connection_after_writing
    end
  end

end # module LineCounter

EventMachine::run {
  host,port = "192.168.0.100", 8090
  EventMachine::start_server host, port, LineCounter
  puts "Now accepting connections on address #{host}, port #{port}..."
  EventMachine::add_periodic_timer( 10 ) { $stderr.write "*" }
}


487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/eventmachine.rb', line 487

def EventMachine::start_server server, port, handler=nil, *args, &block
  klass = if (handler and handler.is_a?(Class))
    handler
  else
    Class.new( Connection ) {handler and include handler}
  end

  arity = klass.instance_method(:initialize).arity
  expected = arity >= 0 ? arity : -(arity + 1)
  if (arity >= 0 and args.size != expected) or (arity < 0 and args.size < expected)
    raise ArgumentError, "wrong number of arguments for #{klass}#initialize (#{args.size} for #{expected})" 
  end

  s = start_tcp_server server, port
  @acceptors[s] = [klass,args,block]
  s
end

.start_tcp_server(host, port) ⇒ Object

#start_tcp_server



98
99
100
# File 'lib/pr_eventmachine.rb', line 98

def self.start_tcp_server server, port
	@em.startTcpServer server, port
end

.start_tls(sig) ⇒ Object



83
84
85
# File 'lib/jeventmachine.rb', line 83

def self.start_tls sig
	@em.startTls sig
end

.start_unix_domain_server(filename, handler = nil, *args, &block) ⇒ Object



515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/eventmachine.rb', line 515

def EventMachine::start_unix_domain_server filename, handler=nil, *args, &block
  klass = if (handler and handler.is_a?(Class))
    handler
  else
    Class.new( Connection ) {handler and include handler}
  end

  arity = klass.instance_method(:initialize).arity
  expected = arity >= 0 ? arity : -(arity + 1)
  if (arity >= 0 and args.size != expected) or (arity < 0 and args.size < expected)
    raise ArgumentError, "wrong number of arguments for #{klass}#initialize (#{args.size} for #{expected})" 
  end

  s = start_unix_server filename
  @acceptors[s] = [klass,args,block]
end

.start_unix_server(chain) ⇒ Object

#start_unix_server



110
111
112
113
# File 'lib/pr_eventmachine.rb', line 110

def start_unix_server chain
  (s = EvmaUNIXServer.start_server chain) or raise "no acceptor"
  s.uuid
end

.stopObject

#stop



72
73
74
# File 'lib/pr_eventmachine.rb', line 72

def self.stop
	@em.stop
end

.stop_event_loopObject

stop_event_loop may called from within a callback method while EventMachine’s processing loop is running. It causes the processing loop to stop executing, which will cause all open connections and accepting servers to be run down and closed. Callbacks for connection-termination will be called as part of the processing of stop_event_loop. (There currently is no option to panic-stop the loop without closing connections.) When all of this processing is complete, the call to EventMachine::run which started the processing loop will return and program flow will resume from the statement following EventMachine::run call.

Usage example

require 'rubygems'
require 'eventmachine'

module Redmond

  def post_init
    puts "We're sending a dumb HTTP request to the remote peer."
    send_data "GET / HTTP/1.1\r\nHost: www.microsoft.com\r\n\r\n"
  end

  def receive_data data
    puts "We received #{data.length} bytes from the remote peer."
    puts "We're going to stop the event loop now."
    EventMachine::stop_event_loop
  end

  def unbind
    puts "A connection has terminated."
  end

end

puts "We're starting the event loop now."
EventMachine::run {
  EventMachine::connect "www.microsoft.com", 80, Redmond
}
puts "The event loop has stopped."

This program will produce approximately the following output:

We're starting the event loop now.
We're sending a dumb HTTP request to the remote peer.
We received 1440 bytes from the remote peer.
We're going to stop the event loop now.
A connection has terminated.
The event loop has stopped.


395
396
397
# File 'lib/eventmachine.rb', line 395

def EventMachine::stop_event_loop
  EventMachine::stop
end

.stop_server(signature) ⇒ Object

Stop a TCP server socket that was started with EventMachine#start_server. – Requested by Kirk Haines. TODO, this isn’t OOP enough. We ought somehow to have #start_server return an object that has a close or a stop method on it.



511
512
513
# File 'lib/eventmachine.rb', line 511

def EventMachine::stop_server signature
 EventMachine::stop_tcp_server signature
end

.stop_tcp_server(sig) ⇒ Object

#stop_tcp_server



104
105
106
# File 'lib/pr_eventmachine.rb', line 104

def self.stop_tcp_server sig
	@em.stopTcpServer sig
end

.yield(&block) ⇒ Object



78
79
80
# File 'lib/em/spawnable.rb', line 78

def EventMachine.yield &block
	return YieldBlockFromSpawnedProcess.new( block, false )
end

.yield_and_notify(&block) ⇒ Object



82
83
84
# File 'lib/em/spawnable.rb', line 82

def EventMachine.yield_and_notify &block
	return YieldBlockFromSpawnedProcess.new( block, true )
end