From 8ce9ce72d3e32427d01ebe4bf8bef6aa863979ca Mon Sep 17 00:00:00 2001
From: whycq <913841844@qq.com>
Date: 星期五, 17 一月 2025 15:48:36 +0800
Subject: [PATCH] #
---
app/src/main/java/com/example/agvcontroller/socket/NettyServerHandler.java | 70 ++++++++++++++++++++++++++++++++---
1 files changed, 64 insertions(+), 6 deletions(-)
diff --git a/app/src/main/java/com/example/agvcontroller/socket/NettyServerHandler.java b/app/src/main/java/com/example/agvcontroller/socket/NettyServerHandler.java
index 921875a..6496e59 100644
--- a/app/src/main/java/com/example/agvcontroller/socket/NettyServerHandler.java
+++ b/app/src/main/java/com/example/agvcontroller/socket/NettyServerHandler.java
@@ -7,6 +7,10 @@
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
+
+import android.os.Handler;
+import android.os.Looper;
+import android.os.Message;
import android.util.Log;
import com.example.agvcontroller.Item;
@@ -19,6 +23,7 @@
import com.example.agvcontroller.protocol.AGV_A1_DOWN;
import com.example.agvcontroller.protocol.AGV_F0_DOWN;
import com.example.agvcontroller.protocol.AGV_F0_UP;
+import com.example.agvcontroller.protocol.AGV_F0_UP8;
import com.example.agvcontroller.protocol.AgvAction;
import com.example.agvcontroller.protocol.AgvPackage;
import com.example.agvcontroller.protocol.ProtocolType;
@@ -26,28 +31,74 @@
import org.greenrobot.eventbus.EventBus;
+import java.net.InetSocketAddress;
+import java.util.HashMap;
+import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class NettyServerHandler extends AbstractInboundHandler<AgvPackage> {
private static final String TAG = "NettyServerHandler";
private static ConcurrentHashMap<String, Channel> channelMap = new ConcurrentHashMap<>();
+ private Map<String, Runnable> pendingRemovals = new HashMap<>();
+ private Handler handler = new Handler(Looper.getMainLooper()) {
+ @Override
+ public void handleMessage(Message msg) {
+ super.handleMessage(msg);
+ String clientId = (String) msg.obj;
+ removeItem(clientId);
+ }
+ };
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
-// String clientId = ctx.channel().remoteAddress().toString();
-// channelMap.put(clientId, ctx.channel());
-// EventBus.getDefault().post(new Item("",clientId,"3"));
-// Log.d(TAG, "Client connected: " + clientId);
+ String clientId = ctx.channel().remoteAddress().toString();
+ InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
+ String ip = remoteAddress.getAddress().getHostAddress();
+ int port = remoteAddress.getPort();
+ channelMap.put(clientId, ctx.channel());
+ EventBus.getDefault().post(new Item(clientId,ip,port,"--",0));
+ Log.d(TAG, "Client connected: " + clientId);
+
+ // 鍙栨秷寤惰繜鍒犻櫎鎿嶄綔
+ if (pendingRemovals.containsKey(clientId)) {
+ handler.removeCallbacks(pendingRemovals.get(clientId));
+ pendingRemovals.remove(clientId);
+ }
+
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
String clientId = ctx.channel().remoteAddress().toString();
+ InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
+ String ip = remoteAddress.getAddress().getHostAddress();
+ int port = remoteAddress.getPort();
channelMap.remove(clientId);
EventBus.getDefault().post(clientId);
Log.d(TAG, "Client disconnected: " + clientId);
+
+ // 鍚姩寤惰繜鍒犻櫎鎿嶄綔
+ Runnable removalRunnable = new Runnable() {
+ @Override
+ public void run() {
+ removeItem(clientId);
+ }
+ };
+ pendingRemovals.put(clientId, removalRunnable);
+ handler.postDelayed(removalRunnable, 20000); // 20绉掑悗鎵ц鍒犻櫎鎿嶄綔
+
+ }
+
+
+ private void removeItem(String clientId) {
+ if (channelMap.remove(clientId) != null) {
+ Log.d(TAG, "Client removed after 20 seconds: " + clientId);
+ EventBus.getDefault().post(clientId);
+ } else {
+ Log.d(TAG, "Client already reconnected or not found: " + clientId);
+ }
}
// @Override
@@ -71,6 +122,9 @@
@Override
protected boolean channelRead0(ChannelHandlerContext ctx, AgvPackage pac) throws Exception {
String clientId = ctx.channel().remoteAddress().toString();
+ InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
+ String ip = remoteAddress.getAddress().getHostAddress();
+ int port = remoteAddress.getPort();
Log.i("clientId--->",clientId);
Log.i("substring",pac.toString());
String serialNum = pac.getBody().getMessageBody().getSerialNo();
@@ -96,20 +150,24 @@
AGV_12_UP agv_12_up = (AGV_12_UP) pac.getBody().getMessageBody();
agvNo = pac.getHeader().getUniqueNo();
channelMap.put(clientId, ctx.channel());
- EventBus.getDefault().post(new Item("",clientId,agvNo));
+ EventBus.getDefault().post(new Item(clientId,ip,port,agvNo,1));
break label;
case DATA_WITHOUT_CODE_REPORT:
AGV_13_UP agv_13_up = (AGV_13_UP) pac.getBody().getMessageBody();
+ agvNo = pac.getHeader().getUniqueNo();
+ channelMap.put(clientId, ctx.channel());
+ EventBus.getDefault().post(new Item(clientId,ip,port,agvNo,1));
break label;
case LOGIN_REPORT:
AGV_F0_UP agv_f0_up = (AGV_F0_UP) pac.getBody().getMessageBody();
if (null != ackType) {
AgvPackage ackPac = AckMsgBuilder.ofSuccess(pac, ackType);
+ AGV_F0_DOWN agv_f0_down = (AGV_F0_DOWN) ackPac.getBody().getMessageBody();
ctx.writeAndFlush(ackPac);
}
agvNo = pac.getHeader().getUniqueNo();
channelMap.put(clientId, ctx.channel());
- EventBus.getDefault().post(new Item("",clientId,agvNo));
+ EventBus.getDefault().post(new Item(clientId,ip,port,agvNo,1));
break label;
}
--
Gitblit v1.9.1