Skip to content

Instantly share code, notes, and snippets.

@vkostyukov
Last active May 15, 2017 03:47
Show Gist options
  • Select an option

  • Save vkostyukov/61646852524488c7f463 to your computer and use it in GitHub Desktop.

Select an option

Save vkostyukov/61646852524488c7f463 to your computer and use it in GitHub Desktop.
class DelimEncoder(delim: Char) extends SimpleChannelHandler {
override def writeRequested(ctx: ChannelHandlerContext, evt: MessageEvent) = {
val newMessage = evt.getMessage match {
case m: String => m + delim
case m => m
}
Channels.write(ctx, evt.getFuture, newMessage, evt.getRemoteAddress)
}
}
object StringClientPipeline extends ChannelPipelineFactory {
def getPipeline = {
val pipeline = Channels.pipeline()
pipeline.addLast("stringEncode", new StringEncoder(CharsetUtil.UTF_8))
pipeline.addLast("stringDecode", new StringDecoder(CharsetUtil.UTF_8))
pipeline.addLast("line", new DelimEncoder('\n'))
pipeline
}
}
object Udp extends CodecFactory[String, Unit] {
def server = throw new UnsupportedOperationException("clients only")
def client = Function.const {
new Codec[String, Unit] {
def pipelineFactory = StringClientPipeline
}
}
}
val client = ClientBuilder()
.codec(Udp)
.channelFactory(new NioDatagramChannelFactory(Executors.newCachedThreadPool()))
.hosts("127.0.0.1:2115")
.build()
val f = client("request")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment