Network file transfer
Norman Maurer
norman.maurer at googlemail.com
Tue Oct 11 02:54:20 EDT 2011
Hi there,
comments inside..
2011/10/11 metalstorm <metalheadstorm at gmail.com>:
> Thank you for your reply Norman,
>
> For my little and somwhat basic test app I have this as my client and server
> pipelineFactory ( copied from a tutorial)
>
> // 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 TimeClientHandler());
>
> I think i understand what each part does, i just don't know how to go about
> it. (I understand the business handler class thought, that's all fine for
> now i think)
>
> 'Framer' was removed in this example because messages would have to have
> /n/r at the end to be read / writen, which i didn't want.
You still need the framer if you want to handle "line based
protocols". You can tell it to strip the delimiters.
See:
http://docs.jboss.org/netty/3.2/api/org/jboss/netty/handler/codec/frame/DelimiterBasedFrameDecoder.html
>
> So from the docs:
>
> Protocol Decoder - translates binary data (e.g. ChannelBuffer) into a Java
> object.
> Protocol Encoder - translates a Java object into binary data.
>
> So for c# sending files over tcp, data is read from a file into a fileStream
> then out of that into a networkstream which then goes the opposite was at
> the other end.
>
> So in netty, once data is read from a file into a filestream what process
> must it go through (encoder) before it is sent, from what i understand it is
> already in binary data; which begs the question what about the other end as
> well.
>
> I cannot see what a framer would do in this situation (but tbh i'm not
> entirely sure what a framer does really)
The framer only handle incomming traffic. It basicly "buffer" the data
till the point were the "frame" is complete. What kind of framer you
need depends on the kind of protocol you want to handle.
For more info see:
http://docs.jboss.org/netty/3.2/api/org/jboss/netty/handler/codec/frame/FrameDecoder.html
>
> Finally, you say you can't block the thread so you can't do while(read)
> think of thing, how else is reading from the network and writing to a file
> accomplished?
You would extend the SimpleChannelUpstreamHandler and overwrite the
messageReceived method(...). In the method you pass the pushed data to
an FileOutputStream. Be sure that you add an ExecutionHandler in front
of it as the write to the disk MAY block the IO-Worker-Thread
otherwise.
Maybe this code snipped helps you:
http://svn.apache.org/viewvc/james/protocols/trunk/smtp/src/main/java/org/apache/james/protocols/smtp/core/DataLineMessageHookHandler.java?view=markup
Its just a layer on top of netty.
So in netty you would do something like this:
public class MyChannelHandler extends SimpleChannelUpstreamHandler {
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent
e) throws Exception {
ChannelBuffer buf = (ChannelBuffer) e.getMessage();
byte[] line;
if (buf.hasArray()) {
line = buf.array();
} else {
line = new byte[buf.capacity()];
buf.getBytes(0, line);
}
FileOutputStream out = ...
out.write(line);
// do some other fancy stuff
...
...
// close the FileOutputStream once you are done
out.close();
}
}
>
> Thank you for your time.
>
> P.s I am a kinesthetic learner, please provide source code where possible so
> that I can learn and build upon it.
>
Bye,
Norman
More information about the netty-users
mailing list