use of com.weibo.api.motan.rpc.Request in project motan by weibocom.
the class MotanV2Codec method decode.
/**
* decode data
*
* @return
* @throws IOException
*/
@Override
public Object decode(Channel channel, String remoteIp, byte[] data) throws IOException {
MotanV2Header header = MotanV2Header.buildHeader(data);
Map<String, String> metaMap = new HashMap<String, String>();
ByteBuffer buf = ByteBuffer.wrap(data);
int metaSize = buf.getInt(HEADER_SIZE);
int index = HEADER_SIZE + 4;
if (metaSize > 0) {
byte[] meta = new byte[metaSize];
buf.position(index);
buf.get(meta);
metaMap = decodeMeta(meta);
index += metaSize;
}
int bodySize = buf.getInt(index);
index += 4;
Object obj = null;
if (bodySize > 0) {
byte[] body = new byte[bodySize];
buf.position(index);
buf.get(body);
if (header.isGzip()) {
body = ByteUtil.unGzip(body);
}
// 默认自适应序列化
Serialization serialization = getSerializationByNum(header.getSerialize());
obj = new DeserializableObject(serialization, body);
}
if (header.isRequest()) {
if (header.isHeartbeat()) {
Request request = DefaultRpcHeartbeatFactory.getDefaultHeartbeatRequest(header.getRequestId());
request.setRpcProtocolVersion(RpcProtocolVersion.VERSION_2.getVersion());
return request;
} else {
DefaultRequest request = new DefaultRequest();
request.setRequestId(header.getRequestId());
request.setInterfaceName(metaMap.remove(M2_PATH));
request.setMethodName(metaMap.remove(M2_METHOD));
request.setParamtersDesc(metaMap.remove(M2_METHOD_DESC));
request.setAttachments(metaMap);
request.setRpcProtocolVersion(RpcProtocolVersion.VERSION_2.getVersion());
request.setSerializeNumber(header.getSerialize());
if (obj != null) {
request.setArguments(new Object[] { obj });
}
if (metaMap.get(M2_GROUP) != null) {
request.setAttachment(URLParamType.group.getName(), metaMap.get(M2_GROUP));
}
if (StringUtils.isNotBlank(metaMap.get(M2_VERSION))) {
request.setAttachment(URLParamType.version.getName(), metaMap.get(M2_VERSION));
}
if (StringUtils.isNotBlank(metaMap.get(M2_SOURCE))) {
request.setAttachment(URLParamType.application.getName(), metaMap.get(M2_SOURCE));
}
if (StringUtils.isNotBlank(metaMap.get(M2_MODULE))) {
request.setAttachment(URLParamType.module.getName(), metaMap.get(M2_MODULE));
}
return request;
}
} else {
if (header.isHeartbeat()) {
return DefaultRpcHeartbeatFactory.getDefaultHeartbeatResponse(header.getRequestId());
}
DefaultResponse response = new DefaultResponse();
response.setRequestId(header.getRequestId());
response.setProcessTime(MathUtil.parseLong(metaMap.remove(M2_PROCESS_TIME), 0));
response.setAttachments(metaMap);
if (header.getStatus() == MotanV2Header.MessageStatus.NORMAL.getStatus()) {
// 只解析正常消息
response.setValue(obj);
} else {
String errmsg = metaMap.remove(M2_ERROR);
Exception e = ExceptionUtil.fromMessage(errmsg);
if (e == null) {
e = (Exception) new MotanServiceException("default remote exception. remote errmsg:" + errmsg, false);
}
response.setException(e);
}
return response;
}
}
use of com.weibo.api.motan.rpc.Request in project motan by weibocom.
the class ClusterTest method testCall.
@Test
public void testCall() {
final Request request = mockery.mock(Request.class);
final Response rs = mockery.mock(Response.class);
mockery.checking(new Expectations() {
{
allowing(any(Referer.class)).method("getUrl").withNoArguments();
will(returnValue(new URL(MotanConstants.PROTOCOL_MOTAN, NetUtils.getLocalAddress().getHostAddress(), 18080, Object.class.getName())));
allowing(any(Referer.class)).method("isAvailable").withNoArguments();
will(returnValue(true));
allowing(any(Referer.class)).method("call").with(same(request));
will(returnValue(rs));
allowing(any(Request.class)).method("getRequestId").withNoArguments();
will(returnValue(0L));
allowing(request).setAttachment(with(any(String.class)), with(any(String.class)));
atLeast(0).of(request).setRetries(0);
will(returnValue(null));
atLeast(0).of(request).getRetries();
will(returnValue(0));
atLeast(0).of(request).getMethodName();
will(returnValue("get"));
atLeast(0).of(request).getParamtersDesc();
will(returnValue("void"));
}
});
Response callRs = cluster.call(request);
Assert.assertEquals(rs, callRs);
}
use of com.weibo.api.motan.rpc.Request in project motan by weibocom.
the class AccessLogFilterTest method testCall.
@SuppressWarnings("unchecked")
public void testCall() {
final Request request = mockery.mock(Request.class);
final Response response = mockery.mock(Response.class);
final URL url = new URL(MotanConstants.PROTOCOL_MOTAN, NetUtils.getLocalAddress().getHostAddress(), 0, RegistryService.class.getName());
url.addParameter(URLParamType.accessLog.getName(), String.valueOf(true));
final Caller<IHello> caller = mockery.mock(Caller.class);
final Map<String, String> attachments = new HashMap<String, String>();
attachments.put(URLParamType.host.getName(), URLParamType.host.getValue());
attachments.put(URLParamType.application.getName(), URLParamType.application.getValue());
attachments.put(URLParamType.module.getName(), URLParamType.module.getValue());
mockery.checking(new Expectations() {
{
atLeast(1).of(caller).getUrl();
will(returnValue(url));
exactly(1).of(caller).call(request);
will(returnValue(response));
exactly(1).of(request).getInterfaceName();
will(returnValue(IHello.class.getName()));
exactly(1).of(request).getMethodName();
will(returnValue("get"));
exactly(1).of(request).getParamtersDesc();
will(returnValue("param_desc"));
atLeast(1).of(request).getAttachments();
will(returnValue(attachments));
allowing(request).getRequestId();
}
});
accessLogFilter.filter(caller, request);
mockery.assertIsSatisfied();
}
use of com.weibo.api.motan.rpc.Request in project motan by weibocom.
the class SwitcherFilterTest method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
request = mockery.mock(Request.class);
response = mockery.mock(Response.class);
caller = mockery.mock(Caller.class);
url = new URL(MotanConstants.PROTOCOL_MOTAN, NetUtils.getLocalAddress().getHostAddress(), 0, RegistryService.class.getName());
attachments = new HashMap<String, String>();
attachments.put(URLParamType.host.getName(), URLParamType.host.getValue());
attachments.put(URLParamType.application.getName(), URLParamType.application.getValue());
attachments.put(URLParamType.module.getName(), URLParamType.module.getValue());
}
use of com.weibo.api.motan.rpc.Request in project motan by weibocom.
the class RandomLoadBalanceTest method testSelectToHolder.
public void testSelectToHolder() {
final Request request = mockery.mock(Request.class);
mockery.checking(new Expectations() {
{
atLeast(1).of(request).getArguments();
will(returnValue(new Object[] { 1, 2, 3 }));
atLeast(0).of(request).getParamtersDesc();
will(returnValue("void_"));
}
});
for (int i = 0; i < 10; i++) {
List<Referer<IHello>> refHolder = new ArrayList<Referer<IHello>>();
randomLoadBalance.selectToHolder(request, refHolder);
assertEquals(refHolder.size(), (referers.size() - falseCount));
}
}
Aggregations