Search in sources :

Example 6 with Request

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;
    }
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) HashMap(java.util.HashMap) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Request(com.weibo.api.motan.rpc.Request) ByteBuffer(java.nio.ByteBuffer) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) MotanServiceException(com.weibo.api.motan.exception.MotanServiceException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Serialization(com.weibo.api.motan.codec.Serialization) DeserializableObject(com.weibo.api.motan.serialize.DeserializableObject) DeserializableObject(com.weibo.api.motan.serialize.DeserializableObject)

Example 7 with Request

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);
}
Also used : Response(com.weibo.api.motan.rpc.Response) Expectations(org.jmock.Expectations) Request(com.weibo.api.motan.rpc.Request) URL(com.weibo.api.motan.rpc.URL) Test(org.junit.Test)

Example 8 with Request

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();
}
Also used : Response(com.weibo.api.motan.rpc.Response) Expectations(org.jmock.Expectations) HashMap(java.util.HashMap) Request(com.weibo.api.motan.rpc.Request) RegistryService(com.weibo.api.motan.registry.RegistryService) URL(com.weibo.api.motan.rpc.URL) IHello(com.weibo.api.motan.protocol.example.IHello)

Example 9 with Request

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());
}
Also used : Response(com.weibo.api.motan.rpc.Response) Caller(com.weibo.api.motan.rpc.Caller) Request(com.weibo.api.motan.rpc.Request) URL(com.weibo.api.motan.rpc.URL)

Example 10 with Request

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));
    }
}
Also used : Expectations(org.jmock.Expectations) Request(com.weibo.api.motan.rpc.Request) Referer(com.weibo.api.motan.rpc.Referer) ArrayList(java.util.ArrayList) IHello(com.weibo.api.motan.protocol.example.IHello)

Aggregations

Request (com.weibo.api.motan.rpc.Request)31 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)12 Expectations (org.jmock.Expectations)12 Response (com.weibo.api.motan.rpc.Response)10 IHello (com.weibo.api.motan.protocol.example.IHello)9 URL (com.weibo.api.motan.rpc.URL)9 Test (org.junit.Test)9 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)7 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)6 Referer (com.weibo.api.motan.rpc.Referer)5 HashMap (java.util.HashMap)5 RegistryService (com.weibo.api.motan.registry.RegistryService)4 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)3 IWorld (com.weibo.api.motan.protocol.example.IWorld)3 Channel (com.weibo.api.motan.transport.Channel)3 MessageHandler (com.weibo.api.motan.transport.MessageHandler)3 YarRequest (com.weibo.yar.YarRequest)3 ArrayList (java.util.ArrayList)3 Serialization (com.weibo.api.motan.codec.Serialization)2 IOException (java.io.IOException)2