Skip to content

Instantly share code, notes, and snippets.

@caniszczyk
Created February 10, 2014 18:04
Show Gist options
  • Save caniszczyk/8921026 to your computer and use it in GitHub Desktop.
Save caniszczyk/8921026 to your computer and use it in GitHub Desktop.
Netty based Listener
case class Netty3Listener[In, Out](
pipelineFactory: ChannelPipelineFactory,
channelFactory: ServerChannelFactory
bootstrapOptions: Map[String, Object], ... // stats/timeouts/ssl config
) extends Listener[In, Out] {
def newServerPipelineFactory(
statsReceiver: StatsReceiver, newBridge: () => ChannelHandler
) = new ChannelPipelineFactory { // #1
def getPipeline() = {
val pipeline = pipelineFactory.getPipeline()
// ... add stats/timeouts/ssl
pipeline.addLast("finagleBridge", newBridge()) // #2
pipeline
}
}
def listen(addr: SocketAddress)(
serveTransport: Transport[In, Out] => Unit
): ListeningServer =
new ListeningServer with CloseAwaitably {
val newBridge = () => new ServerBridge(serveTransport, ...)
val bootstrap = new ServerBootstrap(channelFactory)
bootstrap.setOptions(bootstrapOptions.asJava)
bootstrap.setPipelineFactory(
newServerPipelineFactory(scopedStatsReceiver, newBridge))
val ch = bootstrap.bind(addr)
}
}
}
// #1 Create a new ChannelPipelineFactory
// #2 Add the Bridge into the ChannelPipeline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment