foxbat 0.0.1 → 0.0.2
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.
- data/lib/em/connection.rb +28 -11
- data/lib/em/periodic_timer.rb +17 -0
- data/lib/em/timer.rb +15 -0
- data/lib/eventmachine.rb +8 -3
- data/lib/foxbat/barrier.rb +68 -0
- data/lib/foxbat/server.rb +10 -36
- data/lib/foxbat/version.rb +1 -1
- data/lib/foxbat.rb +3 -0
- metadata +5 -2
data/lib/em/connection.rb
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
import java.nio.ByteBuffer
|
2
|
-
import javax.net.ssl.SSLEngineResult::HandshakeStatus
|
3
2
|
|
4
3
|
module EventMachine
|
5
4
|
|
6
5
|
class Connection
|
7
6
|
|
8
|
-
|
7
|
+
BUF_SIZE = 256
|
8
|
+
|
9
|
+
attr_accessor :channel, :block
|
10
|
+
attr_reader :open_time, :close_time
|
9
11
|
|
10
12
|
def peername
|
11
13
|
@channel.getRemoteAddress
|
@@ -16,29 +18,44 @@ module EventMachine
|
|
16
18
|
buf = ByteBuffer.allocate(arr.length)
|
17
19
|
buf.put(arr)
|
18
20
|
buf.flip
|
21
|
+
|
19
22
|
@channel.write(buf)
|
20
23
|
end
|
21
24
|
|
22
25
|
def post_init; end
|
23
26
|
|
27
|
+
def server_post_init
|
28
|
+
@open_time ||= Time.now.to_i
|
29
|
+
end
|
30
|
+
|
24
31
|
def unbind; end
|
25
32
|
|
33
|
+
def start_tls(args={}); end
|
34
|
+
|
35
|
+
def receive_data(data)
|
36
|
+
p 'Incoming data...'
|
37
|
+
end
|
38
|
+
|
26
39
|
def close_connection(after_writing=false)
|
27
|
-
|
40
|
+
if after_writing == false
|
41
|
+
@channel.close
|
42
|
+
else
|
43
|
+
@channel.shutdownInput
|
44
|
+
@channel.shutdownOutput
|
45
|
+
end
|
46
|
+
@close_time = Time.now.to_i
|
28
47
|
end
|
29
48
|
|
30
|
-
def
|
31
|
-
|
32
|
-
|
49
|
+
def close_connection_after_writing
|
50
|
+
close_connection(true)
|
51
|
+
end
|
33
52
|
|
34
|
-
|
35
|
-
# @abs ||= @ssl_session.getApplicationBufferSize
|
36
|
-
# @pbs ||= @ssl_session.getPacketBufferSize
|
53
|
+
def read_channel(buffer=nil)
|
37
54
|
|
38
|
-
@
|
55
|
+
@block.call(self)
|
39
56
|
|
40
57
|
if buffer.nil?
|
41
|
-
bb = ByteBuffer.allocate(
|
58
|
+
bb = ByteBuffer.allocate(BUF_SIZE)
|
42
59
|
bb.clear
|
43
60
|
return read_channel(bb)
|
44
61
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module EventMachine
|
2
|
+
|
3
|
+
class PeriodicTimer
|
4
|
+
|
5
|
+
def initialize(interval, callback=nil, repeat=0, &block)
|
6
|
+
work = callback || block
|
7
|
+
t = lambda { sleep interval; work.call }
|
8
|
+
@timer = Foxbat::Barrier.new([t], repeat)
|
9
|
+
end
|
10
|
+
|
11
|
+
def cancel
|
12
|
+
@timer.cancel
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
data/lib/em/timer.rb
ADDED
data/lib/eventmachine.rb
CHANGED
@@ -3,7 +3,7 @@ import java.util.concurrent.Executors
|
|
3
3
|
module EventMachine
|
4
4
|
|
5
5
|
def self.start_server host, port=nil, handler=nil, *args, &block
|
6
|
-
s = Foxbat::Server.new(host, port, handler, block)
|
6
|
+
s = Foxbat::Server.new(host, port, handler, args.first, block)
|
7
7
|
|
8
8
|
@@servers ||= []
|
9
9
|
@@servers << s
|
@@ -13,9 +13,10 @@ module EventMachine
|
|
13
13
|
|
14
14
|
# We're on the JVM- this does nothing!
|
15
15
|
def self.epoll; end
|
16
|
+
def self.kqueue; end
|
16
17
|
|
17
18
|
def self.run(blk=nil, tail=nil, &block)
|
18
|
-
@@threadpool
|
19
|
+
@@threadpool = Executors.newCachedThreadPool
|
19
20
|
|
20
21
|
block.call
|
21
22
|
|
@@ -23,9 +24,13 @@ module EventMachine
|
|
23
24
|
end
|
24
25
|
|
25
26
|
def self.stop
|
26
|
-
@@servers.each { |s| s.stop }
|
27
|
+
@@servers.each { |s| s.stop } unless @@servers.nil?
|
27
28
|
@@threadpool.shutdownNow
|
28
29
|
end
|
29
30
|
|
31
|
+
def self.executor
|
32
|
+
@@threadpool
|
33
|
+
end
|
34
|
+
|
30
35
|
end
|
31
36
|
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'timeout'
|
2
|
+
|
3
|
+
module Foxbat
|
4
|
+
|
5
|
+
class FBPhaser < java.util.concurrent.Phaser
|
6
|
+
|
7
|
+
attr_writer :limit, :callback
|
8
|
+
|
9
|
+
def onAdvance(phase, parties)
|
10
|
+
if (phase + 1) == @limit
|
11
|
+
true
|
12
|
+
else
|
13
|
+
@callback.call if @callback
|
14
|
+
false
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class Barrier
|
20
|
+
|
21
|
+
def initialize(tasks, repeat=1, callback=nil, timeout=Long::MAX_VALUE, err=nil)
|
22
|
+
@phaser = FBPhaser.new(1)
|
23
|
+
if repeat == 0
|
24
|
+
repeat = java.lang.Integer::MAX_VALUE
|
25
|
+
end
|
26
|
+
@phaser.limit = repeat
|
27
|
+
@phaser.callback = callback
|
28
|
+
|
29
|
+
phased_tasks = tasks.map do |t|
|
30
|
+
Proc.new do
|
31
|
+
@phaser.register
|
32
|
+
while !@phaser.isTerminated
|
33
|
+
begin
|
34
|
+
Timeout::timeout(timeout) { t.call }
|
35
|
+
rescue Exception => e
|
36
|
+
err.call(e) if err
|
37
|
+
break
|
38
|
+
end
|
39
|
+
@phaser.arriveAndAwaitAdvance
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
phased_tasks.each { |t| EM.executor.execute(t) }
|
45
|
+
|
46
|
+
start = Proc.new do
|
47
|
+
parties = @phaser.getRegisteredParties
|
48
|
+
if parties > 1
|
49
|
+
@phaser.arriveAndDeregister
|
50
|
+
else
|
51
|
+
java.lang.Thread.sleep(10)
|
52
|
+
start.call
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
start.call
|
57
|
+
end
|
58
|
+
|
59
|
+
def iterations
|
60
|
+
@phaser.getPhase + 1
|
61
|
+
end
|
62
|
+
|
63
|
+
def cancel
|
64
|
+
@phaser.forceTermination()
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
end
|
data/lib/foxbat/server.rb
CHANGED
@@ -4,43 +4,23 @@ import java.nio.channels.AsynchronousSocketChannel
|
|
4
4
|
import java.nio.channels.AsynchronousChannelGroup
|
5
5
|
import java.util.concurrent.TimeUnit
|
6
6
|
import java.lang.Long
|
7
|
-
|
8
|
-
# SSL stuff
|
9
|
-
import javax.net.ssl.SSLContext
|
10
|
-
import javax.net.ssl.KeyManagerFactory
|
11
|
-
import javax.net.ssl.TrustManagerFactory
|
12
|
-
import javax.net.ssl.SSLEngineResult
|
13
|
-
import java.security.KeyStore
|
14
|
-
import java.io.FileInputStream
|
7
|
+
import java.io.IOException
|
15
8
|
|
16
9
|
module Foxbat
|
17
10
|
|
18
11
|
class Server
|
19
12
|
|
20
|
-
def
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
password = 'marsbars'.to_java.toCharArray
|
26
|
-
@keystore.load(fis, password)
|
27
|
-
fis.close
|
28
|
-
|
29
|
-
@kmf = KeyManagerFactory.getInstance('SunX509')
|
30
|
-
@tmf = TrustManagerFactory.getInstance('SunX509')
|
31
|
-
|
32
|
-
@kmf.init(@keystore, password)
|
33
|
-
@tmf.init(@keystore)
|
34
|
-
|
35
|
-
@ssl_context.init(@kmf.getKeyManagers, @tmf.getTrustManagers, nil)
|
13
|
+
def create_ssl_engine(connection)
|
14
|
+
engine = @ssl_context.createSSLEngine
|
15
|
+
engine.setUseClientMode(false)
|
16
|
+
engine.setNeedClientAuth(false)
|
17
|
+
connection.ssl_engine = engine
|
36
18
|
end
|
37
19
|
|
38
|
-
def initialize(host, port, klass, block=nil)
|
20
|
+
def initialize(host, port, klass, options, block=nil)
|
39
21
|
@bind_address = InetSocketAddress.new(host, port)
|
40
22
|
@klass = klass
|
41
23
|
@block = block || Proc.new {}
|
42
|
-
|
43
|
-
# setup_ssl
|
44
24
|
end
|
45
25
|
|
46
26
|
def start(threadpool)
|
@@ -51,16 +31,10 @@ module Foxbat
|
|
51
31
|
handler = Foxbat::Handler.new(@server) do |source,socket|
|
52
32
|
source.accept(nil,handler)
|
53
33
|
|
54
|
-
connection = @klass.new({
|
34
|
+
connection = @klass.new({})
|
55
35
|
connection.channel = socket
|
56
36
|
connection.block = @block
|
57
|
-
connection.
|
58
|
-
|
59
|
-
# engine = @ssl_context.createSSLEngine
|
60
|
-
# engine.setUseClientMode(false)
|
61
|
-
# engine.setNeedClientAuth(false)
|
62
|
-
|
63
|
-
# connection.ssl_engine = engine
|
37
|
+
connection.server_post_init
|
64
38
|
connection.post_init
|
65
39
|
|
66
40
|
connection.read_channel
|
@@ -73,7 +47,7 @@ module Foxbat
|
|
73
47
|
|
74
48
|
def stop
|
75
49
|
@server.close
|
76
|
-
@group.awaitTermination(
|
50
|
+
@group.awaitTermination(0, TimeUnit::SECONDS)
|
77
51
|
end
|
78
52
|
end
|
79
53
|
|
data/lib/foxbat/version.rb
CHANGED
data/lib/foxbat.rb
CHANGED
metadata
CHANGED
@@ -2,14 +2,14 @@
|
|
2
2
|
name: foxbat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Chris Mowforth
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-03-
|
12
|
+
date: 2012-03-18 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: A drop-in replacement for EventMachine, designed & built from the ground-up around Java 7 A/IO.
|
15
15
|
email:
|
@@ -20,9 +20,12 @@ extra_rdoc_files: []
|
|
20
20
|
files:
|
21
21
|
- lib/foxbat.rb
|
22
22
|
- lib/eventmachine.rb
|
23
|
+
- lib/foxbat/barrier.rb
|
23
24
|
- lib/foxbat/server.rb
|
24
25
|
- lib/foxbat/version.rb
|
25
26
|
- lib/foxbat/handler.rb
|
27
|
+
- lib/em/periodic_timer.rb
|
28
|
+
- lib/em/timer.rb
|
26
29
|
- lib/em/connection.rb
|
27
30
|
homepage: http://github.com/cmowforth/foxbat
|
28
31
|
licenses: []
|