Created
March 2, 2010 03:17
-
-
Save durana/319086 to your computer and use it in GitHub Desktop.
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
import org.jboss.netty.channel.{ | |
ChannelPipelineCoverage, | |
SimpleChannelHandler, | |
ChannelHandlerContext, | |
MessageEvent, | |
ExceptionEvent | |
} | |
import org.jboss.netty.channel.socket.nio.NioDatagramChannelFactory | |
import org.jboss.netty.bootstrap.ConnectionlessBootstrap | |
import java.util.concurrent.Executors | |
import java.net.InetSocketAddress | |
object EchoServerApp { | |
def main (args: Array[String]) { | |
val server = new EchoServer(1234) | |
server.start | |
} | |
} | |
@ChannelPipelineCoverage("all") | |
class EchoServerHandler extends SimpleChannelHandler { | |
override def messageReceived (context: ChannelHandlerContext, event: MessageEvent) { | |
event.getChannel.write(event.getMessage, event.getRemoteAddress) | |
println("Echoed back packet from %s".format(event.getRemoteAddress.toString)) | |
} | |
override def exceptionCaught (context: ChannelHandlerContext, event: ExceptionEvent) { | |
println("Exception caught...") | |
event.getCause.printStackTrace | |
} | |
} | |
class EchoServer (port: Int) { | |
val channelFactory = new NioDatagramChannelFactory(Executors.newCachedThreadPool()) | |
val boostrap = new ConnectionlessBootstrap(channelFactory) | |
def start () { | |
boostrap.getPipeline.addLast("handler", new EchoServerHandler()) | |
boostrap.bind(new InetSocketAddress(port)) | |
println("EchoServer started on port %d".format(port)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment