foxbat 0.2.0 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/em/connection.rb CHANGED
@@ -1,14 +1,20 @@
1
- import java.nio.ByteBuffer
2
- import org.jboss.netty.buffer.ChannelBuffers
3
- import org.jboss.netty.channel.SimpleChannelUpstreamHandler
4
-
5
1
  module EventMachine
6
2
 
7
- class Connection < SimpleChannelUpstreamHandler
3
+ class Connection
4
+
5
+ attr_writer :netty_handler
6
+
7
+ def self.new(*args)
8
+ allocate.instance_eval do
9
+ initialize(*args)
10
+ self
11
+ end
12
+ end
13
+
14
+ def initialize(*args); end
8
15
 
9
16
  def send_data(data)
10
- buf = ChannelBuffers.copiedBuffer(data, "UTF-8")
11
- @channel.write(buf)
17
+ @netty_handler.write(data)
12
18
  end
13
19
 
14
20
  def post_init; end
@@ -22,7 +28,11 @@ module EventMachine
22
28
  end
23
29
 
24
30
  def close_connection(after_writing=false)
25
- @channel.close
31
+ if after_writing
32
+ @close_scheduled = true
33
+ else
34
+ @netty_handler.close
35
+ end
26
36
  end
27
37
 
28
38
  def close_connection_after_writing
@@ -30,27 +40,7 @@ module EventMachine
30
40
  end
31
41
 
32
42
  def get_peername
33
- addr = @channel.getRemoteAddress
34
- [addr.getPort, addr.getHostString]
35
- end
36
-
37
- private
38
-
39
- # The netty channel callbacks
40
-
41
- def channelConnected(ctx, e)
42
- @pipeline = ctx.getPipeline
43
- @channel = e.getChannel
44
- post_init
45
- end
46
-
47
- def messageReceived(ctx, e)
48
- data = e.getMessage.toString('UTF-8')
49
- receive_data(data)
50
- end
51
-
52
- def exceptionCaught(ctx, e)
53
- p e.toString
43
+ @netty_handler.peername
54
44
  end
55
45
 
56
46
  end
data/lib/eventmachine.rb CHANGED
@@ -5,7 +5,7 @@ import java.util.concurrent.TimeUnit
5
5
  module EventMachine
6
6
 
7
7
  def self.start_server host, port=nil, handler=nil, *args, &block
8
- s = Foxbat::Server.new(host, port, handler, args.first || {}, block)
8
+ s = Foxbat::Server.new(host, port, handler, args.first || {}, &block)
9
9
 
10
10
  @@servers ||= []
11
11
  @@servers << s
@@ -17,7 +17,7 @@ module EventMachine
17
17
  def self.epoll; end
18
18
  def self.kqueue; end
19
19
 
20
- def self.run(blk=nil, tail=nil, &block)
20
+ def self.run(blk=nil, tail=nil, &block)
21
21
  @@threadpool = Executors.newCachedThreadPool
22
22
 
23
23
  block.call
@@ -26,7 +26,7 @@ module EventMachine
26
26
  end
27
27
 
28
28
  def self.stop
29
- @@servers.each { |s| s.stop } unless @@servers.nil?
29
+ @@servers.each { |s| s.stop }
30
30
  @@threadpool.shutdownNow
31
31
  end
32
32
 
@@ -0,0 +1,47 @@
1
+ import java.nio.ByteBuffer
2
+ import org.jboss.netty.buffer.ChannelBuffers
3
+ import org.jboss.netty.channel.SimpleChannelUpstreamHandler
4
+
5
+ module Foxbat
6
+
7
+ class NettyConnection < SimpleChannelUpstreamHandler
8
+
9
+ def initialize(connection)
10
+ @connection = connection
11
+ connection.netty_handler = self
12
+ super()
13
+ end
14
+
15
+ def write(data)
16
+ data = data.to_java_bytes if data.is_a?(String)
17
+ buf = ChannelBuffers.copiedBuffer(data)
18
+ @channel.write(buf)
19
+ end
20
+
21
+ def close
22
+ @channel.close
23
+ end
24
+
25
+ private
26
+
27
+ def channelConnected(ctx, e)
28
+ @pipeline = ctx.getPipeline
29
+ @channel = e.getChannel
30
+ @connection.post_init
31
+ end
32
+
33
+ def channelClosed(ctx, e)
34
+ @connection.unbind
35
+ end
36
+
37
+ def messageReceived(ctx, e)
38
+ data = String.from_java_bytes(e.getMessage.array)
39
+ @connection.receive_data(data)
40
+ end
41
+
42
+ def exceptionCaught(ctx, e)
43
+ p e.toString
44
+ end
45
+ end
46
+
47
+ end
@@ -3,14 +3,17 @@ import org.jboss.netty.channel.ChannelPipelineFactory
3
3
  import org.jboss.netty.handler.ssl.SslHandler
4
4
 
5
5
  require_relative 'security'
6
+ require_relative 'netty_connection'
6
7
 
7
8
  module Foxbat
8
9
 
9
10
  class Pipeline
10
11
  include ChannelPipelineFactory
11
12
 
12
- def initialize(handler, ssl_context=nil)
13
+ def initialize(handler, options={}, ssl_context=nil, &block)
14
+ @options = options
13
15
  @handler = handler
16
+ @block = block
14
17
  @context = ssl_context
15
18
  end
16
19
 
@@ -20,7 +23,10 @@ module Foxbat
20
23
  engine = Security.create_ssl_engine(@context)
21
24
  pipeline.addLast("ssl", SslHandler.new(engine))
22
25
  end
23
- pipeline.addLast("handler", @handler.new)
26
+ h = @handler.new(@options)
27
+ @block.call(h) if @block
28
+ connection = NettyConnection.new(h)
29
+ pipeline.addLast("handler", connection)
24
30
  pipeline
25
31
  end
26
32
 
data/lib/foxbat/server.rb CHANGED
@@ -8,12 +8,13 @@ module Foxbat
8
8
 
9
9
  class Server
10
10
 
11
- def initialize(host, port, klass, options, block=nil)
11
+ def initialize(host, port, klass, options, &block)
12
12
  if options[:secure]
13
13
  @context = Security.setup_ssl_context(options[:keystore])
14
14
  end
15
+
15
16
  @address = InetSocketAddress.new(host, port)
16
- @pipeline = Pipeline.new(klass || block, @context)
17
+ @pipeline = Pipeline.new(klass, options, @context, &block)
17
18
  end
18
19
 
19
20
  def start(threadpool)
@@ -24,6 +25,7 @@ module Foxbat
24
25
  end
25
26
 
26
27
  def stop
28
+ @bootstrap.releaseExternalResources
27
29
  end
28
30
  end
29
31
 
@@ -1,3 +1,3 @@
1
1
  module Foxbat
2
- VERSION = '0.0.2'
2
+ VERSION = '0.2.3'
3
3
  end
data/lib/foxbat.rb CHANGED
@@ -1,4 +1,35 @@
1
1
  require 'java'
2
+
3
+ NETTY_VERSION = '3.5.0.Final'
4
+ JAR_SIZE = 1110903.0 # TODO: remove hard-coded jar size
5
+
6
+ netty_jar = File.dirname(__FILE__) + "/netty-#{NETTY_VERSION}.jar"
7
+
8
+ if !File.exist?(netty_jar)
9
+ puts "Couldn't find netty's JAR to load. Let's download it now."
10
+
11
+ require 'net/http'
12
+ f = open(netty_jar, 'w')
13
+
14
+ Net::HTTP.start('search.maven.org') do |http|
15
+
16
+ begin
17
+ http.request_get("/remotecontent?filepath=io/netty/netty/#{NETTY_VERSION}/netty-#{NETTY_VERSION}.jar") do |resp|
18
+ acc = 0
19
+ resp.read_body do |segment|
20
+ acc += segment.length
21
+ percent = ((acc / JAR_SIZE) * 100).to_i
22
+ print "Downloading netty #{NETTY_VERSION} from maven repository... [#{percent}%]\r"
23
+ f.write(segment)
24
+ end
25
+ puts "\nDone. You shouldn't see me again :)"
26
+ end
27
+ ensure
28
+ f.close()
29
+ end
30
+ end
31
+ end
32
+
2
33
  require 'netty-3.5.0.Final.jar'
3
34
 
4
35
  require 'em/connection'
@@ -8,5 +39,13 @@ require 'foxbat/server'
8
39
  require 'foxbat/version'
9
40
  require_relative 'eventmachine'
10
41
 
42
+
43
+
11
44
  module EventMachine; end
12
- EM = EventMachine
45
+
46
+ begin
47
+ Kernel.const_get(:EM)
48
+ rescue
49
+ EM = EventMachine
50
+ end
51
+
Binary file
metadata CHANGED
@@ -1,17 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foxbat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
5
- prerelease:
4
+ version: 0.2.3
5
+ prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Chris Mowforth
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
  date: 2012-09-18 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: A drop-in replacement for EM, providing functionality missing from the Java port.
14
+ description: A drop-in replacement for EM, providing functionality missing from the
15
+ Java port.
15
16
  email:
16
17
  - chris@mowforth.com
17
18
  executables: []
@@ -19,39 +20,39 @@ extensions: []
19
20
  extra_rdoc_files: []
20
21
  files:
21
22
  - lib/eventmachine.rb
22
- - lib/netty-3.5.0.Final.jar
23
- - lib/foxbat.rb
24
23
  - lib/foxbat/barrier.rb
25
24
  - lib/foxbat/version.rb
26
25
  - lib/foxbat/server.rb
27
26
  - lib/foxbat/pipeline.rb
27
+ - lib/foxbat/netty_connection.rb
28
28
  - lib/foxbat/security.rb
29
+ - lib/netty-3.5.0.Final.jar
30
+ - lib/foxbat.rb
29
31
  - lib/em/periodic_timer.rb
30
32
  - lib/em/timer.rb
31
33
  - lib/em/connection.rb
32
- homepage: http://github.com/cmowforth/foxbat
34
+ homepage: http://github.com/m0wfo/foxbat
33
35
  licenses: []
34
- post_install_message:
36
+ post_install_message:
35
37
  rdoc_options: []
36
38
  require_paths:
37
39
  - lib
38
40
  required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
39
42
  requirements:
40
43
  - - ! '>='
41
44
  - !ruby/object:Gem::Version
42
45
  version: '0'
43
- none: false
44
46
  required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
45
48
  requirements:
46
49
  - - ! '>='
47
50
  - !ruby/object:Gem::Version
48
51
  version: '0'
49
- none: false
50
52
  requirements: []
51
- rubyforge_project:
52
- rubygems_version: 1.8.24
53
- signing_key:
53
+ rubyforge_project:
54
+ rubygems_version: 1.8.23
55
+ signing_key:
54
56
  specification_version: 3
55
57
  summary: EventMachine replacement for JRuby.
56
58
  test_files: []
57
- ...