eventmachine 0.7.0 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/COPYING +44 -265
  2. data/GNU +281 -0
  3. data/LEGAL +25 -0
  4. data/README +5 -3
  5. data/RELEASE_NOTES +7 -2
  6. data/ext/binder.cpp +8 -15
  7. data/ext/binder.h +8 -15
  8. data/ext/cmain.cpp +29 -15
  9. data/ext/ed.cpp +36 -43
  10. data/ext/ed.h +8 -15
  11. data/ext/em.cpp +116 -21
  12. data/ext/em.h +11 -16
  13. data/ext/emwin.cpp +9 -16
  14. data/ext/emwin.h +9 -16
  15. data/ext/eventmachine.h +9 -14
  16. data/ext/extconf.rb +9 -17
  17. data/ext/files.cpp +7 -14
  18. data/ext/files.h +7 -14
  19. data/ext/page.cpp +8 -15
  20. data/ext/page.h +8 -15
  21. data/ext/project.h +15 -15
  22. data/ext/rubymain.cpp +37 -16
  23. data/ext/sigs.cpp +8 -15
  24. data/ext/sigs.h +8 -15
  25. data/ext/ssl.cpp +8 -15
  26. data/ext/ssl.h +8 -15
  27. data/lib/em/deferrable.rb +40 -27
  28. data/lib/em/eventable.rb +12 -23
  29. data/lib/em/future.rb +62 -0
  30. data/lib/eventmachine.rb +156 -66
  31. data/lib/eventmachine_version.rb +12 -23
  32. data/lib/evma.rb +15 -18
  33. data/lib/evma/callback.rb +16 -19
  34. data/lib/evma/container.rb +17 -19
  35. data/lib/evma/factory.rb +16 -19
  36. data/lib/evma/protocol.rb +17 -19
  37. data/lib/evma/reactor.rb +17 -19
  38. data/lib/pr_eventmachine.rb +12 -23
  39. data/lib/protocols/buftok.rb +121 -0
  40. data/lib/protocols/header_and_content.rb +12 -23
  41. data/lib/protocols/httpclient.rb +44 -31
  42. data/lib/protocols/line_and_text.rb +97 -118
  43. data/lib/protocols/tcptest.rb +12 -22
  44. data/tests/test_basic.rb +14 -24
  45. data/tests/test_eventables.rb +14 -24
  46. data/tests/test_exc.rb +57 -0
  47. data/tests/test_futures.rb +136 -0
  48. data/tests/test_hc.rb +14 -24
  49. data/tests/test_httpclient.rb +106 -25
  50. data/tests/test_ltp.rb +14 -23
  51. data/tests/test_timers.rb +109 -0
  52. data/tests/test_ud.rb +14 -24
  53. metadata +11 -2
@@ -1,26 +1,19 @@
1
1
  /*****************************************************************************
2
2
 
3
- $Id: sigs.cpp 47 2006-05-15 21:42:38Z blackhedd $
3
+ $Id: sigs.cpp 318 2007-05-22 22:06:24Z blackhedd $
4
4
 
5
5
  File: sigs.cpp
6
6
  Date: 06Apr06
7
7
 
8
- Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
9
- Gmail: garbagecat20
8
+ Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
9
+ Gmail: blackhedd
10
10
 
11
11
  This program is free software; you can redistribute it and/or modify
12
- it under the terms of the GNU General Public License as published by
13
- the Free Software Foundation; either version 2 of the License, or
14
- (at your option) any later version.
15
-
16
- This program is distributed in the hope that it will be useful,
17
- but WITHOUT ANY WARRANTY; without even the implied warranty of
18
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
- GNU General Public License for more details.
20
-
21
- You should have received a copy of the GNU General Public License
22
- along with this program; if not, write to the Free Software
23
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
12
+ it under the terms of either: 1) the GNU General Public License
13
+ as published by the Free Software Foundation; either version 2 of the
14
+ License, or (at your option) any later version; or 2) Ruby's License.
15
+
16
+ See the file COPYING for complete licensing information.
24
17
 
25
18
  *****************************************************************************/
26
19
 
data/ext/sigs.h CHANGED
@@ -1,26 +1,19 @@
1
1
  /*****************************************************************************
2
2
 
3
- $Id: sigs.h 47 2006-05-15 21:42:38Z blackhedd $
3
+ $Id: sigs.h 318 2007-05-22 22:06:24Z blackhedd $
4
4
 
5
5
  File: sigs.h
6
6
  Date: 06Apr06
7
7
 
8
- Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
9
- Gmail: garbagecat20
8
+ Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
9
+ Gmail: blackhedd
10
10
 
11
11
  This program is free software; you can redistribute it and/or modify
12
- it under the terms of the GNU General Public License as published by
13
- the Free Software Foundation; either version 2 of the License, or
14
- (at your option) any later version.
15
-
16
- This program is distributed in the hope that it will be useful,
17
- but WITHOUT ANY WARRANTY; without even the implied warranty of
18
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
- GNU General Public License for more details.
20
-
21
- You should have received a copy of the GNU General Public License
22
- along with this program; if not, write to the Free Software
23
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
12
+ it under the terms of either: 1) the GNU General Public License
13
+ as published by the Free Software Foundation; either version 2 of the
14
+ License, or (at your option) any later version; or 2) Ruby's License.
15
+
16
+ See the file COPYING for complete licensing information.
24
17
 
25
18
  *****************************************************************************/
26
19
 
@@ -1,26 +1,19 @@
1
1
  /*****************************************************************************
2
2
 
3
- $Id: ssl.cpp 51 2006-05-16 00:56:51Z blackhedd $
3
+ $Id: ssl.cpp 318 2007-05-22 22:06:24Z blackhedd $
4
4
 
5
5
  File: ssl.cpp
6
6
  Date: 30Apr06
7
7
 
8
- Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
9
- Gmail: garbagecat10
8
+ Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
9
+ Gmail: blackhedd
10
10
 
11
11
  This program is free software; you can redistribute it and/or modify
12
- it under the terms of the GNU General Public License as published by
13
- the Free Software Foundation; either version 2 of the License, or
14
- (at your option) any later version.
15
-
16
- This program is distributed in the hope that it will be useful,
17
- but WITHOUT ANY WARRANTY; without even the implied warranty of
18
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
- GNU General Public License for more details.
20
-
21
- You should have received a copy of the GNU General Public License
22
- along with this program; if not, write to the Free Software
23
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
12
+ it under the terms of either: 1) the GNU General Public License
13
+ as published by the Free Software Foundation; either version 2 of the
14
+ License, or (at your option) any later version; or 2) Ruby's License.
15
+
16
+ See the file COPYING for complete licensing information.
24
17
 
25
18
  *****************************************************************************/
26
19
 
data/ext/ssl.h CHANGED
@@ -1,26 +1,19 @@
1
1
  /*****************************************************************************
2
2
 
3
- $Id: ssl.h 47 2006-05-15 21:42:38Z blackhedd $
3
+ $Id: ssl.h 318 2007-05-22 22:06:24Z blackhedd $
4
4
 
5
5
  File: ssl.h
6
6
  Date: 30Apr06
7
7
 
8
- Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
9
- Gmail: garbagecat20
8
+ Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
9
+ Gmail: blackhedd
10
10
 
11
11
  This program is free software; you can redistribute it and/or modify
12
- it under the terms of the GNU General Public License as published by
13
- the Free Software Foundation; either version 2 of the License, or
14
- (at your option) any later version.
15
-
16
- This program is distributed in the hope that it will be useful,
17
- but WITHOUT ANY WARRANTY; without even the implied warranty of
18
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
- GNU General Public License for more details.
20
-
21
- You should have received a copy of the GNU General Public License
22
- along with this program; if not, write to the Free Software
23
- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
12
+ it under the terms of either: 1) the GNU General Public License
13
+ as published by the Free Software Foundation; either version 2 of the
14
+ License, or (at your option) any later version; or 2) Ruby's License.
15
+
16
+ See the file COPYING for complete licensing information.
24
17
 
25
18
  *****************************************************************************/
26
19
 
@@ -1,34 +1,23 @@
1
- # $Id: deferrable.rb 216 2006-07-17 10:20:42Z blackhedd $
1
+ # $Id: deferrable.rb 320 2007-05-22 22:12:59Z blackhedd $
2
2
  #
3
- # Author:: blackhedd (gmail address: garbagecat10).
4
- # Date:: 16 July 2006
3
+ # Author:: Francis Cianfrocca (gmail: blackhedd)
4
+ # Homepage:: http://rubyeventmachine.com
5
+ # Date:: 16 Jul 2006
5
6
  #
6
- # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
7
- #
8
- # This program is made available under the terms of the GPL version 2.
9
- #
10
7
  # See EventMachine and EventMachine::Connection for documentation and
11
8
  # usage examples.
12
9
  #
13
10
  #----------------------------------------------------------------------------
14
11
  #
15
- # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
16
- #
17
- # Gmail: garbagecat10
18
- #
12
+ # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
13
+ # Gmail: blackhedd
14
+ #
19
15
  # This program is free software; you can redistribute it and/or modify
20
- # it under the terms of the GNU General Public License as published by
21
- # the Free Software Foundation; either version 2 of the License, or
22
- # (at your option) any later version.
23
- #
24
- # This program is distributed in the hope that it will be useful,
25
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
26
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
- # GNU General Public License for more details.
28
- #
29
- # You should have received a copy of the GNU General Public License
30
- # along with this program; if not, write to the Free Software
31
- # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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.
32
21
  #
33
22
  #---------------------------------------------------------------------------
34
23
  #
@@ -42,7 +31,7 @@ module Deferrable
42
31
  def callback &block
43
32
  return unless block
44
33
  if @deferred_status == :succeeded
45
- block.call
34
+ block.call(*@deferred_args)
46
35
  else
47
36
  @callbacks ||= []
48
37
  @callbacks << block
@@ -52,7 +41,7 @@ module Deferrable
52
41
  def errback &block
53
42
  return unless block
54
43
  if @deferred_status == :failed
55
- block.call
44
+ block.call(*@deferred_args)
56
45
  else
57
46
  @errbacks ||= []
58
47
  @errbacks << block
@@ -65,6 +54,18 @@ module Deferrable
65
54
  # user code will throw an argument exception.
66
55
  # Implementors of deferrable classes <b>must</b>
67
56
  # document the arguments they will supply to user callbacks.
57
+ #
58
+ # OBSERVE SOMETHING VERY SPECIAL here: you may call this method even
59
+ # on the INSIDE of a callback. This is very useful when a previously-registered
60
+ # callback wants to change the parameters that will be passed to subsequently-registered
61
+ # ones.
62
+ # --
63
+ # We're shifting callbacks off and discarding them as we execute them.
64
+ # This is valid because by definition callbacks are executed no more than
65
+ # once. It also has the magic effect of permitting recursive calls, which
66
+ # means that a callback can call #set_deferred_status and change the parameters
67
+ # that will be sent to subsequent callbacks down the chain.
68
+ #
68
69
  def set_deferred_status arg, *args
69
70
  @deferred_status = arg
70
71
  @deferred_args = args
@@ -72,18 +73,30 @@ module Deferrable
72
73
  when :succeeded
73
74
  if @callbacks
74
75
  while cb = @callbacks.shift
75
- cb.call *@deferred_args
76
+ cb.call(*@deferred_args)
76
77
  end
77
78
  end
78
79
  when :failed
79
80
  if @errbacks
80
81
  while eb = @errbacks.shift
81
- eb.call *@deferred_args
82
+ eb.call(*@deferred_args)
82
83
  end
83
84
  end
84
85
  end
85
86
  end
86
87
 
88
+ # Equivalent to set_deferred_status(:succeeded, ...)
89
+ #
90
+ def set_deferred_success *args
91
+ set_deferred_status :succeeded, *args
92
+ end
93
+
94
+ # Equivalent to set_deferred_status(:failed, ...)
95
+ #
96
+ def set_deferred_failure *args
97
+ set_deferred_status :failed, *args
98
+ end
99
+
87
100
  end
88
101
  end
89
102
 
@@ -1,34 +1,23 @@
1
- # $Id: eventable.rb 252 2006-09-02 19:23:20Z blackhedd $
1
+ # $Id: eventable.rb 320 2007-05-22 22:12:59Z blackhedd $
2
2
  #
3
- # Author:: blackhedd (gmail address: garbagecat10).
4
- # Date:: 16 July 2006
3
+ # Author:: Francis Cianfrocca (gmail: blackhedd)
4
+ # Homepage:: http://rubyeventmachine.com
5
+ # Date:: 16 Jul 2006
5
6
  #
6
- # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
7
- #
8
- # This program is made available under the terms of the GPL version 2.
9
- #
10
7
  # See EventMachine and EventMachine::Connection for documentation and
11
8
  # usage examples.
12
9
  #
13
10
  #----------------------------------------------------------------------------
14
11
  #
15
- # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
16
- #
17
- # Gmail: garbagecat10
18
- #
12
+ # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
13
+ # Gmail: blackhedd
14
+ #
19
15
  # This program is free software; you can redistribute it and/or modify
20
- # it under the terms of the GNU General Public License as published by
21
- # the Free Software Foundation; either version 2 of the License, or
22
- # (at your option) any later version.
23
- #
24
- # This program is distributed in the hope that it will be useful,
25
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
26
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
- # GNU General Public License for more details.
28
- #
29
- # You should have received a copy of the GNU General Public License
30
- # along with this program; if not, write to the Free Software
31
- # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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.
32
21
  #
33
22
  #---------------------------------------------------------------------------
34
23
  #
@@ -0,0 +1,62 @@
1
+ # $Id: future.rb 320 2007-05-22 22:12:59Z blackhedd $
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
+ # This defines EventMachine::Deferrable#future, which requires
27
+ # that the rest of EventMachine::Deferrable has already been seen.
28
+ # (It's in deferrable.rb.)
29
+ #
30
+ # A future is a sugaring of a typical deferrable usage.
31
+
32
+ module EventMachine
33
+ module Deferrable
34
+
35
+ class << self
36
+ # Evaluate arg (which may be an expression or a block).
37
+ # What's the class of arg?
38
+ # If arg is an ordinary expression, then return it.
39
+ # If arg is deferrable (responds to :set_deferred_status),
40
+ # then look at the arguments. If either callback or errback
41
+ # are defined, then use them. If neither are defined, then
42
+ # use the supplied block (if any) as the callback.
43
+ # Then return arg.
44
+ def future arg, cb=nil, eb=nil, &blk
45
+ arg = arg.call if arg.respond_to?(:call)
46
+
47
+ if arg.respond_to?(:set_deferred_status)
48
+ if cb || eb
49
+ arg.callback(&cb) if cb
50
+ arg.errback(&eb) if eb
51
+ else
52
+ arg.callback(&blk) if blk
53
+ end
54
+ end
55
+
56
+ arg
57
+ end
58
+ end
59
+
60
+ end
61
+ end
62
+
@@ -1,34 +1,23 @@
1
- # $Id: eventmachine.rb 283 2006-11-22 14:44:38Z blackhedd $
1
+ # $Id: eventmachine.rb 319 2007-05-22 22:11:18Z blackhedd $
2
2
  #
3
- # Author:: blackhedd (gmail address: garbagecat10).
3
+ # Author:: Francis Cianfrocca (gmail: blackhedd)
4
+ # Homepage:: http://rubyeventmachine.com
4
5
  # Date:: 8 Apr 2006
5
6
  #
6
- # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
7
- #
8
- # This program is made available under the terms of the GPL version 2.
9
- #
10
7
  # See EventMachine and EventMachine::Connection for documentation and
11
8
  # usage examples.
12
9
  #
13
10
  #----------------------------------------------------------------------------
14
11
  #
15
- # Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
16
- #
17
- # Gmail: garbagecat10
18
- #
12
+ # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
13
+ # Gmail: blackhedd
14
+ #
19
15
  # This program is free software; you can redistribute it and/or modify
20
- # it under the terms of the GNU General Public License as published by
21
- # the Free Software Foundation; either version 2 of the License, or
22
- # (at your option) any later version.
23
- #
24
- # This program is distributed in the hope that it will be useful,
25
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
26
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27
- # GNU General Public License for more details.
28
- #
29
- # You should have received a copy of the GNU General Public License
30
- # along with this program; if not, write to the Free Software
31
- # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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.
32
21
  #
33
22
  #---------------------------------------------------------------------------
34
23
  #
@@ -36,6 +25,7 @@
36
25
 
37
26
 
38
27
  #-- Select in a library based on a global variable.
28
+ $eventmachine_library ||= nil
39
29
  case $eventmachine_library
40
30
  when :pure_ruby
41
31
  require 'pr_eventmachine'
@@ -54,6 +44,7 @@ end
54
44
 
55
45
  require "eventmachine_version"
56
46
  require 'em/deferrable'
47
+ require 'em/future'
57
48
  require 'em/eventable'
58
49
  #-- Additional requires are at the BOTTOM of this file, because they
59
50
  #-- depend on stuff defined in here. Refactor that someday.
@@ -157,50 +148,59 @@ require 'em/eventable'
157
148
  module EventMachine
158
149
 
159
150
 
160
- # EventMachine::run initializes and runs an event loop.
161
- # This method only returns if user-callback code calls stop_event_loop.
162
- # Use the supplied block to define your clients and servers.
163
- # The block is called by EventMachine::run immediately after initializing
164
- # its internal event loop but <i>before</i> running the loop.
165
- # Therefore this block is the right place to call start_server if you
166
- # want to accept connections from remote clients.
167
- #
168
- # For programs that are structured as servers, it's usually appropriate
169
- # to start an event loop by calling EventMachine::run, and let it
170
- # run forever. It's also possible to use EventMachine::run to make a single
171
- # client-connection to a remote server, process the data flow from that
172
- # single connection, and then call stop_event_loop to force EventMachine::run
173
- # to return. Your program will then continue from the point immediately
174
- # following the call to EventMachine::run.
175
- #
176
- # You can of course do both client and servers simultaneously in the same program.
177
- # One of the strengths of the event-driven programming model is that the
178
- # handling of network events on many different connections will be interleaved,
179
- # and scheduled according to the actual events themselves. This maximizes
180
- # efficiency.
181
- #
182
- # === Server usage example
183
- #
184
- # See the text at the top of this file for an example of an echo server.
185
- #
186
- # === Client usage example
187
- #
188
- # See the description of stop_event_loop for an extremely simple client example.
189
- #
190
- #--
191
- # Obsoleted the use_threads mechanism.
192
- #
193
- def EventMachine::run &block
194
- #def EventMachine::run use_threads = true, &block
195
- @conns = {}
196
- @acceptors = {}
197
- @timers = {}
198
- initialize_event_machine
199
- block and add_timer 0, block
200
- #use_threads ? run_machine : run_machine_without_threads
201
- run_machine
202
- release_machine
203
- end
151
+ # EventMachine::run initializes and runs an event loop.
152
+ # This method only returns if user-callback code calls stop_event_loop.
153
+ # Use the supplied block to define your clients and servers.
154
+ # The block is called by EventMachine::run immediately after initializing
155
+ # its internal event loop but <i>before</i> running the loop.
156
+ # Therefore this block is the right place to call start_server if you
157
+ # want to accept connections from remote clients.
158
+ #
159
+ # For programs that are structured as servers, it's usually appropriate
160
+ # to start an event loop by calling EventMachine::run, and let it
161
+ # run forever. It's also possible to use EventMachine::run to make a single
162
+ # client-connection to a remote server, process the data flow from that
163
+ # single connection, and then call stop_event_loop to force EventMachine::run
164
+ # to return. Your program will then continue from the point immediately
165
+ # following the call to EventMachine::run.
166
+ #
167
+ # You can of course do both client and servers simultaneously in the same program.
168
+ # One of the strengths of the event-driven programming model is that the
169
+ # handling of network events on many different connections will be interleaved,
170
+ # and scheduled according to the actual events themselves. This maximizes
171
+ # efficiency.
172
+ #
173
+ # === Server usage example
174
+ #
175
+ # See the text at the top of this file for an example of an echo server.
176
+ #
177
+ # === Client usage example
178
+ #
179
+ # See the description of stop_event_loop for an extremely simple client example.
180
+ #
181
+ #--
182
+ # Obsoleted the use_threads mechanism.
183
+ # 25Nov06: Added the begin/ensure block. We need to be sure that release_machine
184
+ # gets called even if an exception gets thrown within any of the user code
185
+ # that the event loop runs. The best way to see this is to run a unit
186
+ # test with two functions, each of which calls EventMachine#run and each of
187
+ # which throws something inside of #run. Without the ensure, the second test
188
+ # will start without release_machine being called and will immediately throw
189
+ # a C++ runtime error.
190
+ #
191
+ def EventMachine::run &block
192
+ @conns = {}
193
+ @acceptors = {}
194
+ @timers = {}
195
+ begin
196
+ initialize_event_machine
197
+ block and add_timer 0, block
198
+ run_machine
199
+ ensure
200
+ release_machine
201
+ end
202
+ end
203
+
204
204
 
205
205
  # +deprecated+
206
206
  #--
@@ -259,6 +259,7 @@ module EventMachine
259
259
  # check too many timers!
260
260
  s = add_oneshot_timer((interval * 1000).to_i)
261
261
  @timers[s] = code
262
+ s
262
263
  end
263
264
  end
264
265
 
@@ -290,6 +291,13 @@ module EventMachine
290
291
  end
291
292
  end
292
293
 
294
+ #--
295
+ #
296
+ def EventMachine::cancel_timer signature
297
+ @timers[signature] = proc{} if @timers.has_key?(signature)
298
+ end
299
+ private_class_method :cancel_timer
300
+
293
301
 
294
302
  # stop_event_loop may called from within a callback method
295
303
  # while EventMachine's processing loop is running.
@@ -594,6 +602,37 @@ module EventMachine
594
602
  end
595
603
 
596
604
 
605
+
606
+
607
+ # Make a connection to a Unix-domain socket. This is not implemented on Windows platforms.
608
+ # The parameter socketname is a String which identifies the Unix-domain socket you want
609
+ # to connect to. socketname is the name of a file on your local system, and in most cases
610
+ # is a fully-qualified path name. Make sure that your process has enough local permissions
611
+ # to open the Unix-domain socket.
612
+ # See also the documentation for #connect_server. This method behaves like #connect_server
613
+ # in all respects except for the fact that it connects to a local Unix-domain
614
+ # socket rather than a TCP socket.
615
+ # NOTE: this functionality will soon be subsumed into the #connect method. This method
616
+ # will still be supported as an alias.
617
+ #--
618
+ # For making connections to Unix-domain sockets.
619
+ # Eventually this has to get properly documented and unified with the TCP-connect methods.
620
+ # Note how nearly identical this is to EventMachine#connect
621
+ def EventMachine::connect_unix_domain socketname, handler=nil
622
+ klass = if (handler and handler.is_a?(Class))
623
+ handler
624
+ else
625
+ Class.new( Connection ) {handler and include handler}
626
+ end
627
+
628
+ s = connect_unix_server socketname
629
+ c = klass.new s
630
+ @conns[s] = c
631
+ block_given? and yield c
632
+ c
633
+ end
634
+
635
+
597
636
  # EventMachine#open_datagram_socket is for support of UDP-based
598
637
  # protocols. Its usage is similar to that of EventMachine#start_server.
599
638
  # It takes three parameters: an IP address (which must be valid
@@ -763,6 +802,18 @@ module EventMachine
763
802
  @threadqueue << [op,callback]
764
803
  end
765
804
 
805
+ # A wrapper over the setuid system call. Particularly useful when opening a network
806
+ # server on a privileged port because you can use this call to drop privileges
807
+ # after opening the port.
808
+ # This method has no effective implementation on Windows or in the pure-Ruby
809
+ # implementation of EventMachine.
810
+ # Call #set_effective_user by passing it a string containing the effective name
811
+ # of the user whose privilege-level your process should attain.
812
+ # This method is intended for use in enforcing security requirements, consequently
813
+ # it will throw a fatal error and end your program if it fails.
814
+ def self::set_effective_user username
815
+ EventMachine::setuid_string username
816
+ end
766
817
 
767
818
 
768
819
  private
@@ -1055,6 +1106,45 @@ class Connection
1055
1106
  EventMachine::reconnect server, port, self
1056
1107
  end
1057
1108
 
1109
+
1110
+
1111
+
1112
+ #
1113
+ #
1114
+ #
1115
+ class EventMachine::PeriodicTimer
1116
+ def initialize *args, &block
1117
+ @interval = args.shift
1118
+ @code = args.shift || block
1119
+ schedule
1120
+ end
1121
+ def schedule
1122
+ EventMachine::add_timer @interval, proc {self.fire}
1123
+ end
1124
+ def fire
1125
+ @code.call
1126
+ schedule unless @cancelled
1127
+ end
1128
+ def cancel
1129
+ @cancelled = true
1130
+ end
1131
+ end
1132
+
1133
+ #
1134
+ #
1135
+ #
1136
+ class EventMachine::Timer
1137
+ def initialize *args, &block
1138
+ @signature = EventMachine::add_timer(*args, &block)
1139
+ end
1140
+ def cancel
1141
+ EventMachine.send :cancel_timer, @signature
1142
+ end
1143
+ end
1144
+
1145
+
1146
+
1147
+
1058
1148
  end
1059
1149
 
1060
1150