潘志宝
2024-11-28 231897591c909b164defebfdb5936387ec2807d0
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
118
119
120
121
122
123
124
125
package com.iailab.netsdk.demo.module;
 
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
 
import com.sun.jna.Memory;
import com.sun.jna.Pointer;
 
import com.iailab.netsdk.lib.NetSDKLib.*;
import com.iailab.netsdk.lib.ToolKits;
import com.iailab.netsdk.lib.enumeration.EM_SEND_SEARCH_TYPE;
import com.iailab.netsdk.lib.structure.NET_IN_STARTSERACH_DEVICE;
import com.iailab.netsdk.lib.structure.NET_OUT_STARTSERACH_DEVICE;
 
 
/**
 * 设备搜索接口实现
 * 主要功能有 : 设备组播和广播搜索、设备IP单播搜索
 */
public class DeviceSearchModule {
 
    /**
     * 设备组播和广播搜索
     * @throws SocketException 
     */
    public static LLong multiBroadcastDeviceSearch(fSearchDevicesCBEx cbSearchDevices,String szlocalIp) throws SocketException {
        NET_IN_STARTSERACH_DEVICE pInparm = new NET_IN_STARTSERACH_DEVICE();
        
        pInparm.cbSearchDevices=cbSearchDevices;        
        System.arraycopy(szlocalIp.getBytes(), 0, pInparm.szLocalIp, 0, szlocalIp.getBytes().length);
        pInparm.emSendType=EM_SEND_SEARCH_TYPE.EM_SEND_SEARCH_TYPE_MULTICAST_AND_BROADCAST.ordinal();
        
        Pointer pInBuf =new Memory(pInparm.size());
        ToolKits.SetStructDataToPointer(pInparm, pInBuf, 0);
        
        NET_OUT_STARTSERACH_DEVICE pOutparm =new NET_OUT_STARTSERACH_DEVICE();
        
        Pointer pOutBuf =new Memory(pOutparm.size());
        ToolKits.SetStructDataToPointer(pOutparm, pOutBuf, 0);
        
        return LoginModule.netsdk.CLIENT_StartSearchDevicesEx(pInBuf, pOutBuf);
    }
    
    /**
     * 停止设备组播和广播搜索
     */
    public static void stopDeviceSearch(LLong m_DeviceSearchHandle) {
        if(m_DeviceSearchHandle.longValue() == 0) {
            return;
        }
        
        LoginModule.netsdk.CLIENT_StopSearchDevices(m_DeviceSearchHandle);
        m_DeviceSearchHandle.setValue(0);
    }
    
    /**
     * 设备IP单播搜索
     * @param startIP 起始IP
     * @param nIpNum IP个数,最大 256
     * @throws SocketException 
     */
    public static boolean unicastDeviceSearch(String localIp,String startIP, int nIpNum, fSearchDevicesCB cbSearchDevices) throws SocketException {
        String[] szIPStr = startIP.split("\\.");
        
        DEVICE_IP_SEARCH_INFO deviceSearchInfo = new DEVICE_IP_SEARCH_INFO();
        deviceSearchInfo.nIpNum = nIpNum;
        for(int i = 0; i < deviceSearchInfo.nIpNum; i++) {
            System.arraycopy(getIp(szIPStr, i).getBytes(), 0, deviceSearchInfo.szIPArr[i].szIP, 0, getIp(szIPStr, i).getBytes().length);
        }
        if(LoginModule.netsdk.CLIENT_SearchDevicesByIPs(deviceSearchInfo, cbSearchDevices, null, localIp, 6000)) {
            System.out.println("SearchDevicesByIPs Succeed!");
            return true;
        }
        return false;
    }
    
    public static String getIp(String[] ip, int num) {
        String szIp = "";
        if(Integer.parseInt(ip[3]) >= 255) {
            szIp = ip[0] + "." + ip[1] + "." + String.valueOf(Integer.parseInt(ip[2]) + 1) + "." + String.valueOf(Integer.parseInt(ip[3]) + num - 255);
        } else {
            szIp = ip[0] + "." + ip[1] + "." + ip[2] + "." + String.valueOf(Integer.parseInt(ip[3]) + num);
        }
        
        return szIp;
    }
    
    /**
     * 获取多网卡IP 
     */
    public static List<String> getHostAddress() throws SocketException {
        
        List<String> ipList = new ArrayList<String>();
        if(NetworkInterface.getNetworkInterfaces() == null) {
            return ipList;
        }        
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();       
        
        while (networkInterfaces.hasMoreElements()) {
            NetworkInterface networkInterface = networkInterfaces.nextElement();
            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
            
            while (inetAddresses.hasMoreElements()) {
                InetAddress inetAddress = inetAddresses.nextElement();                             
                
                if (inetAddress.isLoopbackAddress()) {//回路地址,如127.0.0.1
//                    System.out.println("loop addr:" + inetAddress);
                } else if (inetAddress.isLinkLocalAddress()) {//169.254.x.x
//                    System.out.println("link addr:" + inetAddress);
                } else if(inetAddress instanceof Inet4Address){
                    //非链接和回路真实ip                                          
                         String localname = inetAddress.getHostName();
                         String localip = inetAddress.getHostAddress();
                         ipList.add(localip);                                                                              
                }
            }
        }        
        return ipList;
    }
}