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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 186d0b9d7728190111fcf3f7eaa2d49512b71331
4
- data.tar.gz: 64fed5d4ccb6492608c1149317802d7bb13e6ee8
3
+ metadata.gz: af90dd06e107ddfab402d4e78eb7b9aa3112bca4
4
+ data.tar.gz: 4e9081e0309625ebe177256489f22d11c7cef1b3
5
5
  SHA512:
6
- metadata.gz: 882227c9d3806937747f38de1d5a1881352d668913900291c5d9a55dac13ac6a9fae26ed3cc4d8f6116b84cbcff4326bd479c8c3ff561df094dd0d3bafecb1f8
7
- data.tar.gz: 14facf0bfadb6b9873389d916528f5976afde7f5b971b19a58daa856b988d82efe4dd48d8a8ac6f48d6cf04d5f43874c425f083776f40775fb860c3a9015517a
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
@@ -1,3 +1,3 @@
1
1
  module EmmyMachine
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -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
@@ -0,0 +1,8 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require File.expand_path("./lib/emmy_machine")
5
+
6
+ RSpec.configure do |config|
7
+ config.color = true
8
+ end
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.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