Reputation: 3081
I am learning Netty framework. As far as I know it is roughly built around two components, ByteBuffer
(for low level data representation) and Channel
(for transmission of ByteBuffers in the wire). I was looking through Netty examples which is bundled with it (Netty 3.2). And I came to simple Telnet
program which was given below. Here I do not quite understand the order of pipeline, in which the data coming into ChannelPipeline
was first decoded and again encoded and after that forwarded to handler, which means data type inside (Channel)
ChannelPipeline
is binary (correct me If I am wrong), but I thought it would be text. What is the reason of favouring binary over text representation inside Channel (ChannelPipeline)
?
package org.jboss.netty.example.telnet;
public class TelnetServerPipelineFactory implements
ChannelPipelineFactory {
public ChannelPipeline getPipeline() throws Exception {
// Create a default pipeline implementation.
ChannelPipeline pipeline = Channel.pipeline();
// Add the text line codec combination first,
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(
8192, Delimiters.lineDelimiter()));
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
// and then business logic.
pipeline.addLast("handler", new TelnetServerHandler());
return pipeline;
}
}
Upvotes: 0
Views: 173
Reputation: 1251
This seems confusing at first. The normal path for a message passing through the pipeline should be as follows.
So where does the encoder come into play ? Why is it even there in the pipeline and that too before the TelnetServerHandler ? Will it not encode the String back to a stream of bytes ?
So basically in Netty, the handlers are of two types. They can be either downstream handlers or upstream handlers or even both. Basically Downstream is when a pipeline is sending something to a remote. Upstream is when it is reading something from a remote. So the decoders are upstream handlers and they will intercept the data coming in from the remote i.e. outside.
Encoders are downstream handlers and they intercept data that the server sends to the remote. So in case you want to send a string from your TelnetServerHandler to outside world i.e. your client, then that String will be encoded to ByteBuf by the StringEncoder.
Hope this clears your doubts.
Upvotes: 2
Reputation: 3018
Normally decoders only handle incoming messages. They extend ChannelInboundHandlerAdapter
. The encoder does not do anything with incoming messages apart from calling the next handler in the pipeline. You will have a String
in the TelnetServerHandler
.
Now if this handler would send a message it would be encoded (encoder extends ChannelOutboundHandlerAdapter
and overwrites the write()
method at some point) and the ByteBuf
would be sent to the connected peer.
The decoder does not do anything with outgoing messages. Its write()
method just calls the next handler.
Upvotes: 1