Skip to content

Commit

Permalink
feat: add netty bootstrap
Browse files Browse the repository at this point in the history
Signed-off-by: ZhangJian He <[email protected]>
  • Loading branch information
ZhangJian He committed Sep 7, 2024
1 parent a990b59 commit 09db498
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,58 @@
package io.github.protocol.mdtp.client;

public class MdtpClient {
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import lombok.extern.slf4j.Slf4j;

import java.io.Closeable;
import java.io.IOException;

@Slf4j
public class MdtpClient implements Closeable {
private final MdtpClientConfig config;

private EventLoopGroup group;

public MdtpClient(MdtpClientConfig config) {
this.config = config;
}

public void start() throws Exception {
if (group != null) {
throw new IllegalAccessException("mdtp client already started");
}
log.info("mdtp client is starting, config is {}", this.config);
this.group = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
bootstrap.remoteAddress(this.config.host, this.config.port);
bootstrap.group(this.group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
}
});
ChannelFuture channelFuture = bootstrap.connect().sync();
if (channelFuture.isSuccess()) {
log.info("mdtp client started");
} else {
log.error("mdtp client start failed", channelFuture.cause());
throw new Exception("mdtp client start failed", channelFuture.cause());
}
}

@Override
public void close() throws IOException {
if (this.group == null) {
log.info("mdtp client already closed");
return;
}
this.group.shutdownGracefully();
log.info("mdtp client closed");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.github.protocol.mdtp.client;

public class MdtpClientConfig {
public String host;

public int port;

public MdtpClientConfig host(String host) {
this.host = host;
return this;
}

public MdtpClientConfig port(int port) {
this.port = port;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,62 @@
package io.github.protocol.mdtp.server;

public class MdtpServer {
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import lombok.extern.slf4j.Slf4j;

import java.io.Closeable;
import java.io.IOException;
import java.net.InetSocketAddress;

@Slf4j
public class MdtpServer implements Closeable {
private final MdtpServerConfig config;

private EventLoopGroup acceptorGroup;

private EventLoopGroup ioGroup;

public MdtpServer(MdtpServerConfig config) {
this.config = config;
}

public void start() throws Exception {
if (this.acceptorGroup != null) {
throw new IllegalStateException("mdtp server already started");
}
log.info("mdtp server is starting, config is {}", this.config);
this.acceptorGroup = new NioEventLoopGroup();
this.ioGroup = new NioEventLoopGroup();
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(this.acceptorGroup, this.ioGroup);
serverBootstrap.channel(NioServerSocketChannel.class);
serverBootstrap.localAddress(new InetSocketAddress(this.config.host, this.config.port));
serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
}
});
ChannelFuture channelFuture = serverBootstrap.bind().sync();
if (channelFuture.isSuccess()) {
log.info("mdtp server started");
} else {
log.error("mdtp server start failed", channelFuture.cause());
throw new Exception("mdtp server start failed", channelFuture.cause());
}
}

@Override
public void close() throws IOException {
if (this.acceptorGroup != null) {
this.acceptorGroup.shutdownGracefully();
}
if (this.ioGroup != null) {
this.ioGroup.shutdownGracefully();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.github.protocol.mdtp.server;

public class MdtpServerConfig {
public String host;

public int port;

public MdtpServerConfig host(String host) {
this.host = host;
return this;
}

public MdtpServerConfig port(int port) {
this.port = port;
return this;
}
}
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
Expand Down

0 comments on commit 09db498

Please sign in to comment.