Skip to content

Commit

Permalink
Merge pull request #8 from Rummikube/3
Browse files Browse the repository at this point in the history
3
  • Loading branch information
junhaa authored Dec 20, 2023
2 parents cb245a4 + 1a6537b commit 67f519d
Show file tree
Hide file tree
Showing 28 changed files with 1,947 additions and 369 deletions.
Binary file added asset/images/AddTilesBTN.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added asset/images/BUGIKUB.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added asset/images/GroupSortBTN.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added asset/images/QuitBTN.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added asset/images/RunSortBTN.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added asset/images/emptyProfile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added asset/images/minitile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added asset/images/ready.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added asset/images/start.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added asset/images/tile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ public static void main(String[] args) {
GameController controller = new GameController(view);
controller.start();
}
}
}
147 changes: 147 additions & 0 deletions src/Controller/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package Controller;
import java.io.*;
import java.net.*;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import Exceptions.NotEmptySpaceException;
import Exceptions.OnProgressException;

public class Client{


private Socket curSocket;
private ClientOutput output;
private ClientInput input;

private GameController gameController;


public void sendObject(SerializeObject object){
output.addObject(object);
}

public Client(GameController gameController, String address, int port, String name) throws UnknownHostException, NotEmptySpaceException, OnProgressException{
this.gameController = gameController;
try {
curSocket = new Socket(address, port);

output = new ClientOutput(curSocket);
input = new ClientInput(curSocket);

SerializeObject answer = null;

while (answer == null) {
try {
answer = (SerializeObject) input.getObjectInputStream().readObject();
} catch (IOException e) {
// IOException 처리
e.printStackTrace();
}
}
System.out.println("받은 객체 : " + answer.getEventObject() + answer.getObjectType());

if(!answer.getObjectType().equals("Index")) {
String answerStr = (String) answer.getEventObject();
if (answerStr.equals("방이 가득찼습니다.")) {
throw new NotEmptySpaceException();
} else if (answerStr.equals("현재 게임이 진행중입니다.")) {
throw new OnProgressException();
}
}
else{
gameController.setIndex((int) answer.getEventObject());
}
Thread outputThread = new Thread(output);
Thread inputThread = new Thread(input);

outputThread.start();
inputThread.start();

System.out.println("서버에 연결되었습니다.");
sendObject(new SerializeObject(name, "Name", gameController.getIndex()));

} catch (ClassNotFoundException e) {
System.out.println("클래스가 맞지 않습니다.");
} catch (IOException e) {
e.printStackTrace();
}
}

class ClientOutput implements Runnable{
private ObjectOutputStream objectOutputStream;
private Socket clientSocket;

private Queue<SerializeObject> outputQueue;

public ClientOutput(Socket clientSocket) throws IOException {
this.clientSocket = clientSocket;
objectOutputStream = new ObjectOutputStream(clientSocket.getOutputStream());
outputQueue = new ConcurrentLinkedQueue<>();
}

public ObjectOutputStream getObjectOutputStream() {
return objectOutputStream;
}

public void addObject(SerializeObject object){
this.outputQueue.offer(object);
}


@Override
public void run() {
try {
while (true) {
SerializeObject sendObj = outputQueue.poll();
if (sendObj != null) {
System.out.println(sendObj.getEventObject());
objectOutputStream.writeObject(sendObj);
System.out.println(sendObj.getObjectType() + " " + sendObj.getEventObject() + " 전달");
}
}
}
catch (IOException e){
e.printStackTrace();
}
finally{
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

class ClientInput implements Runnable{
ObjectInputStream objectInputStream;
Socket clientSocker;

Object curInput = null;

public ClientInput(Socket clientSocker) throws IOException {
this.clientSocker = clientSocker;
objectInputStream = new ObjectInputStream(clientSocker.getInputStream());
}

public ObjectInputStream getObjectInputStream() {
return objectInputStream;
}

@Override
public void run() {
while(true){
try {
if((curInput = objectInputStream.readObject()) != null){
SerializeObject sInput = (SerializeObject) curInput;
gameController.excuteQuery(sInput);
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
}
130 changes: 130 additions & 0 deletions src/Controller/ClientHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package Controller;

import Model.Player;

import java.io.*;
import java.net.Socket;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;


public class ClientHandler{
private Socket clientSocket;
private Object input = null;

private ServerOutput out;
private ServerInput in;
private Server server;
private int index;


// 클라이언트에 객체 전달
public void sendObject(SerializeObject obj){
out.addObject(obj);
}

public void startThread(){
Thread outThread = new Thread(out);
Thread inThread = new Thread(in);

outThread.start();
inThread.start();
}

public ClientHandler(Socket socket, Server server, int index) {
this.clientSocket = socket;
this.server = server;
this.index = index;
try {
out = new ServerOutput(clientSocket);
in = new ServerInput(clientSocket);
} catch (IOException e) {
e.printStackTrace();
}
}

class ServerOutput implements Runnable{
private ObjectOutputStream objectOutputStream;
private Socket serverSocket;

private Queue<SerializeObject> outputQueue;

public ServerOutput(Socket serverSocket) throws IOException {
this.serverSocket = serverSocket;
objectOutputStream = new ObjectOutputStream(serverSocket.getOutputStream());
outputQueue = new ConcurrentLinkedQueue<>();
}

public ObjectOutputStream getObjectOutputStream() {
return objectOutputStream;
}

public void addObject(SerializeObject object){
this.outputQueue.offer(object);
}


@Override
public void run() {
try {
while (true) {
SerializeObject sendObj = outputQueue.poll();
if (sendObj != null) {
objectOutputStream.writeObject(sendObj);
if(sendObj.getObjectType() == "Player[]") {
Player[] cur = (Player[]) sendObj.getEventObject();
for(int i = 0 ; i < 4 ; i ++){
if(cur[i] != null) {
System.out.println(cur[i].getName());
}
}
System.out.println(sendObj.getEventObject());
}
System.out.println("출력 추가됨 " + sendObj.getEventObject() + " index : " + index);
}
}
}
catch (IOException e){
e.printStackTrace();
}
finally{
try {
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}


class ServerInput implements Runnable{
ObjectInputStream objectInputStream;
Socket serverSocket;

Object curInput = null;

public ServerInput(Socket serverSocket) throws IOException {
this.serverSocket = serverSocket;
objectInputStream = new ObjectInputStream(serverSocket.getInputStream());
}

public ObjectInputStream getObjectInputStream() {
return objectInputStream;
}

@Override
public void run() {
while(true){
try {
if((curInput = objectInputStream.readObject()) != null){
SerializeObject sInput = (SerializeObject) curInput;
server.getGameController().excuteQuery(sInput);
}
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
}
Loading

0 comments on commit 67f519d

Please sign in to comment.