package jp.dip.arimodoki.websocket;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import jp.dip.arimodoki.common.CConst;
public class CPUHandler extends TextWebSocketHandler implements CConst {
private Map<String, WebSocketSession> sessionMap_ = new ConcurrentHashMap<>();
private Map<String, CPUHandlerRun> handleMap = new HashMap<>();
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
if(this.sessionMap_.size()>=100) {
logger.log_error(this,"too many Connection");
throw new Exception("too many Connection");
}
this.sessionMap_.put(session.getId(), session);
}
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
this.sessionMap_.remove(session.getId());
CPUHandlerRun thmap = this.handleMap.get(session.getId());
if(thmap != null) {
thmap.stop();
this.handleMap.remove(session.getId());
}
}
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
CPUHandlerRun run = null;
CPUHandlerRun thmap = this.handleMap.get(session.getId());
if(thmap == null) {
run = new CPUHandlerRun(session,this.sessionMap_);
this.handleMap.put(session.getId(), run);
} else {
run = thmap;
}
if(message.getPayload().equals("stop")) {
run.stop();
this.handleMap.remove(session.getId());
} else {
run.start();
}
}
}
|