smparkes-eventmachine 0.12.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. data/.gitignore +15 -0
  2. data/README +81 -0
  3. data/Rakefile +374 -0
  4. data/docs/COPYING +60 -0
  5. data/docs/ChangeLog +211 -0
  6. data/docs/DEFERRABLES +133 -0
  7. data/docs/EPOLL +141 -0
  8. data/docs/GNU +281 -0
  9. data/docs/INSTALL +13 -0
  10. data/docs/KEYBOARD +38 -0
  11. data/docs/LEGAL +25 -0
  12. data/docs/LIGHTWEIGHT_CONCURRENCY +70 -0
  13. data/docs/PURE_RUBY +75 -0
  14. data/docs/RELEASE_NOTES +94 -0
  15. data/docs/SMTP +2 -0
  16. data/docs/SPAWNED_PROCESSES +89 -0
  17. data/docs/TODO +8 -0
  18. data/eventmachine.gemspec +40 -0
  19. data/examples/ex_channel.rb +43 -0
  20. data/examples/ex_queue.rb +2 -0
  21. data/examples/helper.rb +2 -0
  22. data/ext/binder.cpp +125 -0
  23. data/ext/binder.h +46 -0
  24. data/ext/cmain.cpp +827 -0
  25. data/ext/cplusplus.cpp +202 -0
  26. data/ext/ed.cpp +1901 -0
  27. data/ext/ed.h +424 -0
  28. data/ext/em.cpp +2288 -0
  29. data/ext/em.h +229 -0
  30. data/ext/emwin.cpp +300 -0
  31. data/ext/emwin.h +94 -0
  32. data/ext/epoll.cpp +26 -0
  33. data/ext/epoll.h +25 -0
  34. data/ext/eventmachine.h +122 -0
  35. data/ext/eventmachine_cpp.h +96 -0
  36. data/ext/extconf.rb +150 -0
  37. data/ext/fastfilereader/extconf.rb +85 -0
  38. data/ext/fastfilereader/mapper.cpp +214 -0
  39. data/ext/fastfilereader/mapper.h +59 -0
  40. data/ext/fastfilereader/rubymain.cpp +127 -0
  41. data/ext/files.cpp +94 -0
  42. data/ext/files.h +65 -0
  43. data/ext/kb.cpp +81 -0
  44. data/ext/page.cpp +107 -0
  45. data/ext/page.h +51 -0
  46. data/ext/pipe.cpp +349 -0
  47. data/ext/project.h +156 -0
  48. data/ext/rubymain.cpp +1194 -0
  49. data/ext/sigs.cpp +89 -0
  50. data/ext/sigs.h +32 -0
  51. data/ext/ssl.cpp +460 -0
  52. data/ext/ssl.h +94 -0
  53. data/java/.classpath +8 -0
  54. data/java/.project +17 -0
  55. data/java/src/com/rubyeventmachine/EmReactor.java +570 -0
  56. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  57. data/java/src/com/rubyeventmachine/EventableChannel.java +69 -0
  58. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +189 -0
  59. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +364 -0
  60. data/java/src/com/rubyeventmachine/application/Application.java +194 -0
  61. data/java/src/com/rubyeventmachine/application/Connection.java +74 -0
  62. data/java/src/com/rubyeventmachine/application/ConnectionFactory.java +37 -0
  63. data/java/src/com/rubyeventmachine/application/DefaultConnectionFactory.java +46 -0
  64. data/java/src/com/rubyeventmachine/application/PeriodicTimer.java +38 -0
  65. data/java/src/com/rubyeventmachine/application/Timer.java +54 -0
  66. data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +109 -0
  67. data/java/src/com/rubyeventmachine/tests/ConnectTest.java +148 -0
  68. data/java/src/com/rubyeventmachine/tests/EMTest.java +80 -0
  69. data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +53 -0
  70. data/java/src/com/rubyeventmachine/tests/TestServers.java +75 -0
  71. data/java/src/com/rubyeventmachine/tests/TestTimers.java +90 -0
  72. data/lib/em/buftok.rb +138 -0
  73. data/lib/em/callback.rb +26 -0
  74. data/lib/em/channel.rb +57 -0
  75. data/lib/em/connection.rb +564 -0
  76. data/lib/em/deferrable.rb +192 -0
  77. data/lib/em/file_watch.rb +54 -0
  78. data/lib/em/future.rb +61 -0
  79. data/lib/em/messages.rb +66 -0
  80. data/lib/em/process_watch.rb +44 -0
  81. data/lib/em/processes.rb +119 -0
  82. data/lib/em/protocols/header_and_content.rb +138 -0
  83. data/lib/em/protocols/httpclient.rb +263 -0
  84. data/lib/em/protocols/httpclient2.rb +590 -0
  85. data/lib/em/protocols/line_and_text.rb +125 -0
  86. data/lib/em/protocols/linetext2.rb +161 -0
  87. data/lib/em/protocols/memcache.rb +323 -0
  88. data/lib/em/protocols/object_protocol.rb +45 -0
  89. data/lib/em/protocols/postgres3.rb +247 -0
  90. data/lib/em/protocols/saslauth.rb +175 -0
  91. data/lib/em/protocols/smtpclient.rb +357 -0
  92. data/lib/em/protocols/smtpserver.rb +547 -0
  93. data/lib/em/protocols/socks4.rb +66 -0
  94. data/lib/em/protocols/stomp.rb +200 -0
  95. data/lib/em/protocols/tcptest.rb +53 -0
  96. data/lib/em/protocols.rb +36 -0
  97. data/lib/em/queue.rb +61 -0
  98. data/lib/em/spawnable.rb +85 -0
  99. data/lib/em/streamer.rb +130 -0
  100. data/lib/em/timers.rb +56 -0
  101. data/lib/em/version.rb +3 -0
  102. data/lib/eventmachine.rb +1592 -0
  103. data/lib/evma/callback.rb +32 -0
  104. data/lib/evma/container.rb +75 -0
  105. data/lib/evma/factory.rb +77 -0
  106. data/lib/evma/protocol.rb +87 -0
  107. data/lib/evma/reactor.rb +48 -0
  108. data/lib/evma.rb +32 -0
  109. data/lib/jeventmachine.rb +257 -0
  110. data/lib/pr_eventmachine.rb +1022 -0
  111. data/setup.rb +1585 -0
  112. data/tasks/cpp.rake_example +77 -0
  113. data/tests/client.crt +31 -0
  114. data/tests/client.key +51 -0
  115. data/tests/test_attach.rb +126 -0
  116. data/tests/test_basic.rb +284 -0
  117. data/tests/test_channel.rb +63 -0
  118. data/tests/test_connection_count.rb +35 -0
  119. data/tests/test_defer.rb +47 -0
  120. data/tests/test_epoll.rb +160 -0
  121. data/tests/test_error_handler.rb +35 -0
  122. data/tests/test_errors.rb +82 -0
  123. data/tests/test_exc.rb +55 -0
  124. data/tests/test_file_watch.rb +49 -0
  125. data/tests/test_futures.rb +198 -0
  126. data/tests/test_get_sock_opt.rb +30 -0
  127. data/tests/test_handler_check.rb +37 -0
  128. data/tests/test_hc.rb +218 -0
  129. data/tests/test_httpclient.rb +218 -0
  130. data/tests/test_httpclient2.rb +153 -0
  131. data/tests/test_inactivity_timeout.rb +50 -0
  132. data/tests/test_kb.rb +60 -0
  133. data/tests/test_ltp.rb +182 -0
  134. data/tests/test_ltp2.rb +317 -0
  135. data/tests/test_next_tick.rb +133 -0
  136. data/tests/test_object_protocol.rb +37 -0
  137. data/tests/test_pause.rb +70 -0
  138. data/tests/test_pending_connect_timeout.rb +48 -0
  139. data/tests/test_process_watch.rb +48 -0
  140. data/tests/test_processes.rb +128 -0
  141. data/tests/test_proxy_connection.rb +92 -0
  142. data/tests/test_pure.rb +125 -0
  143. data/tests/test_queue.rb +44 -0
  144. data/tests/test_running.rb +42 -0
  145. data/tests/test_sasl.rb +72 -0
  146. data/tests/test_send_file.rb +242 -0
  147. data/tests/test_servers.rb +76 -0
  148. data/tests/test_smtpclient.rb +83 -0
  149. data/tests/test_smtpserver.rb +85 -0
  150. data/tests/test_spawn.rb +322 -0
  151. data/tests/test_ssl_args.rb +79 -0
  152. data/tests/test_ssl_methods.rb +50 -0
  153. data/tests/test_ssl_verify.rb +82 -0
  154. data/tests/test_timers.rb +162 -0
  155. data/tests/test_ud.rb +36 -0
  156. data/tests/testem.rb +31 -0
  157. data/web/whatis +7 -0
  158. metadata +237 -0
@@ -0,0 +1,80 @@
1
+ /**
2
+ * $Id$
3
+ *
4
+ * Author:: Francis Cianfrocca (gmail: blackhedd)
5
+ * Homepage:: http://rubyeventmachine.com
6
+ * Date:: 15 Jul 2007
7
+ *
8
+ * See EventMachine and EventMachine::Connection for documentation and
9
+ * usage examples.
10
+ *
11
+ *
12
+ *----------------------------------------------------------------------------
13
+ *
14
+ * Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
15
+ * Gmail: blackhedd
16
+ *
17
+ * This program is free software; you can redistribute it and/or modify
18
+ * it under the terms of either: 1) the GNU General Public License
19
+ * as published by the Free Software Foundation; either version 2 of the
20
+ * License, or (at your option) any later version; or 2) Ruby's License.
21
+ *
22
+ * See the file COPYING for complete licensing information.
23
+ *
24
+ *---------------------------------------------------------------------------
25
+ *
26
+ *
27
+ */
28
+
29
+
30
+ package com.rubyeventmachine.tests;
31
+
32
+ //import static org.junit.Assert.*;
33
+
34
+ import org.junit.After;
35
+ import org.junit.AfterClass;
36
+ import org.junit.Before;
37
+ import org.junit.BeforeClass;
38
+ import org.junit.Test;
39
+
40
+ import com.rubyeventmachine.EmReactor;
41
+
42
+ import java.io.*;
43
+ import java.nio.*;
44
+
45
+
46
+ public class EMTest {
47
+
48
+ class ShortTimer extends EmReactor {
49
+ public void eventCallback (String sig, int eventCode, ByteBuffer data) {
50
+ System.out.println ("Short Callback "+sig+" "+eventCode+" "+data);
51
+ this.stop();
52
+ }
53
+ }
54
+
55
+ @BeforeClass
56
+ public static void setUpBeforeClass() throws Exception {
57
+ }
58
+
59
+ @AfterClass
60
+ public static void tearDownAfterClass() throws Exception {
61
+ }
62
+
63
+ @Before
64
+ public void setUp() throws Exception {
65
+ }
66
+
67
+ @After
68
+ public void tearDown() throws Exception {
69
+ }
70
+
71
+
72
+ @Test
73
+ public final void testOneShort() throws IOException {
74
+ EmReactor em = new ShortTimer();
75
+ em.installOneshotTimer(1050);
76
+ em.run();
77
+ }
78
+
79
+
80
+ }
@@ -0,0 +1,53 @@
1
+ package com.rubyeventmachine.tests;
2
+
3
+ import com.rubyeventmachine.application.*;
4
+ import java.net.*;
5
+ import java.nio.*;
6
+
7
+ import org.junit.After;
8
+ import org.junit.AfterClass;
9
+ import org.junit.Before;
10
+ import org.junit.BeforeClass;
11
+ import org.junit.Test;
12
+
13
+
14
+ public class TestDatagrams {
15
+
16
+ @BeforeClass
17
+ public static void setUpBeforeClass() throws Exception {
18
+ }
19
+
20
+ @AfterClass
21
+ public static void tearDownAfterClass() throws Exception {
22
+ }
23
+
24
+ @Before
25
+ public void setUp() throws Exception {
26
+ }
27
+
28
+ @After
29
+ public void tearDown() throws Exception {
30
+ }
31
+
32
+ class A extends Connection {
33
+ public void receiveData (ByteBuffer bb) {
34
+ application.stop();
35
+ }
36
+ }
37
+ class B extends Connection {
38
+ public void postInit() {
39
+ this.sendDatagram(ByteBuffer.wrap(new String("ABC").getBytes()), new InetSocketAddress ("127.0.0.1", 9550));
40
+ }
41
+
42
+ }
43
+ @Test
44
+ public final void testA() {
45
+ final Application a = new Application();
46
+ a.run (new Runnable() {
47
+ public void run() {
48
+ a.openDatagramSocket( new InetSocketAddress ("0.0.0.0", 9550), new A() );
49
+ a.openDatagramSocket( new B() );
50
+ }
51
+ });
52
+ }
53
+ }
@@ -0,0 +1,75 @@
1
+ /**
2
+ * $Id$
3
+ *
4
+ * Author:: Francis Cianfrocca (gmail: blackhedd)
5
+ * Homepage:: http://rubyeventmachine.com
6
+ * Date:: 15 Jul 2007
7
+ *
8
+ * See EventMachine and EventMachine::Connection for documentation and
9
+ * usage examples.
10
+ *
11
+ *
12
+ *----------------------------------------------------------------------------
13
+ *
14
+ * Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
15
+ * Gmail: blackhedd
16
+ *
17
+ * This program is free software; you can redistribute it and/or modify
18
+ * it under the terms of either: 1) the GNU General Public License
19
+ * as published by the Free Software Foundation; either version 2 of the
20
+ * License, or (at your option) any later version; or 2) Ruby's License.
21
+ *
22
+ * See the file COPYING for complete licensing information.
23
+ *
24
+ *---------------------------------------------------------------------------
25
+ *
26
+ *
27
+ */
28
+
29
+
30
+ package com.rubyeventmachine.tests;
31
+
32
+
33
+ import com.rubyeventmachine.*;
34
+ import com.rubyeventmachine.application.*;
35
+ import java.net.*;
36
+ import org.junit.After;
37
+ import org.junit.AfterClass;
38
+ import org.junit.Before;
39
+ import org.junit.BeforeClass;
40
+ import org.junit.Test;
41
+ import org.junit.Assert;
42
+
43
+ public class TestServers {
44
+
45
+ @BeforeClass
46
+ public static void setUpBeforeClass() throws Exception {
47
+ }
48
+
49
+ @AfterClass
50
+ public static void tearDownAfterClass() throws Exception {
51
+ }
52
+
53
+ @Before
54
+ public void setUp() throws Exception {
55
+ }
56
+
57
+ @After
58
+ public void tearDown() throws Exception {
59
+ }
60
+
61
+
62
+ @Test
63
+ public void testBadServerAddress() {
64
+ final Application a = new Application();
65
+ a.run (new Runnable() {
66
+ public void run() {
67
+ try {
68
+ a.startServer(new InetSocketAddress ("100.100.100.100", 100), new DefaultConnectionFactory());
69
+ Assert.fail ("was supposed to throw a reactor exception");
70
+ } catch (EmReactorException e) {}
71
+ a.stop();
72
+ }
73
+ });
74
+ }
75
+ }
@@ -0,0 +1,90 @@
1
+ /**
2
+ * $Id$
3
+ *
4
+ * Author:: Francis Cianfrocca (gmail: blackhedd)
5
+ * Homepage:: http://rubyeventmachine.com
6
+ * Date:: 15 Jul 2007
7
+ *
8
+ * See EventMachine and EventMachine::Connection for documentation and
9
+ * usage examples.
10
+ *
11
+ *
12
+ *----------------------------------------------------------------------------
13
+ *
14
+ * Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
15
+ * Gmail: blackhedd
16
+ *
17
+ * This program is free software; you can redistribute it and/or modify
18
+ * it under the terms of either: 1) the GNU General Public License
19
+ * as published by the Free Software Foundation; either version 2 of the
20
+ * License, or (at your option) any later version; or 2) Ruby's License.
21
+ *
22
+ * See the file COPYING for complete licensing information.
23
+ *
24
+ *---------------------------------------------------------------------------
25
+ *
26
+ *
27
+ */
28
+
29
+
30
+ package com.rubyeventmachine.tests;
31
+
32
+ import com.rubyeventmachine.*;
33
+ import com.rubyeventmachine.application.*;
34
+ import java.io.*;
35
+
36
+ import org.junit.Assert;
37
+ import org.junit.After;
38
+ import org.junit.AfterClass;
39
+ import org.junit.Before;
40
+ import org.junit.BeforeClass;
41
+ import org.junit.Test;
42
+
43
+
44
+ public class TestTimers {
45
+
46
+ @BeforeClass
47
+ public static void setUpBeforeClass() throws Exception {
48
+ }
49
+
50
+ @AfterClass
51
+ public static void tearDownAfterClass() throws Exception {
52
+ }
53
+
54
+ @Before
55
+ public void setUp() throws Exception {
56
+ }
57
+
58
+ @After
59
+ public void tearDown() throws Exception {
60
+ }
61
+
62
+
63
+
64
+ @Test
65
+ public final void test2() throws IOException {
66
+ Application a = new Application();
67
+ a.addTimer(0, new Timer() {
68
+ public void fire() {
69
+ application.stop();
70
+ }
71
+ });
72
+ a.run();
73
+ Assert.assertEquals (1, 1); // just to make sure the reactor halts.
74
+ }
75
+
76
+ @Test
77
+ public final void test3() throws IOException {
78
+ Application a = new Application();
79
+ a.addTimer (0.1, new PeriodicTimer() {
80
+ int n = 0;
81
+ public void fire() {
82
+ n++;
83
+ if (n == 5)
84
+ application.stop();
85
+ }
86
+ });
87
+ a.run();
88
+ Assert.assertEquals(1, 1);
89
+ }
90
+ }
data/lib/em/buftok.rb ADDED
@@ -0,0 +1,138 @@
1
+ # BufferedTokenizer - Statefully split input data by a specifiable token
2
+ #
3
+ # Authors:: Tony Arcieri, Martin Emde
4
+ #
5
+ #----------------------------------------------------------------------------
6
+ #
7
+ # Copyright (C) 2006-07 by Tony Arcieri and Martin Emde
8
+ #
9
+ # Distributed under the Ruby license (http://www.ruby-lang.org/en/LICENSE.txt)
10
+ #
11
+ #---------------------------------------------------------------------------
12
+ #
13
+
14
+ # (C)2006 Tony Arcieri, Martin Emde
15
+ # Distributed under the Ruby license (http://www.ruby-lang.org/en/LICENSE.txt)
16
+
17
+ # BufferedTokenizer takes a delimiter upon instantiation, or acts line-based
18
+ # by default. It allows input to be spoon-fed from some outside source which
19
+ # receives arbitrary length datagrams which may-or-may-not contain the token
20
+ # by which entities are delimited.
21
+ #
22
+ # Commonly used to parse lines out of incoming data:
23
+ #
24
+ # module LineBufferedConnection
25
+ # def receive_data(data)
26
+ # (@buffer ||= BufferedTokenizer.new).extract(data).each do |line|
27
+ # receive_line(line)
28
+ # end
29
+ # end
30
+ # end
31
+
32
+ class BufferedTokenizer
33
+ # New BufferedTokenizers will operate on lines delimited by "\n" by default
34
+ # or allow you to specify any delimiter token you so choose, which will then
35
+ # be used by String#split to tokenize the input data
36
+ def initialize(delimiter = "\n", size_limit = nil)
37
+ # Store the specified delimiter
38
+ @delimiter = delimiter
39
+
40
+ # Store the specified size limitation
41
+ @size_limit = size_limit
42
+
43
+ # The input buffer is stored as an array. This is by far the most efficient
44
+ # approach given language constraints (in C a linked list would be a more
45
+ # appropriate data structure). Segments of input data are stored in a list
46
+ # which is only joined when a token is reached, substantially reducing the
47
+ # number of objects required for the operation.
48
+ @input = []
49
+
50
+ # Size of the input buffer
51
+ @input_size = 0
52
+ end
53
+
54
+ # Extract takes an arbitrary string of input data and returns an array of
55
+ # tokenized entities, provided there were any available to extract. This
56
+ # makes for easy processing of datagrams using a pattern like:
57
+ #
58
+ # tokenizer.extract(data).map { |entity| Decode(entity) }.each do ...
59
+ def extract(data)
60
+ # Extract token-delimited entities from the input string with the split command.
61
+ # There's a bit of craftiness here with the -1 parameter. Normally split would
62
+ # behave no differently regardless of if the token lies at the very end of the
63
+ # input buffer or not (i.e. a literal edge case) Specifying -1 forces split to
64
+ # return "" in this case, meaning that the last entry in the list represents a
65
+ # new segment of data where the token has not been encountered
66
+ entities = data.split @delimiter, -1
67
+
68
+ # Check to see if the buffer has exceeded capacity, if we're imposing a limit
69
+ if @size_limit
70
+ raise 'input buffer full' if @input_size + entities.first.size > @size_limit
71
+ @input_size += entities.first.size
72
+ end
73
+
74
+ # Move the first entry in the resulting array into the input buffer. It represents
75
+ # the last segment of a token-delimited entity unless it's the only entry in the list.
76
+ @input << entities.shift
77
+
78
+ # If the resulting array from the split is empty, the token was not encountered
79
+ # (not even at the end of the buffer). Since we've encountered no token-delimited
80
+ # entities this go-around, return an empty array.
81
+ return [] if entities.empty?
82
+
83
+ # At this point, we've hit a token, or potentially multiple tokens. Now we can bring
84
+ # together all the data we've buffered from earlier calls without hitting a token,
85
+ # and add it to our list of discovered entities.
86
+ entities.unshift @input.join
87
+
88
+ =begin
89
+ # Note added by FC, 10Jul07. This paragraph contains a regression. It breaks
90
+ # empty tokens. Think of the empty line that delimits an HTTP header. It will have
91
+ # two "\n" delimiters in a row, and this code mishandles the resulting empty token.
92
+ # It someone figures out how to fix the problem, we can re-enable this code branch.
93
+ # Multi-character token support.
94
+ # Split any tokens that were incomplete on the last iteration buf complete now.
95
+ entities.map! do |e|
96
+ e.split @delimiter, -1
97
+ end
98
+ # Flatten the resulting array. This has the side effect of removing the empty
99
+ # entry at the end that was produced by passing -1 to split. Add it again if
100
+ # necessary.
101
+ if (entities[-1] == [])
102
+ entities.flatten! << []
103
+ else
104
+ entities.flatten!
105
+ end
106
+ =end
107
+
108
+ # Now that we've hit a token, joined the input buffer and added it to the entities
109
+ # list, we can go ahead and clear the input buffer. All of the segments that were
110
+ # stored before the join can now be garbage collected.
111
+ @input.clear
112
+
113
+ # The last entity in the list is not token delimited, however, thanks to the -1
114
+ # passed to split. It represents the beginning of a new list of as-yet-untokenized
115
+ # data, so we add it to the start of the list.
116
+ @input << entities.pop
117
+
118
+ # Set the new input buffer size, provided we're keeping track
119
+ @input_size = @input.first.size if @size_limit
120
+
121
+ # Now we're left with the list of extracted token-delimited entities we wanted
122
+ # in the first place. Hooray!
123
+ entities
124
+ end
125
+
126
+ # Flush the contents of the input buffer, i.e. return the input buffer even though
127
+ # a token has not yet been encountered
128
+ def flush
129
+ buffer = @input.join
130
+ @input.clear
131
+ buffer
132
+ end
133
+
134
+ # Is the buffer empty?
135
+ def empty?
136
+ @input.empty?
137
+ end
138
+ end
@@ -0,0 +1,26 @@
1
+ module EventMachine
2
+ # Utility method for coercing arguments to an object that responds to #call
3
+ # Accepts an object and a method name to send to, or a block, or an object
4
+ # that responds to call.
5
+ #
6
+ # cb = EM.Callback{ |msg| puts(msg) }
7
+ # cb.call('hello world')
8
+ #
9
+ # cb = EM.Callback(Object, :puts)
10
+ # cb.call('hello world')
11
+ #
12
+ # cb = EM.Callback(proc{ |msg| puts(msg) })
13
+ # cb.call('hello world')
14
+ #
15
+ def self.Callback(object = nil, method = nil, &blk)
16
+ if object && method
17
+ lambda { |*args| object.send method, *args }
18
+ else
19
+ if object.respond_to? :call
20
+ object
21
+ else
22
+ blk || raise(ArgumentError)
23
+ end
24
+ end
25
+ end
26
+ end
data/lib/em/channel.rb ADDED
@@ -0,0 +1,57 @@
1
+ module EventMachine
2
+ # Provides a simple interface to push items to a number of subscribers. The
3
+ # channel will schedule all operations on the main reactor thread for thread
4
+ # safe reactor operations.
5
+ #
6
+ # This provides a convenient way for connections to consume messages from
7
+ # long running code in defer, without threading issues.
8
+ #
9
+ # channel = EM::Channel.new
10
+ # sid = channel.subscribe{ |msg| p [:got, msg] }
11
+ # channel.push('hello world')
12
+ # channel.unsubscribe(sid)
13
+ #
14
+ # See examples/ex_channel.rb for a detailed example.
15
+ class Channel
16
+ # Create a new channel
17
+ def initialize
18
+ @subs = {}
19
+ @uid = 0
20
+ end
21
+
22
+ # Takes any arguments suitable for EM::Callback() and returns a subscriber
23
+ # id for use when unsubscribing.
24
+ def subscribe(*a, &b)
25
+ name = gen_id
26
+ EM.schedule { @subs[name] = EM::Callback(*a, &b) }
27
+ name
28
+ end
29
+
30
+ # Removes this subscriber from the list.
31
+ def unsubscribe(name)
32
+ EM.schedule { @subs.delete name }
33
+ end
34
+
35
+ # Add items to the channel, which are pushed out to all subscribers.
36
+ def push(*items)
37
+ items = items.dup
38
+ EM.schedule { @subs.values.each { |s| items.each { |i| s.call i } } }
39
+ end
40
+ alias << push
41
+
42
+ # Receive exactly one message from the channel.
43
+ def pop(*a, &b)
44
+ EM.schedule {
45
+ name = subscribe do |*args|
46
+ unsubscribe(name)
47
+ EM::Callback(*a, &b).call(*args)
48
+ end
49
+ }
50
+ end
51
+
52
+ private
53
+ def gen_id # :nodoc:
54
+ @uid += 1
55
+ end
56
+ end
57
+ end