did/did-sdk/src/main/java/io/github/ehlxr/did/client/SdkClient.java

107 lines
3.3 KiB
Java
Raw Normal View History

2021-01-22 07:40:02 +00:00
package io.github.ehlxr.did.client;
2018-08-14 07:21:56 +00:00
2021-01-22 07:40:02 +00:00
import io.github.ehlxr.did.client.handler.SdkClientDecoder;
import io.github.ehlxr.did.client.handler.SdkClientEncoder;
import io.github.ehlxr.did.client.handler.SdkClientHandler;
import io.github.ehlxr.did.common.Constants;
2021-01-20 07:25:58 +00:00
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
2018-08-14 07:21:56 +00:00
import io.netty.channel.socket.SocketChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.InetSocketAddress;
2021-01-27 08:04:41 +00:00
import java.util.Objects;
2018-08-14 07:21:56 +00:00
/**
* @author ehlxr
*/
public class SdkClient extends AbstractClient {
2021-01-19 06:45:49 +00:00
private final Logger logger = LoggerFactory.getLogger(SdkClient.class);
2018-08-14 07:21:56 +00:00
public SdkClient(String host, int port) {
2021-01-27 08:04:41 +00:00
this(host, port, 0);
2021-01-20 07:25:58 +00:00
}
public SdkClient(String host, int port, int timeoutMillis) {
2021-01-27 08:04:41 +00:00
this.host = Objects.isNull(host) ?
"".equals(Constants.getEnv("SDK_HOST")) ? Constants.SERVER_HOST : Constants.getEnv("HTTP_PORT") :
host;
this.port = port <= 0 ?
"".equals(Constants.getEnv("SDK_PORT")) ? Constants.SDK_PORT : Integer.parseInt(Constants.getEnv("SDK_PORT")) :
port;
this.timeoutMillis = timeoutMillis <= 0 ? Constants.SDK_CLIENT_TIMEOUTMILLIS : timeoutMillis;
2018-08-14 07:21:56 +00:00
}
public SdkClient() {
2021-01-27 08:04:41 +00:00
this(null, 0);
}
public static SdkClientBuilder newBuilder() {
return new SdkClientBuilder();
2018-08-14 07:21:56 +00:00
}
@Override
public void start() {
2021-01-20 07:25:58 +00:00
init();
2021-01-27 08:04:41 +00:00
bootstrap.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) {
socketChannel.pipeline().addLast("SdkServerDecoder", new SdkClientDecoder(12))
.addLast("SdkServerEncoder", new SdkClientEncoder())
.addLast("SdkClientHandler", new SdkClientHandler());
}
});
2018-08-14 07:21:56 +00:00
try {
2021-02-07 14:11:59 +00:00
channelFuture = bootstrap.connect(host, port)
.sync()
.channel()
.closeFuture()
.addListener((ChannelFutureListener) channelFuture ->
logger.warn("client channel close.", channelFuture.cause()));
2018-08-14 07:21:56 +00:00
InetSocketAddress address = (InetSocketAddress) channelFuture.channel().remoteAddress();
logger.info("SdkClient start success, host is {}, port is {}", address.getHostName(), address.getPort());
} catch (InterruptedException e) {
logger.error("SdkClient start error", e);
shutdown();
}
}
2021-01-27 08:04:41 +00:00
public static final class SdkClientBuilder {
int timeoutMillis;
String host;
int port;
private SdkClientBuilder() {
}
public static SdkClientBuilder aSdkClient() {
return new SdkClientBuilder();
}
public SdkClientBuilder timeoutMillis(int timeoutMillis) {
this.timeoutMillis = timeoutMillis;
return this;
}
public SdkClientBuilder host(String host) {
this.host = host;
return this;
}
public SdkClientBuilder port(int port) {
this.port = port;
return this;
}
public SdkClient build() {
return new SdkClient(host, port, timeoutMillis);
}
}
2018-08-14 07:21:56 +00:00
}