zmachine 0.4.0 → 0.4.1

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: 219aed15ccbcbb2ea4f9e83d0d686107cf13c5fb
4
- data.tar.gz: 3bea68dd63ef8b91582f6d62ee69114bf4888d1f
3
+ metadata.gz: b5e36adcf8fc731b92bdc5d54272901f14caaf5d
4
+ data.tar.gz: f51726dae520643f03491643bd2ed65c11d9cad2
5
5
  SHA512:
6
- metadata.gz: ac07ecb821f35b73fe173f83ef9220323d42a9af6af09012e25cac8a75042e68ee59d71894bfb2496e09cf90857053ba09de359964acfa94bf5c383208f2d9ef
7
- data.tar.gz: 2f20659edb1c266d058e625da024e9eaf5f1f9c536cbc48430a18d6375cfd914bd457aac3da0c4bf98f04f04c85da587c4af5215874f2f449cd649382301b850
6
+ metadata.gz: 10c3cd9dfab40a3bdbaae428bd168aa99f5ce9536778f31d627d2ce6bb1e6986f83316d55d01eefe70235573264d47852cdd8bf3bd104fc3edc2ea16dd209205
7
+ data.tar.gz: c5d87d4b03ac6fbfa883abe2e9850bd2cedaa2bcacfdac2a0a68105afa260a6da5bdd104844140a22ddb1fe833de3352ccee1fe46565a9cb17a6b151ec1dcf5c
data/Gemfile CHANGED
@@ -3,6 +3,6 @@ source 'https://rubygems.org'
3
3
  gemspec
4
4
 
5
5
  gem 'liquid-development'
6
- gem 'liquid-ext'
6
+ gem 'liquid-ext', github: 'liquidm/ext'
7
7
  gem 'ruby-debug'
8
- gem 'z-http-request', path: '../z-http-request'
8
+ gem 'z-http-request', github: 'liquidm/z-http-request'
@@ -3,30 +3,51 @@ package com.liquidm.zmachine;
3
3
  import java.lang.System;
4
4
  import java.util.ArrayList;
5
5
  import java.util.Iterator;
6
+ import java.util.SortedSet;
7
+ import java.util.TreeSet;
6
8
  import java.util.concurrent.Callable;
7
9
 
8
10
  public class HashedWheel
9
11
  {
10
- private class Timeout
12
+ private class Timeout implements Comparable<Timeout>
11
13
  {
12
- long deadline;
13
- Object callback;
14
- boolean canceled;
14
+ private long deadline;
15
+ private Callable<Object> callback;
16
+ private boolean canceled;
15
17
 
16
- public Timeout(long deadline, Object callback)
18
+ public Timeout(long deadline, Callable<Object> callback)
17
19
  {
18
20
  this.deadline = deadline;
19
21
  this.callback = callback;
20
22
  this.canceled = false;
21
23
  }
22
24
 
25
+ public int compareTo(Timeout other)
26
+ {
27
+ return (int)(this.deadline - other.getDeadline());
28
+ }
29
+
30
+ public long getDeadline()
31
+ {
32
+ return this.deadline;
33
+ }
34
+
23
35
  public Object getCallback()
24
36
  {
25
37
  return this.callback;
26
38
  }
27
39
 
40
+ public void call() throws Exception
41
+ {
42
+ if (this.callback != null) {
43
+ this.callback.call();
44
+ this.cancel();
45
+ }
46
+ }
47
+
28
48
  public void cancel()
29
49
  {
50
+ this.callback = null;
30
51
  this.canceled = true;
31
52
  }
32
53
 
@@ -37,9 +58,11 @@ public class HashedWheel
37
58
  }
38
59
 
39
60
  int number_of_slots;
61
+ ArrayList<SortedSet<Timeout>> slots;
62
+
40
63
  long tick_length;
41
- Object[] slots;
42
64
  int current_tick;
65
+
43
66
  long last;
44
67
 
45
68
  public HashedWheel(int number_of_slots, long tick_length)
@@ -49,8 +72,7 @@ public class HashedWheel
49
72
  reset();
50
73
  }
51
74
 
52
- @SuppressWarnings("unchecked")
53
- public Object[] getSlots()
75
+ public ArrayList<SortedSet<Timeout>> getSlots()
54
76
  {
55
77
  return this.slots;
56
78
  }
@@ -61,15 +83,15 @@ public class HashedWheel
61
83
  }
62
84
 
63
85
  @SuppressWarnings("unchecked")
64
- public Timeout add(long timeout, Object callback)
86
+ public Timeout add(long timeout, Callable<Object> callback)
65
87
  {
66
88
  timeout = timeout * 1000000; // ms to ns
89
+ long deadline = System.nanoTime() + timeout;
67
90
  long ticks = timeout / this.tick_length;
68
91
  int slot = (int)((this.current_tick + ticks) % this.number_of_slots);
69
- long deadline = System.nanoTime() + timeout;
70
92
  Timeout hwt = new Timeout(deadline, callback);
71
- ArrayList<Timeout> list = (ArrayList<Timeout>) this.slots[slot];
72
- list.add(hwt);
93
+ SortedSet<Timeout> timeouts = this.slots.get(slot);
94
+ timeouts.add(hwt);
73
95
  return hwt;
74
96
  }
75
97
 
@@ -80,41 +102,42 @@ public class HashedWheel
80
102
 
81
103
  public long reset(long last)
82
104
  {
83
- this.slots = new Object[this.number_of_slots];
105
+ this.slots = new ArrayList<SortedSet<Timeout>>(this.number_of_slots);
84
106
  for (int i = 0; i < this.number_of_slots; i++) {
85
- this.slots[i] = new ArrayList<Timeout>();
107
+ this.slots.add(i, new TreeSet<Timeout>());
86
108
  }
87
109
  this.current_tick = 0;
88
110
  this.last = last;
89
111
  return last;
90
112
  }
91
113
 
92
- public ArrayList<Timeout> advance()
114
+ public int advance() throws Exception
93
115
  {
94
116
  return advance(System.nanoTime());
95
117
  }
96
118
 
97
- @SuppressWarnings("unchecked")
98
- public ArrayList<Timeout> advance(long now)
119
+ public int advance(long now) throws Exception
99
120
  {
121
+ int timedout = 0;
100
122
  long passed_ticks = (now - this.last) / this.tick_length;
101
- ArrayList<Timeout> result = new ArrayList<Timeout>();
102
123
  do {
103
124
  this.current_tick = this.current_tick % this.number_of_slots;
104
- Iterator<Timeout> it = ((ArrayList<Timeout>)this.slots[this.current_tick]).iterator();
125
+ SortedSet<Timeout> timeouts = this.slots.get(this.current_tick);
126
+ Iterator<Timeout> it = timeouts.iterator();
105
127
  while (it.hasNext()) {
106
128
  Timeout timeout = it.next();
107
- if (timeout.deadline < now) {
108
- if (!timeout.isCanceled()) {
109
- result.add(timeout);
110
- }
111
- it.remove();
129
+ long deadline = timeout.getDeadline();
130
+ if (deadline > now) {
131
+ break;
112
132
  }
133
+ timedout++;
134
+ timeout.call();
135
+ it.remove();
113
136
  }
114
137
  this.current_tick += 1;
115
138
  passed_ticks -= 1;
116
139
  } while (passed_ticks > 0);
117
140
  this.last = now;
118
- return result;
141
+ return timedout;
119
142
  }
120
143
  }
data/lib/zmachine.rb CHANGED
@@ -1,6 +1,3 @@
1
- require 'zmachine/jeromq-0.3.2-SNAPSHOT.jar'
2
- java_import org.zeromq.ZContext
3
-
4
1
  require 'liquid/boot'
5
2
 
6
3
  require 'zmachine/connection'
@@ -24,8 +21,9 @@ module ZMachine
24
21
  Thread.current[:reactor] ||= Reactor.new
25
22
  end
26
23
 
24
+ # for backwards compat. please use ZContext.instance directly.
27
25
  def self.context
28
- Thread.current[:context] ||= ZContext.new
26
+ ZContext.instance
29
27
  end
30
28
 
31
29
  def self.add_periodic_timer(*args, &block)
@@ -122,7 +122,7 @@ module ZMachine
122
122
  @running = false
123
123
  Reactor.unregister_reactor(self)
124
124
  ZMachine.logger.debug("zmachine:reactor:#{__method__}", stop: :zcontext) if ZMachine.debug
125
- ZMachine.context.destroy
125
+ ZContext.destroy
126
126
  ZMachine.clear_current_reactor
127
127
  end
128
128
  end
@@ -131,7 +131,7 @@ module ZMachine
131
131
  ZMachine.logger.debug("zmachine:reactor:#{__method__}") if ZMachine.debug
132
132
  run_deferred_callbacks
133
133
  return unless @run_reactor
134
- run_timers
134
+ @wheel.advance
135
135
  return unless @run_reactor
136
136
  @connection_manager.cleanup
137
137
  if @connection_manager.idle?
@@ -172,14 +172,6 @@ module ZMachine
172
172
  end
173
173
  end
174
174
 
175
- def run_timers
176
- ZMachine.logger.debug("zmachine:reactor:#{__method__}") if ZMachine.debug
177
- @wheel.advance.each do |timeout|
178
- ZMachine.logger.info("zmachine:reactor:#{__method__}", callback: timeout.callback) if ZMachine.debug
179
- timeout.callback.call
180
- end
181
- end
182
-
183
175
  def wakeup
184
176
  ZMachine.logger.debug("zmachine:reactor:#{__method__}") if ZMachine.debug
185
177
  @selector.wakeup if @selector
@@ -1,8 +1,3 @@
1
- require 'zmachine/jeromq-0.3.2-SNAPSHOT.jar'
2
- java_import org.zeromq.ZMsg
3
- java_import org.zeromq.ZMQ
4
- java_import org.zeromq.ZMQException
5
-
6
1
  require 'zmachine'
7
2
  require 'zmachine/channel'
8
3
 
@@ -42,7 +37,7 @@ module ZMachine
42
37
  ZMachine.logger.debug("zmachine:zmq_channel:#{__method__}", channel: self) if ZMachine.debug
43
38
  @bound = true
44
39
  @connected = true
45
- @socket = ZMachine.context.create_socket(type)
40
+ @socket = ZContext.create_socket(type)
46
41
  @socket.bind(address)
47
42
  end
48
43
 
@@ -58,7 +53,7 @@ module ZMachine
58
53
  def connect(address, type)
59
54
  ZMachine.logger.debug("zmachine:zmq_channel:#{__method__}", channel: self) if ZMachine.debug
60
55
  @connection_pending = true
61
- @socket = ZMachine.context.create_socket(type)
56
+ @socket = ZContext.create_socket(type)
62
57
  @socket.connect(address)
63
58
  end
64
59
 
@@ -84,34 +79,12 @@ module ZMachine
84
79
  data
85
80
  end
86
81
 
87
- def send1(a)
88
- @socket.send_byte_array(a, ZMQ::DONTWAIT)
89
- end
90
-
91
- def send2(a, b)
92
- @socket.send_byte_array(a, ZMQ::SNDMORE | ZMQ::DONTWAIT) and
93
- @socket.send_byte_array(b, ZMQ::DONTWAIT)
94
- end
95
-
96
- def send3(a, b, c)
97
- @socket.send_byte_array(a, ZMQ::SNDMORE | ZMQ::DONTWAIT) and
98
- @socket.send_byte_array(b, ZMQ::SNDMORE | ZMQ::DONTWAIT) and
99
- @socket.send_byte_array(c, ZMQ::DONTWAIT)
100
- end
101
-
102
- def send4(a, b, c, d)
103
- @socket.send_byte_array(a, ZMQ::SNDMORE | ZMQ::DONTWAIT) and
104
- @socket.send_byte_array(b, ZMQ::SNDMORE | ZMQ::DONTWAIT) and
105
- @socket.send_byte_array(c, ZMQ::SNDMORE | ZMQ::DONTWAIT) and
106
- @socket.send_byte_array(d, ZMQ::DONTWAIT)
107
- end
108
-
109
82
  def close!
110
83
  ZMachine.logger.debug("zmachine:zmq_channel:#{__method__}", channel: self) if ZMachine.debug
111
84
  @closed = true
112
85
  @connected = false
113
86
  @bound = false
114
- ZMachine.context.destroySocket(@socket)
87
+ ZContext.destroy_socket(@socket)
115
88
  end
116
89
 
117
90
  def closed?
data/spec/channel_spec.rb CHANGED
@@ -106,7 +106,7 @@ describe TCPChannel do
106
106
  after(:each) do
107
107
  @client.close
108
108
  @server.close
109
- ZMachine.context.destroy
109
+ ZContext.destroy
110
110
  end
111
111
 
112
112
  let(:data) { "foo".to_java_bytes }
@@ -157,7 +157,7 @@ describe ZMQChannel do
157
157
  after(:each) do
158
158
  @client.close
159
159
  @server.close
160
- ZMachine.context.destroy
160
+ ZContext.destroy
161
161
  end
162
162
 
163
163
  let(:data) { "foo".to_java_bytes }
@@ -14,7 +14,7 @@ shared_examples_for "a Connection" do
14
14
  after(:each) do
15
15
  @client.close!
16
16
  @server.close!
17
- ZMachine.context.destroy
17
+ ZContext.destroy
18
18
  end
19
19
 
20
20
  let(:data) { "foo" }
@@ -20,7 +20,7 @@ describe ZMachine::HashedWheel do
20
20
  wheel.add 10
21
21
  wheel.add 50
22
22
  timedout = wheel.advance(now + 30 * 1_000_000)
23
- expect(timedout.length).to eq(1)
23
+ expect(timedout).to eq(1)
24
24
  end
25
25
 
26
26
  it 'calculates the timeout set correctly' do
@@ -31,18 +31,17 @@ describe ZMachine::HashedWheel do
31
31
  wheel.add 3300
32
32
  wheel.add 4000
33
33
  timedout = wheel.advance(now + 3900 * 1_000_000)
34
- expect(timedout).to be
35
- expect(timedout.length).to eq(4)
34
+ expect(timedout).to eq(4)
36
35
  end
37
36
 
38
37
  it 'cancels timers correctly' do
39
38
  now = wheel.reset
40
- t1 = wheel.add 90
41
- t2 = wheel.add 110
42
- t1.cancel
39
+ result = nil
40
+ wheel.add(90, -> { result = true })
41
+ wheel.add(110, -> { result = false }).cancel
43
42
  timedout = wheel.advance(now + 200 * 1_000_000)
44
- expect(timedout.first).to eq(t2)
45
- expect(timedout.length).to eq(1)
43
+ expect(result).to eq(true)
44
+ expect(timedout).to eq(2)
46
45
  end
47
46
 
48
47
  end
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.4.0"
5
+ spec.version = "0.4.1"
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.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - LiquidM, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-12 00:00:00.000000000 Z
11
+ date: 2014-02-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: liquid-ext
@@ -54,7 +54,6 @@ files:
54
54
  - lib/zmachine/connection_manager.rb
55
55
  - lib/zmachine/deferrable.rb
56
56
  - lib/zmachine/hashed_wheel.rb
57
- - lib/zmachine/jeromq-0.3.2-SNAPSHOT.jar
58
57
  - lib/zmachine/reactor.rb
59
58
  - lib/zmachine/tcp_channel.rb
60
59
  - lib/zmachine/timers.rb
Binary file