This repository has been archived by the owner on Oct 15, 2019. It is now read-only.
/
NiftyDispatcher.java
86 lines (76 loc) · 2.76 KB
/
NiftyDispatcher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.facebook.nifty.core;
import org.apache.thrift.TException;
import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.server.TConnectionContext;
import org.apache.thrift.transport.TTransport;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.concurrent.Executor;
/**
* Dispatch TNiftyTransport to the TProcessor and write output back.
*
*/
public class NiftyDispatcher extends SimpleChannelUpstreamHandler {
private static final Logger log = LoggerFactory.getLogger(NiftyDispatcher.class);
private final TProcessorFactory processorFactory;
private final TProtocolFactory inProtocolFactory;
private final TProtocolFactory outProtocolFactory;
private final Executor exe;
public NiftyDispatcher(ThriftServerDef def) {
this.processorFactory = def.getProcessorFactory();
this.inProtocolFactory = def.getInProtocolFactory();
this.outProtocolFactory = def.getOutProtocolFactory();
this.exe = def.getExecutor();
}
@Override
public void messageReceived(final ChannelHandlerContext ctx, final MessageEvent e)
throws Exception {
if (e.getMessage() instanceof TTransport) {
exe.execute(
new Runnable() {
@Override
public void run() {
TTransport t = (TTransport) e.getMessage();
TProtocol inProtocol = inProtocolFactory.getProtocol(t);
TProtocol outProtocol = outProtocolFactory.getProtocol(t);
try {
processorFactory.getProcessor(t).process(
inProtocol,
outProtocol,
new TConnectionContext(inProtocol, outProtocol) {
@Override
public InetAddress getPeerAddress() {
return ((InetSocketAddress) ctx.getChannel().getRemoteAddress()).getAddress();
}
}
);
} catch (TException e1) {
log.error("Exception while invoking!", e1);
closeChannel(ctx);
}
}
}
);
} else {
ctx.sendUpstream(e);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
// Any out of band exception are caught here and we tear down the socket
closeChannel(ctx);
}
private void closeChannel(ChannelHandlerContext ctx) {
if (ctx.getChannel().isOpen()) {
ctx.getChannel().close();
}
}
}