em-nodes 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +1 -0
- data/benchmark/client.rb +27 -0
- data/benchmark/run.sh +9 -0
- data/benchmark/server.rb +44 -0
- data/examples/chat_client.rb +4 -4
- data/examples/chat_server.rb +6 -6
- data/lib/em-nodes/client/hello.rb +21 -0
- data/lib/em-nodes/client/task.rb +18 -0
- data/lib/em-nodes/client.rb +9 -8
- data/lib/em-nodes/commands.rb +26 -12
- data/lib/em-nodes/server/hello.rb +27 -0
- data/lib/em-nodes/server/task.rb +56 -0
- data/lib/em-nodes/server.rb +22 -13
- data/lib/em-nodes.rb +1 -1
- data/spec/base_spec.rb +56 -0
- data/spec/hello_spec.rb +32 -0
- data/spec/simple2_spec.rb +6 -6
- data/spec/simple_spec.rb +7 -7
- data/spec/task_spec.rb +64 -0
- metadata +16 -5
- data/lib/em-nodes/abstract_command.rb +0 -19
- data/lib/em-nodes/client/server.rb +0 -13
- data/lib/em-nodes/server/client.rb +0 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ddccb975cd4137ced9c16858feba42614f81bcf5
|
4
|
+
data.tar.gz: 2e61ef3174a99ea5b024d4debf8fc31011a2c475
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 09ae7052192a4bdfc4726dc810db41ba2f97c5d710652fdd7b6bf8fb33719d4d5386a607238a726fc3d3c4fb17270f48bab3509a3148e36e0f81084615349a22
|
7
|
+
data.tar.gz: 427c10dae0acd687425c51c49d5a9f749ef55b74cc7b2a700444caf1d90aa42bfcf27bccef1054027b03468bd5e51bcd5562734dfa0d30cb19bcb2f2fcfcef7a
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/benchmark/client.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require "bundler/setup"
|
3
|
+
Bundler.require
|
4
|
+
|
5
|
+
class Client < EM::Nodes::Client
|
6
|
+
include HelloFeature
|
7
|
+
include TaskFeature
|
8
|
+
|
9
|
+
def info
|
10
|
+
{ :name => "client" }
|
11
|
+
end
|
12
|
+
|
13
|
+
def on_task(task_id, data)
|
14
|
+
send_task_result(task_id, data + 1)
|
15
|
+
end
|
16
|
+
|
17
|
+
def unbind
|
18
|
+
super
|
19
|
+
EM.stop
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
EM.run do
|
24
|
+
puts "client run"
|
25
|
+
Client.connect '/tmp/test_em_nodes_sock'
|
26
|
+
end
|
27
|
+
|
data/benchmark/run.sh
ADDED
data/benchmark/server.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require "bundler/setup"
|
3
|
+
Bundler.require
|
4
|
+
|
5
|
+
CLIENTS_COUNT = 5
|
6
|
+
TASKS_COUNT = 100000
|
7
|
+
|
8
|
+
class Server < EM::Nodes::Server
|
9
|
+
include HelloFeature
|
10
|
+
include TaskFeature
|
11
|
+
|
12
|
+
def on_task_result(res)
|
13
|
+
$res_count += 1
|
14
|
+
$res += res
|
15
|
+
|
16
|
+
if $res_count >= TASKS_COUNT
|
17
|
+
EM.next_tick { EM.stop }
|
18
|
+
else
|
19
|
+
send_task(res + 1)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
$res = 0
|
25
|
+
$res_count = 0
|
26
|
+
|
27
|
+
tm = Time.now
|
28
|
+
|
29
|
+
EM.run do
|
30
|
+
Server.start '/tmp/test_em_nodes_sock'
|
31
|
+
|
32
|
+
tm = Time.now
|
33
|
+
Thread.new do
|
34
|
+
sleep 0.5 while Server.ready_clients.size < CLIENTS_COUNT
|
35
|
+
tm = Time.now
|
36
|
+
|
37
|
+
Server.ready_clients.each do |client|
|
38
|
+
client.send_task(0)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
puts "executed with #{Time.now - tm}, res: #{$res}, res_count: #{$res_count}"
|
data/examples/chat_client.rb
CHANGED
@@ -8,11 +8,11 @@ class ChatClient < EM::Nodes::Client
|
|
8
8
|
super
|
9
9
|
end
|
10
10
|
|
11
|
-
def
|
12
|
-
|
11
|
+
def on_who_are_you?
|
12
|
+
send_i_am @name
|
13
13
|
end
|
14
14
|
|
15
|
-
def
|
15
|
+
def on_say(msg)
|
16
16
|
puts msg
|
17
17
|
end
|
18
18
|
|
@@ -23,7 +23,7 @@ class ChatClient < EM::Nodes::Client
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def say(msg)
|
26
|
-
$cl.
|
26
|
+
$cl.send_say(msg)
|
27
27
|
end
|
28
28
|
|
29
29
|
EM.run do
|
data/examples/chat_server.rb
CHANGED
@@ -4,16 +4,16 @@ Bundler.require
|
|
4
4
|
class ChatServer < EM::Nodes::Server
|
5
5
|
def post_init
|
6
6
|
super
|
7
|
-
|
7
|
+
send_who_are_you?
|
8
8
|
end
|
9
9
|
|
10
|
-
def
|
11
|
-
|
10
|
+
def on_i_am(name)
|
11
|
+
self.data.name = name
|
12
12
|
to_all "> coming #{name} <"
|
13
13
|
end
|
14
14
|
|
15
|
-
def
|
16
|
-
to_all "#{
|
15
|
+
def on_say(msg)
|
16
|
+
to_all "#{data.name} say: #{msg}"
|
17
17
|
end
|
18
18
|
|
19
19
|
def to_all(msg)
|
@@ -22,7 +22,7 @@ class ChatServer < EM::Nodes::Server
|
|
22
22
|
|
23
23
|
def unbind
|
24
24
|
super
|
25
|
-
to_all("quiting #{
|
25
|
+
to_all("quiting #{data.name}")
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class EM::Nodes::Client
|
2
|
+
module HelloFeature
|
3
|
+
|
4
|
+
def on_who_are_you?
|
5
|
+
i = info
|
6
|
+
raise "info should be a Hash, but not #{i.inspect}" unless i.is_a?(Hash)
|
7
|
+
send_i_am(i.merge(__default_info__))
|
8
|
+
end
|
9
|
+
|
10
|
+
def info
|
11
|
+
{} # redefine me
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def __default_info__
|
17
|
+
{ :hostname => (`hostname` rescue "").chop }
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
class EM::Nodes::Client
|
2
|
+
module TaskFeature
|
3
|
+
|
4
|
+
def on_task(task_id, data)
|
5
|
+
# redefine me
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def on_task_internal(task_id, data)
|
11
|
+
on_task(task_id, data)
|
12
|
+
end
|
13
|
+
|
14
|
+
def send_task_result(task_id, result)
|
15
|
+
send_task_result_internal(task_id, result)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/em-nodes/client.rb
CHANGED
@@ -1,22 +1,23 @@
|
|
1
1
|
class EM::Nodes::Client < EM::Connection
|
2
|
-
autoload :
|
3
|
-
|
4
|
-
attr_reader :server
|
2
|
+
autoload :HelloFeature, 'em-nodes/client/hello'
|
3
|
+
autoload :TaskFeature, 'em-nodes/client/task'
|
5
4
|
|
6
5
|
include EM::P::ObjectProtocol
|
7
6
|
include EM::Nodes::Commands
|
8
7
|
|
8
|
+
attr_reader :alive
|
9
|
+
|
9
10
|
def post_init
|
10
|
-
@
|
11
|
-
EM::Nodes.logger.
|
11
|
+
@alive = true
|
12
|
+
EM::Nodes.logger.info { "Connected to server" }
|
12
13
|
end
|
13
14
|
|
14
15
|
def unbind
|
15
|
-
@
|
16
|
-
EM::Nodes.logger.warn "
|
16
|
+
@alive = false
|
17
|
+
EM::Nodes.logger.warn { "Connection has terminated" }
|
17
18
|
end
|
18
19
|
|
19
|
-
def self.connect(host, port, *args, &block)
|
20
|
+
def self.connect(host, port = nil, *args, &block)
|
20
21
|
EM.connect(host, port, self, *args)
|
21
22
|
end
|
22
23
|
end
|
data/lib/em-nodes/commands.rb
CHANGED
@@ -1,23 +1,37 @@
|
|
1
1
|
module EM::Nodes::Commands
|
2
|
-
def receive_object(h)
|
3
|
-
unless h.is_a?(Hash)
|
4
|
-
EM::Nodes.logger.error "received unknown object: #{obj.inspect}"
|
5
|
-
return
|
6
|
-
end
|
7
2
|
|
8
|
-
|
9
|
-
args = h
|
10
|
-
|
3
|
+
def receive_object(h)
|
4
|
+
method, args = h
|
5
|
+
method = 'on_' + method
|
11
6
|
t = Time.now
|
12
7
|
send(method, *args)
|
13
|
-
EM::Nodes.logger.
|
8
|
+
EM::Nodes.logger.debug { "<= #{method} #{args.inspect} (#{Time.now - t}s)" }
|
9
|
+
|
14
10
|
rescue Object => ex
|
15
11
|
EM::Nodes.exception(ex)
|
16
12
|
end
|
17
13
|
|
18
14
|
def send_command(method, args)
|
19
|
-
|
20
|
-
EM
|
21
|
-
|
15
|
+
EM::Nodes.logger.debug { "=> #{method}" }
|
16
|
+
EM.schedule { send_object [method.to_s, args] }
|
17
|
+
end
|
18
|
+
|
19
|
+
COMMAND_PREFIX = 'send_'
|
20
|
+
|
21
|
+
def method_missing(method, *args)
|
22
|
+
method = method.to_s
|
23
|
+
|
24
|
+
unless method.start_with?(COMMAND_PREFIX)
|
25
|
+
EM::Nodes.logger.warn { "unknown send :#{method} #{args.inspect}" }
|
26
|
+
super(method, *args)
|
27
|
+
return
|
28
|
+
end
|
29
|
+
|
30
|
+
if @alive
|
31
|
+
send_command(method[5..-1], args)
|
32
|
+
else
|
33
|
+
EM::Nodes.logger.error { "failed command attempt #{method}, connection dead" }
|
34
|
+
end
|
22
35
|
end
|
36
|
+
|
23
37
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class EM::Nodes::Server
|
2
|
+
module HelloFeature
|
3
|
+
def self.included(base)
|
4
|
+
base.send :extend, ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def ready_clients
|
9
|
+
clients.select{ |cl| cl.data.ready }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def post_init
|
14
|
+
super
|
15
|
+
send_who_are_you?
|
16
|
+
end
|
17
|
+
|
18
|
+
def on_i_am(params)
|
19
|
+
params.each do |key, value|
|
20
|
+
self.data.send "#{key}=", value
|
21
|
+
end
|
22
|
+
|
23
|
+
self.data.ready = true
|
24
|
+
EM::Nodes.logger.info { "Hello client #{self.data.inspect}" }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'thread'
|
2
|
+
|
3
|
+
class EM::Nodes::Server
|
4
|
+
module TaskFeature
|
5
|
+
def initialize(*args)
|
6
|
+
super(*args)
|
7
|
+
@mutex = Mutex.new
|
8
|
+
@tasks = {}
|
9
|
+
@task_inc = 0
|
10
|
+
end
|
11
|
+
|
12
|
+
def task_count
|
13
|
+
@mutex.synchronize { @tasks.size }
|
14
|
+
end
|
15
|
+
|
16
|
+
def send_task(data)
|
17
|
+
task_id = next_task_id
|
18
|
+
add_task(task_id, data)
|
19
|
+
send_task_internal(task_id, data)
|
20
|
+
end
|
21
|
+
|
22
|
+
def on_task_result(res)
|
23
|
+
# redefine me
|
24
|
+
end
|
25
|
+
|
26
|
+
def on_reschedule_tasks(values)
|
27
|
+
# redefine me
|
28
|
+
end
|
29
|
+
|
30
|
+
def unbind
|
31
|
+
super
|
32
|
+
on_reschedule_tasks(@tasks.values)
|
33
|
+
@mutex.synchronize { @tasks.clear }
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def on_task_result_internal(task_id, res)
|
39
|
+
del_task(task_id)
|
40
|
+
on_task_result(res)
|
41
|
+
end
|
42
|
+
|
43
|
+
def add_task(task_id, data)
|
44
|
+
@mutex.synchronize { @tasks[task_id] = data }
|
45
|
+
end
|
46
|
+
|
47
|
+
def del_task(task_id)
|
48
|
+
@mutex.synchronize { @tasks.delete(task_id) }
|
49
|
+
end
|
50
|
+
|
51
|
+
def next_task_id
|
52
|
+
@mutex.synchronize { @task_inc += 1 }
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
data/lib/em-nodes/server.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
1
|
require 'socket'
|
2
|
+
require 'ostruct'
|
2
3
|
|
3
4
|
class EM::Nodes::Server < EM::Connection
|
4
|
-
autoload :
|
5
|
-
|
6
|
-
attr_reader :client
|
5
|
+
autoload :HelloFeature, 'em-nodes/server/hello'
|
6
|
+
autoload :TaskFeature, 'em-nodes/server/task'
|
7
7
|
|
8
8
|
include EM::P::ObjectProtocol
|
9
9
|
include EM::Nodes::Commands
|
10
10
|
|
11
|
+
attr_reader :alive
|
12
|
+
|
11
13
|
class << self
|
12
14
|
def clients
|
13
15
|
@clients ||= []
|
@@ -18,6 +20,8 @@ class EM::Nodes::Server < EM::Connection
|
|
18
20
|
end
|
19
21
|
end
|
20
22
|
|
23
|
+
attr_reader :data
|
24
|
+
|
21
25
|
def accept?(host, port)
|
22
26
|
true
|
23
27
|
end
|
@@ -27,27 +31,32 @@ class EM::Nodes::Server < EM::Connection
|
|
27
31
|
end
|
28
32
|
|
29
33
|
def post_init
|
30
|
-
|
34
|
+
@data = OpenStruct.new
|
31
35
|
|
32
|
-
|
36
|
+
self.comm_inactivity_timeout = inactivity_timeout if EM.reactor_running?
|
37
|
+
|
38
|
+
port, host = Socket.unpack_sockaddr_in(get_peername) rescue []
|
33
39
|
unless accept?(host, port)
|
34
40
|
unbind
|
35
41
|
return
|
36
42
|
end
|
43
|
+
self.data.host = host
|
44
|
+
self.data.port = port
|
37
45
|
|
38
|
-
@
|
39
|
-
self.class.clients <<
|
40
|
-
EM::Nodes.logger.info "Incomming connection from #{host}:#{port}"
|
46
|
+
@alive = true
|
47
|
+
self.class.clients << self
|
48
|
+
EM::Nodes.logger.info { "Incomming connection from #{host}:#{port}" }
|
41
49
|
end
|
42
50
|
|
43
51
|
def unbind
|
44
|
-
@
|
45
|
-
self.class.clients.delete
|
46
|
-
EM::Nodes.logger.info "Client has disconnected"
|
52
|
+
@alive = false
|
53
|
+
self.class.clients.delete self
|
54
|
+
EM::Nodes.logger.info { "Client #{self.data.inspect} has disconnected" }
|
47
55
|
end
|
48
56
|
|
49
|
-
def self.start(host, port, *args)
|
50
|
-
EM::Nodes.logger.info "
|
57
|
+
def self.start(host, port = nil, *args)
|
58
|
+
EM::Nodes.logger.info { "Start server #{host}:#{port}" }
|
51
59
|
EM.start_server host, port, self, *args
|
52
60
|
end
|
53
61
|
end
|
62
|
+
|
data/lib/em-nodes.rb
CHANGED
data/spec/base_spec.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
class Client0 < EM::Nodes::Client
|
4
|
+
attr_reader :bla
|
5
|
+
|
6
|
+
def post_init
|
7
|
+
super
|
8
|
+
@bla = 1
|
9
|
+
end
|
10
|
+
|
11
|
+
def unbind
|
12
|
+
super
|
13
|
+
@bla = 2
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Server0 < EM::Nodes::Server
|
18
|
+
attr_reader :bla
|
19
|
+
|
20
|
+
def post_init
|
21
|
+
super
|
22
|
+
@bla = 1
|
23
|
+
end
|
24
|
+
|
25
|
+
def unbind
|
26
|
+
super
|
27
|
+
@bla = 2
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "Base spec" do
|
32
|
+
it "base callbacks should work" do
|
33
|
+
EM.run do
|
34
|
+
$server0 = Server0.start('/tmp/_emn_server0')
|
35
|
+
$client0 = Client0.connect('/tmp/_emn_server0')
|
36
|
+
|
37
|
+
EM.next_tick do
|
38
|
+
$client0.bla.should == 1
|
39
|
+
$client0.alive.should == true
|
40
|
+
|
41
|
+
$client00 = Server0.clients.first
|
42
|
+
$client00.bla.should == 1
|
43
|
+
$client00.alive.should == true
|
44
|
+
end
|
45
|
+
|
46
|
+
EM.add_timer(0.2) { EM.next_tick { EM.stop } }
|
47
|
+
end
|
48
|
+
|
49
|
+
$client0.bla.should == 2
|
50
|
+
$client0.alive.should == false
|
51
|
+
|
52
|
+
Server0.clients.size.should == 0
|
53
|
+
$client00.bla.should == 2
|
54
|
+
$client00.alive.should == false
|
55
|
+
end
|
56
|
+
end
|
data/spec/hello_spec.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
class Client3 < EM::Nodes::Client
|
4
|
+
include HelloFeature
|
5
|
+
|
6
|
+
def info
|
7
|
+
{ :name => "vasya" }
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Server3 < EM::Nodes::Server
|
12
|
+
include HelloFeature
|
13
|
+
|
14
|
+
def on_i_am(params)
|
15
|
+
super
|
16
|
+
$client3_result = self.data
|
17
|
+
$server3_ready_clients = Server3.ready_clients
|
18
|
+
EM.stop
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "Hello spec" do
|
23
|
+
it "should work" do
|
24
|
+
EM.run do
|
25
|
+
$server3 = Server3.start('127.0.0.1', 19994)
|
26
|
+
$client3 = Client3.connect('127.0.0.1', 19994)
|
27
|
+
end
|
28
|
+
|
29
|
+
$client3_result.name.should == 'vasya'
|
30
|
+
$server3_ready_clients.size.should == 1
|
31
|
+
end
|
32
|
+
end
|
data/spec/simple2_spec.rb
CHANGED
@@ -3,15 +3,15 @@ require File.dirname(__FILE__) + '/spec_helper'
|
|
3
3
|
$pong = []
|
4
4
|
|
5
5
|
class Client2 < EM::Nodes::Client
|
6
|
-
def
|
6
|
+
def on_pong(a)
|
7
7
|
$pong << a
|
8
8
|
EM.stop if $pong.size >= 2
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
12
12
|
class Server2 < EM::Nodes::Server
|
13
|
-
def
|
14
|
-
|
13
|
+
def on_ping(a)
|
14
|
+
send_pong(a + 1)
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
@@ -22,10 +22,10 @@ describe "Simple spec" do
|
|
22
22
|
$client21 = Client2.connect('127.0.0.1', 19993)
|
23
23
|
$client22 = Client2.connect('127.0.0.1', 19993)
|
24
24
|
|
25
|
-
$client21.
|
26
|
-
$client22.
|
25
|
+
$client21.send_ping(1)
|
26
|
+
$client22.send_ping(2)
|
27
27
|
end
|
28
28
|
|
29
|
-
$pong.should == [
|
29
|
+
$pong.should == [2, 3]
|
30
30
|
end
|
31
31
|
end
|
data/spec/simple_spec.rb
CHANGED
@@ -3,11 +3,11 @@ require File.dirname(__FILE__) + '/spec_helper'
|
|
3
3
|
class Client1 < EM::Nodes::Client
|
4
4
|
def initialize(name)
|
5
5
|
@name = name
|
6
|
-
super
|
6
|
+
super()
|
7
7
|
end
|
8
8
|
|
9
|
-
def
|
10
|
-
|
9
|
+
def on_who_are_you?
|
10
|
+
send_i_am @name
|
11
11
|
end
|
12
12
|
|
13
13
|
def unbind
|
@@ -19,13 +19,13 @@ end
|
|
19
19
|
class Server1 < EM::Nodes::Server
|
20
20
|
def post_init
|
21
21
|
super
|
22
|
-
|
22
|
+
send_who_are_you?
|
23
23
|
end
|
24
24
|
|
25
|
-
def
|
26
|
-
|
25
|
+
def on_i_am(name)
|
26
|
+
self.data.name = name
|
27
27
|
$client1_result = { :name => name, :clients => Server1.clients.clone }
|
28
|
-
|
28
|
+
EM.stop
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
data/spec/task_spec.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
class Client4 < EM::Nodes::Client
|
5
|
+
include TaskFeature
|
6
|
+
|
7
|
+
def on_task(task_id, data)
|
8
|
+
$client4_results << data
|
9
|
+
send_task_result(task_id, data + 1) if $client4_results.size <= 10
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class Server4 < EM::Nodes::Server
|
14
|
+
include TaskFeature
|
15
|
+
|
16
|
+
def on_task_result(res)
|
17
|
+
$server4_results << res
|
18
|
+
EM.next_tick { EM.stop } if $server4_results.size >= 10
|
19
|
+
end
|
20
|
+
|
21
|
+
def on_reschedule_tasks(values)
|
22
|
+
$server4_unbind_results = values
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "Task spec" do
|
27
|
+
before {
|
28
|
+
$client4_results = []
|
29
|
+
$server4_results = []
|
30
|
+
$server4_unbind_results = []
|
31
|
+
}
|
32
|
+
|
33
|
+
it "should work" do
|
34
|
+
client4 = nil
|
35
|
+
EM.run do
|
36
|
+
$server4 = Server4.start('127.0.0.1', 19995)
|
37
|
+
$client4 = Client4.connect('127.0.0.1', 19995)
|
38
|
+
|
39
|
+
EM.next_tick do
|
40
|
+
client4 = Server4.clients.first
|
41
|
+
10.times { |i| client4.send_task(i) }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
$server4_results.should == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
46
|
+
$server4_unbind_results.should == []
|
47
|
+
client4.task_count.should == 0
|
48
|
+
end
|
49
|
+
|
50
|
+
it "on unbind" do
|
51
|
+
EM.run do
|
52
|
+
$server4 = Server4.start('127.0.0.1', 19996)
|
53
|
+
$client4 = Client4.connect('127.0.0.1', 19996)
|
54
|
+
|
55
|
+
EM.next_tick do
|
56
|
+
client4 = Server4.clients.first
|
57
|
+
20.times { |i| client4.send_task(i) }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
$server4_results.should == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
62
|
+
$server4_unbind_results.sort.should == [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
|
63
|
+
end
|
64
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: em-nodes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- '''Konstantin Makarchev'''
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-04-
|
11
|
+
date: 2014-04-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: eventmachine
|
@@ -74,23 +74,31 @@ extensions: []
|
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
76
|
- .gitignore
|
77
|
+
- .rspec
|
77
78
|
- Gemfile
|
78
79
|
- LICENSE.txt
|
79
80
|
- README.md
|
80
81
|
- Rakefile
|
82
|
+
- benchmark/client.rb
|
83
|
+
- benchmark/run.sh
|
84
|
+
- benchmark/server.rb
|
81
85
|
- em-nodes.gemspec
|
82
86
|
- examples/chat_client.rb
|
83
87
|
- examples/chat_server.rb
|
84
88
|
- lib/em-nodes.rb
|
85
|
-
- lib/em-nodes/abstract_command.rb
|
86
89
|
- lib/em-nodes/client.rb
|
87
|
-
- lib/em-nodes/client/
|
90
|
+
- lib/em-nodes/client/hello.rb
|
91
|
+
- lib/em-nodes/client/task.rb
|
88
92
|
- lib/em-nodes/commands.rb
|
89
93
|
- lib/em-nodes/server.rb
|
90
|
-
- lib/em-nodes/server/
|
94
|
+
- lib/em-nodes/server/hello.rb
|
95
|
+
- lib/em-nodes/server/task.rb
|
96
|
+
- spec/base_spec.rb
|
97
|
+
- spec/hello_spec.rb
|
91
98
|
- spec/simple2_spec.rb
|
92
99
|
- spec/simple_spec.rb
|
93
100
|
- spec/spec_helper.rb
|
101
|
+
- spec/task_spec.rb
|
94
102
|
homepage: https://github.com/kostya/em-nodes
|
95
103
|
licenses:
|
96
104
|
- MIT
|
@@ -116,6 +124,9 @@ signing_key:
|
|
116
124
|
specification_version: 4
|
117
125
|
summary: Simple EM client server, and some stuffs
|
118
126
|
test_files:
|
127
|
+
- spec/base_spec.rb
|
128
|
+
- spec/hello_spec.rb
|
119
129
|
- spec/simple2_spec.rb
|
120
130
|
- spec/simple_spec.rb
|
121
131
|
- spec/spec_helper.rb
|
132
|
+
- spec/task_spec.rb
|
@@ -1,19 +0,0 @@
|
|
1
|
-
module EM::Nodes::AbstractCommand
|
2
|
-
COMMAND_PREFIX = 'send_'
|
3
|
-
|
4
|
-
def method_missing(method, *args)
|
5
|
-
method = method.to_s
|
6
|
-
|
7
|
-
unless method.start_with?(COMMAND_PREFIX)
|
8
|
-
EM::Nodes.logger.debug "unknown send #{method} #{args.inspect}"
|
9
|
-
super
|
10
|
-
return
|
11
|
-
end
|
12
|
-
|
13
|
-
if @alive
|
14
|
-
@connection.send_command(method.sub(COMMAND_PREFIX, ''), args)
|
15
|
-
else
|
16
|
-
EM::Nodes.logger.error "failed command attempt #{method}, connection dead"
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
require 'ostruct'
|
2
|
-
|
3
|
-
class EM::Nodes::Server::Client
|
4
|
-
include EM::Nodes::AbstractCommand
|
5
|
-
|
6
|
-
attr_accessor :connection, :alive
|
7
|
-
attr_reader :data
|
8
|
-
|
9
|
-
def initialize(conn)
|
10
|
-
@connection = conn
|
11
|
-
@alive = true
|
12
|
-
@data = OpenStruct.new
|
13
|
-
end
|
14
|
-
|
15
|
-
def disconnect!
|
16
|
-
@alive = false
|
17
|
-
end
|
18
|
-
end
|