backport 1.1.2 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile +6 -6
- data/lib/backport.rb +22 -8
- data/lib/backport/adapter.rb +1 -0
- data/lib/backport/client.rb +12 -9
- data/lib/backport/machine.rb +6 -4
- data/lib/backport/server/base.rb +2 -0
- data/lib/backport/server/connectable.rb +3 -0
- data/lib/backport/server/interval.rb +1 -0
- data/lib/backport/server/stdio.rb +5 -0
- data/lib/backport/server/tcpip.rb +11 -4
- data/lib/backport/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bb7787742f0eda4aab85f3b7cd388f69ad1870a4298f4b6f4862577f198685f0
|
4
|
+
data.tar.gz: 00c7263e5c3dc511f3a13a12d2e5cf7335d9c2feab66b63430ae363975284b73
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cc68f707447563a776948af2e86cfd2d683659fe11ce81ee21d37536e7fc5818935ff9e763a4fcb4d4022460cf3088c14f830fa1faefb9577aff959a5a6e0c3a
|
7
|
+
data.tar.gz: 14be66a01490aaa143da303aac0f9f0c3061ea4dbad0e284f4e07a463fc5dd6b807b677b3f70f1ce8191312b3b12d1429bb8e87d883bf3ea61e9bc8fcd91eee9
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
source "https://rubygems.org"
|
2
|
-
|
3
|
-
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
|
4
|
-
|
5
|
-
# Specify your gem's dependencies in backport.gemspec
|
6
|
-
gemspec
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in backport.gemspec
|
6
|
+
gemspec
|
data/lib/backport.rb
CHANGED
@@ -15,7 +15,7 @@ module Backport
|
|
15
15
|
# @param adapter [Adapter]
|
16
16
|
# @return [void]
|
17
17
|
def prepare_stdio_server adapter: Adapter
|
18
|
-
|
18
|
+
machines.last.prepare Backport::Server::Stdio.new(adapter: adapter)
|
19
19
|
end
|
20
20
|
|
21
21
|
# Prepare a TCP server to run in Backport.
|
@@ -25,7 +25,7 @@ module Backport
|
|
25
25
|
# @param adapter [Adapter]
|
26
26
|
# @return [void]
|
27
27
|
def prepare_tcp_server host: 'localhost', port: 1117, adapter: Adapter
|
28
|
-
|
28
|
+
machines.last.prepare Backport::Server::Tcpip.new(host: host, port: port, adapter: adapter)
|
29
29
|
end
|
30
30
|
|
31
31
|
# Prepare an interval server to run in Backport.
|
@@ -33,7 +33,7 @@ module Backport
|
|
33
33
|
# @param period [Float] Seconds between intervals
|
34
34
|
# @return [void]
|
35
35
|
def prepare_interval period, &block
|
36
|
-
|
36
|
+
machines.last.prepare Backport::Server::Interval.new(period, &block)
|
37
37
|
end
|
38
38
|
|
39
39
|
# Run the Backport machine. The provided block will be executed before the
|
@@ -48,25 +48,39 @@ module Backport
|
|
48
48
|
#
|
49
49
|
# @return [void]
|
50
50
|
def run &block
|
51
|
+
machine = Machine.new
|
52
|
+
machines.push machine
|
51
53
|
machine.run &block
|
54
|
+
machines.delete machine
|
52
55
|
end
|
53
56
|
|
54
|
-
# Stop
|
57
|
+
# Stop all running Backport machines.
|
58
|
+
#
|
59
|
+
# For more accurate control, consider stopping the machine
|
60
|
+
# from the self reference in Machine#run, e.g.:
|
61
|
+
#
|
62
|
+
# ```
|
63
|
+
# Backport.run do |machine|
|
64
|
+
# # ...
|
65
|
+
# machine.stop
|
66
|
+
# end
|
67
|
+
# ```
|
55
68
|
#
|
56
69
|
# @return [void]
|
57
70
|
def stop
|
58
|
-
|
71
|
+
machines.last.stop unless machines.empty?
|
59
72
|
end
|
60
73
|
|
74
|
+
# @return [Logger]
|
61
75
|
def logger
|
62
76
|
@logger ||= Logger.new(STDERR, level: Logger::WARN, progname: 'Backport')
|
63
77
|
end
|
64
78
|
|
65
79
|
private
|
66
80
|
|
67
|
-
# @return [Machine]
|
68
|
-
def
|
69
|
-
@
|
81
|
+
# @return [Array<Machine>]
|
82
|
+
def machines
|
83
|
+
@machines ||= []
|
70
84
|
end
|
71
85
|
end
|
72
86
|
end
|
data/lib/backport/adapter.rb
CHANGED
data/lib/backport/client.rb
CHANGED
@@ -16,6 +16,7 @@ module Backport
|
|
16
16
|
def initialize input, output, adapter, remote = {}
|
17
17
|
@in = input
|
18
18
|
@out = output
|
19
|
+
@mutex = Mutex.new
|
19
20
|
@adapter = make_adapter(adapter, remote)
|
20
21
|
@stopped = true
|
21
22
|
@buffer = ''
|
@@ -33,6 +34,7 @@ module Backport
|
|
33
34
|
# callback. The server is responsible for implementation details like
|
34
35
|
# closing the client's socket.
|
35
36
|
#
|
37
|
+
# @return [void]
|
36
38
|
def stop
|
37
39
|
return if stopped?
|
38
40
|
@adapter.closing
|
@@ -44,6 +46,7 @@ module Backport
|
|
44
46
|
# Start running the client. This method will start the thread that reads
|
45
47
|
# client input from IO.
|
46
48
|
#
|
49
|
+
# @return [void]
|
47
50
|
def start
|
48
51
|
return unless stopped?
|
49
52
|
@stopped = false
|
@@ -77,22 +80,22 @@ module Backport
|
|
77
80
|
return tmp unless tmp.empty?
|
78
81
|
end
|
79
82
|
|
83
|
+
# @param mod_cls [Module, Class] The Adapter module or class
|
84
|
+
# @param remote [Hash] Remote client data
|
80
85
|
# @return [Adapter]
|
81
|
-
def make_adapter
|
82
|
-
if
|
83
|
-
@adapter =
|
84
|
-
elsif
|
86
|
+
def make_adapter mod_cls, remote
|
87
|
+
if mod_cls.is_a?(Class) && mod_cls <= Backport::Adapter
|
88
|
+
@adapter = mod_cls.new(@out, remote)
|
89
|
+
elsif mod_cls.class == Module
|
85
90
|
@adapter = Adapter.new(@out, remote)
|
86
|
-
@adapter.extend
|
91
|
+
@adapter.extend mod_cls
|
87
92
|
else
|
88
|
-
raise TypeError, "#{
|
93
|
+
raise TypeError, "#{mod_cls} is not a valid Backport adapter"
|
89
94
|
end
|
90
95
|
end
|
91
96
|
|
92
97
|
# @return [Mutex]
|
93
|
-
|
94
|
-
@mutex ||= Mutex.new
|
95
|
-
end
|
98
|
+
attr_reader :mutex
|
96
99
|
|
97
100
|
# Start the thread that checks the input IO for client data.
|
98
101
|
#
|
data/lib/backport/machine.rb
CHANGED
@@ -4,18 +4,20 @@ module Backport
|
|
4
4
|
class Machine
|
5
5
|
def initialize
|
6
6
|
@stopped = true
|
7
|
+
@mutex = Mutex.new
|
7
8
|
end
|
8
9
|
|
9
10
|
# Run the machine. If a block is provided, it gets executed before the
|
10
11
|
# maching starts its main loop. The main loop blocks program execution
|
11
12
|
# until the machine is stopped.
|
12
13
|
#
|
14
|
+
# @yieldparam [self]
|
13
15
|
# @return [void]
|
14
16
|
def run
|
15
17
|
return unless stopped?
|
16
18
|
servers.clear
|
17
19
|
@stopped = false
|
18
|
-
yield if block_given?
|
20
|
+
yield self if block_given?
|
19
21
|
run_server_thread
|
20
22
|
end
|
21
23
|
|
@@ -52,6 +54,7 @@ module Backport
|
|
52
54
|
end
|
53
55
|
|
54
56
|
# @param server [Server::Base]
|
57
|
+
# @return [void]
|
55
58
|
def update server
|
56
59
|
if server.stopped?
|
57
60
|
servers.delete server
|
@@ -63,9 +66,8 @@ module Backport
|
|
63
66
|
|
64
67
|
private
|
65
68
|
|
66
|
-
|
67
|
-
|
68
|
-
end
|
69
|
+
# @return [Mutex]
|
70
|
+
attr_reader :mutex
|
69
71
|
|
70
72
|
# Start the thread that updates servers via the #tick method.
|
71
73
|
#
|
data/lib/backport/server/base.rb
CHANGED
@@ -10,6 +10,7 @@ module Backport
|
|
10
10
|
|
11
11
|
# Start the server.
|
12
12
|
#
|
13
|
+
# @return [void]
|
13
14
|
def start
|
14
15
|
return if started?
|
15
16
|
starting
|
@@ -18,6 +19,7 @@ module Backport
|
|
18
19
|
|
19
20
|
# Stop the server.
|
20
21
|
#
|
22
|
+
# @return [void]
|
21
23
|
def stop
|
22
24
|
return if stopped?
|
23
25
|
stopping
|
@@ -5,10 +5,12 @@ module Backport
|
|
5
5
|
# Connectable servers check clients for incoming data on each tick.
|
6
6
|
#
|
7
7
|
module Connectable
|
8
|
+
# @return [void]
|
8
9
|
def starting
|
9
10
|
clients.map(&:run)
|
10
11
|
end
|
11
12
|
|
13
|
+
# @return [void]
|
12
14
|
def stopping
|
13
15
|
clients.map(&:stop)
|
14
16
|
end
|
@@ -20,6 +22,7 @@ module Backport
|
|
20
22
|
|
21
23
|
private
|
22
24
|
|
25
|
+
# @return [Mutex]
|
23
26
|
def mutex
|
24
27
|
@mutex ||= Mutex.new
|
25
28
|
end
|
@@ -5,6 +5,9 @@ module Backport
|
|
5
5
|
class Stdio < Base
|
6
6
|
include Connectable
|
7
7
|
|
8
|
+
# @param input [IO]
|
9
|
+
# @param output [IO]
|
10
|
+
# @param adapter [Module, Class]
|
8
11
|
def initialize input: STDIN, output: STDOUT, adapter: Adapter
|
9
12
|
@in = input
|
10
13
|
@out = output
|
@@ -14,6 +17,8 @@ module Backport
|
|
14
17
|
clients.last.add_observer self
|
15
18
|
end
|
16
19
|
|
20
|
+
# @param client [Client]
|
21
|
+
# @return [void]
|
17
22
|
def update client
|
18
23
|
client.tick
|
19
24
|
end
|
@@ -8,6 +8,10 @@ module Backport
|
|
8
8
|
class Tcpip < Base
|
9
9
|
include Connectable
|
10
10
|
|
11
|
+
# @param host [String]
|
12
|
+
# @param port [Integer]
|
13
|
+
# @param adapter [Module, Class]
|
14
|
+
# @param socket_class [Class]
|
11
15
|
def initialize host: 'localhost', port: 1117, adapter: Adapter, socket_class: TCPServer
|
12
16
|
@socket = socket_class.new(host, port)
|
13
17
|
@adapter = adapter
|
@@ -58,17 +62,19 @@ module Backport
|
|
58
62
|
result = clients.last
|
59
63
|
rescue IO::WaitReadable, Errno::EAGAIN
|
60
64
|
# ignore
|
61
|
-
rescue Errno::ENOTSOCK, IOError =>
|
62
|
-
Backport.logger.info "Server stopped with minor exception [#{
|
65
|
+
rescue Errno::ENOTSOCK, IOError => e
|
66
|
+
Backport.logger.info "Server stopped with minor exception [#{e.class}] #{e.message}"
|
63
67
|
stop
|
64
|
-
rescue
|
65
|
-
Backport.logger.warn "Server stopped with major exception [#{
|
68
|
+
rescue StandardError => e
|
69
|
+
Backport.logger.warn "Server stopped with major exception [#{e.class}] #{e.message}"
|
66
70
|
stop
|
67
71
|
end
|
68
72
|
end
|
69
73
|
result
|
70
74
|
end
|
71
75
|
|
76
|
+
# @param client [Client]
|
77
|
+
# @return [void]
|
72
78
|
def update client
|
73
79
|
if client.stopped?
|
74
80
|
clients.delete client
|
@@ -82,6 +88,7 @@ module Backport
|
|
82
88
|
# @return [TCPSocket]
|
83
89
|
attr_reader :socket
|
84
90
|
|
91
|
+
# @return [void]
|
85
92
|
def start_accept_thread
|
86
93
|
Thread.new do
|
87
94
|
until stopped?
|
data/lib/backport/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: backport
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fred Snyder
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-06-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -101,7 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
101
101
|
- !ruby/object:Gem::Version
|
102
102
|
version: '0'
|
103
103
|
requirements: []
|
104
|
-
rubygems_version: 3.
|
104
|
+
rubygems_version: 3.1.2
|
105
105
|
signing_key:
|
106
106
|
specification_version: 4
|
107
107
|
summary: A pure Ruby library for event-driven IO
|