diff --git a/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/ClientContains.java b/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/ClientContains.java index 19c854c..7ccafcd 100644 --- a/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/ClientContains.java +++ b/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/ClientContains.java @@ -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 serverSides = new LinkedHashMap<>(); + public static ClientSide clientSide = null; } diff --git a/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/SimpleClient.java b/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/SimpleClient.java index 6ba9e30..ae6e84e 100644 --- a/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/SimpleClient.java +++ b/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/SimpleClient.java @@ -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(){ - - @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(); } } + } diff --git a/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener.java b/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener.java new file mode 100644 index 0000000..9ce9052 --- /dev/null +++ b/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener.java @@ -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 { + + public void call(Channel channel, ClientTransferData clientTransferData); + +} diff --git a/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CONNECT.java b/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CONNECT.java new file mode 100644 index 0000000..6aeaf58 --- /dev/null +++ b/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_CONNECT.java @@ -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{ + + @Override + public void call(Channel channel, ClientTransferData 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); + } + + + +} diff --git a/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_JOIN_ROOM_SUCCESS.java b/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_JOIN_ROOM_SUCCESS.java new file mode 100644 index 0000000..d5bea98 --- /dev/null +++ b/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_JOIN_ROOM_SUCCESS.java @@ -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{ + + @Override + public void call(Channel channel, ClientTransferData clientTransferData) { + SimplePrinter.println("You has been join room:" + clientTransferData.getData()); + SimplePrinter.println("Please wait for other players to join !"); + } + + + +} diff --git a/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_STARTING.java b/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_STARTING.java new file mode 100644 index 0000000..9a0e86d --- /dev/null +++ b/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_ROOM_STARTING.java @@ -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{ + + @Override + public void call(Channel channel, ClientTransferData clientTransferData) { + System.out.println(clientTransferData.getData() + " game starting !"); + } + + + +} diff --git a/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java b/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java new file mode 100644 index 0000000..774bce8 --- /dev/null +++ b/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/event/ClientEventListener_CODE_SHOW_OPTIONS.java @@ -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>{ + + @Override + public void call(Channel channel, ClientTransferData> 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 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 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); + } + } + + } + + + +} diff --git a/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/DefaultChannelInitializer.java b/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/DefaultChannelInitializer.java new file mode 100644 index 0000000..e4f4af4 --- /dev/null +++ b/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/DefaultChannelInitializer.java @@ -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{ + + + @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()); + } + +} diff --git a/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/TransferHandler.java b/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/TransferHandler.java index 3e53942..fc7405c 100644 --- a/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/TransferHandler.java +++ b/ratel-landlords/landlords-client/src/main/java/org/nico/ratel/landlords/client/handler/TransferHandler.java @@ -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 listenerClass = (Class) Class.forName(eventListener); + ClientEventListener listener = listenerClass.newInstance(); + listener.call(ctx.channel(), clientTransferData); + }catch(ClassNotFoundException e) { + e.printStackTrace(); + } + } + } } + } diff --git a/ratel-landlords/landlords-common/pom.xml b/ratel-landlords/landlords-common/pom.xml index 5ecf3fe..86146d5 100644 --- a/ratel-landlords/landlords-common/pom.xml +++ b/ratel-landlords/landlords-common/pom.xml @@ -13,11 +13,4 @@ 1.0.0 - - - com.smallnico - noson - 1.1.4 - - \ No newline at end of file diff --git a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/channel/ChannelUtils.java b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/channel/ChannelUtils.java new file mode 100644 index 0000000..aabac92 --- /dev/null +++ b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/channel/ChannelUtils.java @@ -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 void pushToClient(Channel channel, ClientEventCode code, T datas) { + pushToClient(channel, code, datas, null); + } + + public static void pushToClient(Channel channel, ClientEventCode code, T datas, String msg) { + ClientTransferData clientTransferData = new ClientTransferData(code, datas, msg); + channel.writeAndFlush(clientTransferData); + } + + public static void pushToServer(Channel channel, int clientId, int roomId, ServerEventCode code, T datas) { + ServerTransferData serverTransferData = new ServerTransferData(clientId, roomId, code, datas); + channel.writeAndFlush(serverTransferData); + } + + public static void pushToServer(Channel channel, int clientId, ServerEventCode code, T datas) { + pushToServer(channel, clientId, -1, code, datas); + } + + public static void pushToServer(Channel channel, ServerEventCode code, T datas) { + pushToServer(channel, -1, -1, code, datas); + } + +} diff --git a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ClientSide.java b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ClientSide.java index a30d0b2..2d77a09 100644 --- a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ClientSide.java +++ b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ClientSide.java @@ -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 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 getPokers() { diff --git a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ClientTransferData.java b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ClientTransferData.java index a565900..cc057ae 100644 --- a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ClientTransferData.java +++ b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ClientTransferData.java @@ -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 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 + "]"; } - + } diff --git a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Poker.java b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Poker.java index d38f485..05af5b6 100644 --- a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Poker.java +++ b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Poker.java @@ -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; diff --git a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Room.java b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Room.java new file mode 100644 index 0000000..6f5a2bb --- /dev/null +++ b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/entity/Room.java @@ -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 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 getClientSideMap() { + return clientSideMap; + } + + public final void setClientSideMap(Map clientSideMap) { + this.clientSideMap = clientSideMap; + } + + @Override + public String toString() { + return status.getMsg() + "|" + clientSideMap.size() + " online"; + } + +} diff --git a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ServerSide.java b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ServerSide.java deleted file mode 100644 index 240c83f..0000000 --- a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ServerSide.java +++ /dev/null @@ -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 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 getPokers() { - return pokers; - } - - public final void setPokers(Map pokers) { - this.pokers = pokers; - } - -} diff --git a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ServerTransferData.java b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ServerTransferData.java index 1f61ab6..2dd2856 100644 --- a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ServerTransferData.java +++ b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/entity/ServerTransferData.java @@ -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 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 + "]"; - } - } diff --git a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientEventCode.java b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientEventCode.java index 7c0e213..a7a2388 100644 --- a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientEventCode.java +++ b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientEventCode.java @@ -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; + } } diff --git a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientStatus.java b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientStatus.java index d7ebbb5..ecdeb52 100644 --- a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientStatus.java +++ b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientStatus.java @@ -1,6 +1,8 @@ package org.nico.ratel.landlords.enums; -public enum ClientStatus { +import java.io.Serializable; + +public enum ClientStatus implements Serializable{ TO_CHOOSE, diff --git a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientType.java b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientType.java index 8dacbd3..cdfb8e5 100644 --- a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientType.java +++ b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ClientType.java @@ -1,6 +1,8 @@ package org.nico.ratel.landlords.enums; -public enum ClientType { +import java.io.Serializable; + +public enum ClientType implements Serializable{ LANDLORD, diff --git a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/PokerLevel.java b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/PokerLevel.java index 7f683a1..5300dc7 100644 --- a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/PokerLevel.java +++ b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/PokerLevel.java @@ -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"), diff --git a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/PokerType.java b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/PokerType.java index 6d3a484..ce9ba47 100644 --- a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/PokerType.java +++ b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/PokerType.java @@ -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("♦"), diff --git a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/RoomStatus.java b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/RoomStatus.java new file mode 100644 index 0000000..13a39c8 --- /dev/null +++ b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/RoomStatus.java @@ -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; + } + +} diff --git a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ServerEventCode.java b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ServerEventCode.java index 52e8dcc..4fa0b66 100644 --- a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ServerEventCode.java +++ b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ServerEventCode.java @@ -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; + } } diff --git a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ServerStatus.java b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ServerStatus.java deleted file mode 100644 index 245d5aa..0000000 --- a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/enums/ServerStatus.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.nico.ratel.landlords.enums; - -public enum ServerStatus { - - BLANK, - - WAIT, - - STARTING - -} diff --git a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/print/SimplePrinter.java b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/print/SimplePrinter.java new file mode 100644 index 0000000..a92b133 --- /dev/null +++ b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/print/SimplePrinter.java @@ -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); + } +} diff --git a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/print/SimpleWriter.java b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/print/SimpleWriter.java new file mode 100644 index 0000000..bdb8e6f --- /dev/null +++ b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/print/SimpleWriter.java @@ -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; + } +} diff --git a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/transfer/TransferProtocolUtils.java b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/transfer/TransferProtocolUtils.java index b77777f..b255f39 100644 --- a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/transfer/TransferProtocolUtils.java +++ b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/transfer/TransferProtocolUtils.java @@ -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); } } - + } diff --git a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/utils/OptionsUtils.java b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/utils/OptionsUtils.java new file mode 100644 index 0000000..d3c4403 --- /dev/null +++ b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/utils/OptionsUtils.java @@ -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; + } +} diff --git a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/view/ViewPrinter.java b/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/view/ViewPrinter.java deleted file mode 100644 index 75d046f..0000000 --- a/ratel-landlords/landlords-common/src/main/java/org/nico/ratel/landlords/view/ViewPrinter.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.nico.ratel.landlords.view; - -public class ViewPrinter { - - public static void view(String msg) { - System.out.println(msg); - } -} diff --git a/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/ServerContains.java b/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/ServerContains.java index 5ba317b..82ee2f2 100644 --- a/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/ServerContains.java +++ b/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/ServerContains.java @@ -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 SERVER_SIDE_MAP = new ConcurrentHashMap<>(); + public final static Map ROOM_MAP = new ConcurrentSkipListMap<>(); /** * The list of client side */ - public final static Queue CLIENT_SIDE_LIST = new ConcurrentLinkedQueue(); + public final static Map 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(); diff --git a/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener.java b/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener.java new file mode 100644 index 0000000..16af28c --- /dev/null +++ b/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener.java @@ -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 { + + public void call(Channel channel, ServerTransferData serverTransferData); + +} diff --git a/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CREATE_ROOM.java b/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CREATE_ROOM.java new file mode 100644 index 0000000..531d4a8 --- /dev/null +++ b/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_CREATE_ROOM.java @@ -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{ + + @Override + public void call(Channel channel, ServerTransferData 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); + } + + + + + +} diff --git a/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_JOIN_ROOM.java b/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_JOIN_ROOM.java new file mode 100644 index 0000000..31ac4bb --- /dev/null +++ b/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_JOIN_ROOM.java @@ -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{ + + @Override + public void call(Channel channel, ServerTransferData 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 !"); + } + } + } + } + } + + + + + +} diff --git a/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_RENAME.java b/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_RENAME.java new file mode 100644 index 0000000..66196b2 --- /dev/null +++ b/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/event/ServerEventListener_CODE_RENAME.java @@ -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{ + + @Override + public void call(Channel channel, ServerTransferData 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); + } + +} diff --git a/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/DefaultChannelInitializer.java b/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/DefaultChannelInitializer.java index 7b821bf..868e863 100644 --- a/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/DefaultChannelInitializer.java +++ b/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/DefaultChannelInitializer.java @@ -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{ - @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); } + } diff --git a/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/ServerDecoder.java b/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/ServerDecoder.java deleted file mode 100644 index 6b097dd..0000000 --- a/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/ServerDecoder.java +++ /dev/null @@ -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 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); - } - } - -} diff --git a/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/TransferHandler.java b/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/TransferHandler.java index bf7b670..8805ed1 100644 --- a/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/TransferHandler.java +++ b/ratel-landlords/landlords-server/src/main/java/org/nico/ratel/landlords/server/handler/TransferHandler.java @@ -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 listenerClass = (Class) 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); } } diff --git a/ratel-landlords/pom.xml b/ratel-landlords/pom.xml index f9271ae..991a837 100644 --- a/ratel-landlords/pom.xml +++ b/ratel-landlords/pom.xml @@ -57,6 +57,11 @@ protobuf-java 3.6.1 + + com.smallnico + noson + 1.1.4 +