Advertisement
Guest User

Untitled

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