Skip to content

Commit

Permalink
feat: socket 예외처리 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
redcarrot1 committed Jan 11, 2024
1 parent 41a341e commit cd9de93
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package solution.gdsc.PathPal.domain.inference.api;

import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.BinaryMessage;
import org.springframework.web.socket.CloseStatus;
Expand All @@ -19,7 +18,6 @@
import java.util.concurrent.ConcurrentHashMap;

@Component
@Profile("!test")
public class ClientInferenceController extends BinaryWebSocketHandler {

private final String hostName = "127.0.0.1";
Expand All @@ -31,11 +29,7 @@ public class ClientInferenceController extends BinaryWebSocketHandler {

public ClientInferenceController(InferenceService inferenceService) {
this.inferenceService = inferenceService;
try {
this.socketClient = new SocketClient(hostName, port, 2000);
} catch (IOException e) {
throw new RuntimeException("SocketClient 생성 실패", e);
}
this.socketClient = new SocketClient(hostName, port, 2000);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
Expand All @@ -23,7 +24,7 @@ public class SocketClient {
private BufferedInputStream bis;
private BufferedOutputStream bos;

public SocketClient(String hostName, int port, int timeout) throws IOException {
public SocketClient(String hostName, int port, int timeout) {
this.port = port;
this.timeout = timeout;
this.hostName = hostName;
Expand All @@ -32,18 +33,32 @@ public SocketClient(String hostName, int port, int timeout) throws IOException {
socketConnect();
}

private void socketConnect() throws IOException {
if (this.socket != null && this.socket.isConnected()) {
this.socket.close();
private void socketConnect() {
if (this.socket != null) {
if (socket.isConnected() && !socket.isClosed()) {
try {
this.socket.close();
} catch (IOException e) {
}
}
}
this.socket = new Socket();
try {
this.socket.setSoTimeout(timeout);
} catch (IOException e) {
}

try {
this.socket.connect(new InetSocketAddress(hostName, port), timeout);
this.bos = new BufferedOutputStream(socket.getOutputStream());
this.bis = new BufferedInputStream(socket.getInputStream());
} catch (IOException e) {
System.err.println("소켓 연결 실패. 프로그램을 종료합니다.");
System.exit(1);
}
this.socket = new Socket(hostName, port);
this.socket.setSoTimeout(timeout);
this.bos = new BufferedOutputStream(socket.getOutputStream());
this.bis = new BufferedInputStream(socket.getInputStream());
}

public List<Inference> inferenceImage(byte[] file) {

try {
// 1. Send File Size
System.out.println("파일 사이즈 전송:" + file.length);
Expand Down Expand Up @@ -73,13 +88,8 @@ public List<Inference> inferenceImage(byte[] file) {
} catch (SocketTimeoutException e) {
System.err.println("소켓 타임아웃 발생");
System.err.println("소켓 재연결 시도");
try {
socketConnect();
return inferenceImage(file);
} catch (IOException ioException) {
System.err.println("소켓 재연결 실패");
return new ArrayList<>();
}
socketConnect();
return inferenceImage(file);
} catch (Exception e) {
e.printStackTrace();
return new ArrayList<>();
Expand Down

0 comments on commit cd9de93

Please sign in to comment.