smparkes-eventmachine 0.12.10
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +15 -0
- data/README +81 -0
- data/Rakefile +374 -0
- data/docs/COPYING +60 -0
- data/docs/ChangeLog +211 -0
- data/docs/DEFERRABLES +133 -0
- data/docs/EPOLL +141 -0
- data/docs/GNU +281 -0
- data/docs/INSTALL +13 -0
- data/docs/KEYBOARD +38 -0
- data/docs/LEGAL +25 -0
- data/docs/LIGHTWEIGHT_CONCURRENCY +70 -0
- data/docs/PURE_RUBY +75 -0
- data/docs/RELEASE_NOTES +94 -0
- data/docs/SMTP +2 -0
- data/docs/SPAWNED_PROCESSES +89 -0
- data/docs/TODO +8 -0
- data/eventmachine.gemspec +40 -0
- data/examples/ex_channel.rb +43 -0
- data/examples/ex_queue.rb +2 -0
- data/examples/helper.rb +2 -0
- data/ext/binder.cpp +125 -0
- data/ext/binder.h +46 -0
- data/ext/cmain.cpp +827 -0
- data/ext/cplusplus.cpp +202 -0
- data/ext/ed.cpp +1901 -0
- data/ext/ed.h +424 -0
- data/ext/em.cpp +2288 -0
- data/ext/em.h +229 -0
- data/ext/emwin.cpp +300 -0
- data/ext/emwin.h +94 -0
- data/ext/epoll.cpp +26 -0
- data/ext/epoll.h +25 -0
- data/ext/eventmachine.h +122 -0
- data/ext/eventmachine_cpp.h +96 -0
- data/ext/extconf.rb +150 -0
- data/ext/fastfilereader/extconf.rb +85 -0
- data/ext/fastfilereader/mapper.cpp +214 -0
- data/ext/fastfilereader/mapper.h +59 -0
- data/ext/fastfilereader/rubymain.cpp +127 -0
- data/ext/files.cpp +94 -0
- data/ext/files.h +65 -0
- data/ext/kb.cpp +81 -0
- data/ext/page.cpp +107 -0
- data/ext/page.h +51 -0
- data/ext/pipe.cpp +349 -0
- data/ext/project.h +156 -0
- data/ext/rubymain.cpp +1194 -0
- data/ext/sigs.cpp +89 -0
- data/ext/sigs.h +32 -0
- data/ext/ssl.cpp +460 -0
- data/ext/ssl.h +94 -0
- data/java/.classpath +8 -0
- data/java/.project +17 -0
- data/java/src/com/rubyeventmachine/EmReactor.java +570 -0
- data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
- data/java/src/com/rubyeventmachine/EventableChannel.java +69 -0
- data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +189 -0
- data/java/src/com/rubyeventmachine/EventableSocketChannel.java +364 -0
- data/java/src/com/rubyeventmachine/application/Application.java +194 -0
- data/java/src/com/rubyeventmachine/application/Connection.java +74 -0
- data/java/src/com/rubyeventmachine/application/ConnectionFactory.java +37 -0
- data/java/src/com/rubyeventmachine/application/DefaultConnectionFactory.java +46 -0
- data/java/src/com/rubyeventmachine/application/PeriodicTimer.java +38 -0
- data/java/src/com/rubyeventmachine/application/Timer.java +54 -0
- data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +109 -0
- data/java/src/com/rubyeventmachine/tests/ConnectTest.java +148 -0
- data/java/src/com/rubyeventmachine/tests/EMTest.java +80 -0
- data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +53 -0
- data/java/src/com/rubyeventmachine/tests/TestServers.java +75 -0
- data/java/src/com/rubyeventmachine/tests/TestTimers.java +90 -0
- data/lib/em/buftok.rb +138 -0
- data/lib/em/callback.rb +26 -0
- data/lib/em/channel.rb +57 -0
- data/lib/em/connection.rb +564 -0
- data/lib/em/deferrable.rb +192 -0
- data/lib/em/file_watch.rb +54 -0
- data/lib/em/future.rb +61 -0
- data/lib/em/messages.rb +66 -0
- data/lib/em/process_watch.rb +44 -0
- data/lib/em/processes.rb +119 -0
- data/lib/em/protocols/header_and_content.rb +138 -0
- data/lib/em/protocols/httpclient.rb +263 -0
- data/lib/em/protocols/httpclient2.rb +590 -0
- data/lib/em/protocols/line_and_text.rb +125 -0
- data/lib/em/protocols/linetext2.rb +161 -0
- data/lib/em/protocols/memcache.rb +323 -0
- data/lib/em/protocols/object_protocol.rb +45 -0
- data/lib/em/protocols/postgres3.rb +247 -0
- data/lib/em/protocols/saslauth.rb +175 -0
- data/lib/em/protocols/smtpclient.rb +357 -0
- data/lib/em/protocols/smtpserver.rb +547 -0
- data/lib/em/protocols/socks4.rb +66 -0
- data/lib/em/protocols/stomp.rb +200 -0
- data/lib/em/protocols/tcptest.rb +53 -0
- data/lib/em/protocols.rb +36 -0
- data/lib/em/queue.rb +61 -0
- data/lib/em/spawnable.rb +85 -0
- data/lib/em/streamer.rb +130 -0
- data/lib/em/timers.rb +56 -0
- data/lib/em/version.rb +3 -0
- data/lib/eventmachine.rb +1592 -0
- data/lib/evma/callback.rb +32 -0
- data/lib/evma/container.rb +75 -0
- data/lib/evma/factory.rb +77 -0
- data/lib/evma/protocol.rb +87 -0
- data/lib/evma/reactor.rb +48 -0
- data/lib/evma.rb +32 -0
- data/lib/jeventmachine.rb +257 -0
- data/lib/pr_eventmachine.rb +1022 -0
- data/setup.rb +1585 -0
- data/tasks/cpp.rake_example +77 -0
- data/tests/client.crt +31 -0
- data/tests/client.key +51 -0
- data/tests/test_attach.rb +126 -0
- data/tests/test_basic.rb +284 -0
- data/tests/test_channel.rb +63 -0
- data/tests/test_connection_count.rb +35 -0
- data/tests/test_defer.rb +47 -0
- data/tests/test_epoll.rb +160 -0
- data/tests/test_error_handler.rb +35 -0
- data/tests/test_errors.rb +82 -0
- data/tests/test_exc.rb +55 -0
- data/tests/test_file_watch.rb +49 -0
- data/tests/test_futures.rb +198 -0
- data/tests/test_get_sock_opt.rb +30 -0
- data/tests/test_handler_check.rb +37 -0
- data/tests/test_hc.rb +218 -0
- data/tests/test_httpclient.rb +218 -0
- data/tests/test_httpclient2.rb +153 -0
- data/tests/test_inactivity_timeout.rb +50 -0
- data/tests/test_kb.rb +60 -0
- data/tests/test_ltp.rb +182 -0
- data/tests/test_ltp2.rb +317 -0
- data/tests/test_next_tick.rb +133 -0
- data/tests/test_object_protocol.rb +37 -0
- data/tests/test_pause.rb +70 -0
- data/tests/test_pending_connect_timeout.rb +48 -0
- data/tests/test_process_watch.rb +48 -0
- data/tests/test_processes.rb +128 -0
- data/tests/test_proxy_connection.rb +92 -0
- data/tests/test_pure.rb +125 -0
- data/tests/test_queue.rb +44 -0
- data/tests/test_running.rb +42 -0
- data/tests/test_sasl.rb +72 -0
- data/tests/test_send_file.rb +242 -0
- data/tests/test_servers.rb +76 -0
- data/tests/test_smtpclient.rb +83 -0
- data/tests/test_smtpserver.rb +85 -0
- data/tests/test_spawn.rb +322 -0
- data/tests/test_ssl_args.rb +79 -0
- data/tests/test_ssl_methods.rb +50 -0
- data/tests/test_ssl_verify.rb +82 -0
- data/tests/test_timers.rb +162 -0
- data/tests/test_ud.rb +36 -0
- data/tests/testem.rb +31 -0
- data/web/whatis +7 -0
- metadata +237 -0
@@ -0,0 +1,200 @@
|
|
1
|
+
#--
|
2
|
+
#
|
3
|
+
# Author:: Francis Cianfrocca (gmail: blackhedd)
|
4
|
+
# Homepage:: http://rubyeventmachine.com
|
5
|
+
# Date:: 15 November 2006
|
6
|
+
#
|
7
|
+
# See EventMachine and EventMachine::Connection for documentation and
|
8
|
+
# usage examples.
|
9
|
+
#
|
10
|
+
#----------------------------------------------------------------------------
|
11
|
+
#
|
12
|
+
# Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
|
13
|
+
# Gmail: blackhedd
|
14
|
+
#
|
15
|
+
# This program is free software; you can redistribute it and/or modify
|
16
|
+
# it under the terms of either: 1) the GNU General Public License
|
17
|
+
# as published by the Free Software Foundation; either version 2 of the
|
18
|
+
# License, or (at your option) any later version; or 2) Ruby's License.
|
19
|
+
#
|
20
|
+
# See the file COPYING for complete licensing information.
|
21
|
+
#
|
22
|
+
#---------------------------------------------------------------------------
|
23
|
+
#
|
24
|
+
#
|
25
|
+
#
|
26
|
+
|
27
|
+
module EventMachine
|
28
|
+
module Protocols
|
29
|
+
|
30
|
+
# Implements Stomp (http://docs.codehaus.org/display/STOMP/Protocol).
|
31
|
+
#
|
32
|
+
# == Usage example
|
33
|
+
#
|
34
|
+
# module StompClient
|
35
|
+
# include EM::Protocols::Stomp
|
36
|
+
#
|
37
|
+
# def connection_completed
|
38
|
+
# connect :login => 'guest', :passcode => 'guest'
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
# def receive_msg msg
|
42
|
+
# if msg.command == "CONNECTED"
|
43
|
+
# subscribe '/some/topic'
|
44
|
+
# else
|
45
|
+
# p ['got a message', msg]
|
46
|
+
# puts msg.body
|
47
|
+
# end
|
48
|
+
# end
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
# EM.run{
|
52
|
+
# EM.connect 'localhost', 61613, StompClient
|
53
|
+
# }
|
54
|
+
#
|
55
|
+
module Stomp
|
56
|
+
include LineText2
|
57
|
+
|
58
|
+
class Message
|
59
|
+
# The command associated with the message, usually 'CONNECTED' or 'MESSAGE'
|
60
|
+
attr_accessor :command
|
61
|
+
# Hash containing headers such as destination and message-id
|
62
|
+
attr_accessor :header
|
63
|
+
alias :headers :header
|
64
|
+
# Body of the message
|
65
|
+
attr_accessor :body
|
66
|
+
|
67
|
+
def initialize # :nodoc:
|
68
|
+
@header = {}
|
69
|
+
@state = :precommand
|
70
|
+
@content_length = nil
|
71
|
+
end
|
72
|
+
def consume_line line # :nodoc:
|
73
|
+
if @state == :precommand
|
74
|
+
unless line =~ /\A\s*\Z/
|
75
|
+
@command = line
|
76
|
+
@state = :headers
|
77
|
+
end
|
78
|
+
elsif @state == :headers
|
79
|
+
if line == ""
|
80
|
+
if @content_length
|
81
|
+
yield( [:sized_text, @content_length+1] )
|
82
|
+
else
|
83
|
+
@state = :body
|
84
|
+
yield( [:unsized_text] )
|
85
|
+
end
|
86
|
+
elsif line =~ /\A([^:]+):(.+)\Z/
|
87
|
+
k = $1.dup.strip
|
88
|
+
v = $2.dup.strip
|
89
|
+
@header[k] = v
|
90
|
+
if k == "content-length"
|
91
|
+
@content_length = v.to_i
|
92
|
+
end
|
93
|
+
else
|
94
|
+
# This is a protocol error. How to signal it?
|
95
|
+
end
|
96
|
+
elsif @state == :body
|
97
|
+
@body = line
|
98
|
+
yield( [:dispatch] )
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
# :stopdoc:
|
104
|
+
|
105
|
+
def send_frame verb, headers={}, body=""
|
106
|
+
ary = [verb, "\n"]
|
107
|
+
headers.each {|k,v| ary << "#{k}:#{v}\n" }
|
108
|
+
ary << "content-length: #{body.to_s.length}\n"
|
109
|
+
ary << "content-type: text/plain; charset=UTF-8\n"
|
110
|
+
ary << "\n"
|
111
|
+
ary << body.to_s
|
112
|
+
ary << "\0"
|
113
|
+
send_data ary.join
|
114
|
+
end
|
115
|
+
|
116
|
+
def receive_line line
|
117
|
+
@stomp_initialized || init_message_reader
|
118
|
+
@stomp_message.consume_line(line) {|outcome|
|
119
|
+
if outcome.first == :sized_text
|
120
|
+
set_text_mode outcome[1]
|
121
|
+
elsif outcome.first == :unsized_text
|
122
|
+
set_delimiter "\0"
|
123
|
+
elsif outcome.first == :dispatch
|
124
|
+
receive_msg(@stomp_message) if respond_to?(:receive_msg)
|
125
|
+
init_message_reader
|
126
|
+
end
|
127
|
+
}
|
128
|
+
end
|
129
|
+
|
130
|
+
def receive_binary_data data
|
131
|
+
@stomp_message.body = data[0..-2]
|
132
|
+
receive_msg(@stomp_message) if respond_to?(:receive_msg)
|
133
|
+
init_message_reader
|
134
|
+
end
|
135
|
+
|
136
|
+
def init_message_reader
|
137
|
+
@stomp_initialized = true
|
138
|
+
set_delimiter "\n"
|
139
|
+
set_line_mode
|
140
|
+
@stomp_message = Message.new
|
141
|
+
end
|
142
|
+
|
143
|
+
# :startdoc:
|
144
|
+
|
145
|
+
# Invoked with an incoming Stomp::Message received from the STOMP server
|
146
|
+
def receive_msg msg
|
147
|
+
# stub, overwrite this in your handler
|
148
|
+
end
|
149
|
+
|
150
|
+
# CONNECT command, for authentication
|
151
|
+
#
|
152
|
+
# connect :login => 'guest', :passcode => 'guest'
|
153
|
+
#
|
154
|
+
def connect parms={}
|
155
|
+
send_frame "CONNECT", parms
|
156
|
+
end
|
157
|
+
|
158
|
+
# SEND command, for publishing messages to a topic
|
159
|
+
#
|
160
|
+
# send '/topic/name', 'some message here'
|
161
|
+
#
|
162
|
+
def send destination, body, parms={}
|
163
|
+
send_frame "SEND", parms.merge( :destination=>destination ), body.to_s
|
164
|
+
end
|
165
|
+
|
166
|
+
# SUBSCRIBE command, for subscribing to topics
|
167
|
+
#
|
168
|
+
# subscribe '/topic/name', false
|
169
|
+
#
|
170
|
+
def subscribe dest, ack=false
|
171
|
+
send_frame "SUBSCRIBE", {:destination=>dest, :ack=>(ack ? "client" : "auto")}
|
172
|
+
end
|
173
|
+
|
174
|
+
# ACK command, for acknowledging receipt of messages
|
175
|
+
#
|
176
|
+
# module StompClient
|
177
|
+
# include EM::P::Stomp
|
178
|
+
#
|
179
|
+
# def connection_completed
|
180
|
+
# connect :login => 'guest', :passcode => 'guest'
|
181
|
+
# # subscribe with ack mode
|
182
|
+
# subscribe '/some/topic', true
|
183
|
+
# end
|
184
|
+
#
|
185
|
+
# def receive_msg msg
|
186
|
+
# if msg.command == "MESSAGE"
|
187
|
+
# ack msg.headers['message-id']
|
188
|
+
# puts msg.body
|
189
|
+
# end
|
190
|
+
# end
|
191
|
+
# end
|
192
|
+
#
|
193
|
+
def ack msgid
|
194
|
+
send_frame "ACK", 'message-id'=> msgid
|
195
|
+
end
|
196
|
+
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
@@ -0,0 +1,53 @@
|
|
1
|
+
#--
|
2
|
+
#
|
3
|
+
# Author:: Francis Cianfrocca (gmail: blackhedd)
|
4
|
+
# Homepage:: http://rubyeventmachine.com
|
5
|
+
# Date:: 16 July 2006
|
6
|
+
#
|
7
|
+
# See EventMachine and EventMachine::Connection for documentation and
|
8
|
+
# usage examples.
|
9
|
+
#
|
10
|
+
#----------------------------------------------------------------------------
|
11
|
+
#
|
12
|
+
# Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
|
13
|
+
# Gmail: blackhedd
|
14
|
+
#
|
15
|
+
# This program is free software; you can redistribute it and/or modify
|
16
|
+
# it under the terms of either: 1) the GNU General Public License
|
17
|
+
# as published by the Free Software Foundation; either version 2 of the
|
18
|
+
# License, or (at your option) any later version; or 2) Ruby's License.
|
19
|
+
#
|
20
|
+
# See the file COPYING for complete licensing information.
|
21
|
+
#
|
22
|
+
#---------------------------------------------------------------------------
|
23
|
+
#
|
24
|
+
#
|
25
|
+
#
|
26
|
+
|
27
|
+
module EventMachine
|
28
|
+
module Protocols
|
29
|
+
|
30
|
+
class TcpConnectTester < Connection # :nodoc:
|
31
|
+
include EventMachine::Deferrable
|
32
|
+
|
33
|
+
def self.test( host, port )
|
34
|
+
EventMachine.connect( host, port, self )
|
35
|
+
end
|
36
|
+
|
37
|
+
def post_init
|
38
|
+
@start_time = Time.now
|
39
|
+
end
|
40
|
+
|
41
|
+
def connection_completed
|
42
|
+
@completed = true
|
43
|
+
set_deferred_status :succeeded, (Time.now - @start_time)
|
44
|
+
close_connection
|
45
|
+
end
|
46
|
+
|
47
|
+
def unbind
|
48
|
+
set_deferred_status :failed, (Time.now - @start_time) unless @completed
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
end
|
data/lib/em/protocols.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module EventMachine
|
2
|
+
# This module contains various protocol implementations, including:
|
3
|
+
# - HttpClient and HttpClient2
|
4
|
+
# - Stomp
|
5
|
+
# - Memcache
|
6
|
+
# - SmtpClient and SmtpServer
|
7
|
+
# - SASLauth and SASLauthclient
|
8
|
+
# - LineAndTextProtocol and LineText2
|
9
|
+
# - HeaderAndContentProtocol
|
10
|
+
# - Postgres3
|
11
|
+
# - ObjectProtocol
|
12
|
+
#
|
13
|
+
# The protocol implementations live in separate files in the protocols/ subdirectory,
|
14
|
+
# but are auto-loaded when they are first referenced in your application.
|
15
|
+
#
|
16
|
+
# EventMachine::Protocols is also aliased to EM::P for easier usage.
|
17
|
+
#
|
18
|
+
module Protocols
|
19
|
+
# TODO : various autotools are completely useless with the lack of naming
|
20
|
+
# convention, we need to correct that!
|
21
|
+
autoload :TcpConnectTester, 'em/protocols/tcptest'
|
22
|
+
autoload :HttpClient, 'em/protocols/httpclient'
|
23
|
+
autoload :HttpClient2, 'em/protocols/httpclient2'
|
24
|
+
autoload :LineAndTextProtocol, 'em/protocols/line_and_text'
|
25
|
+
autoload :HeaderAndContentProtocol, 'em/protocols/header_and_content'
|
26
|
+
autoload :LineText2, 'em/protocols/linetext2'
|
27
|
+
autoload :Stomp, 'em/protocols/stomp'
|
28
|
+
autoload :SmtpClient, 'em/protocols/smtpclient'
|
29
|
+
autoload :SmtpServer, 'em/protocols/smtpserver'
|
30
|
+
autoload :SASLauth, 'em/protocols/saslauth'
|
31
|
+
autoload :Memcache, 'em/protocols/memcache'
|
32
|
+
autoload :Postgres3, 'em/protocols/postgres3'
|
33
|
+
autoload :ObjectProtocol, 'em/protocols/object_protocol'
|
34
|
+
autoload :Socks4, 'em/protocols/socks4'
|
35
|
+
end
|
36
|
+
end
|
data/lib/em/queue.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
module EventMachine
|
2
|
+
# A cross thread, reactor scheduled, linear queue.
|
3
|
+
#
|
4
|
+
# This class provides a simple "Queue" like abstraction on top of the reactor
|
5
|
+
# scheduler. It services two primary purposes:
|
6
|
+
# * API sugar for stateful protocols
|
7
|
+
# * Pushing processing onto the same thread as the reactor
|
8
|
+
#
|
9
|
+
# See examples/ex_queue.rb for a detailed example.
|
10
|
+
#
|
11
|
+
# q = EM::Queue.new
|
12
|
+
# q.push('one', 'two', 'three')
|
13
|
+
# 3.times do
|
14
|
+
# q.pop{ |msg| puts(msg) }
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
class Queue
|
18
|
+
# Create a new queue
|
19
|
+
def initialize
|
20
|
+
@items = []
|
21
|
+
@popq = []
|
22
|
+
end
|
23
|
+
|
24
|
+
# Pop items off the queue, running the block on the reactor thread. The pop
|
25
|
+
# will not happen immediately, but at some point in the future, either in
|
26
|
+
# the next tick, if the queue has data, or when the queue is populated.
|
27
|
+
def pop(*a, &b)
|
28
|
+
cb = EM::Callback(*a, &b)
|
29
|
+
EM.schedule do
|
30
|
+
if @items.empty?
|
31
|
+
@popq << cb
|
32
|
+
else
|
33
|
+
cb.call @items.shift
|
34
|
+
end
|
35
|
+
end
|
36
|
+
nil # Always returns nil
|
37
|
+
end
|
38
|
+
|
39
|
+
# Push items onto the queue in the reactor thread. The items will not appear
|
40
|
+
# in the queue immediately, but will be scheduled for addition during the
|
41
|
+
# next reactor tick.
|
42
|
+
def push(*items)
|
43
|
+
EM.schedule do
|
44
|
+
@items.push(*items)
|
45
|
+
@popq.shift.call @items.shift until @items.empty? || @popq.empty?
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# N.B. This is a peek, it's not thread safe, and may only tend toward
|
50
|
+
# accuracy.
|
51
|
+
def empty?
|
52
|
+
@items.empty?
|
53
|
+
end
|
54
|
+
|
55
|
+
# N.B. This is a peek, it's not thread safe, and may only tend toward
|
56
|
+
# accuracy.
|
57
|
+
def size
|
58
|
+
@items.size
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/lib/em/spawnable.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
#--
|
2
|
+
#
|
3
|
+
# Author:: Francis Cianfrocca (gmail: blackhedd)
|
4
|
+
# Homepage:: http://rubyeventmachine.com
|
5
|
+
# Date:: 25 Aug 2007
|
6
|
+
#
|
7
|
+
# See EventMachine and EventMachine::Connection for documentation and
|
8
|
+
# usage examples.
|
9
|
+
#
|
10
|
+
#----------------------------------------------------------------------------
|
11
|
+
#
|
12
|
+
# Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
|
13
|
+
# Gmail: blackhedd
|
14
|
+
#
|
15
|
+
# This program is free software; you can redistribute it and/or modify
|
16
|
+
# it under the terms of either: 1) the GNU General Public License
|
17
|
+
# as published by the Free Software Foundation; either version 2 of the
|
18
|
+
# License, or (at your option) any later version; or 2) Ruby's License.
|
19
|
+
#
|
20
|
+
# See the file COPYING for complete licensing information.
|
21
|
+
#
|
22
|
+
#---------------------------------------------------------------------------
|
23
|
+
#
|
24
|
+
#
|
25
|
+
|
26
|
+
module EventMachine
|
27
|
+
# Support for Erlang-style processes.
|
28
|
+
#
|
29
|
+
class SpawnedProcess
|
30
|
+
# Send a message to the spawned process
|
31
|
+
def notify *x
|
32
|
+
me = self
|
33
|
+
EM.next_tick {
|
34
|
+
# A notification executes in the context of this
|
35
|
+
# SpawnedProcess object. That makes self and notify
|
36
|
+
# work as one would expect.
|
37
|
+
#
|
38
|
+
y = me.call(*x)
|
39
|
+
if y and y.respond_to?(:pull_out_yield_block)
|
40
|
+
a,b = y.pull_out_yield_block
|
41
|
+
set_receiver a
|
42
|
+
self.notify if b
|
43
|
+
end
|
44
|
+
}
|
45
|
+
end
|
46
|
+
alias_method :resume, :notify
|
47
|
+
alias_method :run, :notify # for formulations like (EM.spawn {xxx}).run
|
48
|
+
#attr_accessor :receiver
|
49
|
+
|
50
|
+
#--
|
51
|
+
# I know I'm missing something stupid, but the inside of class << s
|
52
|
+
# can't see locally-bound values. It can see globals, though.
|
53
|
+
def set_receiver blk
|
54
|
+
$em______tmpglobal = blk
|
55
|
+
class << self
|
56
|
+
define_method :call, $em______tmpglobal.dup
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
class YieldBlockFromSpawnedProcess # :nodoc:
|
63
|
+
def initialize block, notify
|
64
|
+
@block = [block,notify]
|
65
|
+
end
|
66
|
+
def pull_out_yield_block
|
67
|
+
@block
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# Spawn an erlang-style process
|
72
|
+
def self.spawn &block
|
73
|
+
s = SpawnedProcess.new
|
74
|
+
s.set_receiver block
|
75
|
+
s
|
76
|
+
end
|
77
|
+
|
78
|
+
def self.yield &block # :nodoc:
|
79
|
+
return YieldBlockFromSpawnedProcess.new( block, false )
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.yield_and_notify &block # :nodoc:
|
83
|
+
return YieldBlockFromSpawnedProcess.new( block, true )
|
84
|
+
end
|
85
|
+
end
|
data/lib/em/streamer.rb
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
#--
|
2
|
+
#
|
3
|
+
# Author:: Francis Cianfrocca (gmail: blackhedd)
|
4
|
+
# Homepage:: http://rubyeventmachine.com
|
5
|
+
# Date:: 16 Jul 2006
|
6
|
+
#
|
7
|
+
# See EventMachine and EventMachine::Connection for documentation and
|
8
|
+
# usage examples.
|
9
|
+
#
|
10
|
+
#----------------------------------------------------------------------------
|
11
|
+
#
|
12
|
+
# Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
|
13
|
+
# Gmail: blackhedd
|
14
|
+
#
|
15
|
+
# This program is free software; you can redistribute it and/or modify
|
16
|
+
# it under the terms of either: 1) the GNU General Public License
|
17
|
+
# as published by the Free Software Foundation; either version 2 of the
|
18
|
+
# License, or (at your option) any later version; or 2) Ruby's License.
|
19
|
+
#
|
20
|
+
# See the file COPYING for complete licensing information.
|
21
|
+
#
|
22
|
+
#---------------------------------------------------------------------------
|
23
|
+
#
|
24
|
+
#
|
25
|
+
|
26
|
+
|
27
|
+
module EventMachine
|
28
|
+
class FileStreamer
|
29
|
+
include Deferrable
|
30
|
+
|
31
|
+
# Use mapped streamer for files bigger than 16k
|
32
|
+
MappingThreshold = 16384
|
33
|
+
# Wait until next tick to send more data when 50k is still in the outgoing buffer
|
34
|
+
BackpressureLevel = 50000
|
35
|
+
# Send 16k chunks at a time
|
36
|
+
ChunkSize = 16384
|
37
|
+
|
38
|
+
# Stream a file over a given connection. An optional :http_chunks => true argument will
|
39
|
+
# use HTTP 1.1 style chunked-encoding semantics.
|
40
|
+
#
|
41
|
+
# module FileSender
|
42
|
+
# def post_init
|
43
|
+
# streamer = EventMachine::FileStreamer.new(self, '/tmp/bigfile.tar')
|
44
|
+
# streamer.callback{
|
45
|
+
# # file was sent successfully
|
46
|
+
# close_connection_after_writing
|
47
|
+
# }
|
48
|
+
# end
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
def initialize connection, filename, args = {}
|
52
|
+
@connection = connection
|
53
|
+
@http_chunks = args[:http_chunks]
|
54
|
+
|
55
|
+
if File.exist?(filename)
|
56
|
+
@size = File.size?(filename)
|
57
|
+
if @size <= MappingThreshold
|
58
|
+
stream_without_mapping filename
|
59
|
+
else
|
60
|
+
stream_with_mapping filename
|
61
|
+
end
|
62
|
+
else
|
63
|
+
fail "file not found"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def stream_without_mapping filename # :nodoc:
|
68
|
+
if @http_chunks
|
69
|
+
@connection.send_data "#{@size.to_s(16)}\r\n"
|
70
|
+
@connection.send_file_data filename
|
71
|
+
@connection.send_data "\r\n0\r\n\r\n"
|
72
|
+
else
|
73
|
+
@connection.send_file_data filename
|
74
|
+
end
|
75
|
+
succeed
|
76
|
+
end
|
77
|
+
private :stream_without_mapping
|
78
|
+
|
79
|
+
def stream_with_mapping filename # :nodoc:
|
80
|
+
ensure_mapping_extension_is_present
|
81
|
+
|
82
|
+
@position = 0
|
83
|
+
@mapping = EventMachine::FastFileReader::Mapper.new filename
|
84
|
+
stream_one_chunk
|
85
|
+
end
|
86
|
+
private :stream_with_mapping
|
87
|
+
|
88
|
+
# Used internally to stream one chunk at a time over multiple reactor ticks
|
89
|
+
def stream_one_chunk
|
90
|
+
loop {
|
91
|
+
if @position < @size
|
92
|
+
if @connection.get_outbound_data_size > BackpressureLevel
|
93
|
+
EventMachine::next_tick {stream_one_chunk}
|
94
|
+
break
|
95
|
+
else
|
96
|
+
len = @size - @position
|
97
|
+
len = ChunkSize if (len > ChunkSize)
|
98
|
+
|
99
|
+
@connection.send_data( "#{len.to_s(16)}\r\n" ) if @http_chunks
|
100
|
+
@connection.send_data( @mapping.get_chunk( @position, len ))
|
101
|
+
@connection.send_data("\r\n") if @http_chunks
|
102
|
+
|
103
|
+
@position += len
|
104
|
+
end
|
105
|
+
else
|
106
|
+
@connection.send_data "0\r\n\r\n" if @http_chunks
|
107
|
+
@mapping.close
|
108
|
+
succeed
|
109
|
+
break
|
110
|
+
end
|
111
|
+
}
|
112
|
+
end
|
113
|
+
|
114
|
+
#--
|
115
|
+
# We use an outboard extension class to get memory-mapped files.
|
116
|
+
# It's outboard to avoid polluting the core distro, but that means
|
117
|
+
# there's a "hidden" dependency on it. The first time we get here in
|
118
|
+
# any run, try to load up the dependency extension. User code will see
|
119
|
+
# a LoadError if it's not available, but code that doesn't require
|
120
|
+
# mapped files will work fine without it. This is a somewhat difficult
|
121
|
+
# compromise between usability and proper modularization.
|
122
|
+
#
|
123
|
+
def ensure_mapping_extension_is_present # :nodoc:
|
124
|
+
@@fastfilereader ||= (require 'fastfilereaderext')
|
125
|
+
end
|
126
|
+
private :ensure_mapping_extension_is_present
|
127
|
+
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
data/lib/em/timers.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
module EventMachine
|
2
|
+
# Creates a one-time timer
|
3
|
+
#
|
4
|
+
# timer = EventMachine::Timer.new(5) do
|
5
|
+
# # this will never fire because we cancel it
|
6
|
+
# end
|
7
|
+
# timer.cancel
|
8
|
+
#
|
9
|
+
class Timer
|
10
|
+
# Create a new timer that fires after a given number of seconds
|
11
|
+
def initialize interval, callback=nil, &block
|
12
|
+
@signature = EventMachine::add_timer(interval, callback || block)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Cancel the timer
|
16
|
+
def cancel
|
17
|
+
EventMachine.send :cancel_timer, @signature
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Creates a periodic timer
|
22
|
+
#
|
23
|
+
# n = 0
|
24
|
+
# timer = EventMachine::PeriodicTimer.new(5) do
|
25
|
+
# puts "the time is #{Time.now}"
|
26
|
+
# timer.cancel if (n+=1) > 5
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
class PeriodicTimer
|
30
|
+
# Create a new periodic timer that executes every interval seconds
|
31
|
+
def initialize interval, callback=nil, &block
|
32
|
+
@interval = interval
|
33
|
+
@code = callback || block
|
34
|
+
@cancelled = false
|
35
|
+
schedule
|
36
|
+
end
|
37
|
+
|
38
|
+
# Cancel the periodic timer
|
39
|
+
def cancel
|
40
|
+
@cancelled = true
|
41
|
+
end
|
42
|
+
|
43
|
+
# Fire the timer every interval seconds
|
44
|
+
attr_accessor :interval
|
45
|
+
|
46
|
+
def schedule # :nodoc:
|
47
|
+
EventMachine::add_timer @interval, method(:fire)
|
48
|
+
end
|
49
|
+
def fire # :nodoc:
|
50
|
+
unless @cancelled
|
51
|
+
@code.call
|
52
|
+
schedule
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/lib/em/version.rb
ADDED