backport 0.1.0
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 +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +48 -0
- data/Rakefile +6 -0
- data/backport.gemspec +26 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/backport/adapter.rb +27 -0
- data/lib/backport/client.rb +73 -0
- data/lib/backport/machine.rb +46 -0
- data/lib/backport/server/base.rb +21 -0
- data/lib/backport/server/connectable.rb +26 -0
- data/lib/backport/server/interval.rb +21 -0
- data/lib/backport/server/stdio.rb +15 -0
- data/lib/backport/server/tcpip.rb +52 -0
- data/lib/backport/server.rb +9 -0
- data/lib/backport/version.rb +3 -0
- data/lib/backport.rb +37 -0
- metadata +107 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 631a99833ea290a7aa20e2e18f188eb19b81a141e32288feeb96ac514c39a86c
|
4
|
+
data.tar.gz: 5dcd3ea31192dbb810d18f7e5ca6c3757f3613a8e0a625f0d14e7801714a0e62
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1ab759e6cd1fb063dbc9f56648d0f98771b3b2390593937c73c6d6b8e655110644b94028f2739dab3bb2f7b5ca704d2bba0838d604866b9939f607eccd85fae5
|
7
|
+
data.tar.gz: 8e6531f1ad46e37f01086991bca2c6ac1ddd262c381fb514a3a4b0674b5d7001412c1b469ae56a03d50a2edf69040bd761940413b3b2d741d4fb7ec3d1b3bfec
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Fred Snyder for Castwide Technologies LLC
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# Backport
|
2
|
+
|
3
|
+
A pure Ruby library for event-driven IO.
|
4
|
+
|
5
|
+
This library is designed with portability as the highest priority, which is why it's written in pure Ruby. Consider [EventMachine](https://github.com/eventmachine/eventmachine) if you need a solution that's faster, more mature, and scalable.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Install the gem:
|
10
|
+
|
11
|
+
```
|
12
|
+
gem install backport
|
13
|
+
```
|
14
|
+
|
15
|
+
Or add it to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'backport'
|
19
|
+
```
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
This example demonstrates a simple echo server.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'backport'
|
27
|
+
|
28
|
+
module MyAdapter
|
29
|
+
def opening
|
30
|
+
write "Opening the connection"
|
31
|
+
end
|
32
|
+
|
33
|
+
def closing
|
34
|
+
write "Closing the connection"
|
35
|
+
end
|
36
|
+
|
37
|
+
def sending data
|
38
|
+
write "Client sent: #{data}"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
Backport.run do
|
43
|
+
Backport.start_tcp_server(adapter: MyAdapter)
|
44
|
+
Backport.start_interval 1 do
|
45
|
+
puts "tick"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
```
|
data/Rakefile
ADDED
data/backport.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "backport/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "backport"
|
8
|
+
spec.version = Backport::VERSION
|
9
|
+
spec.authors = ["Fred Snyder"]
|
10
|
+
spec.email = ["fsnyder@castwide.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A pure Ruby library for event-driven IO}
|
13
|
+
spec.homepage = "http://github.com/castwide/backport"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
26
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "backport"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
module Backport
|
2
|
+
class Adapter
|
3
|
+
def initialize output
|
4
|
+
@out = output
|
5
|
+
end
|
6
|
+
|
7
|
+
def opening
|
8
|
+
STDERR.puts "Opening"
|
9
|
+
end
|
10
|
+
|
11
|
+
def closing
|
12
|
+
STDERR.puts "Closing"
|
13
|
+
end
|
14
|
+
|
15
|
+
def sending data
|
16
|
+
STDERR.puts "Client sent #{data}"
|
17
|
+
end
|
18
|
+
|
19
|
+
def write data
|
20
|
+
@out.print data
|
21
|
+
end
|
22
|
+
|
23
|
+
def write_line data
|
24
|
+
@out.puts data
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module Backport
|
2
|
+
class Client
|
3
|
+
def initialize input, output, adapter
|
4
|
+
@in = input
|
5
|
+
@out = output
|
6
|
+
# @todo Adapter can either be an Adapter class or a module
|
7
|
+
@adapter = make_adapter(adapter)
|
8
|
+
@stopped = true
|
9
|
+
@buffer = ''
|
10
|
+
end
|
11
|
+
|
12
|
+
def stopped?
|
13
|
+
@stopped ||= false
|
14
|
+
end
|
15
|
+
|
16
|
+
def stop
|
17
|
+
return if stopped?
|
18
|
+
@adapter.closing
|
19
|
+
@stopped = true
|
20
|
+
end
|
21
|
+
|
22
|
+
def run
|
23
|
+
return unless stopped?
|
24
|
+
@stopped = false
|
25
|
+
@adapter.opening
|
26
|
+
run_input_thread
|
27
|
+
end
|
28
|
+
|
29
|
+
def sending data
|
30
|
+
@adapter.sending data
|
31
|
+
end
|
32
|
+
|
33
|
+
def read
|
34
|
+
tmp = nil
|
35
|
+
mutex.synchronize do
|
36
|
+
tmp = @buffer.dup
|
37
|
+
@buffer.clear
|
38
|
+
end
|
39
|
+
return tmp unless tmp.empty?
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def make_adapter cls_mod
|
45
|
+
if cls_mod.is_a?(Class)
|
46
|
+
@adapter = cls_mod.new(@out)
|
47
|
+
elsif cls_mod.is_a?(Module)
|
48
|
+
@adapter = Adapter.new(@out)
|
49
|
+
@adapter.extend cls_mod
|
50
|
+
else
|
51
|
+
raise TypeError, "#{cls_mod} is not a valid Backport adapter"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def mutex
|
56
|
+
@mutex ||= Mutex.new
|
57
|
+
end
|
58
|
+
|
59
|
+
def run_input_thread
|
60
|
+
Thread.new do
|
61
|
+
until stopped?
|
62
|
+
char = @in.getc
|
63
|
+
if char.nil?
|
64
|
+
STDERR.puts "Client received nil. Stopping"
|
65
|
+
stop
|
66
|
+
break
|
67
|
+
end
|
68
|
+
mutex.synchronize { @buffer.concat char }
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Backport
|
2
|
+
class Machine
|
3
|
+
def initialize
|
4
|
+
@stopped = true
|
5
|
+
end
|
6
|
+
|
7
|
+
def run
|
8
|
+
return unless stopped?
|
9
|
+
@stopped = false
|
10
|
+
yield if block_given?
|
11
|
+
run_server_thread
|
12
|
+
end
|
13
|
+
|
14
|
+
def stop
|
15
|
+
servers.map(&:stop)
|
16
|
+
servers.clear
|
17
|
+
@stopped = true
|
18
|
+
end
|
19
|
+
|
20
|
+
def stopped?
|
21
|
+
@stopped ||= false
|
22
|
+
end
|
23
|
+
|
24
|
+
def start_server server
|
25
|
+
servers.push server
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
# @return [Array<Backport::Server>]
|
31
|
+
def servers
|
32
|
+
@servers ||= []
|
33
|
+
end
|
34
|
+
|
35
|
+
def run_server_thread
|
36
|
+
servers.map(&:start)
|
37
|
+
until stopped?
|
38
|
+
servers.each do |server|
|
39
|
+
server.tick
|
40
|
+
sleep 0.001
|
41
|
+
end
|
42
|
+
sleep 0.001
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Backport
|
2
|
+
module Server
|
3
|
+
class Base
|
4
|
+
def stopped?
|
5
|
+
@stopped = false if @stopped.nil?
|
6
|
+
@stopped
|
7
|
+
end
|
8
|
+
|
9
|
+
def stop
|
10
|
+
stopping
|
11
|
+
@stopped = true
|
12
|
+
end
|
13
|
+
|
14
|
+
def stopping; end
|
15
|
+
|
16
|
+
def tick; end
|
17
|
+
|
18
|
+
def start; end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Backport
|
2
|
+
module Server
|
3
|
+
module Connectable
|
4
|
+
def tick
|
5
|
+
clients.each do |client|
|
6
|
+
input = client.read
|
7
|
+
client.sending input unless input.nil?
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def start
|
12
|
+
clients.map(&:run)
|
13
|
+
end
|
14
|
+
|
15
|
+
def stopping
|
16
|
+
clients.map(&:stop)
|
17
|
+
end
|
18
|
+
|
19
|
+
protected
|
20
|
+
|
21
|
+
def clients
|
22
|
+
@clients ||= []
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Backport
|
2
|
+
module Server
|
3
|
+
class Interval < Base
|
4
|
+
def initialize period, &block
|
5
|
+
@period = period
|
6
|
+
@block = block
|
7
|
+
@last_time = Time.now
|
8
|
+
end
|
9
|
+
|
10
|
+
def tick
|
11
|
+
return unless Time.now - @last_time > @period
|
12
|
+
@block.call
|
13
|
+
@last_time = Time.now
|
14
|
+
end
|
15
|
+
|
16
|
+
def stop
|
17
|
+
@stopped = true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Backport
|
2
|
+
module Server
|
3
|
+
class Stdio < Base
|
4
|
+
include Connectable
|
5
|
+
|
6
|
+
def initialize input: STDIN, output: STDOUT, adapter: Adapter
|
7
|
+
@in = input
|
8
|
+
@out = output
|
9
|
+
@in.binmode
|
10
|
+
@adapter = adapter
|
11
|
+
clients.push Client.new(input, output, adapter)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'socket'
|
2
|
+
|
3
|
+
module Backport
|
4
|
+
module Server
|
5
|
+
class Tcpip < Base
|
6
|
+
include Connectable
|
7
|
+
|
8
|
+
def initialize host: 'localhost', port: 1117, adapter: Adapter
|
9
|
+
@socket = TCPServer.new(host, port)
|
10
|
+
@adapter = adapter
|
11
|
+
end
|
12
|
+
|
13
|
+
def stopped?
|
14
|
+
@stopped ||= false
|
15
|
+
end
|
16
|
+
|
17
|
+
def tick
|
18
|
+
mutex.synchronize do
|
19
|
+
clients.each do |client|
|
20
|
+
input = client.read
|
21
|
+
client.sending input unless input.nil?
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def start
|
27
|
+
super
|
28
|
+
start_accept_thread
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
attr_reader :socket
|
34
|
+
|
35
|
+
def mutex
|
36
|
+
@mutex ||= Mutex.new
|
37
|
+
end
|
38
|
+
|
39
|
+
def start_accept_thread
|
40
|
+
Thread.new do
|
41
|
+
until stopped?
|
42
|
+
conn = socket.accept
|
43
|
+
mutex.synchronize do
|
44
|
+
clients.push Client.new(conn, conn, @adapter)
|
45
|
+
clients.last.run
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
module Backport
|
2
|
+
module Server
|
3
|
+
autoload :Base, 'backport/server/base'
|
4
|
+
autoload :Connectable, 'backport/server/connectable'
|
5
|
+
autoload :Stdio, 'backport/server/stdio'
|
6
|
+
autoload :Tcpip, 'backport/server/tcpip'
|
7
|
+
autoload :Interval, 'backport/server/interval'
|
8
|
+
end
|
9
|
+
end
|
data/lib/backport.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require "backport/version"
|
2
|
+
require 'socket'
|
3
|
+
|
4
|
+
module Backport
|
5
|
+
autoload :Adapter, 'backport/adapter'
|
6
|
+
autoload :Machine, 'backport/machine'
|
7
|
+
autoload :Server, 'backport/server'
|
8
|
+
autoload :Client, 'backport/client'
|
9
|
+
|
10
|
+
class << self
|
11
|
+
def start_stdio_server adapter: Adapter
|
12
|
+
machine.start_server Backport::Server::Stdio.new(adapter: adapter)
|
13
|
+
end
|
14
|
+
|
15
|
+
def start_tcp_server host: 'localhost', port: 1117, adapter: Adapter
|
16
|
+
machine.start_server Backport::Server::Tcpip.new(host: host, port: port, adapter: adapter)
|
17
|
+
end
|
18
|
+
|
19
|
+
def start_interval period, &block
|
20
|
+
machine.start_server Backport::Server::Interval.new(period, &block)
|
21
|
+
end
|
22
|
+
|
23
|
+
def run &block
|
24
|
+
machine.run &block
|
25
|
+
end
|
26
|
+
|
27
|
+
def stop
|
28
|
+
machine.stop
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def machine
|
34
|
+
@machine ||= Machine.new
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: backport
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Fred Snyder
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-12-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.17'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.17'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- fsnyder@castwide.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- backport.gemspec
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- lib/backport.rb
|
73
|
+
- lib/backport/adapter.rb
|
74
|
+
- lib/backport/client.rb
|
75
|
+
- lib/backport/machine.rb
|
76
|
+
- lib/backport/server.rb
|
77
|
+
- lib/backport/server/base.rb
|
78
|
+
- lib/backport/server/connectable.rb
|
79
|
+
- lib/backport/server/interval.rb
|
80
|
+
- lib/backport/server/stdio.rb
|
81
|
+
- lib/backport/server/tcpip.rb
|
82
|
+
- lib/backport/version.rb
|
83
|
+
homepage: http://github.com/castwide/backport
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
metadata: {}
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options: []
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 2.7.6
|
104
|
+
signing_key:
|
105
|
+
specification_version: 4
|
106
|
+
summary: A pure Ruby library for event-driven IO
|
107
|
+
test_files: []
|