This commit is contained in:
@@ -7,7 +7,9 @@ import io.netty.bootstrap.Bootstrap;
|
|||||||
import io.netty.channel.Channel;
|
import io.netty.channel.Channel;
|
||||||
import io.netty.channel.ChannelFuture;
|
import io.netty.channel.ChannelFuture;
|
||||||
import io.netty.channel.ChannelFutureListener;
|
import io.netty.channel.ChannelFutureListener;
|
||||||
|
import io.netty.channel.ChannelOption;
|
||||||
import io.netty.channel.nio.NioEventLoopGroup;
|
import io.netty.channel.nio.NioEventLoopGroup;
|
||||||
|
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -57,6 +59,11 @@ public abstract class AbstractClient implements Client {
|
|||||||
});
|
});
|
||||||
|
|
||||||
bootstrap = new Bootstrap();
|
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
|
@Override
|
||||||
|
@@ -6,13 +6,12 @@ import io.github.ehlxr.did.client.handler.SdkClientHandler;
|
|||||||
import io.github.ehlxr.did.common.Constants;
|
import io.github.ehlxr.did.common.Constants;
|
||||||
import io.netty.channel.ChannelFutureListener;
|
import io.netty.channel.ChannelFutureListener;
|
||||||
import io.netty.channel.ChannelInitializer;
|
import io.netty.channel.ChannelInitializer;
|
||||||
import io.netty.channel.ChannelOption;
|
|
||||||
import io.netty.channel.socket.SocketChannel;
|
import io.netty.channel.socket.SocketChannel;
|
||||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author ehlxr
|
* @author ehlxr
|
||||||
@@ -21,44 +20,47 @@ public class SdkClient extends AbstractClient {
|
|||||||
private final Logger logger = LoggerFactory.getLogger(SdkClient.class);
|
private final Logger logger = LoggerFactory.getLogger(SdkClient.class);
|
||||||
|
|
||||||
public SdkClient(String host, int port) {
|
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) {
|
public SdkClient(String host, int port, int timeoutMillis) {
|
||||||
this.host = host;
|
this.host = Objects.isNull(host) ?
|
||||||
this.port = port;
|
"".equals(Constants.getEnv("SDK_HOST")) ? Constants.SERVER_HOST : Constants.getEnv("HTTP_PORT") :
|
||||||
this.timeoutMillis = timeoutMillis;
|
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() {
|
public SdkClient() {
|
||||||
this("".equals(Constants.getEnv("SDK_HOST")) ? Constants.SERVER_HOST : Constants.getEnv("HTTP_PORT"),
|
this(null, 0);
|
||||||
"".equals(Constants.getEnv("SDK_PORT")) ? Constants.SDK_PORT : Integer.parseInt(Constants.getEnv("SDK_PORT")));
|
}
|
||||||
|
|
||||||
|
public static SdkClientBuilder newBuilder() {
|
||||||
|
return new SdkClientBuilder();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start() {
|
public void start() {
|
||||||
init();
|
init();
|
||||||
|
|
||||||
bootstrap.group(workGroup)
|
bootstrap.handler(new ChannelInitializer<SocketChannel>() {
|
||||||
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000)
|
@Override
|
||||||
// .option(ChannelOption.TCP_NODELAY, true)
|
protected void initChannel(SocketChannel socketChannel) {
|
||||||
// .option(ChannelOption.SO_KEEPALIVE, true)
|
socketChannel.pipeline().addLast("SdkServerDecoder", new SdkClientDecoder(12))
|
||||||
.channel(NioSocketChannel.class)
|
.addLast("SdkServerEncoder", new SdkClientEncoder())
|
||||||
.handler(new ChannelInitializer<SocketChannel>() {
|
.addLast("SdkClientHandler", new SdkClientHandler());
|
||||||
@Override
|
}
|
||||||
protected void initChannel(SocketChannel socketChannel) {
|
});
|
||||||
socketChannel.pipeline().addLast("SdkServerDecoder", new SdkClientDecoder(12))
|
|
||||||
.addLast("SdkServerEncoder", new SdkClientEncoder())
|
|
||||||
.addLast("SdkClientHandler", new SdkClientHandler());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
channelFuture = bootstrap.connect(host, port).sync();
|
channelFuture = bootstrap.connect(host, port).sync();
|
||||||
|
|
||||||
channelFuture.channel().closeFuture().addListener((ChannelFutureListener) channelFuture -> {
|
channelFuture.channel().closeFuture().addListener((ChannelFutureListener) channelFuture -> {
|
||||||
logger.warn("client channel close.", channelFuture.cause());
|
logger.warn("client channel close.", channelFuture.cause());
|
||||||
shutdown();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
InetSocketAddress address = (InetSocketAddress) channelFuture.channel().remoteAddress();
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -18,7 +18,8 @@ public class DidSdkTest {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init() {
|
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();
|
client.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -15,7 +15,7 @@ import java.util.Objects;
|
|||||||
*/
|
*/
|
||||||
public class SdkServer extends BaseServer {
|
public class SdkServer extends BaseServer {
|
||||||
protected Logger logger = LoggerFactory.getLogger(SdkServer.class);
|
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) {
|
public SdkServer(SnowFlake snowFlake) {
|
||||||
this(snowFlake, 0);
|
this(snowFlake, 0);
|
||||||
@@ -25,7 +25,7 @@ public class SdkServer extends BaseServer {
|
|||||||
Objects.requireNonNull(snowFlake, "snowflake instance not allow null");
|
Objects.requireNonNull(snowFlake, "snowflake instance not allow null");
|
||||||
|
|
||||||
this.snowFlake = snowFlake;
|
this.snowFlake = snowFlake;
|
||||||
this.port = port == 0 ? this.port : port;
|
this.port = port <= 0 ? this.port : port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static SdkServerBuilder newBuilder() {
|
public static SdkServerBuilder newBuilder() {
|
||||||
|
Reference in New Issue
Block a user