package com.example.agvcontroller.socket;
|
|
import java.nio.charset.StandardCharsets;
|
|
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.Unpooled;
|
import io.netty.channel.Channel;
|
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
import android.util.Log;
|
import java.util.concurrent.ConcurrentHashMap;
|
|
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
|
|
private static final String TAG = "NettyServerHandler";
|
private static ConcurrentHashMap<String, Channel> channelMap = new ConcurrentHashMap<>();
|
|
@Override
|
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
String clientId = ctx.channel().remoteAddress().toString();
|
channelMap.put(clientId, ctx.channel());
|
Log.d(TAG, "Client connected: " + clientId);
|
}
|
|
@Override
|
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
String clientId = ctx.channel().remoteAddress().toString();
|
channelMap.remove(clientId);
|
Log.d(TAG, "Client disconnected: " + clientId);
|
}
|
|
@Override
|
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
// 处理接收到的消息
|
ByteBuf byteBuf = (ByteBuf) msg;
|
//byte[] data = new byte[byteBuf.readableBytes()];
|
//byteBuf.readBytes(data);
|
//String received = new String(data, StandardCharsets.UTF_8);
|
//System.out.println("Received from client: " + received);
|
//
|
//// 回复消息
|
////ByteBuf response = Unpooled.copiedBuffer("Response from server", StandardCharsets.UTF_8);
|
////ctx.writeAndFlush(response);
|
//
|
//byte[] responseHex = hexStringToByteArray("48656c6c6f20576f726c64"); // "Hello World" in hex
|
//ByteBuf response = Unpooled.wrappedBuffer(responseHex);
|
//ctx.writeAndFlush(response);
|
|
try {
|
while (byteBuf.isReadable()) {
|
byte[] bytes = new byte[byteBuf.readableBytes()];
|
byteBuf.readBytes(bytes);
|
String hexString = bytesToHex(bytes);
|
// 获取agv信息 添加到list中
|
Log.d(TAG, "ctx: " + ctx.channel().remoteAddress().toString() );
|
Log.d(TAG, "Received: " + hexString);
|
}
|
} finally {
|
byteBuf.release();
|
}
|
}
|
|
private String bytesToHex(byte[] bytes) {
|
StringBuilder sb = new StringBuilder();
|
for (byte b : bytes) {
|
sb.append(String.format("%02x", b));
|
}
|
return sb.toString();
|
}
|
// 将十六进制字符串转换为字节数组
|
private byte[] hexStringToByteArray(String s) {
|
int len = s.length();
|
byte[] data = new byte[len / 2];
|
for (int i = 0; i < len; i += 2) {
|
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
|
+ Character.digit(s.charAt(i+1), 16));
|
}
|
return data;
|
}
|
|
public static void sendMessageToClient(String clientId, byte[] message) {
|
Channel channel = channelMap.get(clientId);
|
if (channel != null && channel.isActive()) {
|
ByteBuf buf = Unpooled.wrappedBuffer(message);
|
channel.writeAndFlush(buf);
|
} else {
|
Log.d(TAG, "Client " + clientId + " is not connected");
|
}
|
}
|
|
@Override
|
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
cause.printStackTrace();
|
ctx.close();
|
}
|
|
}
|