emmy-machine 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/emmy_machine/class_methods.rb +104 -0
- data/lib/emmy_machine/connection.rb +18 -0
- data/lib/emmy_machine/version.rb +1 -1
- data/spec/emmy_machine_spec.rb +44 -0
- data/spec/spec_helper.rb +8 -0
- metadata +8 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af90dd06e107ddfab402d4e78eb7b9aa3112bca4
|
4
|
+
data.tar.gz: 4e9081e0309625ebe177256489f22d11c7cef1b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 56c432fce256b7ac5012f819457dd0f804a7658d53860692fcfc64205fc5043f08dc86f25f275a426aab34a566de697bb6fe4d5dce27abe6952412c11ef95256
|
7
|
+
data.tar.gz: 0a75bf638505a3a690e880c4d46e33da5c0a43d093788bd6c210a3f30ba89a2297c84170450cdcc082cb8552df3367aac03ca0237869cd65b711af42d083292b
|
@@ -0,0 +1,104 @@
|
|
1
|
+
module EmmyMachine
|
2
|
+
module ClassMethods
|
3
|
+
def run(&b)
|
4
|
+
EventMachine.reactor_running? ? b.call : EventMachine.run(&b)
|
5
|
+
end
|
6
|
+
|
7
|
+
def loop
|
8
|
+
EventMachine.run
|
9
|
+
end
|
10
|
+
|
11
|
+
def running?
|
12
|
+
EventMachine.reactor_running?
|
13
|
+
end
|
14
|
+
|
15
|
+
def stop
|
16
|
+
EventMachine.stop
|
17
|
+
end
|
18
|
+
|
19
|
+
def run_block &b
|
20
|
+
EventMachine.run_block do
|
21
|
+
fiber_block &b
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def fiber_block &b
|
26
|
+
Fibre.pool.checkout &b
|
27
|
+
end
|
28
|
+
|
29
|
+
# Periodic timer
|
30
|
+
def timer(interval=1, &b)
|
31
|
+
EventMachine::PeriodicTimer.new(interval) do
|
32
|
+
fiber_block &b
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
#
|
37
|
+
# One run timer
|
38
|
+
#
|
39
|
+
# timeout 5 do
|
40
|
+
# puts "This message shows after 5 seconds"
|
41
|
+
# end
|
42
|
+
#
|
43
|
+
def timeout(interval=1, &b)
|
44
|
+
EventMachine::Timer.new(interval) do
|
45
|
+
fiber_block &b
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
#
|
50
|
+
# Connect to server
|
51
|
+
#
|
52
|
+
# emmy.connect("tcp://localhost:5555")
|
53
|
+
# emmy.connect("ipc://mypoint")
|
54
|
+
# emmy.connect("tcp://localhost:5555", handler, *args)
|
55
|
+
# emmy.connect(*emmy.http.get(url))
|
56
|
+
def connect(url, *a, &b)
|
57
|
+
url = URI(url.to_s)
|
58
|
+
b = a.last if b.nil? && a.size > 0 && a.last.is_a?(Proc)
|
59
|
+
handler = a.empty? ? Connection : a.first
|
60
|
+
|
61
|
+
case url.scheme
|
62
|
+
when "tcp" then
|
63
|
+
EventMachine.connect(url.host, url.port, handler, *a, &b)
|
64
|
+
when "ipc" then
|
65
|
+
EventMachine.connect_unix_domain(url[6..-1], handler, *a, &b)
|
66
|
+
else
|
67
|
+
raise ArgumentError, "unsupported url scheme"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
#
|
72
|
+
# Start server
|
73
|
+
#
|
74
|
+
# emmy.bind("tcp://localhost:5555", ServerConnection)
|
75
|
+
# emmy.bind("ipc://mypoint", ServerConnection)
|
76
|
+
# emmy.bind(* emmy.server.thin("tcp://localhost:4780", app))
|
77
|
+
def bind(url, *a, &b)
|
78
|
+
url = URI(url.to_s)
|
79
|
+
b = a.last if b.nil? && a.size > 0 && a.last.is_a?(Proc)
|
80
|
+
handler = a.empty? ? Connection : a.first
|
81
|
+
|
82
|
+
case url.scheme
|
83
|
+
when "tcp" then
|
84
|
+
EventMachine.start_server(url.host, url.port, handler, *a, &b)
|
85
|
+
when "ipc" then
|
86
|
+
EventMachine.start_unix_domain_server(url[6..-1], handler, *a, &b)
|
87
|
+
else
|
88
|
+
raise ArgumentError, "unsupported url scheme"
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def next_tick
|
93
|
+
fiber = Fiber.current
|
94
|
+
EventMachine.next_tick { fiber.resume }
|
95
|
+
Fiber.yield
|
96
|
+
end
|
97
|
+
|
98
|
+
def sleep(time)
|
99
|
+
fiber = Fiber.current
|
100
|
+
timeout(time) { fiber.resume }
|
101
|
+
Fiber.yield
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module EmmyMachine
|
2
|
+
module Connection
|
3
|
+
using EventObject
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.events :connect, :data, :close, :error
|
7
|
+
base.class_eval do
|
8
|
+
alias_method :connection_completed, :connect!
|
9
|
+
alias_method :receive_data, :data!
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def unbind(reason=nil)
|
14
|
+
close!(reason)
|
15
|
+
error!(reason) if error?
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/emmy_machine/version.rb
CHANGED
@@ -0,0 +1,44 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe EmmyMachine do
|
4
|
+
using EventObject
|
5
|
+
|
6
|
+
it "test run_block" do
|
7
|
+
expect { |block|
|
8
|
+
EmmyMachine.run_block &block
|
9
|
+
}.to yield_control
|
10
|
+
end
|
11
|
+
|
12
|
+
it "test connect" do
|
13
|
+
expect { |block|
|
14
|
+
EventMachine.run do
|
15
|
+
http = EmmyMachine.connect("tcp://github.com:80")
|
16
|
+
http.on :connect do
|
17
|
+
http.send_data "GET / HTTP/1.1\r\n" \
|
18
|
+
"Host: ru.wikipedia.org\r\n" \
|
19
|
+
"User-Agent: Ruby\r\n" \
|
20
|
+
"Accept: text/html\r\n" \
|
21
|
+
"Connection: close\r\n\r\n"
|
22
|
+
end
|
23
|
+
|
24
|
+
http.on :data, &block
|
25
|
+
http.on :close do
|
26
|
+
EventMachine.stop
|
27
|
+
end
|
28
|
+
end
|
29
|
+
}.to yield_control.at_least(1)
|
30
|
+
end
|
31
|
+
|
32
|
+
context "Reactor required" do
|
33
|
+
around do |example|
|
34
|
+
EventMachine.run do
|
35
|
+
example.run
|
36
|
+
EventMachine.stop
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it "test running?" do
|
41
|
+
expect(EmmyMachine.running?).to be true
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: emmy-machine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Che
|
@@ -94,7 +94,11 @@ files:
|
|
94
94
|
- Rakefile
|
95
95
|
- emmy_machine.gemspec
|
96
96
|
- lib/emmy_machine.rb
|
97
|
+
- lib/emmy_machine/class_methods.rb
|
98
|
+
- lib/emmy_machine/connection.rb
|
97
99
|
- lib/emmy_machine/version.rb
|
100
|
+
- spec/emmy_machine_spec.rb
|
101
|
+
- spec/spec_helper.rb
|
98
102
|
homepage: ''
|
99
103
|
licenses:
|
100
104
|
- MIT
|
@@ -119,4 +123,6 @@ rubygems_version: 2.4.1
|
|
119
123
|
signing_key:
|
120
124
|
specification_version: 4
|
121
125
|
summary: Fibered methods for Emmy
|
122
|
-
test_files:
|
126
|
+
test_files:
|
127
|
+
- spec/emmy_machine_spec.rb
|
128
|
+
- spec/spec_helper.rb
|