This commit is contained in:
nico
2018-11-02 21:46:22 +08:00
parent 3452a92560
commit 1ece039ec7
32 changed files with 660 additions and 81 deletions
+8
View File
@@ -9,4 +9,12 @@
<artifactId>landlords</artifactId>
<version>1.0.0</version>
</parent>
<dependencies>
<dependency>
<groupId>com.smallnico.ratel</groupId>
<artifactId>landlords-common</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,14 @@
package org.nico.ratel.landlords.client;
import java.util.LinkedHashMap;
import java.util.Map;
import org.nico.ratel.landlords.entity.ServerSide;
public class ClientContains {
public final static int port = 1024;
public final static Map<Integer, ServerSide> serverSides = new LinkedHashMap<>();
}
@@ -0,0 +1,52 @@
package org.nico.ratel.landlords.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.nico.ratel.landlords.client.handler.TransferHandler;
import org.nico.ratel.landlords.handler.DefaultDecoder;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
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 io.netty.handler.codec.bytes.ByteArrayEncoder;
public class SimpleClient {
public static void main(String[] args) throws InterruptedException, IOException {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap()
.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>(){
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("decoder", new DefaultDecoder());
pipeline.addLast("encoder", new ByteArrayEncoder());
pipeline.addLast("handler", new TransferHandler());
}
});
Channel channel = b.connect("127.0.0.1", 1024).sync().channel();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
channel.writeAndFlush(reader.readLine().getBytes());
}
} finally {
group.shutdownGracefully().sync();
}
}
}
@@ -0,0 +1,19 @@
package org.nico.ratel.landlords.client.handler;
import org.nico.ratel.landlords.entity.ServerTransferData;
import org.nico.ratel.landlords.transfer.TransferProtocolUtils;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class TransferHandler extends ChannelInboundHandlerAdapter{
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
byte[] bs = (byte[]) msg;
ServerTransferData serverTransferData = TransferProtocolUtils.unserialize(bs, ServerTransferData.class);
System.out.println(serverTransferData);
}
}
@@ -0,0 +1,5 @@
package org.nico.ratel.landlords;
public class Default {
}
@@ -0,0 +1,92 @@
package org.nico.ratel.landlords.entity;
import java.util.List;
import org.nico.ratel.landlords.enums.ClientStatus;
import org.nico.ratel.landlords.enums.ClientType;
import io.netty.channel.Channel;
public class ClientSide {
private int id;
private int serverId;
private List<Poker> pokers;
private ClientStatus status;
private ClientType type;
private ClientSide next;
private ClientSide pre;
private Channel channel;
public final Channel getChannel() {
return channel;
}
public final void setChannel(Channel channel) {
this.channel = channel;
}
public final int getServerId() {
return serverId;
}
public final void setServerId(int serverId) {
this.serverId = serverId;
}
public final List<Poker> getPokers() {
return pokers;
}
public final void setPokers(List<Poker> pokers) {
this.pokers = pokers;
}
public final ClientStatus getStatus() {
return status;
}
public final void setStatus(ClientStatus status) {
this.status = status;
}
public final ClientType getType() {
return type;
}
public final void setType(ClientType type) {
this.type = type;
}
public final int getId() {
return id;
}
public final void setId(int id) {
this.id = id;
}
public final ClientSide getNext() {
return next;
}
public final void setNext(ClientSide next) {
this.next = next;
}
public final ClientSide getPre() {
return pre;
}
public final void setPre(ClientSide pre) {
this.pre = pre;
}
}
@@ -0,0 +1,50 @@
package org.nico.ratel.landlords.entity;
import org.nico.ratel.landlords.enums.ClientEventCode;
public class ClientTransferData {
private int serverId;
private ClientEventCode code;
private String data;
public ClientTransferData() {}
public ClientTransferData(int serverId, ClientEventCode code, String data) {
this.serverId = serverId;
this.code = code;
this.data = data;
}
public final int getServerId() {
return serverId;
}
public final void setServerId(int serverId) {
this.serverId = serverId;
}
public final ClientEventCode getCode() {
return code;
}
public final void setCode(ClientEventCode code) {
this.code = code;
}
public final String getData() {
return data;
}
public final void setData(String data) {
this.data = data;
}
@Override
public String toString() {
return "ClientTransferData [serverId=" + serverId + ", code=" + code + ", data=" + data + "]";
}
}
@@ -1,7 +1,7 @@
package org.nico.trap.landlords.entity;
package org.nico.ratel.landlords.entity;
import org.nico.trap.landlords.enums.PokerLevel;
import org.nico.trap.landlords.enums.PokerType;
import org.nico.ratel.landlords.enums.PokerLevel;
import org.nico.ratel.landlords.enums.PokerType;
/**
* Poke, with {@link PokerLevel} and {@link PokerType}
@@ -14,6 +14,14 @@ public class Poker {
private PokerType type;
public Poker() {
}
public Poker(PokerLevel level, PokerType type) {
this.level = level;
this.type = type;
}
public final PokerLevel getLevel() {
return level;
}
@@ -43,5 +51,10 @@ public class Poker {
return false;
return true;
}
@Override
public String toString() {
return "Poker [level=" + level + ", type=" + type + "]";
}
}
@@ -0,0 +1,46 @@
package org.nico.ratel.landlords.entity;
import java.util.LinkedHashMap;
import java.util.Map;
import org.nico.ratel.landlords.enums.ServerStatus;
public class ServerSide {
private int id;
private ServerStatus status;
private Map<Integer, Poker> pokers;
public ServerSide(int id) {
this.id = id;
this.pokers = new LinkedHashMap<>(3);
this.status = ServerStatus.BLANK;
}
public final int getId() {
return id;
}
public final void setId(int id) {
this.id = id;
}
public final ServerStatus getStatus() {
return status;
}
public final void setStatus(ServerStatus status) {
this.status = status;
}
public final Map<Integer, Poker> getPokers() {
return pokers;
}
public final void setPokers(Map<Integer, Poker> pokers) {
this.pokers = pokers;
}
}
@@ -0,0 +1,32 @@
package org.nico.ratel.landlords.entity;
import org.nico.ratel.landlords.enums.ServerEventCode;
public class ServerTransferData {
private ServerEventCode code;
private String data;
public final ServerEventCode getCode() {
return code;
}
public final void setCode(ServerEventCode code) {
this.code = code;
}
public final String getData() {
return data;
}
public final void setData(String data) {
this.data = data;
}
@Override
public String toString() {
return "ServerTransferData [code=" + code + ", data=" + data + "]";
}
}
@@ -0,0 +1,7 @@
package org.nico.ratel.landlords.enums;
public enum ClientEventCode {
CODE_INTO
}
@@ -0,0 +1,16 @@
package org.nico.ratel.landlords.enums;
public enum ClientStatus {
TO_CHOOSE,
NO_READY,
READY,
WAIT,
CALL_LANDLORD,
}
@@ -0,0 +1,9 @@
package org.nico.ratel.landlords.enums;
public enum ClientType {
LANDLORD,
PEASANT
}
@@ -1,4 +1,4 @@
package org.nico.trap.landlords.enums;
package org.nico.ratel.landlords.enums;
/**
* Poker level
@@ -1,4 +1,4 @@
package org.nico.trap.landlords.enums;
package org.nico.ratel.landlords.enums;
/**
* Poker type Spade Heart Diamond Club
@@ -0,0 +1,7 @@
package org.nico.ratel.landlords.enums;
public enum ServerEventCode {
CODE_INTO
}
@@ -0,0 +1,11 @@
package org.nico.ratel.landlords.enums;
public enum ServerStatus {
BLANK,
WAIT,
STARTING
}
@@ -0,0 +1,27 @@
package org.nico.ratel.landlords.exception;
public class LandlordException extends RuntimeException{
private static final long serialVersionUID = -5643145833569293539L;
public LandlordException() {
super();
}
public LandlordException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
public LandlordException(String message, Throwable cause) {
super(message, cause);
}
public LandlordException(String message) {
super(message);
}
public LandlordException(Throwable cause) {
super(cause);
}
}
@@ -0,0 +1,27 @@
package org.nico.ratel.landlords.handler;
import java.util.List;
import org.nico.ratel.landlords.transfer.TransferProtocolUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
public class DefaultDecoder extends ByteToMessageDecoder{
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
int startIndex = -1;
int endIndex = -1;
if((startIndex = in.indexOf(in.readerIndex(), in.writerIndex(), TransferProtocolUtils.PROTOCOL_HAED)) != -1 &&
(endIndex = in.indexOf(startIndex + 1, in.writerIndex(), TransferProtocolUtils.PROTOCOL_TAIL)) != -1) {
endIndex ++;
byte[] bytes = new byte[endIndex - startIndex];
in.skipBytes(startIndex - in.readerIndex());
in.readBytes(bytes, 0, bytes.length);
out.add(bytes);
}
}
}
@@ -1,4 +1,4 @@
package org.nico.trap.landlords.transfer;
package org.nico.ratel.landlords.transfer;
/**
* Byte manipulation tool
@@ -39,10 +39,22 @@ public class ByteKit {
}
}
if(isEquals) {
targetIndex = index;
targetIndex = index;
break;
}
}
}
return targetIndex;
}
/**
* Gets the position of the byte byte in the byte array
*
* @param b Byte
* @param start Matching start index
* @return Match index, not match to return -1
*/
public int indexOf(byte b, int start) {
return indexOf(new byte[] {b}, start);
}
}
@@ -1,4 +1,4 @@
package org.nico.trap.landlords.transfer;
package org.nico.ratel.landlords.transfer;
public class ByteLink{
@@ -0,0 +1,72 @@
package org.nico.ratel.landlords.transfer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.nico.noson.Noson;
import org.nico.noson.entity.NoType;
import org.nico.ratel.landlords.entity.Poker;
import org.nico.ratel.landlords.enums.PokerLevel;
import org.nico.ratel.landlords.enums.PokerType;
import org.nico.ratel.landlords.exception.LandlordException;
/**
* 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()[0];
/**
* The end of the protocol used to represent the end of an available stream of data
*/
public static final byte PROTOCOL_TAIL = "$".getBytes()[0];
/**
* Serialize the poker list to transportable bytes
*
* @param pokers Poker list
* @return Transportable byte array
*/
public static byte[] serialize(Object obj) {
ByteLink bl = new ByteLink();
bl.append(PROTOCOL_HAED);
bl.append(Noson.reversal(obj).getBytes());
bl.append(PROTOCOL_TAIL);
return bl.toArray();
}
/**
* Deserialize the byte stream as an object
*
* @param bytes Byte array
* @return Genericity
*/
public static <T> T unserialize(byte[] bytes, Class<T> clazz) {
ByteKit bk = new ByteKit(bytes);
int start = -1;
int end = -1;
int index = bk.indexOf(PROTOCOL_HAED, 0);
if(index != -1) start = index + 1;
index = bk.indexOf(PROTOCOL_TAIL, 0);
if(index != -1) end = index;
if(start != -1 && end != -1 && start > end) {
throw new LandlordException("Message format error, head and tail error.");
}else {
byte[] content = new byte[end - start];
System.arraycopy(bytes, start, content, 0, content.length);
return Noson.convert(new String(content), clazz);
}
}
}
@@ -0,0 +1,8 @@
package org.nico.ratel.landlords.view;
public class ViewPrinter {
public static void view(String msg) {
System.out.println(msg);
}
}
@@ -1,43 +0,0 @@
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;
}
}
+8
View File
@@ -9,4 +9,12 @@
<artifactId>landlords</artifactId>
<version>1.0.0</version>
</parent>
<dependencies>
<dependency>
<groupId>com.smallnico.ratel</groupId>
<artifactId>landlords-common</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,40 @@
package org.nico.ratel.landlords.server;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicInteger;
import org.nico.ratel.landlords.entity.ClientSide;
import org.nico.ratel.landlords.entity.ServerSide;
public class ServerContains {
/**
* Server port
*/
public final static int PORT = 1024;
/**
* The map of server side
*/
public final static Map<Integer, ServerSide> SERVER_SIDE_MAP = new ConcurrentHashMap<>();
/**
* The list of client side
*/
public final static Queue<ClientSide> CLIENT_SIDE_LIST = new ConcurrentLinkedQueue<ClientSide>();
private final static AtomicInteger CLIENT_ATOMIC_ID = new AtomicInteger(0);
private final static AtomicInteger SERVER_ATOMIC_ID = new AtomicInteger(0);
public final static int getClientId() {
return CLIENT_ATOMIC_ID.getAndIncrement();
}
public final static int getServerId() {
return SERVER_ATOMIC_ID.getAndIncrement();
}
}
@@ -1,16 +1,13 @@
package org.nico.trap.landlords.server;
package org.nico.ratel.landlords.server;
import java.net.InetSocketAddress;
import org.nico.trap.landlords.handler.EchoHandler;
import org.nico.trap.landlords.handler.ServerDecoder;
import org.nico.ratel.landlords.server.handler.DefaultChannelInitializer;
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 {
@@ -20,17 +17,11 @@ public class SimpleServer {
EventLoopGroup group = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(group)
ServerBootstrap b = new ServerBootstrap()
.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());
}
});
.localAddress(new InetSocketAddress(ServerContains.PORT))
.childHandler(new DefaultChannelInitializer());
ChannelFuture f = b.bind().sync();
f.channel().closeFuture().sync();
@@ -0,0 +1,35 @@
package org.nico.ratel.landlords.server.handler;
import org.nico.ratel.landlords.entity.ClientSide;
import org.nico.ratel.landlords.enums.ClientStatus;
import org.nico.ratel.landlords.handler.DefaultDecoder;
import org.nico.ratel.landlords.server.ServerContains;
import org.nico.ratel.landlords.view.ViewPrinter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
public class DefaultChannelInitializer extends ChannelInitializer<SocketChannel>{
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
ClientSide clientSide = new ClientSide();
clientSide.setId(ServerContains.getClientId());
clientSide.setStatus(ClientStatus.TO_CHOOSE);
clientSide.setChannel(ctx.channel());
ServerContains.CLIENT_SIDE_LIST.add(clientSide);
ViewPrinter.view("One client join nico landlord world ");
}
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("decoder", new DefaultDecoder());
pipeline.addLast("encoder", new ByteArrayEncoder());
pipeline.addLast("handler", new TransferHandler());
}
}
@@ -0,0 +1,31 @@
package org.nico.ratel.landlords.server.handler;
import org.nico.ratel.landlords.entity.ClientTransferData;
import org.nico.ratel.landlords.entity.ServerTransferData;
import org.nico.ratel.landlords.enums.ServerEventCode;
import org.nico.ratel.landlords.transfer.TransferProtocolUtils;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class TransferHandler extends ChannelInboundHandlerAdapter{
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
byte[] bs = (byte[]) msg;
System.out.println(new String(bs));
ClientTransferData clientTransferData = TransferProtocolUtils.unserialize(bs, ClientTransferData.class);
System.out.println(clientTransferData.getData());
System.out.println(clientTransferData.getServerId());
System.out.println(clientTransferData.getCode());
ServerTransferData serverTransferData = new ServerTransferData();
serverTransferData.setCode(ServerEventCode.CODE_INTO);
serverTransferData.setData("ok");
byte[] to = TransferProtocolUtils.serialize(serverTransferData);
ctx.writeAndFlush(to);
}
}
@@ -1,15 +0,0 @@
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");
}
}
+5
View File
@@ -52,6 +52,11 @@
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.6.1</version>
</dependency>
</dependencies>
<build>