foxbat 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/em/connection.rb +71 -0
- data/lib/eventmachine.rb +31 -0
- data/lib/foxbat/handler.rb +32 -0
- data/lib/foxbat/server.rb +80 -0
- data/lib/foxbat/version.rb +3 -0
- data/lib/foxbat.rb +10 -0
- metadata +52 -0
@@ -0,0 +1,71 @@
|
|
1
|
+
import java.nio.ByteBuffer
|
2
|
+
import javax.net.ssl.SSLEngineResult::HandshakeStatus
|
3
|
+
|
4
|
+
module EventMachine
|
5
|
+
|
6
|
+
class Connection
|
7
|
+
|
8
|
+
attr_accessor :channel, :ssl_engine, :block, :executor
|
9
|
+
|
10
|
+
def peername
|
11
|
+
@channel.getRemoteAddress
|
12
|
+
end
|
13
|
+
|
14
|
+
def send_data(data)
|
15
|
+
arr = data.to_java_bytes
|
16
|
+
buf = ByteBuffer.allocate(arr.length)
|
17
|
+
buf.put(arr)
|
18
|
+
buf.flip
|
19
|
+
@channel.write(buf)
|
20
|
+
end
|
21
|
+
|
22
|
+
def post_init; end
|
23
|
+
|
24
|
+
def unbind; end
|
25
|
+
|
26
|
+
def close_connection(after_writing=false)
|
27
|
+
@channel.close
|
28
|
+
end
|
29
|
+
|
30
|
+
def read_channel(buffer=nil)
|
31
|
+
|
32
|
+
@block.call(self)
|
33
|
+
|
34
|
+
# @ssl_session ||= @ssl_engine.getSession
|
35
|
+
# @abs ||= @ssl_session.getApplicationBufferSize
|
36
|
+
# @pbs ||= @ssl_session.getPacketBufferSize
|
37
|
+
|
38
|
+
@abs = 128
|
39
|
+
|
40
|
+
if buffer.nil?
|
41
|
+
bb = ByteBuffer.allocate(@abs)
|
42
|
+
bb.clear
|
43
|
+
return read_channel(bb)
|
44
|
+
end
|
45
|
+
|
46
|
+
reader = Foxbat::Handler.new(@channel) do |c,br|
|
47
|
+
if br == -1
|
48
|
+
c.close
|
49
|
+
self.unbind
|
50
|
+
else
|
51
|
+
buffer.flip
|
52
|
+
str = btos(buffer)
|
53
|
+
|
54
|
+
buffer.clear
|
55
|
+
buffer.rewind
|
56
|
+
|
57
|
+
self.receive_data(str)
|
58
|
+
read_channel(buffer)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
@channel.read(buffer, nil, reader)
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def btos(buf)
|
68
|
+
return String.from_java_bytes(buf.array[buf.position..(buf.limit-1)])
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/lib/eventmachine.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
import java.util.concurrent.Executors
|
2
|
+
|
3
|
+
module EventMachine
|
4
|
+
|
5
|
+
def self.start_server host, port=nil, handler=nil, *args, &block
|
6
|
+
s = Foxbat::Server.new(host, port, handler, block)
|
7
|
+
|
8
|
+
@@servers ||= []
|
9
|
+
@@servers << s
|
10
|
+
|
11
|
+
s.start(@@threadpool)
|
12
|
+
end
|
13
|
+
|
14
|
+
# We're on the JVM- this does nothing!
|
15
|
+
def self.epoll; end
|
16
|
+
|
17
|
+
def self.run(blk=nil, tail=nil, &block)
|
18
|
+
@@threadpool ||= Executors.newCachedThreadPool(Executors.defaultThreadFactory)
|
19
|
+
|
20
|
+
block.call
|
21
|
+
|
22
|
+
@@threadpool.awaitTermination(Long::MAX_VALUE, TimeUnit::SECONDS)
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.stop
|
26
|
+
@@servers.each { |s| s.stop }
|
27
|
+
@@threadpool.shutdownNow
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
@@ -0,0 +1,32 @@
|
|
1
|
+
import java.nio.channels.CompletionHandler
|
2
|
+
import java.nio.channels.AsynchronousCloseException
|
3
|
+
|
4
|
+
module Foxbat
|
5
|
+
|
6
|
+
class Handler
|
7
|
+
include CompletionHandler
|
8
|
+
|
9
|
+
attr_writer :on_fail
|
10
|
+
|
11
|
+
def initialize(source, &block)
|
12
|
+
@source = source
|
13
|
+
@completion = block
|
14
|
+
end
|
15
|
+
|
16
|
+
def completed(socket,attachment)
|
17
|
+
@completion.call(@source,socket)
|
18
|
+
end
|
19
|
+
|
20
|
+
def failed(err,attachment)
|
21
|
+
if !err.is_a?(AsynchronousCloseException)
|
22
|
+
if @on_fail.nil?
|
23
|
+
p "ERR: #{err.inspect} -> #{attachment.inspect}"
|
24
|
+
else
|
25
|
+
@on_fail.call(err, attachment)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
import java.net.InetSocketAddress
|
2
|
+
import java.nio.channels.AsynchronousServerSocketChannel
|
3
|
+
import java.nio.channels.AsynchronousSocketChannel
|
4
|
+
import java.nio.channels.AsynchronousChannelGroup
|
5
|
+
import java.util.concurrent.TimeUnit
|
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
|
15
|
+
|
16
|
+
module Foxbat
|
17
|
+
|
18
|
+
class Server
|
19
|
+
|
20
|
+
def setup_ssl
|
21
|
+
@ssl_context = SSLContext.getInstance('TLSv1')
|
22
|
+
@keystore = KeyStore.getInstance(KeyStore.getDefaultType)
|
23
|
+
fis = FileInputStream.new('/tank/me/.keystore')
|
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)
|
36
|
+
end
|
37
|
+
|
38
|
+
def initialize(host, port, klass, block=nil)
|
39
|
+
@bind_address = InetSocketAddress.new(host, port)
|
40
|
+
@klass = klass
|
41
|
+
@block = block || Proc.new {}
|
42
|
+
|
43
|
+
# setup_ssl
|
44
|
+
end
|
45
|
+
|
46
|
+
def start(threadpool)
|
47
|
+
@group = AsynchronousChannelGroup.withCachedThreadPool(threadpool, 1)
|
48
|
+
@server = AsynchronousServerSocketChannel.open(@group)
|
49
|
+
@server.bind(@bind_address)
|
50
|
+
|
51
|
+
handler = Foxbat::Handler.new(@server) do |source,socket|
|
52
|
+
source.accept(nil,handler)
|
53
|
+
|
54
|
+
connection = @klass.new({:debug => true})
|
55
|
+
connection.channel = socket
|
56
|
+
connection.block = @block
|
57
|
+
connection.executor = @service
|
58
|
+
|
59
|
+
# engine = @ssl_context.createSSLEngine
|
60
|
+
# engine.setUseClientMode(false)
|
61
|
+
# engine.setNeedClientAuth(false)
|
62
|
+
|
63
|
+
# connection.ssl_engine = engine
|
64
|
+
connection.post_init
|
65
|
+
|
66
|
+
connection.read_channel
|
67
|
+
end
|
68
|
+
|
69
|
+
@server.accept(nil, handler)
|
70
|
+
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
def stop
|
75
|
+
@server.close
|
76
|
+
@group.awaitTermination(1, TimeUnit::SECONDS)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
data/lib/foxbat.rb
ADDED
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: foxbat
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Chris Mowforth
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-15 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: A drop-in replacement for EventMachine, designed & built from the ground-up around Java 7 A/IO.
|
15
|
+
email:
|
16
|
+
- chris@mowforth.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/foxbat.rb
|
22
|
+
- lib/eventmachine.rb
|
23
|
+
- lib/foxbat/server.rb
|
24
|
+
- lib/foxbat/version.rb
|
25
|
+
- lib/foxbat/handler.rb
|
26
|
+
- lib/em/connection.rb
|
27
|
+
homepage: http://github.com/cmowforth/foxbat
|
28
|
+
licenses: []
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
none: false
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
none: false
|
45
|
+
requirements: []
|
46
|
+
rubyforge_project:
|
47
|
+
rubygems_version: 1.8.15
|
48
|
+
signing_key:
|
49
|
+
specification_version: 3
|
50
|
+
summary: EventMachine replacement for JRuby.
|
51
|
+
test_files: []
|
52
|
+
...
|