RuneHive-Game
Loading...
Searching...
No Matches
FileServerHandler.java
Go to the documentation of this file.
1package dev.advo.fs.net;
2
3import java.util.logging.Level;
4import java.util.logging.Logger;
5
6import org.jboss.netty.channel.ChannelHandlerContext;
7import org.jboss.netty.channel.ExceptionEvent;
8import org.jboss.netty.channel.MessageEvent;
9import org.jboss.netty.handler.timeout.IdleStateAwareChannelUpstreamHandler;
10import org.jboss.netty.handler.timeout.IdleStateEvent;
11
12import dev.advo.fs.dispatch.RequestDispatcher;
13import dev.advo.fs.net.ondemand.OnDemandRequest;
14import dev.advo.fs.net.service.ServiceRequest;
15import dev.advo.fs.net.service.ServiceResponse;
16
17public final class FileServerHandler extends IdleStateAwareChannelUpstreamHandler {
18
19 private static final Logger logger = Logger.getLogger(FileServerHandler.class.getName());
20
21 @Override
22 public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) throws Exception {
23// System.out.println("Closing channel due to idle msg received.");
24// e.getChannel().close();
25 }
26
27 @Override
28 public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
29 Object msg = e.getMessage();
30 if (msg instanceof ServiceRequest) {
31 ServiceRequest request = (ServiceRequest) msg;
32 if (request.invalid()) {
33 // System.out.println("Closing channel due to incorrect msg received. ");
34 // e.getChannel().close();
35 } else {
36 e.getChannel().write(new ServiceResponse());
37 }
38 } else if (msg instanceof OnDemandRequest) {
39 RequestDispatcher.dispatch(e.getChannel(), (OnDemandRequest) msg);
40 } else {
41 throw new Exception("unknown message type");
42 }
43 }
44
45 @Override
46 public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
47// logger.log(Level.SEVERE, "Exception occured, closing channel...", e.getCause());
48// System.out.println("Closing channel due to incorrect msg received.");
49// e.getChannel().close();
50 }
51
52}
A class which dispatches requests to worker threads.
static void dispatch(Channel channel, OnDemandRequest request)
Dispatches an 'on-demand' request.
void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e)