Skip to content

Commit

Permalink
Resolve Merge Conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
ghvfgfcht committed Dec 18, 2023
2 parents f19b022 + c2bf4d0 commit 094b578
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 118 deletions.
49 changes: 22 additions & 27 deletions src/Controller/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,32 @@ public class Client implements Runnable{


private Socket curSocket;
private ObjectOutputStream out;
private ObjectInputStream in;
private ObjectOutputStream objectOutputStream;
private ObjectInputStream objectInputStream;
private SerializeObject obj = null;

private SerializeObject result = null;
private boolean flag = true;
private Object input = null;
private GameController gameController;

// 서버에 객체 전달
public synchronized void returnObj (SerializeObject obj){
this.obj = obj;
flag = false;
while(!flag){
}
}

public SerializeObject getAnswer(){
return result;
public Client(GameController gameController){
this.gameController = gameController;
}



public synchronized void setFlag() {
flag = true;
notify(); // 메인 스레드에 flag 변경 알림
// 서버에 객체 전달
public synchronized void returnObj (SerializeObject obj){
this.obj = obj;
}

public void connect(String address, int port, String name) throws UnknownHostException, NotEmptySpaceException, OnProgressException{
try {
System.out.println("연결을 시도합니다.");
curSocket = new Socket(address, port);
System.out.println(curSocket);
this.out = new ObjectOutputStream(curSocket.getOutputStream());
this.in = new ObjectInputStream(curSocket.getInputStream());
this.objectOutputStream = new ObjectOutputStream(curSocket.getOutputStream());
this.objectInputStream = new ObjectInputStream(curSocket.getInputStream());

SerializeObject answer = (SerializeObject)in.readObject();
SerializeObject answer = (SerializeObject) objectInputStream.readObject();
String answerStr = (String)answer.getEventObject();
returnObj(new SerializeObject("received", "String"));
if(answerStr.equals("방이 가득찼습니다.")) {
Expand All @@ -55,7 +46,7 @@ else if(answerStr.equals("현재 게임이 진행중입니다.")){

System.out.println("서버에 연결되었습니다.");

out.writeObject(new SerializeObject(name, "String"));
objectOutputStream.writeObject(new SerializeObject(name, "String"));
}catch (IOException e){
e.printStackTrace();
} catch (ClassNotFoundException e) {
Expand All @@ -66,11 +57,15 @@ else if(answerStr.equals("현재 게임이 진행중입니다.")){
@Override
public void run() {
try {
while (obj != null) {
out.writeObject(obj);
result = (SerializeObject) in.readObject();
obj = null;
setFlag();
while (obj != null || (input = objectInputStream.readObject()) != null) {
// 입력
if(obj != null) {
objectOutputStream.writeObject(obj);
obj = null;
}
else {
gameController.excuteQuery((SerializeObject) input, -1);
}
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
Expand Down
63 changes: 63 additions & 0 deletions src/Controller/ClientHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package Controller;

import java.io.*;
import java.net.Socket;

public class ClientHandler implements Runnable {
private Socket clientSocket;
private ObjectOutputStream objectOutputStream;
private ObjectInputStream objectInputStream;
private Object input = null;

private Server server;
private int index;
private SerializeObject obj = null;


// 클라이언트에 객체 전달
public void returnObj (SerializeObject obj){
this.obj = obj;
}

public void update(SerializeObject object){
// 현재 오브젝트 파싱 및 처리
server.notifiObservers(this, object);
}


public ClientHandler(Socket socket, Server server, int index) {
this.clientSocket = socket;
this.server = server;
this.index = index;
try {
objectOutputStream = new ObjectOutputStream(clientSocket.getOutputStream());
objectInputStream = new ObjectInputStream(clientSocket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void run() {
try {
while (obj != null || (input = objectInputStream.readObject()) != null) {
// 입력
if(obj != null) {
objectOutputStream.writeObject(obj);
obj = null;
}
else {
update((SerializeObject) input);
}
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
64 changes: 62 additions & 2 deletions src/Controller/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,15 @@ public void makeRoom() {


public void connectRoom(String address) {
client = new Client();
client = new Client(this);
isServer = false;
String name = view.getNameTF().getText();
view.getLoginErrorLabel().setText("연결 중 입니다...");
try {
// TODO
client.connect(address, PORT, name);

view.changePanel("RoomPanel");
curPlayer = new Player(name);
} catch (UnknownHostException e) {
view.getLoginErrorLabel().setText("IP주소로 접속할 수 없습니다. IP주소를 다시 확인해주세요.");
} catch (NotEmptySpaceException e){
Expand All @@ -91,6 +93,64 @@ public void connectRoom(String address) {
view.getLoginErrorLabel().setText("현재 게임이 진행 중인 방입니다.");
}
}


// 플레이어 ready 상태 변경 함수
public void changeReadyState(int index, Player.ReadyState readyState){
// ready
if(readyState == Player.ReadyState.READY && players[index].getReadyState() == Player.ReadyState.NOT_READY){
players[index].setReadyState(Player.ReadyState.READY);
}
// ready 해제
else if(readyState == Player.ReadyState.NOT_READY && players[index].getReadyState() == Player.ReadyState.READY){
players[index].setReadyState(Player.ReadyState.NOT_READY);
}
view.updateRoomReadyPanel(players[index].getReadyState(), index);
}

// 플레이어 정보 변경 함수
public void changePlayers(Player[] getPlayers){
// Model 플레이어 변경
players = getPlayers;

// 플레이어 정보 UI 적용
view.updatePlayers(players);
}


// 클라이언트로 넘어온 쿼리 실행 함수
public void excuteQuery(SerializeObject object, int index){
switch (object.getObjectType()){
case "String" :
String content = (String)object.getEventObject();

break;
case "Player[]":
Player[] getPlayers = (Player[]) object.getEventObject();
changePlayers(getPlayers);
break;

case "Name":
if(isServer) {
String name = (String) object.getEventObject();
players[index].setName(name);
server.notifiObservers(players[index].getClientHandler(), new SerializeObject(players, "Player[]"));
}
break;

case "ReadyState" :
if(isServer) {
Player.ReadyState readyState = (Player.ReadyState) object.getEventObject();
changeReadyState(index, readyState);
server.notifiObservers(players[index].getClientHandler(), new SerializeObject(players, "Player[]"));
break;
}
default:
break;
}

}

}


Expand Down
95 changes: 23 additions & 72 deletions src/Controller/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,36 @@ public class Server {

static int clientCnt = 0;
static int port;
static ClientHandler[] handlers = new ClientHandler[GameController.MAX_PLAYER_COUNT];

static Player[] players;
static private GameController gameController;

public Server(int port, GameController gameController) {
this.port = port;
this.gameController = gameController;
players = gameController.getPlayers();
openServer();
}

static void openServer() {

public void notifiObservers(ClientHandler clientHandler, SerializeObject object){
for(int i = 0 ; i < GameController.MAX_PLAYER_COUNT ; i ++){
if(players[i] == null || players[i].getClientHandler() == clientHandler) continue;
ClientHandler curHandler = players[i].getClientHandler();
curHandler.returnObj(object);
}
}

private void addPlayer(Socket clientSocket){
clientCnt++;
int idx = clientCnt - 1;
ClientHandler curHandler = new ClientHandler(clientSocket, this, idx);
Thread clientThread = new Thread(curHandler);
clientThread.start();
curHandler.returnObj(new SerializeObject("connected", "String"));
players[idx] = new Player("", curHandler);
}

private void openServer() {

Thread serverWaitThread = new Thread(new Runnable() {
@Override
Expand All @@ -40,14 +59,7 @@ public void run() {

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);
addPlayer(clientSocket);
} else {
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
if(clientCnt >= 4) {
Expand All @@ -69,64 +81,3 @@ public void run() {
serverWaitThread.start();
}
}

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 {
objectOutputStream = new ObjectOutputStream(clientSocket.getOutputStream());
objectInputStream = new ObjectInputStream(clientSocket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void run() {
try {
while (obj != null) {
objectOutputStream.writeObject(obj);
result = (SerializeObject) objectInputStream.readObject();
obj = null;
setFlag();
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Loading

0 comments on commit 094b578

Please sign in to comment.