Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 KB | None | 0 0
  1. package Network;
  2. import GameV1.Mark;
  3. import Network.ClientHandler;
  4.  
  5. import java.util.*;
  6. import java.io.IOException;
  7. import java.net.ServerSocket;
  8. import java.net.Socket;
  9.  
  10. import Network.ProtocolConstants;
  11. import Network.ProtocolControl;
  12. import Network.Board;
  13.  
  14. /**
  15. * Server.
  16. * @author Theo Ruys
  17. * @version 2005.02.21
  18. */
  19. public class Server {
  20. private static final String USAGE = "usage: " + Server.class.getName() + " <port>";
  21.  
  22. /** Start een Server-applicatie op. */
  23. public static void main(String[] args) {
  24. if (args.length != 1) {
  25. System.out.println(USAGE);
  26. System.exit(0);
  27. }
  28. Scanner in = new Scanner(System.in);
  29. System.out.println("Enter portnumber: ");
  30. String portInput = in.nextLine();
  31.  
  32. port = Integer.parseInt(portInput);
  33. Server server = new Server(port);
  34. server.run();
  35.  
  36. }
  37. private static int port;
  38. private List<ClientHandler> inactiveThreads;
  39. private List<ClientHandler> waitingThreads;
  40. private List<ClientHandler> matchedPairs;
  41. // private List<Game> activeGames;
  42. /** Constructs a new Server object */
  43. public Server(int port) {
  44. this.inactiveThreads = new ArrayList<ClientHandler>();
  45. this.waitingThreads = new ArrayList<ClientHandler>();
  46. this.matchedPairs = new ArrayList<ClientHandler>();
  47. //this.activeGames = new ArrayList<Game>();
  48. //this.start();
  49. }
  50.  
  51. /**
  52. * Listens to a port of this Server if there are any Clients that
  53. * would like to connect. For every new socket connection a new
  54. * ClientHandler thread is started that takes care of the further
  55. * communication with the Client.
  56. * @param array
  57. */
  58. public void run() {
  59. int i = 0;
  60. try {
  61. ServerSocket ssocket = new ServerSocket(this.port);
  62.  
  63. while(true){
  64. Socket socket = ssocket.accept();
  65. ClientHandler handler = new ClientHandler(this, socket);
  66. System.out.println("[Client no. " + (++i) + "]" + "connected.");
  67. handler.announce();
  68. handler.start();
  69. addInactiveHandler(handler);
  70.  
  71. }} catch (IOException e){}}
  72.  
  73. public void acceptRequest(ClientHandler c){
  74. c.sendMessage(ProtocolControl.acceptRequest + ProtocolConstants.msgSeperator + c.getClientName());
  75. if(waitingThreads.get(0).getClientName().equals(c.getClientName())){
  76. usernameInUse(c);
  77. } else { inactiveThreads.remove(c);
  78. waitingThreads.add(c);
  79. if(waitingThreads.size() == 2){
  80. moveToPairs(c);
  81. }
  82. }
  83. }
  84.  
  85. public void sendBoard(ClientHandler c) {
  86.  
  87. String strBoard = "";
  88. Board copy = new Board();
  89. for(int i = 0; i < (copy.ROW * copy.COL)-1; i++){
  90. strBoard += ProtocolConstants.msgSeperator + copy.getField(i);
  91. }
  92. c.sendMessage(ProtocolControl.sendBoard + strBoard);
  93. }
  94.  
  95. public int aantalZetten(){
  96. int zetten = 0;
  97. Board copy = new Board();
  98. for(int i = 0; i < (copy.ROW * copy.COL)-1; i++){
  99. if(copy.getField(i) != Mark.EMPTY){
  100. zetten++;
  101. }
  102. }
  103. return zetten;
  104. }
  105.  
  106. public String turn(){
  107. return waitingThreads.get(this.aantalZetten()%2).getClientName();
  108. }
  109.  
  110.  
  111. /* if(aantalZetten()%2==0){
  112. waitingThreads.get(0).getClientName();
  113. } else {
  114. waitingThreads.get(1).getClientName();
  115. }
  116. }*/
  117.  
  118.  
  119. public void moveToPairs(ClientHandler c){
  120. if(waitingThreads.size() == 2);
  121. waitingThreads.remove(c);
  122. matchedPairs.add(c);
  123. }
  124. public void colors(ClientHandler c){
  125. }
  126.  
  127.  
  128. //Errors m.b.t. verkeerde command
  129. /*public void invalidUserTurn(ClientHandler c){
  130. c.sendMessage(ProtocolConstants.invalidCommand + ProtocolConstants.msgSeperator + ProtocolConstants.invalidUserTurn + ProtocolConstants.msgSeperator + c.getClientName());
  131. }
  132. public void invalidCommand(ClientHandler c){
  133. c.sendMessage(ProtocolConstants.invalidCommand + ProtocolConstants.msgSeperator + ProtocolConstants.invalidCommand);
  134. }*/
  135. public void usernameInUse(ClientHandler c){
  136. c.sendMessage(ProtocolConstants.invalidCommand + ProtocolConstants.msgSeperator + ProtocolConstants.usernameInUse);
  137. }
  138. /*
  139. public void invalidMove(ClientHandler c){
  140. c.sendMessage(ProtocolConstants.invalidCommand + ProtocolConstants.msgSeperator + ProtocolConstants.invalidMove);
  141. }
  142. public void invalidUserName(ClientHandler c){
  143. c.sendMessage(ProtocolConstants.invalidCommand + ProtocolConstants.msgSeperator + ProtocolConstants.invalidUsername);
  144. }
  145.  
  146.  
  147. /*Endgame constanten
  148. public void endGameWinner(){
  149. c.sendMessage("endGame" + ProtocolConstants.msgSeperator + resultString + ProtocolConstants.msgSeperator + "winner");
  150.  
  151. }
  152. public void endGameConnection(){
  153. c.sendMessage("endGame" + ProtocolConstants.msgSeperator + resultString + ProtocolConstants.msgSeperator + "connectionlost");
  154. }
  155. public void endGameDraw(){
  156. c.sendMessage("endGame" + ProtocolConstants.msgSeperator + resultString + ProtocolConstants.msgSeperator + "draw");
  157. }
  158. public void endGameUnknown(){
  159. c.sendMessage("endGame" + ProtocolConstants.msgSeperator + resultString + ProtocolConstants.msgSeperator + "unknownerror");
  160. }*/
  161.  
  162. /**
  163. * Sends a message using the collection of connected ClientHandlers
  164. * to all connected Clients.
  165. * @param msg message that is send
  166. */
  167.  
  168. public void broadcast(String msg, ClientHandler c) {
  169. String[] splitArray = msg.split(ProtocolConstants.msgSeperator);
  170. if (splitArray[0].equals(ProtocolControl.joinRequest)){
  171. acceptRequest(c);}
  172. else { if(splitArray[0].equals(ProtocolControl.getBoard)){
  173. sendBoard(c);}
  174. else{ if(splitArray[0].equals(ProtocolControl.playerTurn)){
  175. turn();}
  176. }
  177. }
  178. }
  179. /*else { if(splitArray[0].equals(ProtocolControl.doMove)){
  180. moveResult(index, c.getClientName(), valid, nextPlayer)){
  181. }*/
  182.  
  183. //isValid wanneer vakje leeg is
  184. //
  185.  
  186. public void endGame(){
  187. }
  188.  
  189.  
  190.  
  191. /**
  192. * Add a ClientHandler to the collection of ClientHandlers.
  193. * @param handler ClientHandler that will be added
  194. */
  195. public void addInactiveHandler(ClientHandler handler) {
  196. inactiveThreads.add(handler);
  197. }
  198.  
  199. /**
  200. * Remove a ClientHandler from the collection of ClientHanlders.
  201. * @param handler ClientHandler that will be removed
  202. */
  203. public void removeInactiveHandler(ClientHandler handler) {
  204. inactiveThreads.remove(handler);
  205. }
  206.  
  207. public void addWaitingHandler(ClientHandler handler) {
  208. waitingThreads.add(handler);
  209. }
  210.  
  211. public void removeWaitingHandler(ClientHandler handler) {
  212. waitingThreads.remove(handler);
  213. }
  214. public void removeMatchedPairs(ClientHandler handler) {
  215. matchedPairs.remove(handler);
  216. }
  217. public void addMatchedPairs(ClientHandler handler) {
  218. matchedPairs.add(handler);
  219. }
  220. }// end of class Server
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement