-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: ZhangJian He <[email protected]>
- Loading branch information
ZhangJian He
committed
Sep 7, 2024
1 parent
a990b59
commit 09db498
Showing
5 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
56 changes: 55 additions & 1 deletion
56
mdtp-client/src/main/java/io/github/protocol/mdtp/client/MdtpClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
mdtp-client/src/main/java/io/github/protocol/mdtp/client/MdtpClientConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
60 changes: 59 additions & 1 deletion
60
mdtp-server/src/main/java/io/github/protocol/mdtp/server/MdtpServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
mdtp-server/src/main/java/io/github/protocol/mdtp/server/MdtpServerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters