1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.giraph.comm; |
20 | |
|
21 | |
import org.apache.hadoop.io.Writable; |
22 | |
import org.apache.hadoop.io.WritableComparable; |
23 | |
import org.apache.log4j.Logger; |
24 | |
import org.jboss.netty.buffer.ChannelBuffer; |
25 | |
import org.jboss.netty.buffer.ChannelBuffers; |
26 | |
import org.jboss.netty.channel.ChannelHandlerContext; |
27 | |
import org.jboss.netty.channel.ChannelStateEvent; |
28 | |
import org.jboss.netty.channel.ExceptionEvent; |
29 | |
import org.jboss.netty.channel.MessageEvent; |
30 | |
import org.jboss.netty.channel.SimpleChannelUpstreamHandler; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
@SuppressWarnings("rawtypes") |
41 | |
public class RequestServerHandler<I extends WritableComparable, |
42 | |
V extends Writable, E extends Writable, |
43 | |
M extends Writable> extends SimpleChannelUpstreamHandler { |
44 | |
|
45 | 1 | private static final Logger LOG = |
46 | |
Logger.getLogger(RequestServerHandler.class); |
47 | |
|
48 | |
private final ServerData<I, V, E, M> serverData; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | 11 | public RequestServerHandler(ServerData<I, V, E, M> serverData) { |
56 | 11 | this.serverData = serverData; |
57 | 11 | } |
58 | |
|
59 | |
@Override |
60 | |
public void messageReceived( |
61 | |
ChannelHandlerContext ctx, MessageEvent e) { |
62 | 3 | if (LOG.isDebugEnabled()) { |
63 | 0 | LOG.debug("messageReceived: Got " + e.getMessage().getClass()); |
64 | |
} |
65 | |
@SuppressWarnings("unchecked") |
66 | 3 | WritableRequest<I, V, E, M> writableRequest = |
67 | |
(WritableRequest<I, V, E, M>) e.getMessage(); |
68 | 3 | writableRequest.doRequest(serverData); |
69 | |
|
70 | |
|
71 | 3 | ChannelBuffer buffer = ChannelBuffers.directBuffer(9); |
72 | 3 | buffer.writeLong(writableRequest.getRequestId()); |
73 | 3 | buffer.writeByte(0); |
74 | 3 | e.getChannel().write(buffer); |
75 | 3 | } |
76 | |
|
77 | |
@Override |
78 | |
public void channelConnected(ChannelHandlerContext ctx, |
79 | |
ChannelStateEvent e) throws Exception { |
80 | 11 | if (LOG.isDebugEnabled()) { |
81 | 0 | LOG.debug("channelConnected: Connected the channel on " + |
82 | |
ctx.getChannel().getRemoteAddress()); |
83 | |
} |
84 | 11 | } |
85 | |
|
86 | |
@Override |
87 | |
public void channelClosed(ChannelHandlerContext ctx, |
88 | |
ChannelStateEvent e) throws Exception { |
89 | 11 | if (LOG.isDebugEnabled()) { |
90 | 0 | LOG.debug("channelClosed: Closed the channel on " + |
91 | |
ctx.getChannel().getRemoteAddress()); |
92 | |
} |
93 | 11 | } |
94 | |
|
95 | |
@Override |
96 | |
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) { |
97 | 0 | throw new IllegalStateException("exceptionCaught: Channel failed with " + |
98 | |
"remote address " + ctx.getChannel().getRemoteAddress(), e.getCause()); |
99 | |
} |
100 | |
} |