-
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.
- Loading branch information
Showing
11 changed files
with
1,025 additions
and
306 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,79 +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 implements Runnable{ | ||
public class Client{ | ||
|
||
|
||
private Socket curSocket; | ||
private ObjectOutputStream objectOutputStream; | ||
private ObjectInputStream objectInputStream; | ||
private SerializeObject obj = null; | ||
private ClientOutput output; | ||
private ClientInput input; | ||
|
||
private Object input = null; | ||
private GameController gameController; | ||
|
||
public void setObject(SerializeObject object){ | ||
obj = object; | ||
|
||
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); | ||
|
||
this.objectOutputStream = new ObjectOutputStream(curSocket.getOutputStream()); | ||
this.objectInputStream = new ObjectInputStream(curSocket.getInputStream()); | ||
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()); | ||
|
||
SerializeObject answer = (SerializeObject) objectInputStream.readObject(); | ||
System.out.println("받은 객체 : " + answer + " " + answer.getObjectType()); | ||
String answerStr = (String)answer.getEventObject(); | ||
if(answerStr.equals("방이 가득찼습니다.")) { | ||
throw new NotEmptySpaceException(); | ||
if(!answer.getObjectType().equals("Index")) { | ||
String answerStr = (String) answer.getEventObject(); | ||
if (answerStr.equals("방이 가득찼습니다.")) { | ||
throw new NotEmptySpaceException(); | ||
} else if (answerStr.equals("현재 게임이 진행중입니다.")) { | ||
throw new OnProgressException(); | ||
} | ||
} | ||
else if(answerStr.equals("현재 게임이 진행중입니다.")){ | ||
throw new OnProgressException(); | ||
else{ | ||
gameController.setIndex((int) answer.getEventObject()); | ||
} | ||
setObject(new SerializeObject("received", "String")); | ||
Thread clientThread = new Thread(this); | ||
clientThread.start(); | ||
Thread outputThread = new Thread(output); | ||
Thread inputThread = new Thread(input); | ||
|
||
outputThread.start(); | ||
inputThread.start(); | ||
|
||
System.out.println("서버에 연결되었습니다."); | ||
setObject(new SerializeObject(name, "Name")); | ||
sendObject(new SerializeObject(name, "Name", gameController.getIndex())); | ||
|
||
}catch (IOException e){ | ||
e.printStackTrace(); | ||
} catch (ClassNotFoundException e) { | ||
System.out.println("클래스가 맞지 않습니다."); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
System.out.println("클라이언트 소켓 통신 중..."); | ||
while (true) { | ||
if(obj != null || (input = objectInputStream.readObject()) != null) { | ||
// 입력 | ||
if (obj != null) { | ||
objectOutputStream.writeObject(obj); | ||
System.out.println(obj.getObjectType() + " " + obj.getEventObject() + " 전달"); | ||
obj = null; | ||
} else { | ||
gameController.excuteQuery((SerializeObject) input, -1); | ||
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 | ClassNotFoundException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
try { | ||
curSocket.close(); | ||
} catch (IOException e) { | ||
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 |
---|---|---|
@@ -1,67 +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 implements Runnable { | ||
public class ClientHandler{ | ||
private Socket clientSocket; | ||
private ObjectOutputStream objectOutputStream; | ||
private ObjectInputStream objectInputStream; | ||
private Object input = null; | ||
|
||
private ServerOutput out; | ||
private ServerInput in; | ||
private Server server; | ||
private int index; | ||
private SerializeObject obj = null; | ||
|
||
|
||
// 클라이언트에 객체 전달 | ||
public void setObject(SerializeObject obj){ | ||
this.obj = obj; | ||
public void sendObject(SerializeObject obj){ | ||
out.addObject(obj); | ||
} | ||
|
||
public void update(SerializeObject object){ | ||
// 현재 오브젝트 파싱 및 처리 | ||
server.excute(object, index); | ||
} | ||
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 { | ||
objectOutputStream = new ObjectOutputStream(clientSocket.getOutputStream()); | ||
objectInputStream = new ObjectInputStream(clientSocket.getInputStream()); | ||
out = new ServerOutput(clientSocket); | ||
in = new ServerInput(clientSocket); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
while (true) { | ||
if(obj != null || (input = objectInputStream.readObject()) != null) { | ||
// 입력 | ||
if (obj != null) { | ||
System.out.println("출력 추가됨 " + obj.getEventObject() + " "); | ||
objectOutputStream.writeObject(obj); | ||
obj = null; | ||
} else { | ||
SerializeObject sinput = (SerializeObject) input; | ||
System.out.println("입력 추가됨 " + sinput.getEventObject()); | ||
update((SerializeObject) input); | ||
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 | ClassNotFoundException e) { | ||
e.printStackTrace(); | ||
} finally { | ||
try { | ||
clientSocket.close(); | ||
} catch (IOException e) { | ||
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.