eventmachine-with-ipv6 1.0.0.beta.4.ipv6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. data/.gitignore +21 -0
  2. data/.yardopts +7 -0
  3. data/FORK.md +47 -0
  4. data/GNU +281 -0
  5. data/Gemfile +3 -0
  6. data/LICENSE +60 -0
  7. data/README.md +109 -0
  8. data/Rakefile +20 -0
  9. data/docs/DocumentationGuidesIndex.md +27 -0
  10. data/docs/GettingStarted.md +521 -0
  11. data/docs/old/ChangeLog +211 -0
  12. data/docs/old/DEFERRABLES +246 -0
  13. data/docs/old/EPOLL +141 -0
  14. data/docs/old/INSTALL +13 -0
  15. data/docs/old/KEYBOARD +42 -0
  16. data/docs/old/LEGAL +25 -0
  17. data/docs/old/LIGHTWEIGHT_CONCURRENCY +130 -0
  18. data/docs/old/PURE_RUBY +75 -0
  19. data/docs/old/RELEASE_NOTES +94 -0
  20. data/docs/old/SMTP +4 -0
  21. data/docs/old/SPAWNED_PROCESSES +148 -0
  22. data/docs/old/TODO +8 -0
  23. data/eventmachine.gemspec +50 -0
  24. data/examples/guides/getting_started/01_eventmachine_echo_server.rb +18 -0
  25. data/examples/guides/getting_started/02_eventmachine_echo_server_that_recognizes_exit_command.rb +22 -0
  26. data/examples/guides/getting_started/03_simple_chat_server.rb +149 -0
  27. data/examples/guides/getting_started/04_simple_chat_server_step_one.rb +27 -0
  28. data/examples/guides/getting_started/05_simple_chat_server_step_two.rb +43 -0
  29. data/examples/guides/getting_started/06_simple_chat_server_step_three.rb +98 -0
  30. data/examples/guides/getting_started/07_simple_chat_server_step_four.rb +121 -0
  31. data/examples/guides/getting_started/08_simple_chat_server_step_five.rb +141 -0
  32. data/examples/old/ex_channel.rb +43 -0
  33. data/examples/old/ex_queue.rb +2 -0
  34. data/examples/old/ex_tick_loop_array.rb +15 -0
  35. data/examples/old/ex_tick_loop_counter.rb +32 -0
  36. data/examples/old/helper.rb +2 -0
  37. data/ext/binder.cpp +124 -0
  38. data/ext/binder.h +46 -0
  39. data/ext/cmain.cpp +858 -0
  40. data/ext/ed.cpp +1992 -0
  41. data/ext/ed.h +423 -0
  42. data/ext/em.cpp +2358 -0
  43. data/ext/em.h +245 -0
  44. data/ext/eventmachine.h +127 -0
  45. data/ext/extconf.rb +166 -0
  46. data/ext/fastfilereader/extconf.rb +94 -0
  47. data/ext/fastfilereader/mapper.cpp +214 -0
  48. data/ext/fastfilereader/mapper.h +59 -0
  49. data/ext/fastfilereader/rubymain.cpp +127 -0
  50. data/ext/kb.cpp +79 -0
  51. data/ext/page.cpp +107 -0
  52. data/ext/page.h +51 -0
  53. data/ext/pipe.cpp +347 -0
  54. data/ext/project.h +155 -0
  55. data/ext/rubymain.cpp +1280 -0
  56. data/ext/ssl.cpp +468 -0
  57. data/ext/ssl.h +94 -0
  58. data/java/.classpath +8 -0
  59. data/java/.project +17 -0
  60. data/java/src/com/rubyeventmachine/EmReactor.java +571 -0
  61. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  62. data/java/src/com/rubyeventmachine/EventableChannel.java +69 -0
  63. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +189 -0
  64. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +364 -0
  65. data/lib/em/buftok.rb +110 -0
  66. data/lib/em/callback.rb +58 -0
  67. data/lib/em/channel.rb +64 -0
  68. data/lib/em/completion.rb +304 -0
  69. data/lib/em/connection.rb +728 -0
  70. data/lib/em/deferrable.rb +210 -0
  71. data/lib/em/deferrable/pool.rb +2 -0
  72. data/lib/em/file_watch.rb +73 -0
  73. data/lib/em/future.rb +61 -0
  74. data/lib/em/iterator.rb +270 -0
  75. data/lib/em/messages.rb +66 -0
  76. data/lib/em/pool.rb +151 -0
  77. data/lib/em/process_watch.rb +45 -0
  78. data/lib/em/processes.rb +123 -0
  79. data/lib/em/protocols.rb +36 -0
  80. data/lib/em/protocols/header_and_content.rb +138 -0
  81. data/lib/em/protocols/httpclient.rb +279 -0
  82. data/lib/em/protocols/httpclient2.rb +600 -0
  83. data/lib/em/protocols/line_and_text.rb +125 -0
  84. data/lib/em/protocols/line_protocol.rb +29 -0
  85. data/lib/em/protocols/linetext2.rb +161 -0
  86. data/lib/em/protocols/memcache.rb +331 -0
  87. data/lib/em/protocols/object_protocol.rb +46 -0
  88. data/lib/em/protocols/postgres3.rb +246 -0
  89. data/lib/em/protocols/saslauth.rb +175 -0
  90. data/lib/em/protocols/smtpclient.rb +365 -0
  91. data/lib/em/protocols/smtpserver.rb +640 -0
  92. data/lib/em/protocols/socks4.rb +66 -0
  93. data/lib/em/protocols/stomp.rb +202 -0
  94. data/lib/em/protocols/tcptest.rb +54 -0
  95. data/lib/em/pure_ruby.rb +1017 -0
  96. data/lib/em/queue.rb +71 -0
  97. data/lib/em/resolver.rb +195 -0
  98. data/lib/em/spawnable.rb +84 -0
  99. data/lib/em/streamer.rb +118 -0
  100. data/lib/em/threaded_resource.rb +90 -0
  101. data/lib/em/tick_loop.rb +85 -0
  102. data/lib/em/timers.rb +61 -0
  103. data/lib/em/version.rb +3 -0
  104. data/lib/eventmachine.rb +1517 -0
  105. data/lib/jeventmachine.rb +279 -0
  106. data/rakelib/cpp.rake_example +77 -0
  107. data/rakelib/package.rake +98 -0
  108. data/rakelib/test.rake +8 -0
  109. data/tests/client.crt +31 -0
  110. data/tests/client.key +51 -0
  111. data/tests/em_test_helper.rb +64 -0
  112. data/tests/test_attach.rb +126 -0
  113. data/tests/test_basic.rb +294 -0
  114. data/tests/test_channel.rb +62 -0
  115. data/tests/test_completion.rb +177 -0
  116. data/tests/test_connection_count.rb +33 -0
  117. data/tests/test_defer.rb +18 -0
  118. data/tests/test_deferrable.rb +35 -0
  119. data/tests/test_epoll.rb +134 -0
  120. data/tests/test_error_handler.rb +38 -0
  121. data/tests/test_exc.rb +28 -0
  122. data/tests/test_file_watch.rb +65 -0
  123. data/tests/test_futures.rb +170 -0
  124. data/tests/test_get_sock_opt.rb +37 -0
  125. data/tests/test_handler_check.rb +35 -0
  126. data/tests/test_hc.rb +155 -0
  127. data/tests/test_httpclient.rb +190 -0
  128. data/tests/test_httpclient2.rb +128 -0
  129. data/tests/test_inactivity_timeout.rb +54 -0
  130. data/tests/test_ipv4.rb +128 -0
  131. data/tests/test_ipv6.rb +135 -0
  132. data/tests/test_kb.rb +34 -0
  133. data/tests/test_ltp.rb +138 -0
  134. data/tests/test_ltp2.rb +288 -0
  135. data/tests/test_next_tick.rb +104 -0
  136. data/tests/test_object_protocol.rb +36 -0
  137. data/tests/test_pause.rb +78 -0
  138. data/tests/test_pending_connect_timeout.rb +52 -0
  139. data/tests/test_pool.rb +194 -0
  140. data/tests/test_process_watch.rb +48 -0
  141. data/tests/test_processes.rb +133 -0
  142. data/tests/test_proxy_connection.rb +168 -0
  143. data/tests/test_pure.rb +88 -0
  144. data/tests/test_queue.rb +50 -0
  145. data/tests/test_resolver.rb +55 -0
  146. data/tests/test_running.rb +14 -0
  147. data/tests/test_sasl.rb +47 -0
  148. data/tests/test_send_file.rb +217 -0
  149. data/tests/test_servers.rb +33 -0
  150. data/tests/test_set_sock_opt.rb +41 -0
  151. data/tests/test_shutdown_hooks.rb +23 -0
  152. data/tests/test_smtpclient.rb +55 -0
  153. data/tests/test_smtpserver.rb +57 -0
  154. data/tests/test_spawn.rb +293 -0
  155. data/tests/test_ssl_args.rb +78 -0
  156. data/tests/test_ssl_methods.rb +48 -0
  157. data/tests/test_ssl_verify.rb +82 -0
  158. data/tests/test_threaded_resource.rb +53 -0
  159. data/tests/test_tick_loop.rb +59 -0
  160. data/tests/test_timers.rb +123 -0
  161. data/tests/test_ud.rb +8 -0
  162. data/tests/test_udp46.rb +54 -0
  163. data/tests/test_unbind_reason.rb +48 -0
  164. metadata +319 -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,50 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/em/version', __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'eventmachine-with-ipv6'
6
+ s.version = EventMachine::VERSION
7
+ s.homepage = 'http://github.com/cabo/eventmachine'
8
+ # s.rubyforge_project = 'eventmachine'
9
+
10
+ s.authors = ["Francis Cianfrocca", "Aman Gupta", "hacked by Carsten Bormann"]
11
+ s.email = ["garbagecat10@gmail.com", "aman@tmm1.net", "cabo@tzi.org"]
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.extensions = ["ext/extconf.rb", "ext/fastfilereader/extconf.rb"]
15
+
16
+ s.add_development_dependency 'rake-compiler', '0.7.9'
17
+ s.add_development_dependency 'yard', ">= 0.7.2"
18
+ s.add_development_dependency 'bluecloth'
19
+
20
+ s.summary = 'Ruby/EventMachine library with UDP and IPv6 fixes'
21
+ s.description = "EventMachine implements a fast, single-threaded engine for arbitrary network
22
+ communications. It's extremely easy to use in Ruby. EventMachine wraps all
23
+ interactions with IP sockets, allowing programs to concentrate on the
24
+ implementation of network protocols. It can be used to create both network
25
+ servers and clients. To create a server or client, a Ruby program only needs
26
+ to specify the IP address and port, and provide a Module that implements the
27
+ communications protocol. Implementations of several standard network protocols
28
+ are provided with the package, primarily to serve as examples. The real goal
29
+ of EventMachine is to enable programs to easily interface with other programs
30
+ using TCP/IP, especially if custom protocols are required.
31
+
32
+ The present alternative version 'eventmachine-with-ipv6' contains some
33
+ crucial fixes for datagrams (UDP) and IPv6 developed since 2010 by
34
+ Carsten Bormann and Iñaki Baz Castillo. This is needed for many
35
+ applications in 2012, but might detract from the stability achieved
36
+ for other typical uses of the base eventmachine. It is otherwise
37
+ identical with base eventmachine. Install either base eventmachine or
38
+ this version eventmachine-with-ipv6. If you have installed both, use
39
+ gem 'eventmachine-with-ipv6'
40
+ before
41
+ require 'eventmachine'
42
+ Alternatively use Bundler and write this in your gemfile:
43
+ gem \"eventmachine\", :git => \"git://github.com/cabo/eventmachine\"
44
+
45
+ Please send all bugs in this version to https://github.com/cabo/eventmachine/issues
46
+ "
47
+
48
+ s.rdoc_options = ["--title", "EventMachine", "--main", "README.md", "-x", "lib/em/version", "-x", "lib/jeventmachine"]
49
+ s.extra_rdoc_files = ["README.md"] + `git ls-files -- docs/*`.split("\n")
50
+ end
@@ -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