eventmachine 1.2.0.dev.2-x64-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (181) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +105 -0
  3. data/GNU +281 -0
  4. data/LICENSE +60 -0
  5. data/README.md +108 -0
  6. data/docs/DocumentationGuidesIndex.md +27 -0
  7. data/docs/GettingStarted.md +521 -0
  8. data/docs/old/ChangeLog +211 -0
  9. data/docs/old/DEFERRABLES +246 -0
  10. data/docs/old/EPOLL +141 -0
  11. data/docs/old/INSTALL +13 -0
  12. data/docs/old/KEYBOARD +42 -0
  13. data/docs/old/LEGAL +25 -0
  14. data/docs/old/LIGHTWEIGHT_CONCURRENCY +130 -0
  15. data/docs/old/PURE_RUBY +75 -0
  16. data/docs/old/RELEASE_NOTES +94 -0
  17. data/docs/old/SMTP +4 -0
  18. data/docs/old/SPAWNED_PROCESSES +148 -0
  19. data/docs/old/TODO +8 -0
  20. data/examples/guides/getting_started/01_eventmachine_echo_server.rb +18 -0
  21. data/examples/guides/getting_started/02_eventmachine_echo_server_that_recognizes_exit_command.rb +22 -0
  22. data/examples/guides/getting_started/03_simple_chat_server.rb +149 -0
  23. data/examples/guides/getting_started/04_simple_chat_server_step_one.rb +27 -0
  24. data/examples/guides/getting_started/05_simple_chat_server_step_two.rb +43 -0
  25. data/examples/guides/getting_started/06_simple_chat_server_step_three.rb +98 -0
  26. data/examples/guides/getting_started/07_simple_chat_server_step_four.rb +121 -0
  27. data/examples/guides/getting_started/08_simple_chat_server_step_five.rb +141 -0
  28. data/examples/old/ex_channel.rb +43 -0
  29. data/examples/old/ex_queue.rb +2 -0
  30. data/examples/old/ex_tick_loop_array.rb +15 -0
  31. data/examples/old/ex_tick_loop_counter.rb +32 -0
  32. data/examples/old/helper.rb +2 -0
  33. data/ext/binder.cpp +124 -0
  34. data/ext/binder.h +46 -0
  35. data/ext/cmain.cpp +988 -0
  36. data/ext/ed.cpp +2111 -0
  37. data/ext/ed.h +442 -0
  38. data/ext/em.cpp +2379 -0
  39. data/ext/em.h +308 -0
  40. data/ext/eventmachine.h +143 -0
  41. data/ext/extconf.rb +270 -0
  42. data/ext/fastfilereader/extconf.rb +110 -0
  43. data/ext/fastfilereader/mapper.cpp +216 -0
  44. data/ext/fastfilereader/mapper.h +59 -0
  45. data/ext/fastfilereader/rubymain.cpp +127 -0
  46. data/ext/kb.cpp +79 -0
  47. data/ext/page.cpp +107 -0
  48. data/ext/page.h +51 -0
  49. data/ext/pipe.cpp +354 -0
  50. data/ext/project.h +176 -0
  51. data/ext/rubymain.cpp +1504 -0
  52. data/ext/ssl.cpp +615 -0
  53. data/ext/ssl.h +103 -0
  54. data/java/.classpath +8 -0
  55. data/java/.project +17 -0
  56. data/java/src/com/rubyeventmachine/EmReactor.java +591 -0
  57. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  58. data/java/src/com/rubyeventmachine/EventableChannel.java +72 -0
  59. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +201 -0
  60. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +415 -0
  61. data/lib/2.0/fastfilereaderext.so +0 -0
  62. data/lib/2.0/rubyeventmachine.so +0 -0
  63. data/lib/2.1/fastfilereaderext.so +0 -0
  64. data/lib/2.1/rubyeventmachine.so +0 -0
  65. data/lib/2.2/fastfilereaderext.so +0 -0
  66. data/lib/2.2/rubyeventmachine.so +0 -0
  67. data/lib/2.3/fastfilereaderext.so +0 -0
  68. data/lib/2.3/rubyeventmachine.so +0 -0
  69. data/lib/em/buftok.rb +59 -0
  70. data/lib/em/callback.rb +58 -0
  71. data/lib/em/channel.rb +69 -0
  72. data/lib/em/completion.rb +304 -0
  73. data/lib/em/connection.rb +770 -0
  74. data/lib/em/deferrable.rb +210 -0
  75. data/lib/em/deferrable/pool.rb +2 -0
  76. data/lib/em/file_watch.rb +73 -0
  77. data/lib/em/future.rb +61 -0
  78. data/lib/em/iterator.rb +252 -0
  79. data/lib/em/messages.rb +66 -0
  80. data/lib/em/pool.rb +151 -0
  81. data/lib/em/process_watch.rb +45 -0
  82. data/lib/em/processes.rb +123 -0
  83. data/lib/em/protocols.rb +37 -0
  84. data/lib/em/protocols/header_and_content.rb +138 -0
  85. data/lib/em/protocols/httpclient.rb +299 -0
  86. data/lib/em/protocols/httpclient2.rb +600 -0
  87. data/lib/em/protocols/line_and_text.rb +125 -0
  88. data/lib/em/protocols/line_protocol.rb +29 -0
  89. data/lib/em/protocols/linetext2.rb +166 -0
  90. data/lib/em/protocols/memcache.rb +331 -0
  91. data/lib/em/protocols/object_protocol.rb +46 -0
  92. data/lib/em/protocols/postgres3.rb +246 -0
  93. data/lib/em/protocols/saslauth.rb +175 -0
  94. data/lib/em/protocols/smtpclient.rb +394 -0
  95. data/lib/em/protocols/smtpserver.rb +666 -0
  96. data/lib/em/protocols/socks4.rb +66 -0
  97. data/lib/em/protocols/stomp.rb +205 -0
  98. data/lib/em/protocols/tcptest.rb +54 -0
  99. data/lib/em/pure_ruby.rb +1022 -0
  100. data/lib/em/queue.rb +80 -0
  101. data/lib/em/resolver.rb +232 -0
  102. data/lib/em/spawnable.rb +84 -0
  103. data/lib/em/streamer.rb +118 -0
  104. data/lib/em/threaded_resource.rb +90 -0
  105. data/lib/em/tick_loop.rb +85 -0
  106. data/lib/em/timers.rb +61 -0
  107. data/lib/em/version.rb +3 -0
  108. data/lib/eventmachine.rb +1584 -0
  109. data/lib/fastfilereaderext.rb +2 -0
  110. data/lib/jeventmachine.rb +301 -0
  111. data/lib/rubyeventmachine.rb +2 -0
  112. data/rakelib/package.rake +120 -0
  113. data/rakelib/test.rake +8 -0
  114. data/tests/client.crt +31 -0
  115. data/tests/client.key +51 -0
  116. data/tests/dhparam.pem +13 -0
  117. data/tests/em_test_helper.rb +151 -0
  118. data/tests/test_attach.rb +151 -0
  119. data/tests/test_basic.rb +283 -0
  120. data/tests/test_channel.rb +75 -0
  121. data/tests/test_completion.rb +178 -0
  122. data/tests/test_connection_count.rb +54 -0
  123. data/tests/test_connection_write.rb +35 -0
  124. data/tests/test_defer.rb +35 -0
  125. data/tests/test_deferrable.rb +35 -0
  126. data/tests/test_epoll.rb +142 -0
  127. data/tests/test_error_handler.rb +38 -0
  128. data/tests/test_exc.rb +28 -0
  129. data/tests/test_file_watch.rb +66 -0
  130. data/tests/test_fork.rb +75 -0
  131. data/tests/test_futures.rb +170 -0
  132. data/tests/test_get_sock_opt.rb +37 -0
  133. data/tests/test_handler_check.rb +35 -0
  134. data/tests/test_hc.rb +155 -0
  135. data/tests/test_httpclient.rb +233 -0
  136. data/tests/test_httpclient2.rb +128 -0
  137. data/tests/test_idle_connection.rb +25 -0
  138. data/tests/test_inactivity_timeout.rb +54 -0
  139. data/tests/test_ipv4.rb +125 -0
  140. data/tests/test_ipv6.rb +131 -0
  141. data/tests/test_iterator.rb +115 -0
  142. data/tests/test_kb.rb +28 -0
  143. data/tests/test_line_protocol.rb +33 -0
  144. data/tests/test_ltp.rb +138 -0
  145. data/tests/test_ltp2.rb +308 -0
  146. data/tests/test_many_fds.rb +22 -0
  147. data/tests/test_next_tick.rb +104 -0
  148. data/tests/test_object_protocol.rb +36 -0
  149. data/tests/test_pause.rb +107 -0
  150. data/tests/test_pending_connect_timeout.rb +52 -0
  151. data/tests/test_pool.rb +196 -0
  152. data/tests/test_process_watch.rb +50 -0
  153. data/tests/test_processes.rb +128 -0
  154. data/tests/test_proxy_connection.rb +180 -0
  155. data/tests/test_pure.rb +88 -0
  156. data/tests/test_queue.rb +64 -0
  157. data/tests/test_resolver.rb +104 -0
  158. data/tests/test_running.rb +14 -0
  159. data/tests/test_sasl.rb +47 -0
  160. data/tests/test_send_file.rb +217 -0
  161. data/tests/test_servers.rb +33 -0
  162. data/tests/test_set_sock_opt.rb +39 -0
  163. data/tests/test_shutdown_hooks.rb +23 -0
  164. data/tests/test_smtpclient.rb +75 -0
  165. data/tests/test_smtpserver.rb +57 -0
  166. data/tests/test_spawn.rb +293 -0
  167. data/tests/test_ssl_args.rb +78 -0
  168. data/tests/test_ssl_dhparam.rb +83 -0
  169. data/tests/test_ssl_ecdh_curve.rb +79 -0
  170. data/tests/test_ssl_extensions.rb +49 -0
  171. data/tests/test_ssl_methods.rb +65 -0
  172. data/tests/test_ssl_protocols.rb +246 -0
  173. data/tests/test_ssl_verify.rb +126 -0
  174. data/tests/test_stomp.rb +37 -0
  175. data/tests/test_system.rb +46 -0
  176. data/tests/test_threaded_resource.rb +61 -0
  177. data/tests/test_tick_loop.rb +59 -0
  178. data/tests/test_timers.rb +123 -0
  179. data/tests/test_ud.rb +8 -0
  180. data/tests/test_unbind_reason.rb +52 -0
  181. metadata +381 -0
@@ -0,0 +1,4 @@
1
+ This note details the usage of EventMachine's built-in support for SMTP. EM
2
+ supports both client and server connections, which will be described in
3
+ separate sections.
4
+
@@ -0,0 +1,148 @@
1
+ EventMachine (EM) adds two different formalisms for lightweight concurrency
2
+ to the Ruby programmer's toolbox: spawned processes and deferrables. This
3
+ note will show you how to use spawned processes. For more information, see
4
+ the separate document LIGHTWEIGHT_CONCURRENCY.
5
+
6
+
7
+ === What are Spawned Processes?
8
+
9
+ Spawned Processes in EventMachine are inspired directly by the "processes"
10
+ found in the Erlang programming language. EM deliberately borrows much (but
11
+ not all) of Erlang's terminology. However, EM's spawned processes differ from
12
+ Erlang's in ways that reflect not only Ruby style, but also the fact that
13
+ Ruby is not a functional language like Erlang.
14
+
15
+ Let's proceed with a complete, working code sample that we will analyze line
16
+ by line. Here's an EM implementation of the "ping-pong" program that also
17
+ appears in the Erlang tutorial:
18
+
19
+
20
+ require 'eventmachine'
21
+
22
+ EM.run {
23
+ pong = EM.spawn {|x, ping|
24
+ puts "Pong received #{x}"
25
+ ping.notify( x-1 )
26
+ }
27
+
28
+ ping = EM.spawn {|x|
29
+ if x > 0
30
+ puts "Pinging #{x}"
31
+ pong.notify x, self
32
+ else
33
+ EM.stop
34
+ end
35
+ }
36
+
37
+ ping.notify 3
38
+ }
39
+
40
+ If you run this program, you'll see the following output:
41
+
42
+ Pinging 3
43
+ Pong received 3
44
+ Pinging 2
45
+ Pong received 2
46
+ Pinging 1
47
+ Pong received 1
48
+
49
+ Let's take it step by step.
50
+
51
+ EventMachine#spawn works very much like the built-in function spawn in
52
+ Erlang. It returns a reference to a Ruby object of class
53
+ EventMachine::SpawnedProcess, which is actually a schedulable entity. In
54
+ Erlang, the value returned from spawn is called a "process identifier" or
55
+ "pid." But we'll refer to the Ruby object returned from EM#spawn simply as a
56
+ "spawned process."
57
+
58
+ You pass a Ruby block with zero or more parameters to EventMachine#spawn.
59
+ Like all Ruby blocks, this one is a closure, so it can refer to variables
60
+ defined in the local context when you call EM#spawn.
61
+
62
+ However, the code block passed to EM#spawn does NOT execute immediately by
63
+ default. Rather, it will execute only when the Spawned Object is "notified."
64
+ In Erlang, this process is called "message passing," and is done with the
65
+ operator !, but in Ruby it's done simply by calling the #notify method of a
66
+ spawned-process object. The parameters you pass to #notify must match those
67
+ defined in the block that was originally passed to EM#spawn.
68
+
69
+ When you call the #notify method of a spawned-process object, EM's reactor
70
+ core will execute the code block originally passed to EM#spawn, at some point
71
+ in the future. (#notify itself merely adds a notification to the object's
72
+ message queue and ALWAYS returns immediately.)
73
+
74
+ When a SpawnedProcess object executes a notification, it does so in the
75
+ context of the SpawnedProcess object itself. The notified code block can see
76
+ local context from the point at which EM#spawn was called. However, the value
77
+ of "self" inside the notified code block is a reference to the SpawnedProcesss
78
+ object itself.
79
+
80
+ An EM spawned process is nothing more than a Ruby object with a message
81
+ queue attached to it. You can have any number of spawned processes in your
82
+ program without compromising scalability. You can notify a spawned process
83
+ any number of times, and each notification will cause a "message" to be
84
+ placed in the queue of the spawned process. Spawned processes with non-empty
85
+ message queues are scheduled for execution automatically by the EM reactor.
86
+ Spawned processes with no visible references are garbage-collected like any
87
+ other Ruby object.
88
+
89
+ Back to our code sample:
90
+
91
+ pong = EM.spawn {|x, ping|
92
+ puts "Pong received #{x}"
93
+ ping.notify( x-1 )
94
+ }
95
+
96
+ This simply creates a spawned process and assigns it to the local variable
97
+ pong. You can see that the spawned code block takes a numeric parameter and a
98
+ reference to another spawned process. When pong is notified, it expects to
99
+ receive arguments corresponding to these two parameters. It simply prints out
100
+ the number it receives as the first argument. Then it notifies the spawned
101
+ process referenced by the second argument, passing it the first argument
102
+ minus 1.
103
+
104
+ And then the block ends, which is crucial because otherwise nothing else
105
+ can run. (Remember that in LC, scheduled entities run to completion and are
106
+ never preempted.)
107
+
108
+ On to the next bit of the code sample:
109
+
110
+ ping = EM.spawn {|x|
111
+ if x > 0
112
+ puts "Pinging #{x}"
113
+ pong.notify x, self
114
+ else
115
+ EM.stop
116
+ end
117
+ }
118
+
119
+ Here, we're spawning a process that takes a single (numeric) parameter. If
120
+ the parameter is greater than zero, the block writes it to the console. It
121
+ then notifies the spawned process referenced by the pong local variable,
122
+ passing as arguments its number argument, and a reference to itself. The
123
+ latter reference, as you saw above, is used by pong to send a return
124
+ notification.
125
+
126
+ If the ping process receives a zero value, it will stop the reactor loop and
127
+ end the program.
128
+
129
+ Now we've created a pair of spawned processes, but nothing else has happened.
130
+ If we stop now, the program will spin in the EM reactor loop, doing nothing
131
+ at all. Our spawned processes will never be scheduled for execution.
132
+
133
+ But look at the next line in the code sample:
134
+
135
+ ping.notify 3
136
+
137
+ This line gets the ping-pong ball rolling. We call ping's #notify method,
138
+ passing the argument 3. This causes a message to be sent to the ping spawned
139
+ process. The message contains the single argument, and it causes the EM
140
+ reactor to schedule the ping process. And this in turn results in the
141
+ execution of the Ruby code block passed to EM#spawn when ping was created.
142
+ Everything else proceeds as a result of the messages that are subsequently
143
+ passed to each other by the spawned processes.
144
+
145
+ [TODO, present the outbound network i/o use case, and clarify that spawned
146
+ processes are interleaved with normal i/o operations and don't interfere
147
+ with them at all. Also, blame Erlang for the confusing term "process"]
148
+
@@ -0,0 +1,8 @@
1
+ TODO List:
2
+
3
+ 12Aug06: Noticed by Don Stocks. A TCP connect-request that results
4
+ in a failed DNS resolution fires a fatal error back to user code.
5
+ Uuuuuugly. We should probably cause an unbind event to get fired
6
+ instead, and add some parameterization so the caller can detect
7
+ the nature of the failure.
8
+
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems' # or use Bundler.setup
4
+ require 'eventmachine'
5
+
6
+ class EchoServer < EM::Connection
7
+ def receive_data(data)
8
+ send_data(data)
9
+ end
10
+ end
11
+
12
+ EventMachine.run do
13
+ # hit Control + C to stop
14
+ Signal.trap("INT") { EventMachine.stop }
15
+ Signal.trap("TERM") { EventMachine.stop }
16
+
17
+ EventMachine.start_server("0.0.0.0", 10000, EchoServer)
18
+ end
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems' # or use Bundler.setup
4
+ require 'eventmachine'
5
+
6
+ class EchoServer < EM::Connection
7
+ def receive_data(data)
8
+ if data.strip =~ /exit$/i
9
+ EventMachine.stop
10
+ else
11
+ send_data(data)
12
+ end
13
+ end
14
+ end
15
+
16
+ EventMachine.run do
17
+ # hit Control + C to stop
18
+ Signal.trap("INT") { EventMachine.stop }
19
+ Signal.trap("TERM") { EventMachine.stop }
20
+
21
+ EventMachine.start_server("0.0.0.0", 10000, EchoServer)
22
+ end
@@ -0,0 +1,149 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems' # or use Bundler.setup
4
+ require 'eventmachine'
5
+
6
+ class SimpleChatServer < EM::Connection
7
+
8
+ @@connected_clients = Array.new
9
+ DM_REGEXP = /^@([a-zA-Z0-9]+)\s*:?\s*(.+)/.freeze
10
+
11
+ attr_reader :username
12
+
13
+
14
+ #
15
+ # EventMachine handlers
16
+ #
17
+
18
+ def post_init
19
+ @username = nil
20
+
21
+ puts "A client has connected..."
22
+ ask_username
23
+ end
24
+
25
+ def unbind
26
+ @@connected_clients.delete(self)
27
+ puts "[info] #{@username} has left" if entered_username?
28
+ end
29
+
30
+ def receive_data(data)
31
+ if entered_username?
32
+ handle_chat_message(data.strip)
33
+ else
34
+ handle_username(data.strip)
35
+ end
36
+ end
37
+
38
+
39
+ #
40
+ # Username handling
41
+ #
42
+
43
+ def entered_username?
44
+ !@username.nil? && !@username.empty?
45
+ end # entered_username?
46
+
47
+ def handle_username(input)
48
+ if input.empty?
49
+ send_line("Blank usernames are not allowed. Try again.")
50
+ ask_username
51
+ else
52
+ @username = input
53
+ @@connected_clients.push(self)
54
+ self.other_peers.each { |c| c.send_data("#{@username} has joined the room\n") }
55
+ puts "#{@username} has joined"
56
+
57
+ self.send_line("[info] Ohai, #{@username}")
58
+ end
59
+ end # handle_username(input)
60
+
61
+ def ask_username
62
+ self.send_line("[info] Enter your username:")
63
+ end # ask_username
64
+
65
+
66
+ #
67
+ # Message handling
68
+ #
69
+
70
+ def handle_chat_message(msg)
71
+ if command?(msg)
72
+ self.handle_command(msg)
73
+ else
74
+ if direct_message?(msg)
75
+ self.handle_direct_message(msg)
76
+ else
77
+ self.announce(msg, "#{@username}:")
78
+ end
79
+ end
80
+ end # handle_chat_message(msg)
81
+
82
+ def direct_message?(input)
83
+ input =~ DM_REGEXP
84
+ end # direct_message?(input)
85
+
86
+ def handle_direct_message(input)
87
+ username, message = parse_direct_message(input)
88
+
89
+ if connection = @@connected_clients.find { |c| c.username == username }
90
+ puts "[dm] @#{@username} => @#{username}"
91
+ connection.send_line("[dm] @#{@username}: #{message}")
92
+ else
93
+ send_line "@#{username} is not in the room. Here's who is: #{usernames.join(', ')}"
94
+ end
95
+ end # handle_direct_message(input)
96
+
97
+ def parse_direct_message(input)
98
+ return [$1, $2] if input =~ DM_REGEXP
99
+ end # parse_direct_message(input)
100
+
101
+
102
+ #
103
+ # Commands handling
104
+ #
105
+
106
+ def command?(input)
107
+ input =~ /(exit|status)$/i
108
+ end # command?(input)
109
+
110
+ def handle_command(cmd)
111
+ case cmd
112
+ when /exit$/i then self.close_connection
113
+ when /status$/i then self.send_line("[chat server] It's #{Time.now.strftime('%H:%M')} and there are #{self.number_of_connected_clients} people in the room")
114
+ end
115
+ end # handle_command(cmd)
116
+
117
+
118
+ #
119
+ # Helpers
120
+ #
121
+
122
+ def announce(msg = nil, prefix = "[chat server]")
123
+ @@connected_clients.each { |c| c.send_line("#{prefix} #{msg}") } unless msg.empty?
124
+ end # announce(msg)
125
+
126
+ def number_of_connected_clients
127
+ @@connected_clients.size
128
+ end # number_of_connected_clients
129
+
130
+ def other_peers
131
+ @@connected_clients.reject { |c| self == c }
132
+ end # other_peers
133
+
134
+ def send_line(line)
135
+ self.send_data("#{line}\n")
136
+ end # send_line(line)
137
+
138
+ def usernames
139
+ @@connected_clients.map { |c| c.username }
140
+ end # usernames
141
+ end
142
+
143
+ EventMachine.run do
144
+ # hit Control + C to stop
145
+ Signal.trap("INT") { EventMachine.stop }
146
+ Signal.trap("TERM") { EventMachine.stop }
147
+
148
+ EventMachine.start_server("0.0.0.0", 10000, SimpleChatServer)
149
+ end
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems' # or use Bundler.setup
4
+ require 'eventmachine'
5
+
6
+ class SimpleChatServer < EM::Connection
7
+
8
+ #
9
+ # EventMachine handlers
10
+ #
11
+
12
+ def post_init
13
+ puts "A client has connected..."
14
+ end
15
+
16
+ def unbind
17
+ puts "A client has left..."
18
+ end
19
+ end
20
+
21
+ EventMachine.run do
22
+ # hit Control + C to stop
23
+ Signal.trap("INT") { EventMachine.stop }
24
+ Signal.trap("TERM") { EventMachine.stop }
25
+
26
+ EventMachine.start_server("0.0.0.0", 10000, SimpleChatServer)
27
+ end
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems' # or use Bundler.setup
4
+ require 'eventmachine'
5
+
6
+ class SimpleChatServer < EM::Connection
7
+
8
+ @@connected_clients = Array.new
9
+
10
+
11
+ #
12
+ # EventMachine handlers
13
+ #
14
+
15
+ def post_init
16
+ @@connected_clients.push(self)
17
+ puts "A client has connected..."
18
+ end
19
+
20
+ def unbind
21
+ @@connected_clients.delete(self)
22
+ puts "A client has left..."
23
+ end
24
+
25
+
26
+
27
+
28
+ #
29
+ # Helpers
30
+ #
31
+
32
+ def other_peers
33
+ @@connected_clients.reject { |c| self == c }
34
+ end # other_peers
35
+ end
36
+
37
+ EventMachine.run do
38
+ # hit Control + C to stop
39
+ Signal.trap("INT") { EventMachine.stop }
40
+ Signal.trap("TERM") { EventMachine.stop }
41
+
42
+ EventMachine.start_server("0.0.0.0", 10000, SimpleChatServer)
43
+ end
@@ -0,0 +1,98 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems' # or use Bundler.setup
4
+ require 'eventmachine'
5
+
6
+ class SimpleChatServer < EM::Connection
7
+
8
+ @@connected_clients = Array.new
9
+
10
+
11
+ attr_reader :username
12
+
13
+
14
+ #
15
+ # EventMachine handlers
16
+ #
17
+
18
+ def post_init
19
+ @username = nil
20
+
21
+ puts "A client has connected..."
22
+ ask_username
23
+ end
24
+
25
+ def unbind
26
+ @@connected_clients.delete(self)
27
+ puts "A client has left..."
28
+ end
29
+
30
+ def receive_data(data)
31
+ if entered_username?
32
+ handle_chat_message(data.strip)
33
+ else
34
+ handle_username(data.strip)
35
+ end
36
+ end
37
+
38
+
39
+
40
+
41
+ #
42
+ # Username handling
43
+ #
44
+
45
+ def entered_username?
46
+ !@username.nil? && !@username.empty?
47
+ end # entered_username?
48
+
49
+ def handle_username(input)
50
+ if input.empty?
51
+ send_line("Blank usernames are not allowed. Try again.")
52
+ ask_username
53
+ else
54
+ @username = input
55
+ @@connected_clients.push(self)
56
+ self.other_peers.each { |c| c.send_data("#{@username} has joined the room\n") }
57
+ puts "#{@username} has joined"
58
+
59
+ self.send_line("[info] Ohai, #{@username}")
60
+ end
61
+ end # handle_username(input)
62
+
63
+ def ask_username
64
+ self.send_line("[info] Enter your username:")
65
+ end # ask_username
66
+
67
+
68
+
69
+ #
70
+ # Message handling
71
+ #
72
+
73
+ def handle_chat_message(msg)
74
+ raise NotImplementedError
75
+ end
76
+
77
+
78
+
79
+ #
80
+ # Helpers
81
+ #
82
+
83
+ def other_peers
84
+ @@connected_clients.reject { |c| self == c }
85
+ end # other_peers
86
+
87
+ def send_line(line)
88
+ self.send_data("#{line}\n")
89
+ end # send_line(line)
90
+ end
91
+
92
+ EventMachine.run do
93
+ # hit Control + C to stop
94
+ Signal.trap("INT") { EventMachine.stop }
95
+ Signal.trap("TERM") { EventMachine.stop }
96
+
97
+ EventMachine.start_server("0.0.0.0", 10000, SimpleChatServer)
98
+ end