Skip to content

Commit

Permalink
✨ add custom protocol feature
Browse files Browse the repository at this point in the history
  • Loading branch information
sanshengshui committed Aug 10, 2020
1 parent b5543e8 commit e717a14
Show file tree
Hide file tree
Showing 12 changed files with 20 additions and 49 deletions.
16 changes: 0 additions & 16 deletions IOT-Guide-Custom-Protocol/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,6 @@
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
</dependencies>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@

import io.netty.channel.Channel;
import iot.technology.custom.protocol.request.LoginRequestPacket;
import lombok.extern.slf4j.Slf4j;

import java.util.Scanner;

/**
* @author james mu
* @date 2020/8/10 17:35
*/
@Slf4j
public class LoginConsoleCommand implements ConsoleCommand {

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

log.info("输入设备Id登录: ");
System.out.print("输入设备Id登录: ");
loginRequestPacket.setClientId(scanner.nextLine());
loginRequestPacket.setPassword("IoT Technology Guide");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

import io.netty.channel.Channel;
import iot.technology.custom.protocol.request.MessageRequestPacket;
import lombok.extern.slf4j.Slf4j;

import java.util.Scanner;

/**
* @author james mu
* @date 2020/8/10 17:41
*/
@Slf4j
public class SendToServerConsoleCommand implements ConsoleCommand {

@Override
public void exec(Scanner scanner, Channel channel) {
log.info("发送消息给服务端: ");
System.out.print("发送消息给服务端: ");
String message = scanner.next();
channel.writeAndFlush(new MessageRequestPacket(message, false, null));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

import io.netty.channel.Channel;
import iot.technology.custom.protocol.request.MessageRequestPacket;
import lombok.extern.slf4j.Slf4j;

import java.util.Scanner;

/**
* @author james mu
* @date 2020/8/10 17:52
*/
@Slf4j
public class SendToUserConsoleCommand implements ConsoleCommand {

@Override
public void exec(Scanner scanner, Channel channel) {
log.info("发送消息给某个用户: ");
System.out.print("发送消息给某个用户: ");
String toClientId = scanner.next();
String message = scanner.next();
channel.writeAndFlush(new MessageRequestPacket(message, true, toClientId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,28 @@
import iot.technology.custom.protocol.response.LoginResponsePacket;
import iot.technology.custom.session.Session;
import iot.technology.custom.util.SessionUtil;
import lombok.extern.slf4j.Slf4j;


/**
* @author james mu
* @date 2020/8/10 17:24
*/
@Slf4j
public class LoginResponseHandler extends SimpleChannelInboundHandler<LoginResponsePacket> {

@Override
protected void channelRead0(ChannelHandlerContext ctx, LoginResponsePacket loginResponsePacket) throws Exception {
String clientId = loginResponsePacket.getClientId();

if (loginResponsePacket.getSuccess()) {
log.info("[" + clientId + "]登录成功, clientId 为: " + clientId);
System.out.println("[" + clientId + "]登录成功, clientId 为: " + clientId);
SessionUtil.bindSession(new Session(clientId), ctx.channel());
} else {
log.error("[" + clientId + "]登录失败,原因:" + loginResponsePacket.getReason());
System.out.println("[" + clientId + "]登录失败,原因:" + loginResponsePacket.getReason());
}
}

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
log.info("客户端连接被关闭!");
System.out.println("客户端连接被关闭!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
* @author james mu
* @date 2020/8/10 17:21
*/
@Slf4j
public class MessageResponseHandler extends SimpleChannelInboundHandler<MessageResponsePacket> {

@Override
protected void channelRead0(ChannelHandlerContext ctx, MessageResponsePacket messageResponsePacket) throws Exception {
String fromClientId = messageResponsePacket.getFromClientId();
log.info(fromClientId + " -> " + messageResponsePacket.getMessage());
System.out.println(fromClientId + " -> " + messageResponsePacket.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@
* @author james mu
* @date 2020/8/2 21:07
*/
@Slf4j
public class IotIdleStateHandler extends IdleStateHandler {

public static final int READER_IDLE_TIME = 15;
public static final int READER_IDLE_TIME = 60;

public IotIdleStateHandler() {
super(READER_IDLE_TIME, 0, 0, TimeUnit.SECONDS);
}

@Override
protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) {
log.info("{}: 秒内未读到数据,关闭连接", READER_IDLE_TIME);
System.out.println(READER_IDLE_TIME + "秒内未读到数据,关闭连接");
ctx.channel().close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public Packet decode(ByteBuf byteBuf) {

byte[] bytes = new byte[length];
byteBuf.readBytes(bytes);
byte ver = byteBuf.readByte();

Class<? extends Packet> requestType = getRequestType(command);
Encryption encryption = getEncryption(enm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
import iot.technology.custom.server.handler.CustomProtocolHandler;
import iot.technology.custom.server.handler.HeartBeatRequestHandler;
import iot.technology.custom.server.handler.LoginRequestHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

/**
* @author james mu
* @date 2020/5/26 22:56
*/
@Slf4j
public class CustomProtocolServer {

private static final int PORT = 8000;
Expand Down Expand Up @@ -55,9 +53,9 @@ protected void initChannel(NioSocketChannel ch) throws Exception {
private static void bind(final ServerBootstrap serverBootstrap, final int port) {
serverBootstrap.bind(port).addListener(future -> {
if (future.isSuccess()) {
log.info("{} 端口:{}, 绑定成功!",new Date(), port);
System.out.println(new Date() + ": 端口[" + port + "]绑定成功!");
} else {
log.error("端口:{}, 绑定失败!");
System.err.println("端口[" + port + "]绑定失败!");
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* @date 2020/8/10 15:42
*/
@ChannelHandler.Sharable
@Slf4j
public class LoginRequestHandler extends SimpleChannelInboundHandler<LoginRequestPacket> {

public static final LoginRequestHandler INSTANCE = new LoginRequestHandler();
Expand All @@ -36,12 +35,12 @@ protected void channelRead0(ChannelHandlerContext ctx, LoginRequestPacket loginR
loginResponsePacket.setSuccess(true);
String clientId = loginRequestPacket.getClientId();
loginResponsePacket.setClientId(clientId);
log.info("[" + loginRequestPacket.getClientId() + "]登录成功");
System.out.println("[" + loginRequestPacket.getClientId() + "]登录成功");
SessionUtil.bindSession(new Session(clientId), ctx.channel());
} else {
loginResponsePacket.setReason("账号密码校验失败");
loginResponsePacket.setSuccess(false);
log.error(new Date() + ": 登录失败!");
System.out.println(new Date() + ": 登录失败!");
}
//登录响应
ctx.writeAndFlush(loginResponsePacket);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
* @date 2020/8/10 16:16
*/
@ChannelHandler.Sharable
@Slf4j
public class MessageRequestHandler extends SimpleChannelInboundHandler<MessageRequestPacket> {
public static final MessageRequestHandler INSTANCE = new MessageRequestHandler();

Expand All @@ -31,7 +30,8 @@ protected void channelRead0(ChannelHandlerContext ctx, MessageRequestPacket mess
Session session = SessionUtil.getSession(ctx.channel());

MessageResponsePacket messageResponsePacket = new MessageResponsePacket();
messageResponsePacket.setMessage(session.getClientId());
messageResponsePacket.setFromClientId(messageRequestPacket.getToClientId());
messageResponsePacket.setMessage(messageRequestPacket.getMessage());

if (messageRequestPacket.getCoordination()) {
Channel toClientChannel = SessionUtil.getChannel(messageRequestPacket.getToClientId());
Expand All @@ -42,8 +42,10 @@ protected void channelRead0(ChannelHandlerContext ctx, MessageRequestPacket mess
}
});
} else {
log.error("[" + session.getClientId() + "] 不在线,发送失败!");
System.out.println("[" + session.getClientId() + "] 不在线,发送失败!");
}
} else {
System.out.println(session.getClientId() + ":" + messageRequestPacket.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import io.netty.channel.Channel;
import iot.technology.custom.attribute.Attributes;
import iot.technology.custom.session.Session;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -13,7 +12,6 @@
* @author james mu
* @date 2020/8/10 15:11
*/
@Slf4j
public class SessionUtil {

private static final Map<String, Channel> clientIdChannelMap = new ConcurrentHashMap<>();
Expand All @@ -28,7 +26,7 @@ public static void unBindSession(Channel channel) {
Session session = getSession(channel);
clientIdChannelMap.remove(session.getClientId());
channel.attr(Attributes.SESSION).set(null);
log.info(session + " 退出登录!");
System.out.println(session + " 退出登录!");
}
}

Expand Down

0 comments on commit e717a14

Please sign in to comment.