Search in sources :

Example 1 with LoginRequest

use of io.servicecomb.transport.highway.message.LoginRequest in project java-chassis by ServiceComb.

the class TestHighwayClient method testCreateLogin.

@Test
public void testCreateLogin() throws Exception {
    HighwayClient client = new HighwayClient(false);
    TcpOutputStream os = client.createLogin();
    ByteBuf buf = os.getBuffer().getByteBuf();
    byte[] magic = new byte[TcpParser.TCP_MAGIC.length];
    buf.readBytes(magic);
    Assert.assertArrayEquals(TcpParser.TCP_MAGIC, magic);
    Assert.assertEquals(os.getMsgId(), buf.readLong());
    int start = TcpParser.TCP_HEADER_LENGTH;
    int totalLen = buf.readInt();
    int headerLen = buf.readInt();
    Buffer headerBuffer = os.getBuffer().slice(start, start + headerLen);
    int end = start + totalLen;
    start += headerLen;
    Buffer bodyBuffer = os.getBuffer().slice(start, end);
    RequestHeader header = RequestHeader.readObject(headerBuffer);
    Assert.assertEquals(MsgType.LOGIN, header.getMsgType());
    LoginRequest login = LoginRequest.readObject(bodyBuffer);
    Assert.assertEquals(HighwayTransport.NAME, login.getProtocol());
}
Also used : Buffer(io.vertx.core.buffer.Buffer) RequestHeader(io.servicecomb.transport.highway.message.RequestHeader) LoginRequest(io.servicecomb.transport.highway.message.LoginRequest) ByteBuf(io.netty.buffer.ByteBuf) TcpOutputStream(io.servicecomb.foundation.vertx.tcp.TcpOutputStream) Endpoint(io.servicecomb.core.Endpoint) Test(org.junit.Test)

Example 2 with LoginRequest

use of io.servicecomb.transport.highway.message.LoginRequest in project java-chassis by ServiceComb.

the class TestHighwayServerConnection method testSetParameterError.

@Test
public void testSetParameterError() throws Exception {
    header.setMsgType(MsgType.LOGIN);
    Buffer headerBuffer = createBuffer(requestHeaderSchema, header);
    LoginRequest body = new LoginRequest();
    body.setProtocol("p");
    body.setZipName("z");
    Buffer bodyBuffer = createBuffer(setParameterRequestSchema, body);
    bodyBuffer.setByte(0, (byte) 100);
    connection.handle(0, headerBuffer, bodyBuffer);
    Assert.assertEquals(null, connection.getProtocol());
    Assert.assertEquals(null, connection.getZipName());
}
Also used : LinkedBuffer(io.protostuff.LinkedBuffer) Buffer(io.vertx.core.buffer.Buffer) LoginRequest(io.servicecomb.transport.highway.message.LoginRequest) Test(org.junit.Test)

Example 3 with LoginRequest

use of io.servicecomb.transport.highway.message.LoginRequest in project java-chassis by ServiceComb.

the class HighwayServerConnection method onLogin.

protected void onLogin(long msgId, RequestHeader header, Buffer bodyBuffer) {
    LoginRequest request = null;
    try {
        request = LoginRequest.readObject(bodyBuffer);
    } catch (Exception e) {
        String msg = String.format("decode setParameter error, msgId=%d", msgId);
        LOGGER.error(msg, e);
        netSocket.close();
        return;
    }
    if (request != null) {
        this.setProtocol(request.getProtocol());
        this.setZipName(request.getZipName());
    }
    try (HighwayOutputStream os = new HighwayOutputStream(msgId)) {
        ResponseHeader responseHeader = new ResponseHeader();
        responseHeader.setStatusCode(Status.OK.getStatusCode());
        LoginResponse response = new LoginResponse();
        os.write(ResponseHeader.getResponseHeaderSchema(), responseHeader, LoginResponse.getLoginResponseSchema(), response);
        netSocket.write(os.getBuffer());
    } catch (Exception e) {
        throw new Error("impossible.", e);
    }
}
Also used : ResponseHeader(io.servicecomb.transport.highway.message.ResponseHeader) LoginResponse(io.servicecomb.transport.highway.message.LoginResponse) LoginRequest(io.servicecomb.transport.highway.message.LoginRequest)

Example 4 with LoginRequest

use of io.servicecomb.transport.highway.message.LoginRequest in project java-chassis by ServiceComb.

the class HighwayClient method createLogin.

@Override
public TcpOutputStream createLogin() {
    try {
        LinkedBuffer linkedBuffer = LinkedBuffer.allocate();
        ProtobufOutput output = new ProtobufOutput(linkedBuffer);
        RequestHeader header = new RequestHeader();
        header.setMsgType(MsgType.LOGIN);
        header.writeObject(output);
        LoginRequest login = new LoginRequest();
        login.setProtocol(HighwayTransport.NAME);
        login.writeObject(output);
        HighwayOutputStream os = new HighwayOutputStream();
        os.write(header, LoginRequest.getLoginRequestSchema(), login);
        return os;
    } catch (Throwable e) {
        throw new Error("impossible.", e);
    }
}
Also used : LinkedBuffer(io.protostuff.LinkedBuffer) ProtobufOutput(io.protostuff.ProtobufOutput) RequestHeader(io.servicecomb.transport.highway.message.RequestHeader) LoginRequest(io.servicecomb.transport.highway.message.LoginRequest)

Example 5 with LoginRequest

use of io.servicecomb.transport.highway.message.LoginRequest in project java-chassis by ServiceComb.

the class TestHighwayServerConnection method testSetParameterNormal.

@Test
public void testSetParameterNormal() throws Exception {
    header.setMsgType(MsgType.LOGIN);
    Buffer headerBuffer = createBuffer(requestHeaderSchema, header);
    LoginRequest body = new LoginRequest();
    body.setProtocol("p");
    body.setZipName("z");
    Buffer bodyBuffer = createBuffer(setParameterRequestSchema, body);
    connection.handle(0, headerBuffer, bodyBuffer);
    Assert.assertEquals("p", connection.getProtocol());
    Assert.assertEquals("z", connection.getZipName());
}
Also used : LinkedBuffer(io.protostuff.LinkedBuffer) Buffer(io.vertx.core.buffer.Buffer) LoginRequest(io.servicecomb.transport.highway.message.LoginRequest) Test(org.junit.Test)

Aggregations

LoginRequest (io.servicecomb.transport.highway.message.LoginRequest)5 LinkedBuffer (io.protostuff.LinkedBuffer)3 Buffer (io.vertx.core.buffer.Buffer)3 Test (org.junit.Test)3 RequestHeader (io.servicecomb.transport.highway.message.RequestHeader)2 ByteBuf (io.netty.buffer.ByteBuf)1 ProtobufOutput (io.protostuff.ProtobufOutput)1 Endpoint (io.servicecomb.core.Endpoint)1 TcpOutputStream (io.servicecomb.foundation.vertx.tcp.TcpOutputStream)1 LoginResponse (io.servicecomb.transport.highway.message.LoginResponse)1 ResponseHeader (io.servicecomb.transport.highway.message.ResponseHeader)1