Created
October 13, 2010 03:50
-
-
Save headius/623397 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.2.2.Final' | |
| import java.net.InetSocketAddress; | |
| import java.util.concurrent.Executors; | |
| import java.util.concurrent.atomic.AtomicLong; | |
| import java.util.logging.Level; | |
| import java.util.logging.Logger; | |
| import org.jboss.netty.bootstrap.ServerBootstrap; | |
| import org.jboss.netty.channel.ChannelPipelineFactory; | |
| import org.jboss.netty.channel.Channels; | |
| import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; | |
| import org.jboss.netty.channel.SimpleChannelUpstreamHandler; | |
| # Configure the server. | |
| bootstrap = ServerBootstrap.new( | |
| NioServerSocketChannelFactory.new( | |
| Executors.newCachedThreadPool(), | |
| Executors.newCachedThreadPool())) | |
| class EchoServerHandler < SimpleChannelUpstreamHandler | |
| LOGGER = Logger.get_logger "EchoServerHandler" | |
| def initialize | |
| @transferred_bytes = AtomicLong.new | |
| end | |
| def transferred_bytes | |
| @transferred_bytes.get | |
| end | |
| def messageReceived(ctx, e) | |
| @transferred_bytes.add_and_get(e.message.readable_bytes) | |
| e.channel.write(e.message) | |
| end | |
| def exceptionCaught(ctx, evt) | |
| LOGGER.log(Level::WARNING, "Unexpected exception from downstream.", e.cause) | |
| e.channel.close | |
| end | |
| end | |
| # Set up the pipeline factory. | |
| class EchoPipelineFactory | |
| include ChannelPipelineFactory | |
| def getPipeline | |
| return Channels.pipeline EchoServerHandler.new | |
| end | |
| end | |
| bootstrap.pipeline_factory = EchoPipelineFactory.new | |
| # Bind and start to accept incoming connections. | |
| bootstrap.bind(InetSocketAddress.new(8080)); |
This file contains hidden or 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
| /* | |
| * Copyright 2009 Red Hat, Inc. | |
| * | |
| * Red Hat licenses this file to you under the Apache License, version 2.0 | |
| * (the "License"); you may not use this file except in compliance with the | |
| * License. You may obtain a copy of the License at: | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |
| * License for the specific language governing permissions and limitations | |
| * under the License. | |
| */ | |
| package org.jboss.netty.example.echo; | |
| import java.net.InetSocketAddress; | |
| import java.util.concurrent.Executors; | |
| import org.jboss.netty.bootstrap.ServerBootstrap; | |
| import org.jboss.netty.channel.ChannelPipeline; | |
| import org.jboss.netty.channel.ChannelPipelineFactory; | |
| import org.jboss.netty.channel.Channels; | |
| import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; | |
| /** | |
| * Echoes back any received data from a client. | |
| * | |
| * @author <a href="http://www.jboss.org/netty/">The Netty Project</a> | |
| * @author <a href="http://gleamynode.net/">Trustin Lee</a> | |
| * | |
| * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $ | |
| * | |
| */ | |
| public class EchoServer { | |
| public static void main(String[] args) throws Exception { | |
| // Configure the server. | |
| ServerBootstrap bootstrap = new ServerBootstrap( | |
| new NioServerSocketChannelFactory( | |
| Executors.newCachedThreadPool(), | |
| Executors.newCachedThreadPool())); | |
| // Set up the pipeline factory. | |
| bootstrap.setPipelineFactory(new ChannelPipelineFactory() { | |
| public ChannelPipeline getPipeline() throws Exception { | |
| return Channels.pipeline(new EchoServerHandler()); | |
| } | |
| }); | |
| // Bind and start to accept incoming connections. | |
| bootstrap.bind(new InetSocketAddress(8080)); | |
| } | |
| } |
This file contains hidden or 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
| /* | |
| * Copyright 2009 Red Hat, Inc. | |
| * | |
| * Red Hat licenses this file to you under the Apache License, version 2.0 | |
| * (the "License"); you may not use this file except in compliance with the | |
| * License. You may obtain a copy of the License at: | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |
| * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |
| * License for the specific language governing permissions and limitations | |
| * under the License. | |
| */ | |
| package org.jboss.netty.example.echo; | |
| import java.util.concurrent.atomic.AtomicLong; | |
| import java.util.logging.Level; | |
| import java.util.logging.Logger; | |
| import org.jboss.netty.buffer.ChannelBuffer; | |
| import org.jboss.netty.channel.ChannelHandlerContext; | |
| import org.jboss.netty.channel.ExceptionEvent; | |
| import org.jboss.netty.channel.MessageEvent; | |
| import org.jboss.netty.channel.SimpleChannelUpstreamHandler; | |
| /** | |
| * Handler implementation for the echo server. | |
| * | |
| * @author <a href="http://www.jboss.org/netty/">The Netty Project</a> | |
| * @author <a href="http://gleamynode.net/">Trustin Lee</a> | |
| * | |
| * @version $Rev: 2121 $, $Date: 2010-02-02 09:38:07 +0900 (Tue, 02 Feb 2010) $ | |
| */ | |
| public class EchoServerHandler extends SimpleChannelUpstreamHandler { | |
| private static final Logger logger = Logger.getLogger( | |
| EchoServerHandler.class.getName()); | |
| private final AtomicLong transferredBytes = new AtomicLong(); | |
| public long getTransferredBytes() { | |
| return transferredBytes.get(); | |
| } | |
| @Override | |
| public void messageReceived( | |
| ChannelHandlerContext ctx, MessageEvent e) { | |
| // Send back the received message to the remote peer. | |
| transferredBytes.addAndGet(((ChannelBuffer) e.getMessage()).readableBytes()); | |
| e.getChannel().write(e.getMessage()); | |
| } | |
| @Override | |
| public void exceptionCaught( | |
| ChannelHandlerContext ctx, ExceptionEvent e) { | |
| // Close the connection when an exception is raised. | |
| logger.log( | |
| Level.WARNING, | |
| "Unexpected exception from downstream.", | |
| e.getCause()); | |
| e.getChannel().close(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment