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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cda5ef5baf957e3f8c8f22c3e52ad07cbc921de4750a2d5c5bab9dd97898b87a
4
- data.tar.gz: 4f981e41226043f45cae6cb20d5483fc013c6d39b35ac31bedd01181ec750c41
3
+ metadata.gz: bb7787742f0eda4aab85f3b7cd388f69ad1870a4298f4b6f4862577f198685f0
4
+ data.tar.gz: 00c7263e5c3dc511f3a13a12d2e5cf7335d9c2feab66b63430ae363975284b73
5
5
  SHA512:
6
- metadata.gz: 457b297349e1d47ab14fb3f9e047a95ce807ff1371789307a53b067cd606ef96f1ceb24dd5f4fbfb8c26d21ddda437b5fd38e367340b3c3af48cdb21009311aa
7
- data.tar.gz: 12f234691c663322e8e6385245faa13730be2eeccdf4a664a2c6f4d020b8b406ba9cf11bf2c97ea59c2cfe890a41da862c58ff13408a578b4dc41ccf5349b774
6
+ metadata.gz: cc68f707447563a776948af2e86cfd2d683659fe11ce81ee21d37536e7fc5818935ff9e763a4fcb4d4022460cf3088c14f830fa1faefb9577aff959a5a6e0c3a
7
+ data.tar.gz: 14be66a01490aaa143da303aac0f9f0c3061ea4dbad0e284f4e07a463fc5dd6b807b677b3f70f1ce8191312b3b12d1429bb8e87d883bf3ea61e9bc8fcd91eee9
data/.travis.yml CHANGED
@@ -8,6 +8,7 @@ rvm:
8
8
  - 2.4
9
9
  - 2.5
10
10
  - 2.6
11
+ - 2.7
11
12
  - jruby-head
12
13
  matrix:
13
14
  include:
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 1.2.0 - June 13, 2021
2
+ - Improved handling of multiple machines
3
+ - Rescue StandardError instead of Exception
4
+
1
5
  ## 1.1.2 - August 4, 2019
2
6
  - Rescue Errno::ENOTSOCK
3
7
 
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
- machine.prepare Backport::Server::Stdio.new(adapter: adapter)
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
- machine.prepare Backport::Server::Tcpip.new(host: host, port: port, adapter: adapter)
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
- machine.prepare Backport::Server::Interval.new(period, &block)
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 the Backport machine.
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
- machine.stop
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 machine
69
- @machine ||= Machine.new
81
+ # @return [Array<Machine>]
82
+ def machines
83
+ @machines ||= []
70
84
  end
71
85
  end
72
86
  end
@@ -76,6 +76,7 @@ module Backport
76
76
  # The server is responsible for implementation details like closing the
77
77
  # client's socket.
78
78
  #
79
+ # @return [void]
79
80
  def close
80
81
  return if closed?
81
82
  _data[:closed] = true
@@ -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 cls_mod, remote
82
- if cls_mod.is_a?(Class) && cls_mod <= Backport::Adapter
83
- @adapter = cls_mod.new(@out, remote)
84
- elsif cls_mod.class == Module
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 cls_mod
91
+ @adapter.extend mod_cls
87
92
  else
88
- raise TypeError, "#{cls_mod} is not a valid Backport adapter"
93
+ raise TypeError, "#{mod_cls} is not a valid Backport adapter"
89
94
  end
90
95
  end
91
96
 
92
97
  # @return [Mutex]
93
- def mutex
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
  #
@@ -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
- def mutex
67
- @mutex ||= Mutex.new
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
  #
@@ -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
@@ -28,6 +28,7 @@ module Backport
28
28
 
29
29
  private
30
30
 
31
+ # @return [void]
31
32
  def run_ready_thread
32
33
  Thread.new do
33
34
  until stopped?
@@ -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 => err
62
- Backport.logger.info "Server stopped with minor exception [#{err.class}] #{err.message}"
65
+ rescue Errno::ENOTSOCK, IOError => e
66
+ Backport.logger.info "Server stopped with minor exception [#{e.class}] #{e.message}"
63
67
  stop
64
- rescue Exception => err
65
- Backport.logger.warn "Server stopped with major exception [#{err.class}] #{err.message}"
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?
@@ -1,3 +1,3 @@
1
1
  module Backport
2
- VERSION = '1.1.2'.freeze
2
+ VERSION = '1.2.0'.freeze
3
3
  end
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.1.2
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: 2019-08-05 00:00:00.000000000 Z
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.0.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