zmachine 0.3.0 → 0.3.2

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
  SHA1:
3
- metadata.gz: ae108dc1b3349d10953fdbf480eb04399e955210
4
- data.tar.gz: 0c21b76fea1dac030296db4af61132a4378f5c6c
3
+ metadata.gz: 8bfd7346cba3e70c873af45a7db7690424396720
4
+ data.tar.gz: ee92ca58e8ec0996531625d17bdb340c26b754e3
5
5
  SHA512:
6
- metadata.gz: cf74518f087c16dbdc05982c3aae1beeea4e948995b83cdad5f77223cdcb0c6c90c1461df3c616ce5fa4713b337ed6f65736ece7c10874f3677239b38a6ee9b7
7
- data.tar.gz: 1756c03d9be7a36ed549f8e954c74d8bf2614d7e9b1638b2bb9d343b4c4c566366aee682a05382a365754c72d630877db3242fa56ba5a9b76a2a9c85c8ffadc7
6
+ metadata.gz: d1031edf74c86b34f00d0c22ea4fc04fa36c4a54651ce7c5570f23b91713ad9f51bce3aec9a6c61e38d5cbbaa48588268497351c73f8cc1e5de5a86d21ff2072
7
+ data.tar.gz: 72171c7f7958bdfd3bf0e5f5bc0d419be6d9df3ec5d6b94d799f1db5cdaad70cf06477409d0741fbf522e512d4c6c3026d648df5595eb4d9c1294f345dd86604
@@ -1,4 +1,5 @@
1
- java_import java.nio.ByteBuffer
1
+ java_import 'java.nio.ByteBuffer'
2
+ java_import 'java.util.concurrent.ConcurrentLinkedQueue'
2
3
 
3
4
  module ZMachine
4
5
  class Channel
@@ -8,7 +9,7 @@ module ZMachine
8
9
 
9
10
  def initialize
10
11
  @inbound_buffer = ByteBuffer.allocate(1024 * 1024)
11
- @outbound_queue = []
12
+ @outbound_queue = ConcurrentLinkedQueue.new
12
13
  @raw = false
13
14
  end
14
15
 
@@ -38,20 +39,21 @@ module ZMachine
38
39
  return unless data
39
40
  buffer = ByteBuffer.wrap(data)
40
41
  if buffer.has_remaining
41
- @outbound_queue << buffer
42
+ @outbound_queue.add(buffer)
42
43
  end
43
44
  end
44
45
 
45
46
  def write_outbound_data
46
47
  ZMachine.logger.debug("zmachine:channel:#{__method__}", channel: self, can_send: can_send?) if ZMachine.debug
47
48
  while can_send?
48
- buffer = @outbound_queue.first
49
+ buffer = @outbound_queue.peek
50
+ break unless buffer
49
51
  @socket.write(buffer) if buffer.has_remaining
50
52
  # Did we consume the whole outbound buffer? If yes,
51
53
  # pop it off and keep looping. If no, the outbound network
52
54
  # buffers are full, so break out of here.
53
55
  break if buffer.has_remaining
54
- @outbound_queue.shift
56
+ @outbound_queue.poll
55
57
  end
56
58
  maybe_close_with_callback
57
59
  end
@@ -28,6 +28,7 @@ module ZMachine
28
28
  @channel = klass.new
29
29
  @channel.bind(address, port_or_type)
30
30
  @block = block
31
+ @block.call(self) if @block && @channel.is_a?(ZMQChannel)
31
32
  self
32
33
  end
33
34
 
@@ -165,7 +166,7 @@ module ZMachine
165
166
 
166
167
  def register(selector)
167
168
  ZMachine.logger.debug("zmachine:connection:#{__method__}", connection: self, fd: @channel.selectable_fd) if ZMachine.debug
168
- @channel_key ||= @channel.selectable_fd.register(selector, current_events, self)
169
+ @channel_key = @channel.selectable_fd.register(selector, current_events, self)
169
170
  end
170
171
 
171
172
  def valid?
@@ -2,6 +2,7 @@ java_import java.nio.channels.ClosedChannelException
2
2
 
3
3
  require 'zmachine/tcp_channel'
4
4
  require 'zmachine/zmq_channel'
5
+ require 'set'
5
6
 
6
7
  module ZMachine
7
8
  class ConnectionManager
@@ -11,10 +12,10 @@ module ZMachine
11
12
  def initialize(selector)
12
13
  ZMachine.logger.debug("zmachine:connection_manager:#{__method__}") if ZMachine.debug
13
14
  @selector = selector
14
- @connections = []
15
- @zmq_connections = []
16
- @new_connections = []
17
- @unbound_connections = []
15
+ @connections = Set.new
16
+ @zmq_connections = Set.new
17
+ @new_connections = Set.new
18
+ @unbound_connections = Set.new
18
19
  end
19
20
 
20
21
  def idle?
@@ -81,7 +82,7 @@ module ZMachine
81
82
  end
82
83
 
83
84
  def add_new_connections
84
- @new_connections.compact.each do |connection|
85
+ @new_connections.each do |connection|
85
86
  ZMachine.logger.debug("zmachine:connection_manager:#{__method__}", connection: connection) if ZMachine.debug
86
87
  begin
87
88
  connection.register(@selector)
@@ -97,6 +98,10 @@ module ZMachine
97
98
  @new_connections.clear
98
99
  end
99
100
 
101
+ def is_connected?(connection)
102
+ @connections.include?(connection)
103
+ end
104
+
100
105
  def cleanup
101
106
  return if @unbound_connections.empty?
102
107
  ZMachine.logger.debug("zmachine:connection_manager:#{__method__}") if ZMachine.debug
@@ -111,7 +116,7 @@ module ZMachine
111
116
  else
112
117
  connection.unbind
113
118
  end
114
- connection.close
119
+ connection.channel.close!
115
120
  rescue Exception => e
116
121
  ZMachine.logger.exception(e, "failed to unbind connection") if ZMachine.debug
117
122
  end
@@ -35,10 +35,10 @@ module ZMachine
35
35
  def add(timeout, &block)
36
36
  timeout *= 1_000_000_000 # s to ns
37
37
  ticks = timeout / @tick_length
38
- slot = (@current_tick + ticks) % @slots.length
39
- HashedWheelTimeout.new(System.nano_time + timeout, &block).tap do |hwt|
40
- @slots[slot] << hwt
41
- end
38
+ slot = (@current_tick + ticks) % @slots.length
39
+ deadline = System.nano_time + timeout
40
+ @slots[slot] << hwt = HashedWheelTimeout.new(deadline, &block)
41
+ hwt
42
42
  end
43
43
 
44
44
  def reset(time = nil)
@@ -92,9 +92,10 @@ module ZMachine
92
92
  end
93
93
 
94
94
  def reconnect(server, port_or_type, handler)
95
- return if handler && handler.channel.is_a?(ZMQChannel)
95
+ return handler if handler && handler.channel.is_a?(ZMQChannel)
96
96
  ZMachine.logger.debug("zmachine:reactor:#{__method__}", server: server, port_or_type: port_or_type) if ZMachine.debug
97
- @connection_manager.connect(server, port_or_type, handler)
97
+ return handler if handler.connected?
98
+ connect(server, port_or_type, handler)
98
99
  end
99
100
 
100
101
  def run(callback=nil, shutdown_hook=nil, &block)
@@ -21,7 +21,7 @@ module ZMachine
21
21
  end
22
22
 
23
23
  def bound?
24
- @socket.is_a?(ServerSocketChannel) && @socket.bound?
24
+ @socket.is_a?(ServerSocketChannel) && @socket.socket.bound?
25
25
  end
26
26
 
27
27
  def accept
@@ -29,9 +29,9 @@ module ZMachine
29
29
  client_socket = @socket.accept
30
30
  return unless client_socket
31
31
  client_socket.configure_blocking(false)
32
- TCPChannel.new.tap do |channel|
33
- channel.socket = client_socket
34
- end
32
+ channel = TCPChannel.new
33
+ channel.socket = client_socket
34
+ channel
35
35
  end
36
36
 
37
37
  def connect(address, port)
@@ -78,6 +78,7 @@ module ZMachine
78
78
 
79
79
  def read_inbound_data
80
80
  ZMachine.logger.debug("zmachine:zmq_channel:#{__method__}", channel: self) if ZMachine.debug
81
+ return nil unless can_recv?
81
82
  data = ZMsg.recv_msg(@socket)
82
83
  data = String.from_java_bytes(data.first.data) unless @raw
83
84
  data
data/spec/channel_spec.rb CHANGED
@@ -38,6 +38,7 @@ shared_examples_for "a Channel" do
38
38
  before(:each) do
39
39
  @channel = @server.accept
40
40
  @channel.raw = false
41
+ ZMQChannel.any_instance.stub(:can_recv?) { true }
41
42
  @client.finish_connecting
42
43
  end
43
44
 
@@ -84,7 +85,7 @@ shared_examples_for "a Channel" do
84
85
  end
85
86
 
86
87
  it 'closes the connection after writing' do
87
- channel = @server.accept
88
+ @server.accept
88
89
  @client.finish_connecting
89
90
  @client.send_data(data)
90
91
  @client.close(true)
data/zmachine.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "zmachine"
5
- spec.version = "0.3.0"
5
+ spec.version = "0.3.2"
6
6
  spec.authors = ["LiquidM, Inc."]
7
7
  spec.email = ["opensource@liquidm.com"]
8
8
  spec.description = %q{pure JRuby multi-threaded mostly EventMachine compatible event loop}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zmachine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - LiquidM, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-09 00:00:00.000000000 Z
11
+ date: 2014-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: liquid-ext
@@ -45,7 +45,6 @@ files:
45
45
  - examples/echo_client.rb
46
46
  - examples/echo_server.rb
47
47
  - lib/zmachine.rb
48
- - lib/zmachine/acceptor.rb
49
48
  - lib/zmachine/channel.rb
50
49
  - lib/zmachine/connection.rb
51
50
  - lib/zmachine/connection_manager.rb
@@ -83,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
82
  version: '0'
84
83
  requirements: []
85
84
  rubyforge_project:
86
- rubygems_version: 2.1.9
85
+ rubygems_version: 2.2.1
87
86
  signing_key:
88
87
  specification_version: 4
89
88
  summary: pure JRuby multi-threaded mostly EventMachine compatible event loop
@@ -1,20 +0,0 @@
1
- module ZMachine
2
- class Acceptor
3
- attr_reader :klass
4
- attr_reader :args
5
- attr_reader :callback
6
-
7
- def initialize(channel, klass, *args)
8
- @klass, @args, @callback = klass, args[0...-1], args.last
9
- @channel = channel
10
- end
11
-
12
- def unbind
13
- end
14
-
15
- def close
16
- @channel.close
17
- end
18
-
19
- end
20
- end