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();
|
}
|
}
|
}
|