-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
345 additions
and
0 deletions.
There are no files selected for viewing
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
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,47 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.hqctmh</groupId> | ||
<artifactId>netty-learn</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>netty-learn</name> | ||
<url>http://maven.apache.org</url> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.13.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<!-- https://mvnrepository.com/artifact/io.netty/netty-all --> | ||
<dependency> | ||
<groupId>io.netty</groupId> | ||
<artifactId>netty-all</artifactId> | ||
<version>5.0.0.Alpha2</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.8.1</version> | ||
<configuration> | ||
<source>11</source> | ||
<target>11</target> | ||
<encoding>UTF-8</encoding> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
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,12 @@ | ||
package com.hqctmh; | ||
|
||
/** | ||
* Hello world! | ||
* | ||
*/ | ||
public class App { | ||
public static void main( String[] args ) | ||
{ | ||
System.out.println( "Hello World!" ); | ||
} | ||
} |
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,44 @@ | ||
package com.hqctmh; | ||
|
||
import io.netty.bootstrap.Bootstrap; | ||
import io.netty.channel.ChannelFuture; | ||
import io.netty.channel.ChannelInitializer; | ||
import io.netty.channel.ChannelOption; | ||
import io.netty.channel.EventLoopGroup; | ||
import io.netty.channel.nio.NioEventLoopGroup; | ||
import io.netty.channel.socket.SocketChannel; | ||
import io.netty.channel.socket.nio.NioSocketChannel; | ||
|
||
/** | ||
* @Author mh | ||
* @Description | ||
* @Date 2021/8/9 | ||
*/ | ||
public class TimeClient { | ||
|
||
public void connect(int port, String host) throws Exception { | ||
EventLoopGroup group = new NioEventLoopGroup(); | ||
try { | ||
Bootstrap b = new Bootstrap(); | ||
b.group(group).channel(NioSocketChannel.class) | ||
.option(ChannelOption.TCP_NODELAY, true) | ||
.handler(new ChannelInitializer<SocketChannel>() { | ||
|
||
@Override | ||
protected void initChannel(SocketChannel socketChannel) throws Exception { | ||
socketChannel.pipeline().addLast(new TimeClientHandler()); | ||
} | ||
}); | ||
ChannelFuture f = b.connect(host, port).sync(); | ||
f.channel().closeFuture().sync(); | ||
} finally { | ||
group.shutdownGracefully(); | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
int port = 8888; | ||
new TimeClient().connect(port, "127.0.0.1"); | ||
} | ||
|
||
} |
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,46 @@ | ||
package com.hqctmh; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import io.netty.channel.ChannelHandlerAdapter; | ||
import io.netty.channel.ChannelHandlerContext; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* @Author mh | ||
* @Description | ||
* @Date 2021/8/9 | ||
*/ | ||
public class TimeClientHandler extends ChannelHandlerAdapter { | ||
|
||
private final ByteBuf firstMessage; | ||
|
||
public TimeClientHandler(){ | ||
byte[] req = "QUERY TIME ORDER".getBytes(StandardCharsets.UTF_8); | ||
firstMessage = Unpooled.buffer(req.length); | ||
firstMessage.writeBytes(req); | ||
} | ||
|
||
@Override | ||
public void channelActive(ChannelHandlerContext ctx) throws Exception { | ||
ctx.writeAndFlush(firstMessage); | ||
} | ||
|
||
@Override | ||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { | ||
ByteBuf buf = (ByteBuf) msg; | ||
byte[] req = new byte[buf.readableBytes()]; | ||
buf.readBytes(req); | ||
String body = new String(req, StandardCharsets.UTF_8); | ||
System.out.println("Now is : " + body); | ||
} | ||
|
||
@Override | ||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { | ||
System.out.println("Unexpected exeption from downstream : " + cause.getMessage()); | ||
ctx.close(); | ||
} | ||
} |
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,50 @@ | ||
package com.hqctmh; | ||
|
||
import io.netty.bootstrap.ServerBootstrap; | ||
import io.netty.channel.ChannelFuture; | ||
import io.netty.channel.ChannelInitializer; | ||
import io.netty.channel.ChannelOption; | ||
import io.netty.channel.EventLoopGroup; | ||
import io.netty.channel.nio.NioEventLoopGroup; | ||
import io.netty.channel.socket.SocketChannel; | ||
import io.netty.channel.socket.nio.NioServerSocketChannel; | ||
|
||
/** | ||
* @Author mh | ||
* @Description | ||
* @Date 2021/8/9 | ||
*/ | ||
public class TimeServer { | ||
|
||
public void bind(int point) throws Exception { | ||
EventLoopGroup bossGroup = new NioEventLoopGroup(); | ||
EventLoopGroup workerGroup = new NioEventLoopGroup(); | ||
|
||
try { | ||
ServerBootstrap b = new ServerBootstrap(); | ||
b.group(bossGroup, workerGroup) | ||
.channel(NioServerSocketChannel.class) | ||
.option(ChannelOption.SO_BACKLOG, 1024) | ||
.childHandler(new ChildChannelHandler()); | ||
ChannelFuture f = b.bind(point).sync(); | ||
f.channel().closeFuture().sync(); | ||
} finally { | ||
bossGroup.shutdownGracefully(); | ||
workerGroup.shutdownGracefully(); | ||
} | ||
} | ||
|
||
private static class ChildChannelHandler extends ChannelInitializer<SocketChannel> { | ||
@Override | ||
protected void initChannel(SocketChannel socketChannel) throws Exception { | ||
socketChannel.pipeline().addLast(new TimeServerHandler()); | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
int port = 8888; | ||
new TimeServer().bind(port); | ||
} | ||
|
||
|
||
} |
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,44 @@ | ||
package com.hqctmh; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.buffer.Unpooled; | ||
import io.netty.channel.ChannelHandlerAdapter; | ||
import io.netty.channel.ChannelHandlerContext; | ||
|
||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Date; | ||
|
||
/** | ||
* @Author mh | ||
* @Description | ||
* @Date 2021/8/9 | ||
*/ | ||
public class TimeServerHandler extends ChannelHandlerAdapter { | ||
|
||
@Override | ||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { | ||
|
||
ByteBuf buf = (ByteBuf) msg; | ||
byte[] req = new byte[buf.readableBytes()]; | ||
buf.readBytes(req); | ||
String body = new String(req, StandardCharsets.UTF_8); | ||
System.out.println("The time server receive order : " + body); | ||
|
||
String currentTime = "QUERY TIME ORDER".equalsIgnoreCase(body) ? new Date().toString() : "BAD ORDER"; | ||
ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes(StandardCharsets.UTF_8)); | ||
ctx.write(resp); | ||
|
||
super.channelRead(ctx, msg); | ||
} | ||
|
||
@Override | ||
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { | ||
ctx.flush(); | ||
} | ||
|
||
@Override | ||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { | ||
ctx.close(); | ||
} | ||
} |
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,14 @@ | ||
package com.hqctmh; | ||
|
||
import org.junit.Test; | ||
|
||
/** | ||
* Unit test for simple App. | ||
*/ | ||
public class AppTest { | ||
|
||
@Test | ||
public void test(){ | ||
System.out.println(); | ||
} | ||
} |