1
zhanghua
2024-09-26 c775c6953d9759e70f08acbfa8f6d7490aaae3d1
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package com.netsdk.demo.util;
 
import java.io.File;
import java.util.Vector;
 
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.NetSDKLib.LLong;
import com.sun.jna.Pointer;
 
/**
 * SDK 环境配置
 * @author 29779
 *
 */
public class Sdk {
    public static NetSDKLib netsdkApi = NetSDKLib.NETSDK_INSTANCE;
    public static NetSDKLib configApi     = NetSDKLib.CONFIG_INSTANCE;
    
    private Vector<DisconnetListener> disconnects;
    private Vector<ReconnectListener> reconnects;
    private boolean disconnectChanged = false;
    private boolean reconnectChanged = false;
    
    private boolean initialized = false;
    private boolean logOpened = false;
    private String logPath = ".";
    
    private Sdk() {
        disconnects = new Vector<DisconnetListener>();
        reconnects = new Vector<ReconnectListener>();
    }
    
    private static class SDKHolder {
        private static Sdk instance = new Sdk();
    }
    
    public static Sdk environment() {
        return SDKHolder.instance;
    }
    
    /**
     * After the application is started, the method only can be called once
     * 程序启动之后,该接口只能被调用一次 
     */
    public synchronized void init() {
        if (initialized) {
            return;
        }
        initialized = true;
        
        netsdkApi.CLIENT_Init(DisConnectCB.getInstance(), null);
        
        netsdkApi.CLIENT_SetAutoReconnect(HaveReconnectCB.getInstance(), null);
        
        NetSDKLib.NET_PARAM netParam = new NetSDKLib.NET_PARAM();
        netParam.nConnectTime = 5000; // the connection timeout is 5 second
        netsdkApi.CLIENT_SetNetworkParam(netParam);
    }
    
    /**
     * After the application is started, the method only can be called once
     * 程序启动之后,该接口只能被调用一次 
     */
    public synchronized void cleanup() {
        if (logOpened) {
            netsdkApi.CLIENT_LogClose();
            logOpened = false;
        }
        
        if (initialized) {
            netsdkApi.CLIENT_Cleanup();
            initialized = false;
        }
    }
    
    public String getLogPath() {
        return logPath;
    }
 
    public void setLogPath(String logPath) {
        this.logPath = logPath;
    }
 
    /**
     * the default log file (netsdk.log) will be generated in the current directory
     * 将会在当前目录生成默认的日志文件
     */
    public synchronized void openLog() {
        if (logOpened) {
            return;
        }
        logOpened = true;
        
        NetSDKLib.LOG_SET_PRINT_INFO logInfo = new NetSDKLib.LOG_SET_PRINT_INFO();
        
        File currentPath = new File(logPath);        
        String logPath = currentPath.getAbsoluteFile().getParent() + "netsdk.log";
        
        logInfo.bSetFilePath = 1; // enable file path
        System.arraycopy(logPath.getBytes(), 0, logInfo.szLogFilePath, 0, logPath.getBytes().length);
        
        logInfo.bSetPrintStrategy = 1; // enable print strategy
        logInfo.nPrintStrategy = 0;
        logOpened = netsdkApi.CLIENT_LogOpen(logInfo);
        if (!logOpened) {
            System.err.println("Failed to open NetSDK log !!!");
        }
    }
    
    public synchronized void setDisconnectChanged() {
        this.disconnectChanged = true;
    }
    
    public synchronized void clearDisconnectChanged() {
        this.disconnectChanged = false;
    }
 
    public synchronized void setReconnectChanged() {
        this.reconnectChanged = true;
    }
    
    public synchronized void clearReconnectChanged() {
        this.reconnectChanged = false;
    }
 
    public synchronized void addDisconnectListener(DisconnetListener d) {
        if (d == null)
            throw new NullPointerException();
        
        if (!disconnects.contains(d)) {
            disconnects.addElement(d);
        }
    }
    
    public synchronized void deleteDisconnectListener(DisconnetListener d) {
        disconnects.remove(d);
    }
    
    public synchronized void addReconnectListener(ReconnectListener r) {
        if (r == null)
            throw new NullPointerException();
        
        if (!reconnects.contains(r)) {
            reconnects.addElement(r);
        }
    }
    
    public synchronized void deleteReconnectListener(ReconnectListener r) {
        reconnects.remove(r);
    }
    
    public void notifyDisconnect(LLong loginHandle, String deviceIp, int devicePort) {
        Object[] arrLocal;
        synchronized (disconnects) {
            if (!disconnectChanged)
                return;
            arrLocal = disconnects.toArray();
            clearDisconnectChanged();
        }
 
        for (int i = arrLocal.length-1; i>=0; i--)
            ((DisconnetListener)arrLocal[i]).callback(loginHandle, deviceIp, devicePort);
    }
    
    private void notifyReconnect(LLong loginHandle, String deviceIp, int devicePort) {
        Object[] arrLocal;
        synchronized (reconnects) {
            if (!reconnectChanged)
                return;
            arrLocal = reconnects.toArray();
            clearReconnectChanged();
        }
        
        for (int i = arrLocal.length-1; i>=0; i--)
            ((ReconnectListener)arrLocal[i]).callback(loginHandle, deviceIp, devicePort);
    }
    
    private static class DisConnectCB implements NetSDKLib.fDisConnect {
        private static final DisConnectCB instance = new DisConnectCB();
        private DisConnectCB() {}
        public static DisConnectCB getInstance() {
            return instance;
        }
        
        public void invoke(LLong loginHandle, String deviceIp, int devicePort, Pointer dwUser){
            System.out.printf("Device [%s:%d] Disconnect!\n" , deviceIp , devicePort);
            
            Sdk.environment().setDisconnectChanged();
            Sdk.environment().notifyDisconnect(loginHandle, deviceIp, devicePort);
        }
    } 
    
    private static class HaveReconnectCB implements NetSDKLib.fHaveReConnect {
        private static final HaveReconnectCB instance = new HaveReconnectCB();
        private HaveReconnectCB() {}
        public static HaveReconnectCB getInstance() {
            return instance;
        }
        
        public void invoke(LLong loginHandle, String deviceIp, int devicePort, Pointer dwUser) {
            System.out.printf("Device[%s:%d] Reconnect!\n", deviceIp, devicePort);
            
            Sdk.environment().setReconnectChanged();
            Sdk.environment().notifyReconnect(loginHandle, deviceIp, devicePort);
        }
    }
}