em-nodes 0.2.4 → 0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fe922a1ce046c59c80d0e07fef0231bf5fa01e61
4
- data.tar.gz: 1779d2eae2c85475290ff25dd6c885da0bffc3df
3
+ metadata.gz: f1363157af5191aec0b3766f90bbea6f6f796774
4
+ data.tar.gz: c62062bed18a240bedb582f7181496b7801d278c
5
5
  SHA512:
6
- metadata.gz: b654c62869647ee287a5b5e15627020ef2512aafa72db19d18e9eb13e9f8260bbd1959449e5af292fb65e290835b45cab86299edbd1cd743b8e3898805b531d3
7
- data.tar.gz: 571c7a47c76818db7fabfd20c106290bdf6237937da0e5e97d6538e34aaefdcaf34313cfd2706dd75ed20fa21e02364a4985f184d5a87f43ae7314037c835fce
6
+ metadata.gz: de60f8921cce94868c95f6e9206dad695473f6d511f7e185d03ec401cb63fd39823e9edcdd332f3f77e3ae0cde15caad567ca08f02a5c6e54a7b8e274678d54f
7
+ data.tar.gz: 109ee1213f2002c4fe8fa21839386c971e7e4743c9294cd68525682329786f86b7a76370f2d450099a3c9db2898fa5591ca78a8ec17f9f8d29bbce9bf8d50c93
data/.gitignore CHANGED
@@ -16,3 +16,4 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
  *.log
19
+ .todo
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # EM::Nodes
2
2
 
3
- Simple EM client server, and some stuffs
3
+ Simple abstraction on top of EventMachine for easy create clients, servers, workers, ...
4
4
 
5
5
  ## Installation
6
6
 
@@ -8,3 +8,179 @@ Add this line to your application's Gemfile:
8
8
 
9
9
  gem 'em-nodes'
10
10
 
11
+ ### How to use
12
+
13
+ Client should inherit from EM::Nodes::Client, Server should inherit from EM::Nodes::Server.
14
+ Client and Server can define and use methods `on_#{method_name}` and `send_#{method_name}`, `on_#{method_name}` - receive value from `send_#{method_name}` call.
15
+
16
+ Example: client after connection to server, send i_connected to server, server send_client_request, client on_client_request send to server client_responce, and server output value.
17
+
18
+ simple_server.rb
19
+ ```ruby
20
+ class Server < EM::Nodes::Server
21
+ def on_i_connected(value)
22
+ puts "#{value} connected"
23
+ send_client_request(1)
24
+ end
25
+
26
+ def on_client_responce(value)
27
+ puts "client responce #{value}"
28
+ end
29
+ end
30
+
31
+ EM.run do
32
+ Server.start "/tmp/server.sock"
33
+ end
34
+ ```
35
+
36
+ simple_client.rb
37
+ ```ruby
38
+ class Client < EM::Nodes::Client
39
+ def on_client_request(value)
40
+ send_client_responce(value + 1)
41
+ end
42
+ end
43
+
44
+ EM.run do
45
+ client = Client.connect "/tmp/server.sock"
46
+ client.send_i_connected("hahaha")
47
+ end
48
+ ```
49
+
50
+ ### HelloFeature
51
+
52
+ HelloFeature used to simple greetings between client and server. Client should define method `info` which return hash. Server had added method: `ready_clients`, which contain clients which answered on `info` method.
53
+
54
+ hello_server.rb
55
+ ```ruby
56
+ class Server < EM::Nodes::Server
57
+ include HelloFeature
58
+ end
59
+
60
+ EM.run do
61
+ Server.start "/tmp/server.sock"
62
+
63
+ EM.add_periodic_timer(5) do
64
+ vasya = Server.ready_clients.detect { |client| client.data.my_name == 'Vasya' }
65
+ vasya.send_die if vasya
66
+ end
67
+ end
68
+ ```
69
+
70
+ hello_client.rb
71
+ ```ruby
72
+ class Client < EM::Nodes::Client
73
+ include HelloFeature
74
+
75
+ def initialize(name)
76
+ @name = name
77
+ super
78
+ end
79
+
80
+ def info
81
+ { :my_name => @name }
82
+ end
83
+
84
+ def on_die
85
+ puts "oops i should die"
86
+ EM.stop
87
+ end
88
+ end
89
+
90
+ EM.run do
91
+ client = Client.connect "/tmp/server.sock", nil, "Vasya"
92
+ end
93
+ ```
94
+
95
+ ### TaskFeature
96
+
97
+ TaskFeature is simple task sending, executing and getting results. Client should define method `on_task(task_id, param)`, and when execute task and got results, should call `send_task_result(task_id, result)`. Server should send task with method, `send_task(param)`
98
+
99
+ Example: Client after connection send to server `get_me_task`, server send_task to client with rand value, client execute `on_task` method and add to param 1 and call `send_task_result`, server receive `on_task_result` and output result.
100
+
101
+ task_server.rb
102
+ ```ruby
103
+ class Server < EM::Nodes::Server
104
+ include TaskFeature
105
+
106
+ def on_get_me_task
107
+ param = rand
108
+ puts "send to client #{param}"
109
+ send_task(param)
110
+ end
111
+
112
+ def on_task_result(result)
113
+ puts "client return result #{result}"
114
+ end
115
+ end
116
+
117
+ EM.run do
118
+ Server.start "/tmp/server.sock"
119
+ end
120
+ ```
121
+
122
+ task_client.rb
123
+ ```ruby
124
+ class Client < EM::Nodes::Client
125
+ include TaskFeature
126
+
127
+ def on_task(task_id, param)
128
+ EM.next_tick { send_task_result(task_id, param + 1) }
129
+ end
130
+ end
131
+
132
+ EM.run do
133
+ client = Client.connect "/tmp/server.sock"
134
+ client.send_get_me_task
135
+ end
136
+ ```
137
+
138
+ ### DefaultClient, DefaultServer
139
+
140
+ This is abstraction with HelloFeature and TaskFeature by default. Example how to create server and 10 workers. Server schedule for each client every 1 second, some value. Client just add 1 to value and return result to server, server puts value.
141
+
142
+ default_server.rb
143
+ ```ruby
144
+ class Server < EM::Nodes::DefaultServer
145
+ def on_task_result(res)
146
+ puts res
147
+ end
148
+ end
149
+
150
+ EM.run do
151
+ Server.start "127.0.0.1", 8888
152
+
153
+ # periodical schedule tasks to clients
154
+ EM.add_periodic_timer(1) do
155
+ Server.ready_clients.each do |client|
156
+ client.send_task(rand)
157
+ end
158
+ end
159
+ end
160
+ ```
161
+
162
+ default_client.rb
163
+ ```ruby
164
+ class Client < EM::Nodes::DefaultClient
165
+ def info
166
+ { :name => 'bla' }
167
+ end
168
+
169
+ def on_task(task_id, param)
170
+ res = do_something(param)
171
+ send_task_result(task_id, res)
172
+ end
173
+
174
+ def do_something(x)
175
+ x + 1
176
+ end
177
+ end
178
+
179
+ EM.run do
180
+ 10.times do
181
+ Client.connect "127.0.0.1", 8888
182
+ end
183
+ end
184
+ ```
185
+
186
+ [More examples](https://github.com/kostya/em-nodes/tree/master/examples)
data/benchmark/client.rb CHANGED
@@ -14,5 +14,7 @@ end
14
14
 
15
15
  EM.run do
16
16
  puts "client run"
17
- Client.connect '/tmp/test_em_nodes_sock'
17
+ EM.add_timer(0.1) do
18
+ Client.connect '/tmp/test_em_nodes_sock'
19
+ end
18
20
  end
data/em-nodes.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.version = EventMachine::Nodes::VERSION
9
9
  spec.authors = ["'Konstantin Makarchev'"]
10
10
  spec.email = ["'kostya27@gmail.com'"]
11
- spec.summary = %q{Simple EM client server, and some stuffs}
12
- spec.description = %q{Simple EM client server, and some stuffs}
11
+ spec.summary = %q{Simple abstraction on top of EventMachine for easy create clients, servers, workers, ...}
12
+ spec.description = %q{Simple abstraction on top of EventMachine for easy create clients, servers, workers, ...}
13
13
  spec.homepage = "https://github.com/kostya/em-nodes"
14
14
  spec.license = "MIT"
15
15
 
@@ -0,0 +1,28 @@
1
+ require 'bundler/setup'
2
+ Bundler.require
3
+ require 'logger'
4
+
5
+ l = Logger.new(STDOUT)
6
+ l.level = Logger::INFO
7
+ EM::Nodes.logger = l
8
+
9
+ class Client < EM::Nodes::DefaultClient
10
+ def info
11
+ { :name => 'bla' }
12
+ end
13
+
14
+ def on_task(task_id, param)
15
+ res = do_something(param)
16
+ send_task_result(task_id, res)
17
+ end
18
+
19
+ def do_something(x)
20
+ x + 1
21
+ end
22
+ end
23
+
24
+ EM.run do
25
+ 10.times do
26
+ Client.connect "127.0.0.1", 8888
27
+ end
28
+ end
@@ -0,0 +1,24 @@
1
+ require 'bundler/setup'
2
+ Bundler.require
3
+ require 'logger'
4
+
5
+ l = Logger.new(STDOUT)
6
+ l.level = Logger::INFO
7
+ EM::Nodes.logger = l
8
+
9
+ class Server < EM::Nodes::DefaultServer
10
+ def on_task_result(res)
11
+ puts res
12
+ end
13
+ end
14
+
15
+ EM.run do
16
+ Server.start "127.0.0.1", 8888
17
+
18
+ # periodical schedule tasks to clients
19
+ EM.add_periodic_timer(1) do
20
+ Server.ready_clients.each do |client|
21
+ client.send_task(rand)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,29 @@
1
+ require 'bundler/setup'
2
+ Bundler.require
3
+ require 'logger'
4
+
5
+ l = Logger.new(STDOUT)
6
+ l.level = Logger::INFO
7
+ EM::Nodes.logger = l
8
+
9
+ class Client < EM::Nodes::Client
10
+ include HelloFeature
11
+
12
+ def initialize(name)
13
+ @name = name
14
+ super
15
+ end
16
+
17
+ def info
18
+ { :my_name => @name }
19
+ end
20
+
21
+ def on_die
22
+ puts "oops i should die"
23
+ EM.stop
24
+ end
25
+ end
26
+
27
+ EM.run do
28
+ client = Client.connect "/tmp/server.sock", nil, "Vasya"
29
+ end
@@ -0,0 +1,20 @@
1
+ require 'bundler/setup'
2
+ Bundler.require
3
+ require 'logger'
4
+
5
+ l = Logger.new(STDOUT)
6
+ l.level = Logger::INFO
7
+ EM::Nodes.logger = l
8
+
9
+ class Server < EM::Nodes::Server
10
+ include HelloFeature
11
+ end
12
+
13
+ EM.run do
14
+ Server.start "/tmp/server.sock"
15
+
16
+ EM.add_periodic_timer(5) do
17
+ vasya = Server.ready_clients.detect { |client| client.data.my_name == 'Vasya' }
18
+ vasya.send_die if vasya
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ require 'bundler/setup'
2
+ Bundler.require
3
+ require 'logger'
4
+
5
+ l = Logger.new(STDOUT)
6
+ l.level = Logger::INFO
7
+ EM::Nodes.logger = l
8
+
9
+ class Client < EM::Nodes::Client
10
+ def on_server_request(value)
11
+ puts "server send #{value}"
12
+ send_client_responce(value + 1)
13
+ end
14
+ end
15
+
16
+ EM.run do
17
+ client = Client.connect "/tmp/server.sock"
18
+ client.send_i_connected("hahaha")
19
+ end
@@ -0,0 +1,22 @@
1
+ require 'bundler/setup'
2
+ Bundler.require
3
+ require 'logger'
4
+
5
+ l = Logger.new(STDOUT)
6
+ l.level = Logger::INFO
7
+ EM::Nodes.logger = l
8
+
9
+ class Server < EM::Nodes::Server
10
+ def on_i_connected(value)
11
+ puts "#{value} connected"
12
+ send_server_request(1)
13
+ end
14
+
15
+ def on_client_responce(value)
16
+ puts "client responce #{value}"
17
+ end
18
+ end
19
+
20
+ EM.run do
21
+ Server.start "/tmp/server.sock"
22
+ end
@@ -0,0 +1,20 @@
1
+ require 'bundler/setup'
2
+ Bundler.require
3
+ require 'logger'
4
+
5
+ l = Logger.new(STDOUT)
6
+ l.level = Logger::INFO
7
+ EM::Nodes.logger = l
8
+
9
+ class Client < EM::Nodes::Client
10
+ include TaskFeature
11
+
12
+ def on_task(task_id, param)
13
+ EM.next_tick { send_task_result(task_id, param + 1) }
14
+ end
15
+ end
16
+
17
+ EM.run do
18
+ client = Client.connect "/tmp/server.sock"
19
+ client.send_get_me_task
20
+ end
@@ -0,0 +1,25 @@
1
+ require 'bundler/setup'
2
+ Bundler.require
3
+ require 'logger'
4
+
5
+ l = Logger.new(STDOUT)
6
+ l.level = Logger::INFO
7
+ EM::Nodes.logger = l
8
+
9
+ class Server < EM::Nodes::Server
10
+ include TaskFeature
11
+
12
+ def on_get_me_task
13
+ param = rand
14
+ puts "send to client #{param}"
15
+ send_task(param)
16
+ end
17
+
18
+ def on_task_result(result)
19
+ puts "client return result #{result}"
20
+ end
21
+ end
22
+
23
+ EM.run do
24
+ Server.start "/tmp/server.sock"
25
+ end
@@ -17,7 +17,7 @@ class EM::Nodes::Client < EM::Connection
17
17
  end
18
18
 
19
19
  def self.connect(host, port = nil, *args, &block)
20
- EM::Nodes.logger.info { "Connecting to server #{host}:#{port}" }
20
+ EM::Nodes.logger.info { "Connecting to server #{host}#{port ? ':' + port.to_s : nil}" }
21
21
  EM.connect(host, port, self, *args)
22
22
  end
23
23
  end
@@ -37,6 +37,7 @@ class EM::Nodes::Server < EM::Connection
37
37
  self.comm_inactivity_timeout = inactivity_timeout if EM.reactor_running?
38
38
 
39
39
  port, host = Socket.unpack_sockaddr_in(get_peername) rescue []
40
+ host = Socket.unpack_sockaddr_un(get_sockname) unless host
40
41
  unless accept?(host, port)
41
42
  unbind
42
43
  return
@@ -57,7 +58,7 @@ class EM::Nodes::Server < EM::Connection
57
58
  end
58
59
 
59
60
  def self.start(host, port = nil, *args)
60
- EM::Nodes.logger.info { "Start server #{host}:#{port}" }
61
+ EM::Nodes.logger.info { "Start server #{host}#{port ? ':' + port.to_s : nil}" }
61
62
  EM.start_server host, port, self, *args
62
63
  end
63
64
  end
@@ -2,5 +2,5 @@ module EventMachine
2
2
  end
3
3
 
4
4
  module EventMachine::Nodes
5
- VERSION = "0.2.4"
5
+ VERSION = "0.5"
6
6
  end
data/lib/em-nodes.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'eventmachine'
2
2
  require 'logger'
3
3
  require 'em-nodes/em_hacks'
4
+ require 'em-nodes/version'
4
5
 
5
6
  module EM::Nodes
6
7
  class << self
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.2.4
4
+ version: '0.5'
5
5
  platform: ruby
6
6
  authors:
7
7
  - "'Konstantin Makarchev'"
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-05 00:00:00.000000000 Z
11
+ date: 2015-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: eventmachine
@@ -66,7 +66,8 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
- description: Simple EM client server, and some stuffs
69
+ description: Simple abstraction on top of EventMachine for easy create clients, servers,
70
+ workers, ...
70
71
  email:
71
72
  - "'kostya27@gmail.com'"
72
73
  executables: []
@@ -88,6 +89,14 @@ files:
88
89
  - em-nodes.gemspec
89
90
  - examples/chat_client.rb
90
91
  - examples/chat_server.rb
92
+ - examples/default_client.rb
93
+ - examples/default_server.rb
94
+ - examples/hello_client.rb
95
+ - examples/hello_server.rb
96
+ - examples/simple_client.rb
97
+ - examples/simple_server.rb
98
+ - examples/task_client.rb
99
+ - examples/task_server.rb
91
100
  - examples/work_client.rb
92
101
  - examples/work_server.rb
93
102
  - lib/em-nodes.rb
@@ -133,7 +142,8 @@ rubyforge_project:
133
142
  rubygems_version: 2.2.2
134
143
  signing_key:
135
144
  specification_version: 4
136
- summary: Simple EM client server, and some stuffs
145
+ summary: Simple abstraction on top of EventMachine for easy create clients, servers,
146
+ workers, ...
137
147
  test_files:
138
148
  - spec/base_spec.rb
139
149
  - spec/hello_spec.rb