Search in sources :

Example 1 with Ack

use of com.alibaba.otter.canal.protocol.CanalPacket.Ack in project canal by alibaba.

the class SimpleCanalConnector method unsubscribe.

public void unsubscribe() throws CanalClientException {
    waitClientRunning();
    try {
        writeWithHeader(channel, Packet.newBuilder().setType(PacketType.UNSUBSCRIPTION).setBody(Unsub.newBuilder().setDestination(clientIdentity.getDestination()).setClientId(String.valueOf(clientIdentity.getClientId())).build().toByteString()).build().toByteArray());
        //
        Packet p = Packet.parseFrom(readNextPacket(channel));
        Ack ack = Ack.parseFrom(p.getBody());
        if (ack.getErrorCode() > 0) {
            throw new CanalClientException("failed to unSubscribe with reason: " + ack.getErrorMessage());
        }
    } catch (IOException e) {
        throw new CanalClientException(e);
    }
}
Also used : Packet(com.alibaba.otter.canal.protocol.CanalPacket.Packet) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) Ack(com.alibaba.otter.canal.protocol.CanalPacket.Ack) ClientAck(com.alibaba.otter.canal.protocol.CanalPacket.ClientAck) IOException(java.io.IOException)

Example 2 with Ack

use of com.alibaba.otter.canal.protocol.CanalPacket.Ack in project canal by alibaba.

the class CanalServerWithNettyTest method testAuth.

@Test
public void testAuth() {
    try {
        SocketChannel channel = SocketChannel.open();
        channel.connect(new InetSocketAddress("127.0.0.1", 1088));
        Packet p = Packet.parseFrom(readNextPacket(channel));
        if (p.getVersion() != 1) {
            throw new Exception("unsupported version at this client.");
        }
        if (p.getType() != PacketType.HANDSHAKE) {
            throw new Exception("expect handshake but found other type.");
        }
        //
        Handshake handshake = Handshake.parseFrom(p.getBody());
        System.out.println(handshake.getSupportedCompressionsList());
        //
        ClientAuth ca = ClientAuth.newBuilder().setUsername("").setNetReadTimeout(10000).setNetWriteTimeout(10000).build();
        writeWithHeader(channel, Packet.newBuilder().setType(PacketType.CLIENTAUTHENTICATION).setBody(ca.toByteString()).build().toByteArray());
        //
        p = Packet.parseFrom(readNextPacket(channel));
        if (p.getType() != PacketType.ACK) {
            throw new Exception("unexpected packet type when ack is expected");
        }
        Ack ack = Ack.parseFrom(p.getBody());
        if (ack.getErrorCode() > 0) {
            throw new Exception("something goes wrong when doing authentication: " + ack.getErrorMessage());
        }
        writeWithHeader(channel, Packet.newBuilder().setType(PacketType.SUBSCRIPTION).setBody(Sub.newBuilder().setDestination(DESTINATION).setClientId("1").build().toByteString()).build().toByteArray());
        //
        p = Packet.parseFrom(readNextPacket(channel));
        ack = Ack.parseFrom(p.getBody());
        if (ack.getErrorCode() > 0) {
            throw new Exception("failed to subscribe with reason: " + ack.getErrorMessage());
        }
        for (int i = 0; i < 10; i++) {
            writeWithHeader(channel, Packet.newBuilder().setType(PacketType.GET).setBody(Get.newBuilder().setDestination(DESTINATION).setClientId("1").setFetchSize(10).build().toByteString()).build().toByteArray());
            p = Packet.parseFrom(readNextPacket(channel));
            long batchId = -1L;
            switch(p.getType()) {
                case MESSAGES:
                    {
                        Messages messages = Messages.parseFrom(p.getBody());
                        batchId = messages.getBatchId();
                        break;
                    }
                case ACK:
                    {
                        ack = Ack.parseFrom(p.getBody());
                        if (ack.getErrorCode() > 0) {
                            throw new Exception("failed to subscribe with reason: " + ack.getErrorMessage());
                        }
                        break;
                    }
                default:
                    {
                        throw new Exception("unexpected packet type: " + p.getType());
                    }
            }
            System.out.println("!!!!!!!!!!!!!!!!! " + batchId);
            Thread.sleep(1000L);
            writeWithHeader(channel, Packet.newBuilder().setType(PacketType.CLIENTACK).setBody(ClientAck.newBuilder().setDestination(DESTINATION).setClientId("1").setBatchId(batchId).build().toByteString()).build().toByteArray());
        }
        writeWithHeader(channel, Packet.newBuilder().setType(PacketType.CLIENTROLLBACK).setBody(ClientRollback.newBuilder().setDestination(DESTINATION).setClientId("1").build().toByteString()).build().toByteArray());
        writeWithHeader(channel, Packet.newBuilder().setType(PacketType.UNSUBSCRIPTION).setBody(Unsub.newBuilder().setDestination(DESTINATION).setClientId("1").build().toByteString()).build().toByteArray());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : SocketChannel(java.nio.channels.SocketChannel) Packet(com.alibaba.otter.canal.protocol.CanalPacket.Packet) Messages(com.alibaba.otter.canal.protocol.CanalPacket.Messages) InetSocketAddress(java.net.InetSocketAddress) Ack(com.alibaba.otter.canal.protocol.CanalPacket.Ack) ClientAck(com.alibaba.otter.canal.protocol.CanalPacket.ClientAck) ClientAuth(com.alibaba.otter.canal.protocol.CanalPacket.ClientAuth) IOException(java.io.IOException) Handshake(com.alibaba.otter.canal.protocol.CanalPacket.Handshake) Test(org.junit.Test)

Example 3 with Ack

use of com.alibaba.otter.canal.protocol.CanalPacket.Ack in project canal by alibaba.

the class SimpleCanalConnector method subscribe.

public void subscribe(String filter) throws CanalClientException {
    waitClientRunning();
    try {
        writeWithHeader(channel, Packet.newBuilder().setType(PacketType.SUBSCRIPTION).setBody(Sub.newBuilder().setDestination(clientIdentity.getDestination()).setClientId(String.valueOf(clientIdentity.getClientId())).setFilter(filter != null ? filter : "").build().toByteString()).build().toByteArray());
        //
        Packet p = Packet.parseFrom(readNextPacket(channel));
        Ack ack = Ack.parseFrom(p.getBody());
        if (ack.getErrorCode() > 0) {
            throw new CanalClientException("failed to subscribe with reason: " + ack.getErrorMessage());
        }
        clientIdentity.setFilter(filter);
    } catch (IOException e) {
        throw new CanalClientException(e);
    }
}
Also used : Packet(com.alibaba.otter.canal.protocol.CanalPacket.Packet) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) Ack(com.alibaba.otter.canal.protocol.CanalPacket.Ack) ClientAck(com.alibaba.otter.canal.protocol.CanalPacket.ClientAck) IOException(java.io.IOException)

Example 4 with Ack

use of com.alibaba.otter.canal.protocol.CanalPacket.Ack in project canal by alibaba.

the class SimpleCanalConnector method doConnect.

private InetSocketAddress doConnect() throws CanalClientException {
    try {
        channel = SocketChannel.open();
        channel.socket().setSoTimeout(soTimeout);
        SocketAddress address = getAddress();
        if (address == null) {
            address = getNextAddress();
        }
        channel.connect(address);
        Packet p = Packet.parseFrom(readNextPacket(channel));
        if (p.getVersion() != 1) {
            throw new CanalClientException("unsupported version at this client.");
        }
        if (p.getType() != PacketType.HANDSHAKE) {
            throw new CanalClientException("expect handshake but found other type.");
        }
        //
        Handshake handshake = Handshake.parseFrom(p.getBody());
        supportedCompressions.addAll(handshake.getSupportedCompressionsList());
        //
        ClientAuth ca = ClientAuth.newBuilder().setUsername(username != null ? username : "").setNetReadTimeout(soTimeout).setNetWriteTimeout(soTimeout).build();
        writeWithHeader(channel, Packet.newBuilder().setType(PacketType.CLIENTAUTHENTICATION).setBody(ca.toByteString()).build().toByteArray());
        //
        Packet ack = Packet.parseFrom(readNextPacket(channel));
        if (ack.getType() != PacketType.ACK) {
            throw new CanalClientException("unexpected packet type when ack is expected");
        }
        Ack ackBody = Ack.parseFrom(ack.getBody());
        if (ackBody.getErrorCode() > 0) {
            throw new CanalClientException("something goes wrong when doing authentication: " + ackBody.getErrorMessage());
        }
        connected = true;
        return new InetSocketAddress(channel.socket().getLocalAddress(), channel.socket().getLocalPort());
    } catch (IOException e) {
        throw new CanalClientException(e);
    }
}
Also used : Packet(com.alibaba.otter.canal.protocol.CanalPacket.Packet) CanalClientException(com.alibaba.otter.canal.protocol.exception.CanalClientException) InetSocketAddress(java.net.InetSocketAddress) Ack(com.alibaba.otter.canal.protocol.CanalPacket.Ack) ClientAck(com.alibaba.otter.canal.protocol.CanalPacket.ClientAck) IOException(java.io.IOException) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ClientAuth(com.alibaba.otter.canal.protocol.CanalPacket.ClientAuth) Handshake(com.alibaba.otter.canal.protocol.CanalPacket.Handshake)

Aggregations

Ack (com.alibaba.otter.canal.protocol.CanalPacket.Ack)4 ClientAck (com.alibaba.otter.canal.protocol.CanalPacket.ClientAck)4 Packet (com.alibaba.otter.canal.protocol.CanalPacket.Packet)4 IOException (java.io.IOException)4 CanalClientException (com.alibaba.otter.canal.protocol.exception.CanalClientException)3 ClientAuth (com.alibaba.otter.canal.protocol.CanalPacket.ClientAuth)2 Handshake (com.alibaba.otter.canal.protocol.CanalPacket.Handshake)2 InetSocketAddress (java.net.InetSocketAddress)2 Messages (com.alibaba.otter.canal.protocol.CanalPacket.Messages)1 SocketAddress (java.net.SocketAddress)1 SocketChannel (java.nio.channels.SocketChannel)1 Test (org.junit.Test)1