编码解码更改为序列化以及部分业务开发

This commit is contained in:
nico
2018-11-03 17:30:59 +08:00
parent 1ece039ec7
commit 3d20a12b79
39 changed files with 692 additions and 215 deletions
@@ -1,14 +1,11 @@
package org.nico.ratel.landlords.client;
import java.util.LinkedHashMap;
import java.util.Map;
import org.nico.ratel.landlords.entity.ServerSide;
import org.nico.ratel.landlords.entity.ClientSide;
public class ClientContains {
public final static int port = 1024;
public final static Map<Integer, ServerSide> serverSides = new LinkedHashMap<>();
public static ClientSide clientSide = null;
}
@@ -1,21 +1,14 @@
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 org.nico.ratel.landlords.client.handler.DefaultChannelInitializer;
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 {
@@ -27,26 +20,14 @@ public class SimpleClient {
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());
}
});
.handler(new DefaultChannelInitializer());
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());
}
channel.closeFuture().sync();
} finally {
group.shutdownGracefully().sync();
}
}
}
@@ -0,0 +1,11 @@
package org.nico.ratel.landlords.client.event;
import org.nico.ratel.landlords.entity.ClientTransferData;
import io.netty.channel.Channel;
public interface ClientEventListener<T> {
public void call(Channel channel, ClientTransferData<T> clientTransferData);
}
@@ -0,0 +1,30 @@
package org.nico.ratel.landlords.client.event;
import org.nico.ratel.landlords.channel.ChannelUtils;
import org.nico.ratel.landlords.client.ClientContains;
import org.nico.ratel.landlords.entity.ClientSide;
import org.nico.ratel.landlords.entity.ClientTransferData;
import org.nico.ratel.landlords.enums.ServerEventCode;
import org.nico.ratel.landlords.print.SimplePrinter;
import org.nico.ratel.landlords.print.SimpleWriter;
import io.netty.channel.Channel;
public class ClientEventListener_CODE_CONNECT implements ClientEventListener<ClientSide>{
@Override
public void call(Channel channel, ClientTransferData<ClientSide> clientTransferData) {
SimplePrinter.println("You has been join Nico-Landlords, please set your nickname: ");
String nickname = SimpleWriter.write();
ClientSide clientSide = clientTransferData.getData();
clientSide.setNickname(nickname);
ClientContains.clientSide = clientSide;
ChannelUtils.pushToServer(channel, clientSide.getId(), ServerEventCode.CODE_RENAME, clientSide);
}
}
@@ -0,0 +1,19 @@
package org.nico.ratel.landlords.client.event;
import org.nico.ratel.landlords.entity.ClientTransferData;
import org.nico.ratel.landlords.entity.Room;
import org.nico.ratel.landlords.print.SimplePrinter;
import io.netty.channel.Channel;
public class ClientEventListener_CODE_JOIN_ROOM_SUCCESS implements ClientEventListener<Room>{
@Override
public void call(Channel channel, ClientTransferData<Room> clientTransferData) {
SimplePrinter.println("You has been join room" + clientTransferData.getData());
SimplePrinter.println("Please wait for other players to join !");
}
}
@@ -0,0 +1,17 @@
package org.nico.ratel.landlords.client.event;
import org.nico.ratel.landlords.entity.ClientTransferData;
import org.nico.ratel.landlords.entity.Room;
import io.netty.channel.Channel;
public class ClientEventListener_CODE_ROOM_STARTING implements ClientEventListener<Room>{
@Override
public void call(Channel channel, ClientTransferData<Room> clientTransferData) {
System.out.println(clientTransferData.getData() + " game starting !");
}
}
@@ -0,0 +1,62 @@
package org.nico.ratel.landlords.client.event;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentSkipListMap;
import org.nico.ratel.landlords.channel.ChannelUtils;
import org.nico.ratel.landlords.client.ClientContains;
import org.nico.ratel.landlords.entity.ClientSide;
import org.nico.ratel.landlords.entity.ClientTransferData;
import org.nico.ratel.landlords.entity.Room;
import org.nico.ratel.landlords.enums.ServerEventCode;
import org.nico.ratel.landlords.print.SimplePrinter;
import org.nico.ratel.landlords.print.SimpleWriter;
import org.nico.ratel.landlords.utils.OptionsUtils;
import io.netty.channel.Channel;
public class ClientEventListener_CODE_SHOW_OPTIONS implements ClientEventListener<ConcurrentSkipListMap<Integer, Room>>{
@Override
public void call(Channel channel, ClientTransferData<ConcurrentSkipListMap<Integer, Room>> clientTransferData) {
SimplePrinter.println("Options: ");
SimplePrinter.println("1. Create Room");
SimplePrinter.println("2. Join Room");
SimplePrinter.print("Your choose options number");
String line = SimpleWriter.write();
while(line == null || (! line.equals("1") && ! line.equals("2"))) {
SimplePrinter.println("Invalid options, please choose again");
line = SimpleWriter.write();
}
Map<Integer, Room> roomMap = clientTransferData.getData();
int choose = Integer.valueOf(line);
if(choose == 1) {
ChannelUtils.pushToServer(channel, ServerEventCode.CODE_CREATE_ROOM, ClientContains.clientSide.getId());
}else {
if(roomMap != null && ! roomMap.isEmpty()) {
SimplePrinter.println("Room list: ");
for(Entry<Integer, Room> roomEntry: roomMap.entrySet()) {
SimplePrinter.println(roomEntry.getKey() + "\t|\t" + roomEntry.getValue());
}
SimplePrinter.print("Your choose rooms number");
line = SimpleWriter.write();
while(line == null || ! roomMap.keySet().contains(OptionsUtils.getOptions(line))) {
SimplePrinter.println("Invalid options, please choose again");
line = SimpleWriter.write();
}
ChannelUtils.pushToServer(channel, ClientContains.clientSide.getId(), ServerEventCode.CODE_JOIN_ROOM, OptionsUtils.getOptions(line));
}else {
SimplePrinter.println("No available room, please create a room ");
call(channel, clientTransferData);
}
}
}
}
@@ -0,0 +1,23 @@
package org.nico.ratel.landlords.client.handler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
public class DefaultChannelInitializer extends ChannelInitializer<SocketChannel>{
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(
new ObjectDecoder(1024 * 1024, ClassResolvers.cacheDisabled(this
.getClass().getClassLoader())));
ch.pipeline().addLast(new ObjectEncoder());
ch.pipeline().addLast("handler", new TransferHandler());
}
}
@@ -1,6 +1,9 @@
package org.nico.ratel.landlords.client.handler;
import org.nico.ratel.landlords.entity.ServerTransferData;
import org.nico.ratel.landlords.client.event.ClientEventListener;
import org.nico.ratel.landlords.entity.ClientTransferData;
import org.nico.ratel.landlords.enums.ClientEventCode;
import org.nico.ratel.landlords.print.SimplePrinter;
import org.nico.ratel.landlords.transfer.TransferProtocolUtils;
import io.netty.channel.ChannelHandlerContext;
@@ -8,12 +11,35 @@ import io.netty.channel.ChannelInboundHandlerAdapter;
public class TransferHandler extends ChannelInboundHandlerAdapter{
private final static String LISTENER_PREFIX = "org.nico.ratel.landlords.client.event.ClientEventListener_";
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
byte[] bs = (byte[]) msg;
System.out.println(msg);
System.out.println();
ServerTransferData serverTransferData = TransferProtocolUtils.unserialize(bs, ServerTransferData.class);
System.out.println(serverTransferData);
if(msg instanceof ClientTransferData) {
ClientTransferData clientTransferData = (ClientTransferData) msg;
if(clientTransferData.getMsg() != null) {
SimplePrinter.println(clientTransferData.getMsg());
}
ClientEventCode code = clientTransferData.getCode();
if(code != null) {
String eventListener = LISTENER_PREFIX + code.name();
try {
Class<ClientEventListener> listenerClass = (Class<ClientEventListener>) Class.forName(eventListener);
ClientEventListener listener = listenerClass.newInstance();
listener.call(ctx.channel(), clientTransferData);
}catch(ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
}
-7
View File
@@ -13,11 +13,4 @@
<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,34 @@
package org.nico.ratel.landlords.channel;
import org.nico.ratel.landlords.entity.ClientTransferData;
import org.nico.ratel.landlords.entity.ServerTransferData;
import org.nico.ratel.landlords.enums.ClientEventCode;
import org.nico.ratel.landlords.enums.ServerEventCode;
import io.netty.channel.Channel;
public class ChannelUtils {
public static <T> void pushToClient(Channel channel, ClientEventCode code, T datas) {
pushToClient(channel, code, datas, null);
}
public static <T> void pushToClient(Channel channel, ClientEventCode code, T datas, String msg) {
ClientTransferData<T> clientTransferData = new ClientTransferData<T>(code, datas, msg);
channel.writeAndFlush(clientTransferData);
}
public static <T> void pushToServer(Channel channel, int clientId, int roomId, ServerEventCode code, T datas) {
ServerTransferData<T> serverTransferData = new ServerTransferData<T>(clientId, roomId, code, datas);
channel.writeAndFlush(serverTransferData);
}
public static <T> void pushToServer(Channel channel, int clientId, ServerEventCode code, T datas) {
pushToServer(channel, clientId, -1, code, datas);
}
public static <T> void pushToServer(Channel channel, ServerEventCode code, T datas) {
pushToServer(channel, -1, -1, code, datas);
}
}
@@ -1,5 +1,6 @@
package org.nico.ratel.landlords.entity;
import java.io.Serializable;
import java.util.List;
import org.nico.ratel.landlords.enums.ClientStatus;
@@ -7,11 +8,15 @@ import org.nico.ratel.landlords.enums.ClientType;
import io.netty.channel.Channel;
public class ClientSide {
public class ClientSide implements Serializable{
private static final long serialVersionUID = -1208782543528394339L;
private int id;
private int serverId;
private int roomId;
private String nickname;
private List<Poker> pokers;
@@ -23,8 +28,24 @@ public class ClientSide {
private ClientSide pre;
private Channel channel;
private transient Channel channel;
public ClientSide() {}
public ClientSide(int id, ClientStatus status, Channel channel) {
this.id = id;
this.status = status;
this.channel = channel;
}
public final String getNickname() {
return nickname;
}
public final void setNickname(String nickname) {
this.nickname = nickname;
}
public final Channel getChannel() {
return channel;
}
@@ -33,12 +54,12 @@ public class ClientSide {
this.channel = channel;
}
public final int getServerId() {
return serverId;
public final int getRoomId() {
return roomId;
}
public final void setServerId(int serverId) {
this.serverId = serverId;
public final void setRoomId(int roomId) {
this.roomId = roomId;
}
public final List<Poker> getPokers() {
@@ -1,29 +1,39 @@
package org.nico.ratel.landlords.entity;
import java.io.Serializable;
import org.nico.ratel.landlords.enums.ClientEventCode;
public class ClientTransferData {
public class ClientTransferData<T> implements Serializable{
private static final long serialVersionUID = -5144173747228946445L;
private int serverId;
private ClientEventCode code;
private String data;
private T data;
public ClientTransferData() {}
private String msg;
public ClientTransferData(int serverId, ClientEventCode code, String data) {
this.serverId = serverId;
public ClientTransferData() {
}
public ClientTransferData(ClientEventCode code, T data) {
this.code = code;
this.data = data;
}
public final int getServerId() {
return serverId;
public ClientTransferData(ClientEventCode code, T data, String msg) {
this.code = code;
this.data = data;
this.msg = msg;
}
public final String getMsg() {
return msg;
}
public final void setServerId(int serverId) {
this.serverId = serverId;
public final void setMsg(String msg) {
this.msg = msg;
}
public final ClientEventCode getCode() {
@@ -34,17 +44,17 @@ public class ClientTransferData {
this.code = code;
}
public final String getData() {
public final T getData() {
return data;
}
public final void setData(String data) {
public final void setData(T data) {
this.data = data;
}
@Override
public String toString() {
return "ClientTransferData [serverId=" + serverId + ", code=" + code + ", data=" + data + "]";
return "ClientTransferData [code=" + code + ", data=" + data + ", msg=" + msg + "]";
}
}
@@ -1,5 +1,7 @@
package org.nico.ratel.landlords.entity;
import java.io.Serializable;
import org.nico.ratel.landlords.enums.PokerLevel;
import org.nico.ratel.landlords.enums.PokerType;
@@ -8,8 +10,10 @@ import org.nico.ratel.landlords.enums.PokerType;
*
* @author nico
*/
public class Poker {
public class Poker implements Serializable{
private static final long serialVersionUID = -3830261356192537124L;
private PokerLevel level;
private PokerType type;
@@ -0,0 +1,54 @@
package org.nico.ratel.landlords.entity;
import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentSkipListMap;
import org.nico.ratel.landlords.enums.RoomStatus;
public class Room implements Serializable{
private static final long serialVersionUID = -9182226630057841379L;
private int id;
private RoomStatus status;
private Map<Integer, ClientSide> clientSideMap;
public Room(int id) {
this.id = id;
this.clientSideMap = new ConcurrentSkipListMap<>();
this.status = RoomStatus.BLANK;
}
public final int getId() {
return id;
}
public final void setId(int id) {
this.id = id;
}
public final RoomStatus getStatus() {
return status;
}
public final void setStatus(RoomStatus status) {
this.status = status;
}
public final Map<Integer, ClientSide> getClientSideMap() {
return clientSideMap;
}
public final void setClientSideMap(Map<Integer, ClientSide> clientSideMap) {
this.clientSideMap = clientSideMap;
}
@Override
public String toString() {
return status.getMsg() + "|" + clientSideMap.size() + " online";
}
}
@@ -1,46 +0,0 @@
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;
}
}
@@ -1,12 +1,45 @@
package org.nico.ratel.landlords.entity;
import java.io.Serializable;
import org.nico.ratel.landlords.enums.ServerEventCode;
public class ServerTransferData {
public class ServerTransferData<T> implements Serializable{
private static final long serialVersionUID = -5056177734642583364L;
private int clientId;
private int roomId;
private ServerEventCode code;
private String data;
private T data;
public ServerTransferData() {}
public ServerTransferData(int clientId, int roomId, ServerEventCode code, T data) {
this.clientId = clientId;
this.roomId = roomId;
this.code = code;
this.data = data;
}
public final int getClientId() {
return clientId;
}
public final void setClientId(int clientId) {
this.clientId = clientId;
}
public final int getRoomId() {
return roomId;
}
public final void setRoomId(int roomId) {
this.roomId = roomId;
}
public final ServerEventCode getCode() {
return code;
@@ -16,17 +49,12 @@ public class ServerTransferData {
this.code = code;
}
public final String getData() {
public final T getData() {
return data;
}
public final void setData(String data) {
public final void setData(T data) {
this.data = data;
}
@Override
public String toString() {
return "ServerTransferData [code=" + code + ", data=" + data + "]";
}
}
@@ -1,7 +1,30 @@
package org.nico.ratel.landlords.enums;
public enum ClientEventCode {
import java.io.Serializable;
CODE_INTO
public enum ClientEventCode implements Serializable{
CODE_CONNECT("进入系统"),
CODE_SHOW_OPTIONS("展示选项"),
CODE_JOIN_ROOM_SUCCESS("加入房间成功"),
CODE_ROOM_STARTING("开始游戏"),
;
private String msg;
private ClientEventCode(String msg) {
this.msg = msg;
}
public final String getMsg() {
return msg;
}
public final void setMsg(String msg) {
this.msg = msg;
}
}
@@ -1,6 +1,8 @@
package org.nico.ratel.landlords.enums;
public enum ClientStatus {
import java.io.Serializable;
public enum ClientStatus implements Serializable{
TO_CHOOSE,
@@ -1,6 +1,8 @@
package org.nico.ratel.landlords.enums;
public enum ClientType {
import java.io.Serializable;
public enum ClientType implements Serializable{
LANDLORD,
@@ -1,11 +1,13 @@
package org.nico.ratel.landlords.enums;
import java.io.Serializable;
/**
* Poker level
*
* @author nico
*/
public enum PokerLevel {
public enum PokerLevel implements Serializable{
LEVEL_3(3, "3"),
@@ -1,11 +1,13 @@
package org.nico.ratel.landlords.enums;
import java.io.Serializable;
/**
* Poker type Spade、 Heart、 Diamond、 Club
*
* @author nico
*/
public enum PokerType {
public enum PokerType implements Serializable{
DIAMOND(""),
@@ -0,0 +1,29 @@
package org.nico.ratel.landlords.enums;
import java.io.Serializable;
public enum RoomStatus implements Serializable{
BLANK("空闲"),
WAIT("等待"),
STARTING("进行中"),
;
private String msg;
private RoomStatus(String msg) {
this.msg = msg;
}
public final String getMsg() {
return msg;
}
public final void setMsg(String msg) {
this.msg = msg;
}
}
@@ -1,7 +1,28 @@
package org.nico.ratel.landlords.enums;
public enum ServerEventCode {
import java.io.Serializable;
CODE_INTO
public enum ServerEventCode implements Serializable{
CODE_RENAME("修改昵称"),
CODE_CREATE_ROOM("创建房间"),
CODE_JOIN_ROOM("加入房间"),
;
private String msg;
private ServerEventCode(String msg) {
this.msg = msg;
}
public final String getMsg() {
return msg;
}
public final void setMsg(String msg) {
this.msg = msg;
}
}
@@ -1,11 +0,0 @@
package org.nico.ratel.landlords.enums;
public enum ServerStatus {
BLANK,
WAIT,
STARTING
}
@@ -0,0 +1,12 @@
package org.nico.ratel.landlords.print;
public class SimplePrinter {
public static void print(String msg) {
System.out.print(msg);
}
public static void println(String msg) {
System.out.println(msg);
}
}
@@ -0,0 +1,19 @@
package org.nico.ratel.landlords.print;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class SimpleWriter {
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public static String write() {
try {
return reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
@@ -1,14 +1,6 @@
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;
/**
@@ -18,7 +10,8 @@ import org.nico.ratel.landlords.exception.LandlordException;
* @time 2018-11-01 20:43
*/
public class TransferProtocolUtils {
/**
* A protocol header that represents the beginning of an available stream of data
*/
@@ -68,5 +61,5 @@ public class TransferProtocolUtils {
return Noson.convert(new String(content), clazz);
}
}
}
@@ -0,0 +1,12 @@
package org.nico.ratel.landlords.utils;
public class OptionsUtils {
public static int getOptions(String line) {
int option = -1;
try {
option = Integer.valueOf(line);
}catch(Exception e) {}
return option;
}
}
@@ -1,8 +0,0 @@
package org.nico.ratel.landlords.view;
public class ViewPrinter {
public static void view(String msg) {
System.out.println(msg);
}
}
@@ -4,10 +4,11 @@ import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.atomic.AtomicInteger;
import org.nico.ratel.landlords.entity.ClientSide;
import org.nico.ratel.landlords.entity.ServerSide;
import org.nico.ratel.landlords.entity.Room;
public class ServerContains {
@@ -19,16 +20,16 @@ public class ServerContains {
/**
* The map of server side
*/
public final static Map<Integer, ServerSide> SERVER_SIDE_MAP = new ConcurrentHashMap<>();
public final static Map<Integer, Room> ROOM_MAP = new ConcurrentSkipListMap<>();
/**
* The list of client side
*/
public final static Queue<ClientSide> CLIENT_SIDE_LIST = new ConcurrentLinkedQueue<ClientSide>();
public final static Map<Integer, ClientSide> CLIENT_SIDE_MAP = new ConcurrentSkipListMap<>();
private final static AtomicInteger CLIENT_ATOMIC_ID = new AtomicInteger(0);
private final static AtomicInteger CLIENT_ATOMIC_ID = new AtomicInteger(1);
private final static AtomicInteger SERVER_ATOMIC_ID = new AtomicInteger(0);
private final static AtomicInteger SERVER_ATOMIC_ID = new AtomicInteger(1);
public final static int getClientId() {
return CLIENT_ATOMIC_ID.getAndIncrement();
@@ -0,0 +1,11 @@
package org.nico.ratel.landlords.server.event;
import org.nico.ratel.landlords.entity.ServerTransferData;
import io.netty.channel.Channel;
public interface ServerEventListener<T> {
public void call(Channel channel, ServerTransferData<T> serverTransferData);
}
@@ -0,0 +1,33 @@
package org.nico.ratel.landlords.server.event;
import org.nico.ratel.landlords.channel.ChannelUtils;
import org.nico.ratel.landlords.entity.ClientSide;
import org.nico.ratel.landlords.entity.Room;
import org.nico.ratel.landlords.entity.ServerTransferData;
import org.nico.ratel.landlords.enums.ClientEventCode;
import org.nico.ratel.landlords.enums.RoomStatus;
import org.nico.ratel.landlords.server.ServerContains;
import io.netty.channel.Channel;
public class ServerEventListener_CODE_CREATE_ROOM implements ServerEventListener<Integer>{
@Override
public void call(Channel channel, ServerTransferData<Integer> serverTransferData) {
ClientSide clientSide = ServerContains.CLIENT_SIDE_MAP.get(serverTransferData.getData());
Room room = new Room(ServerContains.getServerId());
room.setStatus(RoomStatus.BLANK);
room.getClientSideMap().put(clientSide.getId(), clientSide);
ServerContains.ROOM_MAP.put(room.getId(), room);
ChannelUtils.pushToClient(channel, ClientEventCode.CODE_JOIN_ROOM_SUCCESS, room);
}
}
@@ -0,0 +1,49 @@
package org.nico.ratel.landlords.server.event;
import org.nico.ratel.landlords.channel.ChannelUtils;
import org.nico.ratel.landlords.entity.ClientSide;
import org.nico.ratel.landlords.entity.Room;
import org.nico.ratel.landlords.entity.ServerTransferData;
import org.nico.ratel.landlords.enums.ClientEventCode;
import org.nico.ratel.landlords.enums.RoomStatus;
import org.nico.ratel.landlords.server.ServerContains;
import io.netty.channel.Channel;
public class ServerEventListener_CODE_JOIN_ROOM implements ServerEventListener<Integer>{
@Override
public void call(Channel channel, ServerTransferData<Integer> serverTransferData) {
Room room = ServerContains.ROOM_MAP.get(serverTransferData.getData());
if(room == null) {
ChannelUtils.pushToClient(channel, ClientEventCode.CODE_SHOW_OPTIONS, ServerContains.ROOM_MAP, "Room inexistence !");
}else if(room.getClientSideMap().size() == 3) {
ChannelUtils.pushToClient(channel, ClientEventCode.CODE_SHOW_OPTIONS, ServerContains.ROOM_MAP, "The room is full !");
}else {
ClientSide clientSide = ServerContains.CLIENT_SIDE_MAP.get(serverTransferData.getClientId());
room.getClientSideMap().put(clientSide.getId(), clientSide);
if(room.getClientSideMap().size() == 3) {
room.setStatus(RoomStatus.STARTING);
for(ClientSide client: room.getClientSideMap().values()) {
ChannelUtils.pushToClient(client.getChannel(), ClientEventCode.CODE_ROOM_STARTING, room);
}
}else {
room.setStatus(RoomStatus.WAIT);
ChannelUtils.pushToClient(channel, ClientEventCode.CODE_JOIN_ROOM_SUCCESS, room);
for(ClientSide client: room.getClientSideMap().values()) {
if(client.getId() != clientSide.getId()) {
ChannelUtils.pushToClient(client.getChannel(), null, null, clientSide.getNickname() + " join room !");
}
}
}
}
}
}
@@ -0,0 +1,23 @@
package org.nico.ratel.landlords.server.event;
import org.nico.ratel.landlords.channel.ChannelUtils;
import org.nico.ratel.landlords.entity.ClientSide;
import org.nico.ratel.landlords.entity.ServerTransferData;
import org.nico.ratel.landlords.enums.ClientEventCode;
import org.nico.ratel.landlords.server.ServerContains;
import io.netty.channel.Channel;
public class ServerEventListener_CODE_RENAME implements ServerEventListener<ClientSide>{
@Override
public void call(Channel channel, ServerTransferData<ClientSide> serverTransferData) {
ClientSide clientSide = serverTransferData.getData();
ServerContains.CLIENT_SIDE_MAP.get(clientSide.getId()).setNickname(clientSide.getNickname());;
ChannelUtils.pushToClient(channel, ClientEventCode.CODE_SHOW_OPTIONS, ServerContains.ROOM_MAP);
}
}
@@ -1,35 +1,34 @@
package org.nico.ratel.landlords.server.handler;
import org.nico.ratel.landlords.channel.ChannelUtils;
import org.nico.ratel.landlords.entity.ClientSide;
import org.nico.ratel.landlords.enums.ClientEventCode;
import org.nico.ratel.landlords.enums.ClientStatus;
import org.nico.ratel.landlords.handler.DefaultDecoder;
import org.nico.ratel.landlords.print.SimplePrinter;
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;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
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());
ch.pipeline().addLast(
new ObjectDecoder(1024 * 1024, ClassResolvers
.weakCachingConcurrentResolver(this.getClass()
.getClassLoader())));
ch.pipeline().addLast(new ObjectEncoder());
ch.pipeline().addLast(new TransferHandler());
ClientSide clientSide = new ClientSide(ServerContains.getClientId(), ClientStatus.TO_CHOOSE, ch);
ServerContains.CLIENT_SIDE_MAP.put(clientSide.getId(), clientSide);
SimplePrinter.println("A client connects to the server" + clientSide.getId());
ChannelUtils.pushToClient(ch, ClientEventCode.CODE_CONNECT, clientSide);
}
}
@@ -1,25 +0,0 @@
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);
}
}
}
@@ -1,8 +1,12 @@
package org.nico.ratel.landlords.server.handler;
import java.util.Arrays;
import org.nico.ratel.landlords.channel.ChannelUtils;
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.server.event.ServerEventListener;
import org.nico.ratel.landlords.transfer.TransferProtocolUtils;
import io.netty.channel.ChannelHandlerContext;
@@ -10,22 +14,37 @@ import io.netty.channel.ChannelInboundHandlerAdapter;
public class TransferHandler extends ChannelInboundHandlerAdapter{
private final static String LISTENER_PREFIX = "org.nico.ratel.landlords.server.event.ServerEventListener_";
@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());
System.out.println(msg);
System.out.println();
ServerTransferData serverTransferData = new ServerTransferData();
serverTransferData.setCode(ServerEventCode.CODE_INTO);
serverTransferData.setData("ok");
if(msg instanceof byte[]) {
System.out.println(Arrays.toString((byte[])msg));
System.out.println(new String((byte[])msg));
}
if(msg instanceof ServerTransferData) {
ServerTransferData serverTransferData = (ServerTransferData) msg;
ServerEventCode code = serverTransferData.getCode();
if(code != null) {
String eventListener = LISTENER_PREFIX + code.name();
try {
Class<ServerEventListener> listenerClass = (Class<ServerEventListener>) Class.forName(eventListener);
ServerEventListener listener = listenerClass.newInstance();
listener.call(ctx.channel(), serverTransferData);
}catch(ClassNotFoundException e) {
e.printStackTrace();
}
}
}
byte[] to = TransferProtocolUtils.serialize(serverTransferData);
ctx.writeAndFlush(to);
}
}
+5
View File
@@ -57,6 +57,11 @@
<artifactId>protobuf-java</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>com.smallnico</groupId>
<artifactId>noson</artifactId>
<version>1.1.4</version>
</dependency>
</dependencies>
<build>