Skip to content

Commit

Permalink
✨ FEAT. 루미큐브 메인로직 구현
Browse files Browse the repository at this point in the history
소켓 통신 직렬화 객체 추가

Related to : #3
  • Loading branch information
junhaa committed Dec 16, 2023
1 parent 7344a87 commit b63e1bc
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 27 deletions.
87 changes: 70 additions & 17 deletions src/Controller/Client.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}
21 changes: 21 additions & 0 deletions src/Controller/SerializeObject.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
62 changes: 52 additions & 10 deletions src/Controller/Server.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package Controller;

import Model.Game;
import Model.Player;

import java.io.*;
import java.net.*;
public class Server {
Expand All @@ -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();
}

Expand All @@ -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();
}
}
}
Expand All @@ -53,26 +69,52 @@ 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();
}
}

@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 {
Expand Down

0 comments on commit b63e1bc

Please sign in to comment.