use of org.apache.dubbo.remoting.exchange.Request in project dubbo by alibaba.
the class DeprecatedExchangeCodec method decodeBody.
protected Object decodeBody(Channel channel, InputStream is, byte[] header) throws IOException {
byte flag = header[2], proto = (byte) (flag & SERIALIZATION_MASK);
// get request id.
long id = Bytes.bytes2long(header, 4);
if ((flag & FLAG_REQUEST) == 0) {
// decode response.
Response res = new Response(id);
if ((flag & FLAG_EVENT) != 0) {
res.setEvent(true);
}
// get status.
byte status = header[3];
res.setStatus(status);
try {
ObjectInput in = CodecSupport.deserialize(channel.getUrl(), is, proto);
if (status == Response.OK) {
Object data;
if (res.isHeartbeat()) {
data = decodeHeartbeatData(channel, in);
} else if (res.isEvent()) {
data = decodeEventData(channel, in);
} else {
data = decodeResponseData(channel, in, getRequestData(id));
}
res.setResult(data);
} else {
res.setErrorMessage(in.readUTF());
}
} catch (Throwable t) {
res.setStatus(Response.CLIENT_ERROR);
res.setErrorMessage(StringUtils.toString(t));
}
return res;
} else {
// decode request.
Request req = new Request(id);
req.setVersion(Version.getProtocolVersion());
req.setTwoWay((flag & FLAG_TWOWAY) != 0);
if ((flag & FLAG_EVENT) != 0) {
req.setEvent(true);
}
try {
ObjectInput in = CodecSupport.deserialize(channel.getUrl(), is, proto);
Object data;
if (req.isHeartbeat()) {
data = decodeHeartbeatData(channel, in);
} else if (req.isEvent()) {
data = decodeEventData(channel, in);
} else {
data = decodeRequestData(channel, in);
}
req.setData(data);
} catch (Throwable t) {
// bad request
req.setBroken(true);
req.setData(t);
}
return req;
}
}
use of org.apache.dubbo.remoting.exchange.Request in project dubbo by alibaba.
the class PayloadDropperTest method test.
@Test
public void test() {
Request request = new Request(1);
request.setData(new Object());
Request requestWithoutData = (Request) PayloadDropper.getRequestWithoutData(request);
Assertions.assertEquals(requestWithoutData.getId(), request.getId());
Assertions.assertNull(requestWithoutData.getData());
Response response = new Response(1);
response.setResult(new Object());
Response responseWithoutData = (Response) PayloadDropper.getRequestWithoutData(response);
Assertions.assertEquals(responseWithoutData.getId(), response.getId());
Assertions.assertNull(responseWithoutData.getResult());
Object object = new Object();
Assertions.assertEquals(object, PayloadDropper.getRequestWithoutData(object));
}
use of org.apache.dubbo.remoting.exchange.Request in project dubbo by alibaba.
the class ConnectChannelHandlerTest method test_Received_Event_invoke_direct.
/**
* Events do not pass through the thread pool and execute directly on the IO
*/
@SuppressWarnings("deprecation")
@Disabled("Heartbeat is processed in HeartbeatHandler not WrappedChannelHandler.")
@Test
public void test_Received_Event_invoke_direct() throws RemotingException {
handler = new ConnectionOrderedChannelHandler(new BizChannelHander(false), url);
ThreadPoolExecutor executor = (ThreadPoolExecutor) getField(handler, "SHARED_EXECUTOR", 1);
executor.shutdown();
executor = (ThreadPoolExecutor) getField(handler, "executor", 1);
executor.shutdown();
Request req = new Request();
req.setHeartbeat(true);
final AtomicInteger count = new AtomicInteger(0);
handler.received(new MockedChannel() {
@Override
public void send(Object message) throws RemotingException {
Assertions.assertTrue(((Response) message).isHeartbeat(), "response.heartbeat");
count.incrementAndGet();
}
}, req);
Assertions.assertEquals(1, count.get(), "channel.send must be invoke");
}
use of org.apache.dubbo.remoting.exchange.Request in project dubbo by alibaba.
the class ExchangeCodecTest method testInvalidSerializaitonId.
@Test
public void testInvalidSerializaitonId() throws Exception {
byte[] header = new byte[] { MAGIC_HIGH, MAGIC_LOW, (byte) 0x8F, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Object obj = decode(header);
Assertions.assertTrue(obj instanceof Request);
Request request = (Request) obj;
Assertions.assertTrue(request.isBroken());
Assertions.assertTrue(request.getData() instanceof IOException);
header = new byte[] { MAGIC_HIGH, MAGIC_LOW, (byte) 0x1F, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
obj = decode(header);
Assertions.assertTrue(obj instanceof Response);
Response response = (Response) obj;
Assertions.assertEquals(response.getStatus(), Response.CLIENT_ERROR);
Assertions.assertTrue(response.getErrorMessage().contains("IOException"));
}
use of org.apache.dubbo.remoting.exchange.Request in project dubbo by alibaba.
the class ExchangeCodecTest method test_Decode_Error_Request_Object.
@Test
public void test_Decode_Error_Request_Object() throws IOException {
// 00000010-response/oneway/hearbeat=true |20-stats=ok|id=0|length=0
byte[] header = new byte[] { MAGIC_HIGH, MAGIC_LOW, (byte) 0xe2, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Person person = new Person();
byte[] request = getRequestBytes(person, header);
// bad object
byte[] badbytes = new byte[] { -1, -2, -3, -4, -3, -4, -3, -4, -3, -4, -3, -4 };
System.arraycopy(badbytes, 0, request, 21, badbytes.length);
Request obj = (Request) decode(request);
Assertions.assertTrue(obj.isBroken());
Assertions.assertTrue(obj.getData() instanceof Throwable);
}
Aggregations