|
@@ -1,15 +1,9 @@
|
|
-package com.its.pis.websocket.C2F.server;
|
|
|
|
|
|
+package com.its.pis.websocket;
|
|
|
|
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
import com.fasterxml.jackson.core.type.TypeReference;
|
|
-import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
-import com.its.app.AppUtils;
|
|
|
|
import com.its.app.utils.ItsUtils;
|
|
import com.its.app.utils.ItsUtils;
|
|
-import com.its.app.utils.SysUtils;
|
|
|
|
import com.its.pis.entity.TbPisInfr;
|
|
import com.its.pis.entity.TbPisInfr;
|
|
-import com.its.pis.entity.TbPisInfrStts;
|
|
|
|
-import com.its.pis.global.AppRepository;
|
|
|
|
import com.its.pis.process.DbmsJobData;
|
|
import com.its.pis.process.DbmsJobData;
|
|
-import com.its.pis.process.DbmsJobProcess;
|
|
|
|
import com.its.pis.process.DbmsJobType;
|
|
import com.its.pis.process.DbmsJobType;
|
|
import com.its.pis.websocket.C2F.message.*;
|
|
import com.its.pis.websocket.C2F.message.*;
|
|
import com.its.pis.websocket.common.SubscribeIdentifier;
|
|
import com.its.pis.websocket.common.SubscribeIdentifier;
|
|
@@ -19,82 +13,22 @@ import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.io.FileUtils;
|
|
import org.apache.commons.io.FileUtils;
|
|
import org.springframework.stereotype.Controller;
|
|
import org.springframework.stereotype.Controller;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
-import org.springframework.web.socket.CloseStatus;
|
|
|
|
import org.springframework.web.socket.TextMessage;
|
|
import org.springframework.web.socket.TextMessage;
|
|
import org.springframework.web.socket.WebSocketSession;
|
|
import org.springframework.web.socket.WebSocketSession;
|
|
-import org.springframework.web.socket.handler.TextWebSocketHandler;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
-import java.util.*;
|
|
|
|
|
|
+import java.util.Base64;
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
@Controller
|
|
@Controller
|
|
-@RequestMapping(C2FWebSocketConfig.WS_ENDPOINT)
|
|
|
|
-public class C2FWebSocketHandler extends TextWebSocketHandler {
|
|
|
|
-
|
|
|
|
- private ObjectMapper mapper;
|
|
|
|
- private DbmsJobProcess dbmsJobProcess;
|
|
|
|
-
|
|
|
|
- public C2FWebSocketHandler() {
|
|
|
|
-
|
|
|
|
- Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
|
|
|
- for (Map.Entry<org.springframework.web.socket.WebSocketSession, C2FWebSocketSession> obj : C2FWebSocketSessionManager.getInstance().getMap().entrySet()) {
|
|
|
|
- if (obj.getValue() != null) {
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }));
|
|
|
|
-
|
|
|
|
- this.mapper = new ObjectMapper();
|
|
|
|
- this.dbmsJobProcess = (DbmsJobProcess) AppUtils.getBean(DbmsJobProcess.class);
|
|
|
|
|
|
+@RequestMapping(PisWebSocketConfig.WS_C2F_ENDPOINT)
|
|
|
|
+public class PisWebSocketHandlerC2F extends PisWebSocketHandler {
|
|
|
|
|
|
|
|
+ public PisWebSocketHandlerC2F() {
|
|
|
|
+ super();
|
|
log.info("C2FWebSocketHandler() START");
|
|
log.info("C2FWebSocketHandler() START");
|
|
}
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
|
- public void afterConnectionEstablished(WebSocketSession session) throws Exception {
|
|
|
|
- // 클라이언트가 연결되었을때 실행
|
|
|
|
- String ipAddr = session.getRemoteAddress().getHostString();
|
|
|
|
- String token = null;
|
|
|
|
- for (Map.Entry<String, List<String>> l : session.getHandshakeHeaders().entrySet()) {
|
|
|
|
- if ("sec-websocket-protocol".equals(l.getKey())) {
|
|
|
|
- token = l.getValue().get(0);
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if (token == null) {
|
|
|
|
- log.error("afterConnectionEstablished: {}, token not found, session will be closed", ipAddr);
|
|
|
|
- session.close();
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- log.info("afterConnectionEstablished: ipAddr={}, token={}", ipAddr, token);
|
|
|
|
-
|
|
|
|
- for (Map.Entry<String, TbPisInfr> e : AppRepository.getInstance().getPisNmbrMap().entrySet()) {
|
|
|
|
- TbPisInfr pis = e.getValue();
|
|
|
|
- if (token.equals(pis.getPIS_TOKEN())) {
|
|
|
|
- C2FWebSocketSession vo = new C2FWebSocketSession(pis, this, session);
|
|
|
|
- pis.channelOpen(vo);
|
|
|
|
- C2FWebSocketSessionManager.getInstance().addSession(session, vo);
|
|
|
|
-
|
|
|
|
- session.setBinaryMessageSizeLimit(1*1024*1024);
|
|
|
|
- session.setTextMessageSizeLimit(1*1024*1024);
|
|
|
|
- super.afterConnectionEstablished(session);
|
|
|
|
- log.info("afterConnectionEstablished: connection accept, {}, {}", pis.getPIS_ID(), pis.getPIS_NM());
|
|
|
|
-
|
|
|
|
- // TODO: 주차정보시스템에 연결된 모든 주차장에 대해서 통신상태를 업데이트 해야함
|
|
|
|
- List<TbPisInfrStts> pisSttsList = Collections.synchronizedList(new ArrayList<>());
|
|
|
|
- String UPDT_DT = SysUtils.getSysTime();
|
|
|
|
- pis.getStts().setUPDT_DT(UPDT_DT);
|
|
|
|
- pisSttsList.add(pis.getStts());
|
|
|
|
- dbmsJobProcess.add(new DbmsJobData(DbmsJobType.DATA_TYPE_PIS_STTS, false, pisSttsList, pis));
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- log.error("afterConnectionEstablished: ipAddr={}, token={}, not found token, session will be closed", ipAddr, token);
|
|
|
|
- session.close();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
//클라이언트가 웹소켓 서버로 메시지를 전송했을 때 실행
|
|
//클라이언트가 웹소켓 서버로 메시지를 전송했을 때 실행
|
|
@Override
|
|
@Override
|
|
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
|
|
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
|
|
@@ -106,7 +40,7 @@ public class C2FWebSocketHandler extends TextWebSocketHandler {
|
|
}
|
|
}
|
|
log.info("handleTextMessage: Payload, RX] {}", payloadMessage);
|
|
log.info("handleTextMessage: Payload, RX] {}", payloadMessage);
|
|
|
|
|
|
- C2FWebSocketSession sessionClient = C2FWebSocketSessionManager.getInstance().getSession(session);
|
|
|
|
|
|
+ PisWebSocketSession sessionClient = PisWebSocketSessionManager.getInstance().getSession(session);
|
|
if (sessionClient == null) {
|
|
if (sessionClient == null) {
|
|
log.error("handleTextMessage: Request session not found, session will be closed {}", session);
|
|
log.error("handleTextMessage: Request session not found, session will be closed {}", session);
|
|
session.close();
|
|
session.close();
|
|
@@ -203,39 +137,4 @@ public class C2FWebSocketHandler extends TextWebSocketHandler {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- //클라이언트 연결을 끊었을 때 실행
|
|
|
|
- @Override
|
|
|
|
- public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
|
|
|
|
- log.warn("afterConnectionClosed: session={}, URI={}, UUID={}", session.getRemoteAddress(), session.getUri(), session.getId());
|
|
|
|
- C2FWebSocketSession sessionClient = C2FWebSocketSessionManager.getInstance().getSession(session);
|
|
|
|
- if (sessionClient != null) {
|
|
|
|
- TbPisInfr pis = sessionClient.getObj();
|
|
|
|
- if (pis != null) {
|
|
|
|
- pis.channelClosed();
|
|
|
|
-
|
|
|
|
- // TODO: 주차정보시스템에 연결된 모든 주차장에 대해서 통신상태를 업데이트 해야함
|
|
|
|
- List<TbPisInfrStts> pisSttsList = Collections.synchronizedList(new ArrayList<>());
|
|
|
|
- String UPDT_DT = SysUtils.getSysTime();
|
|
|
|
- pis.getStts().setUPDT_DT(UPDT_DT);
|
|
|
|
- pisSttsList.add(pis.getStts());
|
|
|
|
- log.error("{}", pis.getStts());
|
|
|
|
- dbmsJobProcess.add(new DbmsJobData(DbmsJobType.DATA_TYPE_PIS_STTS, false, pisSttsList, pis));
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- else {
|
|
|
|
- log.warn("afterConnectionClosed: Not Found session={}, URI={}, UUID={}", session.getRemoteAddress(), session.getUri(), session.getId());
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- C2FWebSocketSessionManager.getInstance().removeSession(session);
|
|
|
|
- super.afterConnectionClosed(session, status);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- @Override
|
|
|
|
- public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
|
|
|
|
- log.error("handleTransportError: {}", exception.getMessage());
|
|
|
|
- if (session.isOpen()) {
|
|
|
|
- session.close();
|
|
|
|
- }
|
|
|
|
- super.handleTransportError(session, exception);
|
|
|
|
- }
|
|
|
|
}
|
|
}
|