a
554325746@qq.com
2020-01-15 956063ff14bc75e3a2a97c7bcaa06b9edc84ad24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.basic.security.manager;
 
import android.os.SystemClock;
import android.text.TextUtils;
 
import com.basic.security.base.BaseApplication;
import com.basic.security.utils.ExceptionUtil;
import com.basic.security.utils.FaceId;
import com.basic.security.utils.IpUtils;
 
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
public class NodeNameManager {
    static Map<String, String> deviceIdNodeNameMap = new HashMap<>();
    static Lock lock = new ReentrantLock();
    static DatagramSocket receiveSocket = null;
 
    public static void start() {
        sendNodeName();
        receiveNodeName();
    }
 
    public static String getNodeNameByDeviceId(String deviceId) {
        try {
            lock.lock();
            return deviceIdNodeNameMap.get(deviceId);
        } catch (Exception e) {
            ExceptionUtil.printException(e);
        } finally {
            lock.unlock();
        }
        return "";
    }
 
    public static String getIp(String node) {
        try {
            if (node.contains("@")) {
                String[] parts = node.split("@");
                if (parts.length == 2) {
                    return parts[1];
                }
            }
        } catch (Exception e) {
            ExceptionUtil.printException(e);
        }
        return "";
    }
 
    public static String getNodeNameByNodeName(String nodeName) {
        try {
            lock.lock();
            if (nodeName.contains("@")) {
                String deviceId = nodeName.split("@")[0];
                String newNodeName = deviceIdNodeNameMap.get(deviceId);
                if (!TextUtils.isEmpty(newNodeName)) {
                    return newNodeName;
                }
            }
        } catch (Exception e) {
            ExceptionUtil.printException(e);
        } finally {
            lock.unlock();
        }
        return nodeName;
    }
 
    public static void setNodeName(String deviceId, String nodeName) {
        try {
            lock.lock();
            deviceIdNodeNameMap.put(deviceId, nodeName);
        } finally {
            lock.unlock();
        }
    }
 
    public static void sendNodeName() {
        BaseApplication.getApplication().executorService.execute(() -> {
            while (true) {
                try {
                    SystemClock.sleep(5 * 1000);
                    DatagramSocket sendSocket = new DatagramSocket();
                    sendSocket.setBroadcast(true);
                    String msg = (DeviceManager.getDeviceId() + "@" + IpUtils.getIpAddress(BaseApplication.getApplication()));
                    byte[] buffer = msg.getBytes();
                    DatagramPacket packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName("255.255.255.255"), 4445);
                    sendSocket.send(packet);
                    sendSocket.close();
                } catch (Exception e) {
                    System1.out.println("NodeNameManager.sendNodeName " + e.getMessage());
                }
            }
        });
    }
 
    public static void receiveNodeName() {
        BaseApplication.getApplication().executorService.execute(() -> {
            while (true) {
                try {
                    String nodeName = FaceId.instance.receiveBroadcastJ((char) 4445);
                    if (nodeName.contains("@")) {
                        String deviceId = nodeName.split("@", -1)[0];
                        String deviceIp = nodeName.split("@", -1)[1];
                        setNodeName(deviceId, nodeName);
                    }
                } catch (Exception e) {
                    ExceptionUtil.printException(e);
                }
            }
        });
    }
}