diff --git a/src/Controller/Client.java b/src/Controller/Client.java index 1270ccf..c97f9af 100644 --- a/src/Controller/Client.java +++ b/src/Controller/Client.java @@ -1,30 +1,83 @@ package Controller; - -import Controller.GameController; - import java.io.*; import java.net.*; -public class Client { - public void connect(String address, int port) throws UnknownHostException{ - try (Socket socket = new Socket(address, port); - PrintWriter out = new PrintWriter(socket.getOutputStream(), true); - BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); - BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) { +import Exceptions.NotEmptySpaceException; +import Exceptions.OnProgressException; + +public class Client implements Runnable{ + + + private Socket curSocket; + private ObjectOutputStream out; + private ObjectInputStream in; + private SerializeObject obj = null; + + private SerializeObject result = null; + private boolean flag = true; + + // 서버에 객체 전달 + public synchronized void returnObj (SerializeObject obj){ + this.obj = obj; + flag = false; + while(!flag){ + } + } + + public SerializeObject getAnswer(){ + return result; + } - System.out.println("서버에 연결되었습니다."); - String userInput; - while ((userInput = stdIn.readLine()) != null) { - out.println(userInput); - System.out.println("서버로 전송한 메시지: " + userInput); + public synchronized void setFlag() { + flag = true; + notify(); // 메인 스레드에 flag 변경 알림 + } + + public void connect(String address, int port, String name) throws UnknownHostException, NotEmptySpaceException, OnProgressException{ + try { + curSocket = new Socket(address, port); + this.out = new ObjectOutputStream(curSocket.getOutputStream()); + this.in = new ObjectInputStream(curSocket.getInputStream()); + + SerializeObject answer = (SerializeObject)in.readObject(); + String answerStr = (String)answer.getEventObject(); + returnObj(new SerializeObject("received", "String")); + if(answerStr.equals("방이 가득찼습니다.")) { + throw new NotEmptySpaceException(); + } + else if(answerStr.equals("현재 게임이 진행중입니다.")){ + throw new OnProgressException(); + } + + System.out.println("서버에 연결되었습니다."); + + out.writeObject(new SerializeObject(name, "String")); + }catch (IOException e){ + e.printStackTrace(); + } catch (ClassNotFoundException e) { + System.out.println("클래스가 맞지 않습니다."); + } + } - String response = in.readLine(); - System.out.println("서버로부터 받은 응답: " + response); + @Override + public void run() { + try { + while (obj != null) { + out.writeObject(obj); + result = (SerializeObject) in.readObject(); + obj = null; + setFlag(); } - } catch (IOException e) { + } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); + } finally { + try { + curSocket.close(); + } catch (IOException e) { + e.printStackTrace(); + } } } } \ No newline at end of file diff --git a/src/Controller/SerializeObject.java b/src/Controller/SerializeObject.java new file mode 100644 index 0000000..0846e57 --- /dev/null +++ b/src/Controller/SerializeObject.java @@ -0,0 +1,21 @@ +package Controller; + +import java.io.Serializable; + +// 직렬화 객체 +class SerializeObject implements Serializable { + private Object eventObject; // 실제 객체를 포장하는 필드 + private String objectType; // 객체의 유형을 나타내는 필드 + + public SerializeObject(Object eventObject, String objectType) { + this.eventObject = eventObject; + this.objectType = objectType; + } + + public Object getEventObject() { + return eventObject; + } + public String getObjectType() { + return objectType; + } +} \ No newline at end of file diff --git a/src/Controller/Server.java b/src/Controller/Server.java index 5686e07..63873aa 100644 --- a/src/Controller/Server.java +++ b/src/Controller/Server.java @@ -1,5 +1,8 @@ package Controller; +import Model.Game; +import Model.Player; + import java.io.*; import java.net.*; public class Server { @@ -8,8 +11,12 @@ public class Server { static int clientCnt = 0; static int port; static ClientHandler[] handlers = new ClientHandler[GameController.MAX_PLAYER_COUNT]; - public Server(int port) { + + static private GameController gameController; + + public Server(int port, GameController gameController) { this.port = port; + this.gameController = gameController; openServer(); } @@ -26,17 +33,26 @@ public void run() { Socket clientSocket = serverSocket.accept(); System.out.println("클라이언트 연결됨: " + clientSocket.getInetAddress().getHostAddress()); - if (clientCnt < 4) { + if (clientCnt < 4 || GameController.gameState == GameController.GameState.FINISHED) { // 클라이언트와 통신하는 스레드 생성 ClientHandler curHandler = new ClientHandler(clientSocket); clientCnt++; handlers[clientCnt - 1] = curHandler; Thread clientThread = new Thread(curHandler); clientThread.start(); + curHandler.returnObj(new SerializeObject("connected", "String")); + String name = (String)curHandler.getAnswer().getEventObject(); // 플레이어 객체 받기 + gameController.getPlayers()[clientCnt - 1] = new Player(name); } else { PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); - out.println("인원 초과"); + if(clientCnt >= 4) { + out.println("방이 가득찼습니다."); + } + else{ + out.println("현재 게임이 진행중입니다."); + } clientSocket.close(); + out.close(); } } } @@ -53,12 +69,37 @@ class ClientHandler implements Runnable { private Socket clientSocket; private PrintWriter out; private BufferedReader in; + private ObjectOutputStream objectOutputStream; + private ObjectInputStream objectInputStream; + private SerializeObject result = null; + private boolean flag = true; + + private SerializeObject obj = null; + + + // 클라이언트에 객체 전달 + public synchronized void returnObj (SerializeObject obj){ + this.obj = obj; + flag = false; + while(!flag){ + } + } + + public SerializeObject getAnswer(){ + return result; + } + + public synchronized void setFlag() { + flag = true; + notify(); // 메인 스레드에 flag 변경 알림 + } + public ClientHandler(Socket socket) { this.clientSocket = socket; try { - out = new PrintWriter(clientSocket.getOutputStream(), true); - in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); + objectOutputStream = new ObjectOutputStream(clientSocket.getOutputStream()); + objectInputStream = new ObjectInputStream(clientSocket.getInputStream()); } catch (IOException e) { e.printStackTrace(); } @@ -66,13 +107,14 @@ public ClientHandler(Socket socket) { @Override public void run() { - String message; try { - while ((message = in.readLine()) != null) { - System.out.println("클라이언트로부터 받은 메시지: " + message); - out.println("서버에서 응답: " + message); + while (obj != null) { + objectOutputStream.writeObject(obj); + result = (SerializeObject) objectInputStream.readObject(); + obj = null; + setFlag(); } - } catch (IOException e) { + } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } finally { try {