Managing channels outside of a server's channel handler

273 views
Skip to first unread message

L_DisplayName

unread,
Sep 5, 2016, 8:09:18 AM9/5/16
to Netty discussions
Greetings All,
 I wanted to know how can I manage a group of channels outside of a server's channel handler?  I am implementing a file server and I want the server to be able to transfer files through parallel data channels once it receives a file request from a client.  For example, if I have a single 4GB file and 4 data channels/connections, I want the server to transfer 1GB through each of the data channels simultaneously (note files are divided evenly among all data channels).  In essence, the server receives and processes the file request through one of the channel's  channelHandler - channelRead method, but then how can I inform and have each channel send their part/portion of the file?

For example, if I have a simple FileServerHandler class as below for each of the 4 channels,  and I wanted to know how can I inform and have each channel send their part/portion of the file.  

public class FileServerHandler extends SimpleChannelInboundHandler<ByteBuf> {

 
private Logger logger; private String currentFileName;
 
private String currentDirectory;
 
private long currentFileOffset;
 
private long currentFileChunkSize;
 
private boolean isDirectory;


 
public FileServerHandler() {

 logger
= Logger.getLogger(this.getClass().getName());

 
}

 
public void channelActive(ChannelHandlerContext ctx) throws Exception{
 
 logger
.info("Server:channel is active ");
 
 
}


 
@Override
 
public void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
 logger
.info("Server:channelRead0: filePath = " + msg);
 processFileRequest
(msg); //Obtains the file or directory to retrieve and the size of the file or
 
//files within a directory to retrieve recursively
 
 
/*
 
At this point I wish to tell the other channels the file or directory to retrieve, the file size, and the portion of the file(s) it should send? How can I do this? Also, if I implemented a channelManager, How can I pass this information to the channelManager and have it write the data to each channel?
 */

 
 
}

 
@Override
 
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
 cause
.printStackTrace();

 
if (ctx.channel().isActive()) {
 ctx
.writeAndFlush("ERR: " +
 cause
.getClass().getSimpleName() + ": " +
 cause
.getMessage() + '\n').addListener(ChannelFutureListener.CLOSE);
 
}
 
}
}



 

Rogan Dawes

unread,
Sep 5, 2016, 8:27:57 AM9/5/16
to Netty discussions
Typically you'd have the client open a single connection to discover what files are available, and the metadata, then the client will decide that it wants to use multiple connections. It will then request byte range 1-$((1024*1024*1024)) in the first connection, etc, then stitch them back together once they have all completed.

See the Range header description in RFC2616 for an example. Or here:


Rogan


--
You received this message because you are subscribed to the Google Groups "Netty discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netty+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netty/d499d1de-f373-43c4-a493-69f86de57a9c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

L_DisplayName

unread,
Sep 5, 2016, 4:03:23 PM9/5/16
to Netty discussions
Thank you Rogan for your response, I appreciate it. I am also interested in knowing (implementation/coding) wise, how I can access, write to and manage a group of channels outside of the handler class.  

Thank you!

Rogan Dawes

unread,
Sep 5, 2016, 5:19:00 PM9/5/16
to Netty discussions
Essentially, you would need to maintain a "register" of handlers, I guess, and at some point, instruct them to perform a write operation.

e.g. in your ChannelInitializer, don't simply do 

channel.pipeline().addLast("myHandler", new MyHandler());

, rather keep a reference to it in an accessible structure (e.g. List, Map, whatever). 

e.g.
Pass the (initially empty) list of handlers in to the constructor of your initializer, and save it as an instance variable, then in the initChannel() method:
MyHandler h = new MyHandler();
handlers.add(h);
channel.pipeline().addLast("myHandler", h);

Then you can look up the Handler when you want to instruct it to write something.

You'd want to add locking and concurrency protection to the list, of course, and make sure that the list of MyHandler also gets updated when a connection is closed. No point in keeping them hanging around, and it would likely stop garbage collection.

Rogan



--
You received this message because you are subscribed to the Google Groups "Netty discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netty+un...@googlegroups.com.

L_DisplayName

unread,
Sep 5, 2016, 5:41:41 PM9/5/16
to Netty discussions
Awesome, thank you Rogan!


On Monday, September 5, 2016 at 8:09:18 AM UTC-4, L_DisplayName wrote:

Rogan Dawes

unread,
Sep 5, 2016, 5:51:43 PM9/5/16
to ne...@googlegroups.com
Alternatively, depending on how you decide whether to trigger a write or not, let each Handler wait on a Promise, that will return when its data is ready to write:

In your handler, do:

final Promise<Channel> promise = ctx.executor().newPromise();

promise.addListener(new FutureListener<Channel>() {

public void operationComplete(final Future<Channel> future) throws Exception {

// ready to start doing something now

if (future.isSuccess()) {

// go get the data

} else {

// wait failed, clean up

}

});

MyDispatcher.registerDataRequest(promise, "some data");



--
You received this message because you are subscribed to the Google Groups "Netty discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netty+un...@googlegroups.com.

L_DisplayName

unread,
Sep 5, 2016, 7:40:18 PM9/5/16
to Netty discussions
Thank you Rogan! Great advice!


On Monday, September 5, 2016 at 8:09:18 AM UTC-4, L_DisplayName wrote:
Reply all
Reply to author
Forward
0 new messages