Skip to content

Instantly share code, notes, and snippets.

@durana
Created March 2, 2010 03:17
Show Gist options
  • Save durana/319086 to your computer and use it in GitHub Desktop.
Save durana/319086 to your computer and use it in GitHub Desktop.
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