From d3c89f36fd442cbf3554d442da6a527bcce99f1a Mon Sep 17 00:00:00 2001 From: junhaa Date: Tue, 19 Dec 2023 01:48:52 +0900 Subject: [PATCH] =?UTF-8?q?:sparkles:=20FEAT.=20=EB=A3=A8=EB=AF=B8?= =?UTF-8?q?=ED=81=90=EB=B8=8C=20=EC=86=8C=EC=BC=93=20=EC=97=B0=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 클라이언트 player 객체 전달 기능 추가 related to : #3 --- src/Controller/Client.java | 8 +------- src/Controller/ClientHandler.java | 9 ++------- src/Controller/GameController.java | 5 +++-- src/Controller/Server.java | 11 +++++------ 4 files changed, 11 insertions(+), 22 deletions(-) diff --git a/src/Controller/Client.java b/src/Controller/Client.java index 39da10e..60ff7b1 100644 --- a/src/Controller/Client.java +++ b/src/Controller/Client.java @@ -16,7 +16,6 @@ public class Client implements Runnable{ private Object input = null; private GameController gameController; - public void setObject(SerializeObject object){ obj = object; } @@ -38,7 +37,7 @@ public Client(GameController gameController, String address, int port, String na else if(answerStr.equals("현재 게임이 진행중입니다.")){ throw new OnProgressException(); } - returnObj(new SerializeObject("received", "String")); + setObject(new SerializeObject("received", "String")); Thread clientThread = new Thread(this); clientThread.start(); System.out.println("서버에 연결되었습니다."); @@ -51,11 +50,6 @@ else if(answerStr.equals("현재 게임이 진행중입니다.")){ } } - // 서버에 객체 전달 - public synchronized void returnObj (SerializeObject obj){ - this.obj = obj; - } - @Override public void run() { try { diff --git a/src/Controller/ClientHandler.java b/src/Controller/ClientHandler.java index 49c0367..72d15f5 100644 --- a/src/Controller/ClientHandler.java +++ b/src/Controller/ClientHandler.java @@ -1,10 +1,7 @@ package Controller; -import jdk.swing.interop.SwingInterOpUtils; - import java.io.*; import java.net.Socket; -import java.net.SocketException; public class ClientHandler implements Runnable { private Socket clientSocket; @@ -18,18 +15,17 @@ public class ClientHandler implements Runnable { // 클라이언트에 객체 전달 - public void returnObj (SerializeObject obj){ + public void setObject(SerializeObject obj){ this.obj = obj; } public void update(SerializeObject object){ // 현재 오브젝트 파싱 및 처리 server.excute(object, index); - server.notifiObservers(this, object); } - public ClientHandler(Socket socket, Server server, int index) throws SocketException { + public ClientHandler(Socket socket, Server server, int index) { this.clientSocket = socket; this.server = server; this.index = index; @@ -43,7 +39,6 @@ public ClientHandler(Socket socket, Server server, int index) throws SocketExcep @Override public void run() { - System.out.println("핸들러 스레드 실행중 ..,"); try { while (true) { if(obj != null || (input = objectInputStream.readObject()) != null) { diff --git a/src/Controller/GameController.java b/src/Controller/GameController.java index a724c75..c0df34d 100644 --- a/src/Controller/GameController.java +++ b/src/Controller/GameController.java @@ -133,6 +133,7 @@ public void changePlayers(Player[] getPlayers){ // 클라이언트로 넘어온 쿼리 실행 함수 public void excuteQuery(SerializeObject object, int index){ + System.out.println("받은 객체 : " + object.getEventObject()); switch (object.getObjectType()){ case "String" : String content = (String)object.getEventObject(); @@ -151,7 +152,7 @@ public void excuteQuery(SerializeObject object, int index){ players[index].setName(name); view.updateNameLabel(name, index); System.out.println(playersStr()); - server.notifiObservers(players[index].getClientHandler(), new SerializeObject(players, "Player[]")); + server.notifiObservers(new SerializeObject(players, "Player[]")); } break; @@ -159,7 +160,7 @@ public void excuteQuery(SerializeObject object, int index){ if(isServer) { Player.ReadyState readyState = (Player.ReadyState) object.getEventObject(); changeReadyState(index, readyState); - server.notifiObservers(players[index].getClientHandler(), new SerializeObject(players, "Player[]")); + server.notifiObservers(new SerializeObject(players, "Player[]")); break; } default: diff --git a/src/Controller/Server.java b/src/Controller/Server.java index aea5404..46158de 100644 --- a/src/Controller/Server.java +++ b/src/Controller/Server.java @@ -1,6 +1,5 @@ package Controller; -import Model.Game; import Model.Player; import java.io.*; @@ -22,12 +21,12 @@ public Server(int port, GameController gameController) { openServer(); } - - public void notifiObservers(ClientHandler clientHandler, SerializeObject object) { + public void notifiObservers(SerializeObject object) { for (int i = 1; i < GameController.MAX_PLAYER_COUNT; i++) { - if (players[i] == null || players[i].getClientHandler() == clientHandler) continue; + if (players[i] == null) continue; + System.out.println("notify => " + object.getEventObject()); ClientHandler curHandler = players[i].getClientHandler(); - curHandler.returnObj(object); + curHandler.setObject(object); } } @@ -40,7 +39,7 @@ private void addPlayer(Socket clientSocket) throws SocketException { int idx = clientCnt - 1; ClientHandler curHandler = new ClientHandler(clientSocket, this, idx); Thread clientThread = new Thread(curHandler); - curHandler.returnObj(new SerializeObject("connected", "String")); + curHandler.setObject(new SerializeObject("connected", "String")); clientThread.start(); players[idx] = new Player("", curHandler); }