netty deployment and tool packaging

This commit is contained in:
nico
2018-11-01 21:25:00 +08:00
parent fce24d42d8
commit 3452a92560
11 changed files with 428 additions and 0 deletions
+23
View File
@@ -0,0 +1,23 @@
<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>
<artifactId>landlords-common</artifactId>
<name>Landlords-common</name>
<description>This is a console version of the fight landlord game</description>
<parent>
<groupId>com.smallnico.ratel</groupId>
<artifactId>landlords</artifactId>
<version>1.0.0</version>
</parent>
<dependencies>
<dependency>
<groupId>com.smallnico</groupId>
<artifactId>noson</artifactId>
<version>1.1.4</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,47 @@
package org.nico.trap.landlords.entity;
import org.nico.trap.landlords.enums.PokerLevel;
import org.nico.trap.landlords.enums.PokerType;
/**
* Poke, with {@link PokerLevel} and {@link PokerType}
*
* @author nico
*/
public class Poker {
private PokerLevel level;
private PokerType type;
public final PokerLevel getLevel() {
return level;
}
public final void setLevel(PokerLevel level) {
this.level = level;
}
public final PokerType getType() {
return type;
}
public final void setType(PokerType type) {
this.type = type;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Poker other = (Poker) obj;
if (level != other.level)
return false;
return true;
}
}
@@ -0,0 +1,67 @@
package org.nico.trap.landlords.enums;
/**
* Poker level
*
* @author nico
*/
public enum PokerLevel {
LEVEL_3(3, "3"),
LEVEL_4(4, "4"),
LEVEL_5(5, "5"),
LEVEL_6(6, "6"),
LEVEL_7(7, "7"),
LEVEL_8(8, "8"),
LEVEL_9(9, "9"),
LEVEL_10(10, "10"),
LEVEL_J(11, "J"),
LEVEL_Q(12, "Q"),
LEVEL_K(13, "K"),
LEVEL_A(14, "A"),
LEVEL_2(15, "2"),
LEVEL_SMALL_KING(16, "SMALL JOKER"),
LEVEL_BIG_KING(17, "BIG JOKER"),
;
private int level;
private String name;
private PokerLevel(int level, String name) {
this.name = name;
this.level = level;
}
public final String getName() {
return name;
}
public final void setName(String name) {
this.name = name;
}
public final int getLevel() {
return level;
}
public final void setLevel(int level) {
this.level = level;
}
}
@@ -0,0 +1,28 @@
package org.nico.trap.landlords.enums;
/**
* Poker type Spade、 Heart、 Diamond、 Club
*
* @author nico
*/
public enum PokerType {
DIAMOND(""),
CLUB(""),
SPADE(""),
HEART("")
;
private String name;
private PokerType(String name) {
this.name = name;
}
public final String getName() {
return name;
}
}
@@ -0,0 +1,48 @@
package org.nico.trap.landlords.transfer;
/**
* Byte manipulation tool
*
* @author nico
*/
public class ByteKit {
/**
* Target byte array
*/
private byte[] bytes;
public ByteKit(byte[] bytes) {
this.bytes = bytes;
}
/**
* Gets the index of the incoming array in the target array, not matched to return -1
*
* @param bs Incoming array
* @param start Matching start index
* @return Match index, not match to return -1
*/
public int indexOf(byte[] bs, int start) {
int targetIndex = -1;
if(bs == null) {
return targetIndex;
}
for(int index = start; index < bytes.length; index ++) {
byte cbyte = bytes[index];
if(bs[0] == cbyte) {
boolean isEquals = true;
for(int sindex = 1; sindex < bs.length; sindex ++) {
if(index + sindex >= bytes.length || bs[sindex] != bytes[index + sindex]) {
isEquals = false;
break;
}
}
if(isEquals) {
targetIndex = index;
}
}
}
return targetIndex;
}
}
@@ -0,0 +1,81 @@
package org.nico.trap.landlords.transfer;
public class ByteLink{
private ByteNode start;
private ByteNode current;
private int size;
public void append(byte b){
if(start == null){
start = new ByteNode(b);
current = start;
}else{
ByteNode node = new ByteNode(b);
current.setNext(node);
current = node;
}
size ++;
}
public void append(byte[] bs){
if(bs != null) {
for(byte b: bs) {
append(b);
}
}
}
public byte[] toArray(){
if(size == 0){
return null;
}
byte[] bytes = new byte[size];
int index = 0;
ByteNode s = start.clone();
while(s != null){
bytes[index ++] = s.getB();
s = s.getNext();
}
return bytes;
}
public static class ByteNode{
private byte b;
private ByteNode next;
public ByteNode(byte b) {
this.b = b;
}
public ByteNode(byte b, ByteNode next) {
this.b = b;
this.next = next;
}
protected ByteNode clone(){
return new ByteNode(b, next);
}
public byte getB() {
return b;
}
public void setB(byte b) {
this.b = b;
}
public ByteNode getNext() {
return next;
}
public void setNext(ByteNode next) {
this.next = next;
}
}
}
@@ -0,0 +1,43 @@
package org.nico.trap.landlords.transfer;
import org.nico.noson.Noson;
/**
* Protocol transport related tools
*
* @author nico
* @time 2018-11-01 20:43
*/
public class TransferProtocolUtils {
/**
* A protocol header that represents the beginning of an available stream of data
*/
public static final byte[] PROTOCOL_HAED = "#".getBytes();
/**
* The end of the protocol used to represent the end of an available stream of data
*/
public static final byte[] PROTOCOL_TAIL = "$".getBytes();
/**
* Serialize the poker list to transportable bytes
*
* @param pokers Poker list
* @return Transportable byte array
*/
public static <T> byte[] serialize(T object) {
ByteLink bl = new ByteLink();
bl.append(PROTOCOL_HAED);
bl.append(Noson.reversal(object).getBytes());
bl.append(PROTOCOL_TAIL);
return bl.toArray();
}
public static <T> T unserialize(byte[] bytes) {
ByteLink bl = new ByteLink();
return null;
}
}
@@ -0,0 +1,15 @@
package org.nico.trap.landlords.handler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class EchoHandler extends ChannelInboundHandlerAdapter{
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println(
"Server received: " + msg);
ctx.writeAndFlush("ok");
}
}
@@ -0,0 +1,25 @@
package org.nico.trap.landlords.handler;
import java.util.List;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
public class ServerDecoder extends ByteToMessageDecoder{
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if (in.readableBytes() >= 4) {
byte[] bs = new byte[4];
in.getBytes(0, bs);
for(byte b: bs) {
System.out.print(b + " ");
}
System.out.println();
int i = in.readInt();
out.add(i);
}
}
}
@@ -0,0 +1,43 @@
package org.nico.trap.landlords.server;
import java.net.InetSocketAddress;
import org.nico.trap.landlords.handler.EchoHandler;
import org.nico.trap.landlords.handler.ServerDecoder;
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;
public class SimpleServer {
public static void main(String[] args) throws InterruptedException {
EventLoopGroup group = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(group)
.channel(NioServerSocketChannel.class)
.localAddress(new InetSocketAddress(8000))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ServerDecoder());
ch.pipeline().addLast(new EchoHandler());
}
});
ChannelFuture f = b.bind().sync();
f.channel().closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
}
}
}
+8
View File
@@ -47,6 +47,13 @@
<version>2.0.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
@@ -151,6 +158,7 @@
<modules>
<module>landlords-client</module>
<module>landlords-server</module>
<module>landlords-common</module>
</modules>
<distributionManagement>