-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from Rummikube/3
3
- Loading branch information
Showing
28 changed files
with
1,947 additions
and
369 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.