use of com.alibaba.otter.canal.protocol.AdminPacket.Ack in project canal by alibaba.
the class SimpleAdminConnector method doServerAdmin.
// ==================== helper method ====================
private String doServerAdmin(String action) {
try {
writeWithHeader(Packet.newBuilder().setType(PacketType.SERVER).setBody(ServerAdmin.newBuilder().setAction(action).build().toByteString()).build().toByteArray());
Packet p = Packet.parseFrom(readNextPacket());
Ack ack = Ack.parseFrom(p.getBody());
if (ack.getCode() > 0) {
throw new ServiceException("failed to subscribe with reason: " + ack.getMessage());
}
return ack.getMessage();
} catch (IOException e) {
throw new ServiceException(e);
}
}
use of com.alibaba.otter.canal.protocol.AdminPacket.Ack in project canal by alibaba.
the class SimpleAdminConnector method connect.
@Override
public void connect() throws ServiceException {
try {
if (connected) {
return;
}
channel = SocketChannel.open();
channel.socket().setSoTimeout(soTimeout);
channel.connect(address);
readableChannel = Channels.newChannel(channel.socket().getInputStream());
writableChannel = Channels.newChannel(channel.socket().getOutputStream());
Packet p = Packet.parseFrom(readNextPacket());
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());
// seed for auth
ByteString seed = handshake.getSeeds();
String newPasswd = passwd;
if (passwd != null) {
// encode passwd
newPasswd = SecurityUtil.byte2HexStr(SecurityUtil.scramble411(passwd.getBytes(), seed.toByteArray()));
}
ClientAuth ca = ClientAuth.newBuilder().setUsername(user != null ? user : "").setPassword(ByteString.copyFromUtf8(newPasswd != null ? newPasswd : "")).setNetReadTimeout(idleTimeout).setNetWriteTimeout(idleTimeout).build();
writeWithHeader(Packet.newBuilder().setType(PacketType.CLIENTAUTHENTICATION).setBody(ca.toByteString()).build().toByteArray());
//
Packet ack = Packet.parseFrom(readNextPacket());
if (ack.getType() != PacketType.ACK) {
throw new CanalClientException("unexpected packet type when ack is expected");
}
Ack ackBody = Ack.parseFrom(ack.getBody());
if (ackBody.getCode() > 0) {
throw new ServiceException("something goes wrong when doing authentication: " + ackBody.getMessage());
}
connected = true;
} catch (IOException | NoSuchAlgorithmException e) {
throw new ServiceException(e);
}
}
use of com.alibaba.otter.canal.protocol.AdminPacket.Ack in project canal by alibaba.
the class SimpleAdminConnector method doLogAdmin.
private String doLogAdmin(String type, String action, String destination, String file, int count) {
try {
writeWithHeader(Packet.newBuilder().setType(PacketType.LOG).setBody(LogAdmin.newBuilder().setType(type).setAction(action).setDestination(destination == null ? "" : destination).setFile(file == null ? "" : file).setCount(count).build().toByteString()).build().toByteArray());
Packet p = Packet.parseFrom(readNextPacket());
Ack ack = Ack.parseFrom(p.getBody());
if (ack.getCode() > 0) {
throw new ServiceException("failed to subscribe with reason: " + ack.getMessage());
}
return ack.getMessage();
} catch (IOException e) {
throw new ServiceException(e);
}
}
use of com.alibaba.otter.canal.protocol.AdminPacket.Ack in project canal by alibaba.
the class SimpleAdminConnector method doInstanceAdmin.
private String doInstanceAdmin(String destination, String action) {
try {
writeWithHeader(Packet.newBuilder().setType(PacketType.INSTANCE).setBody(InstanceAdmin.newBuilder().setDestination(destination).setAction(action).build().toByteString()).build().toByteArray());
Packet p = Packet.parseFrom(readNextPacket());
Ack ack = Ack.parseFrom(p.getBody());
if (ack.getCode() > 0) {
throw new ServiceException("failed to subscribe with reason: " + ack.getMessage());
}
return ack.getMessage();
} catch (IOException e) {
throw new ServiceException(e);
}
}
Aggregations