From 9c102220db1b81ab985316db23d34bc830deea0f Mon Sep 17 00:00:00 2001 From: ehlxr Date: Wed, 27 Jan 2021 16:04:41 +0800 Subject: [PATCH] Optimized code --- .../ehlxr/did/client/AbstractClient.java | 7 ++ .../io/github/ehlxr/did/client/SdkClient.java | 77 +++++++++++++------ .../java/io/github/ehlxr/did/DidSdkTest.java | 3 +- .../ehlxr/did/server/sdk/SdkServer.java | 4 +- 4 files changed, 66 insertions(+), 25 deletions(-) diff --git a/did-sdk/src/main/java/io/github/ehlxr/did/client/AbstractClient.java b/did-sdk/src/main/java/io/github/ehlxr/did/client/AbstractClient.java index 5615601..4586a53 100644 --- a/did-sdk/src/main/java/io/github/ehlxr/did/client/AbstractClient.java +++ b/did-sdk/src/main/java/io/github/ehlxr/did/client/AbstractClient.java @@ -7,7 +7,9 @@ import io.netty.bootstrap.Bootstrap; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelOption; import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioSocketChannel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -57,6 +59,11 @@ public abstract class AbstractClient implements Client { }); bootstrap = new Bootstrap(); + bootstrap.group(workGroup) + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000) + // .option(ChannelOption.TCP_NODELAY, true) + // .option(ChannelOption.SO_KEEPALIVE, true) + .channel(NioSocketChannel.class); } @Override diff --git a/did-sdk/src/main/java/io/github/ehlxr/did/client/SdkClient.java b/did-sdk/src/main/java/io/github/ehlxr/did/client/SdkClient.java index 77f042d..e879a10 100644 --- a/did-sdk/src/main/java/io/github/ehlxr/did/client/SdkClient.java +++ b/did-sdk/src/main/java/io/github/ehlxr/did/client/SdkClient.java @@ -6,13 +6,12 @@ import io.github.ehlxr.did.client.handler.SdkClientHandler; import io.github.ehlxr.did.common.Constants; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelInitializer; -import io.netty.channel.ChannelOption; import io.netty.channel.socket.SocketChannel; -import io.netty.channel.socket.nio.NioSocketChannel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.net.InetSocketAddress; +import java.util.Objects; /** * @author ehlxr @@ -21,44 +20,47 @@ public class SdkClient extends AbstractClient { private final Logger logger = LoggerFactory.getLogger(SdkClient.class); public SdkClient(String host, int port) { - this(host, port, Constants.SDK_CLIENT_TIMEOUTMILLIS); + this(host, port, 0); } public SdkClient(String host, int port, int timeoutMillis) { - this.host = host; - this.port = port; - this.timeoutMillis = timeoutMillis; + 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; } public SdkClient() { - this("".equals(Constants.getEnv("SDK_HOST")) ? Constants.SERVER_HOST : Constants.getEnv("HTTP_PORT"), - "".equals(Constants.getEnv("SDK_PORT")) ? Constants.SDK_PORT : Integer.parseInt(Constants.getEnv("SDK_PORT"))); + this(null, 0); + } + + public static SdkClientBuilder newBuilder() { + return new SdkClientBuilder(); } @Override public void start() { init(); - bootstrap.group(workGroup) - .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000) - // .option(ChannelOption.TCP_NODELAY, true) - // .option(ChannelOption.SO_KEEPALIVE, true) - .channel(NioSocketChannel.class) - .handler(new ChannelInitializer() { - @Override - protected void initChannel(SocketChannel socketChannel) { - socketChannel.pipeline().addLast("SdkServerDecoder", new SdkClientDecoder(12)) - .addLast("SdkServerEncoder", new SdkClientEncoder()) - .addLast("SdkClientHandler", new SdkClientHandler()); - } - }); + bootstrap.handler(new ChannelInitializer() { + @Override + protected void initChannel(SocketChannel socketChannel) { + socketChannel.pipeline().addLast("SdkServerDecoder", new SdkClientDecoder(12)) + .addLast("SdkServerEncoder", new SdkClientEncoder()) + .addLast("SdkClientHandler", new SdkClientHandler()); + } + }); try { channelFuture = bootstrap.connect(host, port).sync(); channelFuture.channel().closeFuture().addListener((ChannelFutureListener) channelFuture -> { logger.warn("client channel close.", channelFuture.cause()); - shutdown(); }); InetSocketAddress address = (InetSocketAddress) channelFuture.channel().remoteAddress(); @@ -69,4 +71,35 @@ public class SdkClient extends AbstractClient { } } + 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); + } + } } diff --git a/did-sdk/src/test/java/io/github/ehlxr/did/DidSdkTest.java b/did-sdk/src/test/java/io/github/ehlxr/did/DidSdkTest.java index 831b1dd..1a018c7 100644 --- a/did-sdk/src/test/java/io/github/ehlxr/did/DidSdkTest.java +++ b/did-sdk/src/test/java/io/github/ehlxr/did/DidSdkTest.java @@ -18,7 +18,8 @@ public class DidSdkTest { @Before public void init() { - client = new SdkClient("127.0.0.1", 16831, 5000); + // client = new SdkClient("127.0.0.1", 16831, 5000); + client = SdkClient.newBuilder().build(); client.start(); } diff --git a/did-server/src/main/java/io/github/ehlxr/did/server/sdk/SdkServer.java b/did-server/src/main/java/io/github/ehlxr/did/server/sdk/SdkServer.java index 247f942..7ded6b3 100644 --- a/did-server/src/main/java/io/github/ehlxr/did/server/sdk/SdkServer.java +++ b/did-server/src/main/java/io/github/ehlxr/did/server/sdk/SdkServer.java @@ -15,7 +15,7 @@ import java.util.Objects; */ public class SdkServer extends BaseServer { protected Logger logger = LoggerFactory.getLogger(SdkServer.class); - private int port = "".equals(Constants.getEnv("SDK_PORT")) ? Constants.HTTP_PORT : Integer.parseInt(Constants.getEnv("SDK_PORT")); + private int port = "".equals(Constants.getEnv("SDK_PORT")) ? Constants.SDK_PORT : Integer.parseInt(Constants.getEnv("SDK_PORT")); public SdkServer(SnowFlake snowFlake) { this(snowFlake, 0); @@ -25,7 +25,7 @@ public class SdkServer extends BaseServer { Objects.requireNonNull(snowFlake, "snowflake instance not allow null"); this.snowFlake = snowFlake; - this.port = port == 0 ? this.port : port; + this.port = port <= 0 ? this.port : port; } public static SdkServerBuilder newBuilder() {