Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
闪电侠 committed Oct 4, 2018
1 parent 1acb274 commit ca4bfca
Show file tree
Hide file tree
Showing 26 changed files with 356 additions and 43 deletions.
36 changes: 11 additions & 25 deletions src/main/java/the/flash/client/NettyClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import the.flash.client.console.ConsoleManager;
import the.flash.client.console.LoginConsoleCommand;
import the.flash.client.handler.CreateGroupResponseHandler;
import the.flash.client.handler.LoginResponseHandler;
import the.flash.client.handler.LogoutResponseHandler;
import the.flash.client.handler.MessageResponseHandler;
import the.flash.codec.PacketDecoder;
import the.flash.codec.PacketEncoder;
import the.flash.protocol.request.LoginRequestPacket;
import the.flash.protocol.request.MessageRequestPacket;
import the.flash.util.SessionUtil;

import java.util.Date;
Expand Down Expand Up @@ -44,7 +46,9 @@ public static void main(String[] args) {
public void initChannel(SocketChannel ch) {
ch.pipeline().addLast(new PacketDecoder());
ch.pipeline().addLast(new LoginResponseHandler());
ch.pipeline().addLast(new LogoutResponseHandler());
ch.pipeline().addLast(new MessageResponseHandler());
ch.pipeline().addLast(new CreateGroupResponseHandler());
ch.pipeline().addLast(new PacketEncoder());
}
});
Expand Down Expand Up @@ -73,36 +77,18 @@ private static void connect(Bootstrap bootstrap, String host, int port, int retr
}

private static void startConsoleThread(Channel channel) {
Scanner sc = new Scanner(System.in);
LoginRequestPacket loginRequestPacket = new LoginRequestPacket();
ConsoleManager consoleManager = new ConsoleManager();
LoginConsoleCommand loginConsoleCommand = new LoginConsoleCommand();
Scanner scanner = new Scanner(System.in);

new Thread(() -> {
while (!Thread.interrupted()) {
if (!SessionUtil.hasLogin(channel)) {
System.out.print("输入用户名登录: ");
String username = sc.nextLine();
loginRequestPacket.setUserName(username);

// 密码使用默认的
loginRequestPacket.setPassword("pwd");

// 发送登录数据包
channel.writeAndFlush(loginRequestPacket);
waitForLoginResponse();
loginConsoleCommand.exec(scanner, channel);
} else {
String toUserId = sc.next();
String message = sc.next();
channel.writeAndFlush(new MessageRequestPacket(toUserId, message));
consoleManager.exec(scanner, channel);
}
}
}).start();
}


private static void waitForLoginResponse() {
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
}
}
}
9 changes: 9 additions & 0 deletions src/main/java/the/flash/client/console/ConsoleCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package the.flash.client.console;

import io.netty.channel.Channel;

import java.util.Scanner;

public interface ConsoleCommand {
void exec(Scanner scanner, Channel channel);
}
37 changes: 37 additions & 0 deletions src/main/java/the/flash/client/console/ConsoleManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package the.flash.client.console;

import io.netty.channel.Channel;
import the.flash.util.SessionUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class ConsoleManager implements ConsoleCommand {
private Map<String, ConsoleCommand> consoleCommandMap;

public ConsoleManager() {
consoleCommandMap = new HashMap<>();
consoleCommandMap.put("sendToUser", new SendToUserConsoleCommand());
consoleCommandMap.put("logout", new LogoutConsoleCommand());
consoleCommandMap.put("createGroup", new CreateGroupConsoleCommand());
}

@Override
public void exec(Scanner scanner, Channel channel) {
// 获取第一个指令
String command = scanner.next();

if (!SessionUtil.hasLogin(channel)) {
return;
}

ConsoleCommand consoleCommand = consoleCommandMap.get(command);

if (consoleCommand != null) {
consoleCommand.exec(scanner, channel);
} else {
System.err.println("无法识别[" + command + "]指令,请重新输入!");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package the.flash.client.console;

import io.netty.channel.Channel;
import the.flash.protocol.request.CreateGroupRequestPacket;

import java.util.Arrays;
import java.util.Scanner;

public class CreateGroupConsoleCommand implements ConsoleCommand {

private static final String USER_ID_SPLITER = ",";

@Override
public void exec(Scanner scanner, Channel channel) {
CreateGroupRequestPacket createGroupRequestPacket = new CreateGroupRequestPacket();

System.out.print("【拉人群聊】输入 userId 列表,userId 之间英文逗号隔开:");
String userIds = scanner.next();
createGroupRequestPacket.setUserIdList(Arrays.asList(userIds.split(USER_ID_SPLITER)));
channel.writeAndFlush(createGroupRequestPacket);
}

}
29 changes: 29 additions & 0 deletions src/main/java/the/flash/client/console/LoginConsoleCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package the.flash.client.console;

import io.netty.channel.Channel;
import the.flash.protocol.request.LoginRequestPacket;

import java.util.Scanner;

public class LoginConsoleCommand implements ConsoleCommand {

@Override
public void exec(Scanner scanner, Channel channel) {
LoginRequestPacket loginRequestPacket = new LoginRequestPacket();

System.out.print("输入用户名登录: ");
loginRequestPacket.setUserName(scanner.nextLine());
loginRequestPacket.setPassword("pwd");

// 发送登录数据包
channel.writeAndFlush(loginRequestPacket);
waitForLoginResponse();
}

private static void waitForLoginResponse() {
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
}
}
}
14 changes: 14 additions & 0 deletions src/main/java/the/flash/client/console/LogoutConsoleCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package the.flash.client.console;

import io.netty.channel.Channel;
import the.flash.protocol.request.LogoutRequestPacket;

import java.util.Scanner;

public class LogoutConsoleCommand implements ConsoleCommand {
@Override
public void exec(Scanner scanner, Channel channel) {
LogoutRequestPacket logoutRequestPacket = new LogoutRequestPacket();
channel.writeAndFlush(logoutRequestPacket);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package the.flash.client.console;

import io.netty.channel.Channel;
import the.flash.protocol.request.MessageRequestPacket;

import java.util.Scanner;

public class SendToUserConsoleCommand implements ConsoleCommand {
@Override
public void exec(Scanner scanner, Channel channel) {
System.out.print("发送消息给某个某个用户:");

String toUserId = scanner.next();
String message = scanner.next();
channel.writeAndFlush(new MessageRequestPacket(toUserId, message));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package the.flash.client.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import the.flash.protocol.response.CreateGroupResponsePacket;

public class CreateGroupResponseHandler extends SimpleChannelInboundHandler<CreateGroupResponsePacket> {

@Override
protected void channelRead0(ChannelHandlerContext ctx, CreateGroupResponsePacket createGroupResponsePacket) {
System.out.print("群创建成功,id 为[" + createGroupResponsePacket.getGroupId() + "], ");
System.out.println("群里面有:" + createGroupResponsePacket.getUserNameList());
}
}
14 changes: 14 additions & 0 deletions src/main/java/the/flash/client/handler/LogoutResponseHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package the.flash.client.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import the.flash.protocol.response.LogoutResponsePacket;
import the.flash.util.SessionUtil;

public class LogoutResponseHandler extends SimpleChannelInboundHandler<LogoutResponsePacket> {

@Override
protected void channelRead0(ChannelHandlerContext ctx, LogoutResponsePacket logoutResponsePacket) {
SessionUtil.unBindSession(ctx.channel());
}
}
12 changes: 10 additions & 2 deletions src/main/java/the/flash/protocol/PacketCodeC.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package the.flash.protocol;

import io.netty.buffer.ByteBuf;
import the.flash.protocol.request.CreateGroupRequestPacket;
import the.flash.protocol.request.LoginRequestPacket;
import the.flash.protocol.request.LogoutRequestPacket;
import the.flash.protocol.request.MessageRequestPacket;
import the.flash.protocol.response.CreateGroupResponsePacket;
import the.flash.protocol.response.LoginResponsePacket;
import the.flash.protocol.response.LogoutResponsePacket;
import the.flash.protocol.response.MessageResponsePacket;
import the.flash.serialize.Serializer;
import the.flash.serialize.impl.JSONSerializer;
Expand All @@ -28,10 +32,14 @@ private PacketCodeC() {
packetTypeMap.put(LOGIN_RESPONSE, LoginResponsePacket.class);
packetTypeMap.put(MESSAGE_REQUEST, MessageRequestPacket.class);
packetTypeMap.put(MESSAGE_RESPONSE, MessageResponsePacket.class);
packetTypeMap.put(LOGOUT_REQUEST, LogoutRequestPacket.class);
packetTypeMap.put(LOGOUT_RESPONSE, LogoutResponsePacket.class);
packetTypeMap.put(CREATE_GROUP_REQUEST, CreateGroupRequestPacket.class);
packetTypeMap.put(CREATE_GROUP_RESPONSE, CreateGroupResponsePacket.class);

serializerMap = new HashMap<>();
Serializer serializer = new JSONSerializer();
serializerMap.put(serializer.getSerializerAlogrithm(), serializer);
serializerMap.put(serializer.getSerializerAlgorithm(), serializer);
}

public void encode(ByteBuf byteBuf, Packet packet) {
Expand All @@ -41,7 +49,7 @@ public void encode(ByteBuf byteBuf, Packet packet) {
// 2. 实际编码过程
byteBuf.writeInt(MAGIC_NUMBER);
byteBuf.writeByte(packet.getVersion());
byteBuf.writeByte(Serializer.DEFAULT.getSerializerAlogrithm());
byteBuf.writeByte(Serializer.DEFAULT.getSerializerAlgorithm());
byteBuf.writeByte(packet.getCommand());
byteBuf.writeInt(bytes.length);
byteBuf.writeBytes(bytes);
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/the/flash/protocol/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@ public interface Command {
Byte MESSAGE_REQUEST = 3;

Byte MESSAGE_RESPONSE = 4;

Byte LOGOUT_REQUEST = 5;

Byte LOGOUT_RESPONSE = 6;

Byte CREATE_GROUP_REQUEST = 7;

Byte CREATE_GROUP_RESPONSE = 8;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package the.flash.protocol.request;

import lombok.Data;
import the.flash.protocol.Packet;

import java.util.List;

import static the.flash.protocol.command.Command.CREATE_GROUP_REQUEST;

@Data
public class CreateGroupRequestPacket extends Packet {

private List<String> userIdList;

@Override
public Byte getCommand() {

return CREATE_GROUP_REQUEST;
}
}
15 changes: 15 additions & 0 deletions src/main/java/the/flash/protocol/request/LogoutRequestPacket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package the.flash.protocol.request;

import lombok.Data;
import the.flash.protocol.Packet;

import static the.flash.protocol.command.Command.LOGOUT_REQUEST;

@Data
public class LogoutRequestPacket extends Packet {
@Override
public Byte getCommand() {

return LOGOUT_REQUEST;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package the.flash.protocol.response;

import lombok.Data;
import the.flash.protocol.Packet;

import java.util.List;

import static the.flash.protocol.command.Command.CREATE_GROUP_RESPONSE;

@Data
public class CreateGroupResponsePacket extends Packet {
private boolean success;

private String groupId;

private List<String> userNameList;

@Override
public Byte getCommand() {

return CREATE_GROUP_RESPONSE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class LoginResponsePacket extends Packet {

@Override
public Byte getCommand() {

return LOGIN_RESPONSE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package the.flash.protocol.response;

import lombok.Data;
import the.flash.protocol.Packet;

import static the.flash.protocol.command.Command.LOGOUT_RESPONSE;

@Data
public class LogoutResponsePacket extends Packet {

private boolean success;

private String reason;


@Override
public Byte getCommand() {

return LOGOUT_RESPONSE;
}
}
2 changes: 1 addition & 1 deletion src/main/java/the/flash/serialize/Serializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public interface Serializer {
* 序列化算法
* @return
*/
byte getSerializerAlogrithm();
byte getSerializerAlgorithm();

/**
* java 对象转换成二进制
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package the.flash.serialize;

public interface SerializerAlogrithm {
public interface SerializerAlgorithm {
/**
* json 序列化
*/
Expand Down
Loading

0 comments on commit ca4bfca

Please sign in to comment.