Created
February 7, 2012 18:57
-
-
Save mlooney/1761238 to your computer and use it in GitHub Desktop.
Netty Discard server ported to jruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'java' | |
require 'jar/netty-3.3.1.Final.jar' | |
import 'java.net.InetSocketAddress' | |
import 'java.util.concurrent.Executors' | |
import 'org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory' | |
import 'org.jboss.netty.bootstrap.ServerBootstrap' | |
import 'org.jboss.netty.channel.SimpleChannelHandler' | |
import 'org.jboss.netty.channel.ChannelPipelineFactory' | |
import 'org.jboss.netty.channel.ChannelFactory' | |
import 'org.jboss.netty.channel.Channels' | |
class DiscardHandler < SimpleChannelHandler | |
def exceptionCaught(ctx, e) | |
puts e.cause.print_stacktrace | |
e.channel.close | |
end | |
def messageReceived(ctx, msg) | |
puts "received message: #{msg}" | |
end | |
end | |
class Pipelinefactory | |
include ChannelPipelineFactory | |
def get_pipeline | |
Channels.pipeline(DiscardHandler.new) | |
end | |
end | |
class DiscardServer | |
def initialize | |
factory =NioServerSocketChannelFactory.new( Executors.new_cached_thread_pool, Executors.new_cached_thread_pool) | |
@bootstrap = ServerBootstrap.new(factory) | |
@bootstrap.pipeline_factory=Pipelinefactory.new | |
@bootstrap.set_option('child.tcpNoDelay', true) | |
@bootstrap.set_option('child.keepAlive', true) | |
end | |
def start | |
@bootstrap.bind(InetSocketAddress.new(2020)) | |
end | |
end | |
DiscardServer.new.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment