a
554325746@qq.com
2019-12-25 7340eb0b160eacbbd0f3c2289e3ac6150da235f3
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
package com.basic.security.utils.socket.indoor;
 
import android.os.SystemClock;
 
import com.basic.security.base.BaseApplication;
import com.basic.security.manager.RemoteOutdoorManager;
import com.basic.security.utils.Constants;
import com.basic.security.utils.DetectedResult;
import com.basic.security.utils.SocketUtil;
 
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
public class IndoorReceiveIpSocketServer {
    public static boolean startServer = false;
    public static List<IndoorReceiveIpSocketServer.IndoorAcceptedClient> acceptedClientList = new ArrayList<>();
    public static Lock acceptedClientListLock = new ReentrantLock();
 
    public static void startServer() {
        if (startServer) {
            return;
        }
        startServer = true;
        BaseApplication.getApplication().executorService.execute(() -> {
            ServerSocket server = null;
            try {
                server = new ServerSocket(Constants.indoorServerPort);
            } catch (Exception e) {
                e.printStackTrace();
            }
            while (true) {
                try {
                    if (server == null) {
                        break;
                    }
                    Socket client = server.accept();
                    IndoorReceiveIpSocketServer.IndoorAcceptedClient guestAcceptedSocketClient = new IndoorReceiveIpSocketServer.IndoorAcceptedClient(client);
                    guestAcceptedSocketClient.start();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    SystemClock.sleep(3000);
                }
            }
        });
    }
 
    public static class IndoorAcceptedClient extends Thread {
        private final LinkedList<DetectedResult> detectedResultQueue = new LinkedList<>();
        private final LinkedList<byte[]> cameraDataQueue = new LinkedList<byte[]>();
        Socket mSocket;
 
        public IndoorAcceptedClient(Socket socket) {
            this.mSocket = socket;
        }
 
        public void run() {
            acceptedClientListLock.lock();
            acceptedClientList.add(this);
            acceptedClientListLock.unlock();
            SocketUtil socketUtil = new SocketUtil();
            try {
                socketUtil.setSocket(mSocket);
                while (true) {
                    Map<String, Object> requestMap = socketUtil.readMap();
                    RemoteOutdoorManager.updateOutdoor(requestMap);
                    SystemClock.sleep(1000);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                socketUtil.close();
            }
            acceptedClientListLock.lock();
            acceptedClientList.remove(this);
            acceptedClientListLock.unlock();
        }
    }
}