-
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
8 changed files
with
171 additions
and
97 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
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
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,83 +1,91 @@ | ||
package Controller; | ||
|
||
import Model.Game; | ||
import Model.Player; | ||
|
||
import java.io.*; | ||
import java.net.*; | ||
|
||
public class Server { | ||
|
||
|
||
static int clientCnt = 0; | ||
static int clientCnt = 1; | ||
static int port; | ||
static Player[] players; | ||
static ClientHandler[] handlers = new ClientHandler[GameController.MAX_PLAYER_COUNT]; | ||
static private GameController gameController; | ||
|
||
|
||
public Server(int port, GameController gameController) { | ||
this.port = port; | ||
this.gameController = gameController; | ||
players = gameController.getPlayers(); | ||
openServer(); | ||
} | ||
|
||
|
||
public void notifiObservers(ClientHandler clientHandler, SerializeObject object){ | ||
for(int i = 0 ; i < GameController.MAX_PLAYER_COUNT ; i ++){ | ||
if(players[i] == null || players[i].getClientHandler() == clientHandler) continue; | ||
ClientHandler curHandler = players[i].getClientHandler(); | ||
curHandler.returnObj(object); | ||
public void notifiObservers(SerializeObject object) { | ||
for (int i = 1; i < GameController.MAX_PLAYER_COUNT; i++) { | ||
if (players[i] == null) continue; | ||
System.out.println("notify => " + object.getEventObject()); | ||
handlers[i].setObject(object); | ||
} | ||
} | ||
|
||
private void addPlayer(Socket clientSocket){ | ||
public void excute(SerializeObject object, int index){ | ||
gameController.excuteQuery(object, index); | ||
} | ||
|
||
private void addPlayer(Socket clientSocket) throws SocketException { | ||
clientCnt++; | ||
int idx = clientCnt - 1; | ||
ClientHandler curHandler = new ClientHandler(clientSocket, this, idx); | ||
Thread clientThread = new Thread(curHandler); | ||
curHandler.setObject(new SerializeObject("connected", "String")); | ||
clientThread.start(); | ||
curHandler.returnObj(new SerializeObject("connected", "String")); | ||
players[idx] = new Player("", curHandler); | ||
handlers[idx] = curHandler; | ||
players[idx] = new Player(""); | ||
System.out.println("플레이어 추가됨"); | ||
gameController.updatePlayers(); | ||
} | ||
|
||
private void openServer() { | ||
|
||
Thread serverWaitThread = new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
ServerSocket serverSocket; | ||
try { | ||
serverSocket = new ServerSocket(port); | ||
System.out.println("server 소켓 생성, IP : " + serverSocket.getLocalSocketAddress()); | ||
System.out.println(InetAddress.getLocalHost().getHostAddress()); | ||
|
||
InetAddress serverLocalIpAddress = serverSocket.getInetAddress(); | ||
System.out.println("Serversocket Local IP Address : " + serverLocalIpAddress.getHostAddress()); | ||
while(true) { | ||
System.out.println("기다리는 중"); | ||
Socket clientSocket = serverSocket.accept(); | ||
System.out.println("클라이언트 연결됨: " + clientSocket.getInetAddress().getHostAddress()); | ||
|
||
if (clientCnt < 4 || GameController.gameState == GameController.GameState.FINISHED) { | ||
// 클라이언트와 통신하는 스레드 생성 | ||
addPlayer(clientSocket); | ||
Thread serverWaitThread = new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
ServerSocket serverSocket; | ||
try { | ||
serverSocket = new ServerSocket(port); | ||
System.out.println("server 소켓 생성, IP : " + serverSocket.getLocalSocketAddress()); | ||
System.out.println(InetAddress.getLocalHost().getHostAddress()); | ||
|
||
InetAddress serverLocalIpAddress = serverSocket.getInetAddress(); | ||
System.out.println("Serversocket Local IP Address : " + serverLocalIpAddress.getHostAddress()); | ||
while (true) { | ||
System.out.println("기다리는 중"); | ||
Socket clientSocket = serverSocket.accept(); | ||
System.out.println("클라이언트 연결됨: " + clientSocket.getInetAddress().getHostAddress()); | ||
|
||
if (clientCnt < 4 || GameController.gameState == GameController.GameState.FINISHED) { | ||
// 클라이언트와 통신하는 스레드 생성 | ||
addPlayer(clientSocket); | ||
} else { | ||
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); | ||
if (clientCnt >= 4) { | ||
out.println("방이 가득찼습니다."); | ||
} else { | ||
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true); | ||
if(clientCnt >= 4) { | ||
out.println("방이 가득찼습니다."); | ||
} | ||
else{ | ||
out.println("현재 게임이 진행중입니다."); | ||
} | ||
clientSocket.close(); | ||
out.close(); | ||
out.println("현재 게임이 진행중입니다."); | ||
} | ||
clientSocket.close(); | ||
out.close(); | ||
} | ||
} | ||
catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
}); | ||
serverWaitThread.start(); | ||
} | ||
} | ||
}); | ||
serverWaitThread.start(); | ||
} | ||
|
||
|
||
} |
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
Oops, something went wrong.