Skip to content

Commit

Permalink
✨ FEAT. 루미큐브 소켓 연동
Browse files Browse the repository at this point in the history
클라이언트 player 객체 전달 기능 추가

related to : #3
  • Loading branch information
junhaa committed Dec 18, 2023
1 parent 7c48b9b commit d3c89f3
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 22 deletions.
8 changes: 1 addition & 7 deletions src/Controller/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public class Client implements Runnable{
private Object input = null;
private GameController gameController;


public void setObject(SerializeObject object){
obj = object;
}
Expand All @@ -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("서버에 연결되었습니다.");
Expand All @@ -51,11 +50,6 @@ else if(answerStr.equals("현재 게임이 진행중입니다.")){
}
}

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

@Override
public void run() {
try {
Expand Down
9 changes: 2 additions & 7 deletions src/Controller/ClientHandler.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -151,15 +152,15 @@ 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;

case "ReadyState" :
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:
Expand Down
11 changes: 5 additions & 6 deletions src/Controller/Server.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package Controller;

import Model.Game;
import Model.Player;

import java.io.*;
Expand All @@ -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);
}
}

Expand All @@ -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);
}
Expand Down

0 comments on commit d3c89f3

Please sign in to comment.