private class ServerHandler extends SimpleChannelUpstreamHandler {
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
throws Exception {
final RpcRequest request = (RpcRequest) e.getMessage();
String methodName = request.getMethodName();
MethodDescriptor methodDescriptor =
service.getDescriptorForType().findMethodByName(methodName);
if (methodDescriptor == null) {
throw new RemoteCallException(request.getId(),
new NoSuchMethodException(methodName));
}
Message paramProto = null;
if (request.hasRequestMessage()) {
try {
paramProto = service.getRequestPrototype(methodDescriptor)
.newBuilderForType().mergeFrom(request.getRequestMessage()).
build();
} catch (Throwable t) {
throw new RemoteCallException(request.getId(), methodDescriptor, t);
}
}
Message returnValue;
RpcController controller = new NettyRpcController();
try {
returnValue = service.callBlockingMethod(methodDescriptor,
controller, paramProto);
} catch (Throwable t) {
throw new RemoteCallException(request.getId(), methodDescriptor, t);
}
RpcResponse.Builder builder =
RpcResponse.newBuilder().setId(request.getId());
if (returnValue != null) {
builder.setResponseMessage(returnValue.toByteString());
}