Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 KB | None | 0 0
  1. //...Irrelevant bits ommitted...//
  2. public class Server
  3. {
  4. // The server object reference
  5. static Server server;
  6.  
  7. // Declarations:
  8. private ArrayList<ObjectOutputStream> clientOutputStreams; // out streams
  9. private ArrayList<String> takenNames = new ArrayList<>(); // taken names
  10. private InetAddress ip;
  11. private final int serverPort; // the port the server is running on
  12. private static ObjectOutputStream changer; // the last person to change names
  13. private ArrayList<ObjectOutputStream> shitList = new ArrayList<>();
  14.  
  15. private HashMap <InetAddress, ObjectOutputStream> ipMap =
  16. new HashMap<>(); // <ip, outputstream>
  17.  
  18. //...Irrelevant bits ommited...//
  19.  
  20. public void tellEveryone(Message message, InetAddress senderIP)
  21. {
  22. //...//
  23. // If someone is on my shitlist,
  24. if(shitList.contains(ipMap.get(senderIP)))
  25. {
  26. // Warn them of their sins...
  27. Message nopeMessage = new Message(Message.TYPE.SERVER,
  28. "You may not send any messages until you change your name!",
  29. "Server");
  30. try
  31. {
  32. ipMap.get(senderIP).writeObject(nopeMessage);
  33. }
  34. catch(IOException e)
  35. {
  36. e.printStackTrace();
  37. }
  38. }
  39.  
  40. public class ServerHandler implements Runnable
  41. {
  42. @Override
  43. public void run()
  44. {
  45. // Create a list of client out streams to send stuff...
  46. clientOutputStreams = new ArrayList<>();
  47. try // To establish a connection with clients
  48. {
  49. // Create server socket...
  50. ServerSocket serverSocket = new ServerSocket(serverPort);
  51. while(true) // Will always run! Blocks!
  52. {
  53. // Assign a client socket to any new socket connections...
  54. // (The var used here is temp, but will be passed off soon.)
  55. Socket clientSocket = serverSocket.accept();
  56. // Get's the ip of the client that connected...
  57. ip = clientSocket.getInetAddress();
  58. System.out.println(ip + " " + "connected.");
  59. // Create ooStream to send messages to client...
  60. ObjectOutputStream ooStream =
  61. new ObjectOutputStream(
  62. clientSocket.getOutputStream());
  63. // Add the client oo stream to the list of outputs...
  64. clientOutputStreams.add(ooStream);
  65. // Add user IP data to map of ip's
  66. ipMap.putIfAbsent(ip, ooStream);
  67. // Create new thread to run inner class ClientHandler...
  68. Thread t = new Thread(new ClientHandler(clientSocket));
  69. // Running the thread makes it safe to overwrite the...
  70. // ...clientsocket variable.
  71. t.start();
  72. }
  73. }
  74. catch (IOException e)
  75. {
  76. System.out.println("Exception in server.run()");
  77. // TODO: Revise
  78. e.printStackTrace();
  79. }
  80. }
  81. }
  82.  
  83. public class ClientHandler implements Runnable
  84. {
  85. private ObjectInputStream oInStream; // The client's input stream.
  86. private Socket socket; // Socket to the client
  87.  
  88. public ClientHandler(Socket clientSocket)
  89. {
  90. try // to create an input stream...
  91. {
  92. socket = clientSocket; // <-- The one passed in to the method
  93. // Potential error from previous version... REMOVE WHEN TESTED
  94. oInStream = new ObjectInputStream(socket.getInputStream());
  95. }
  96. catch(IOException e)
  97. {
  98. System.out.println("Error establishing input stream");
  99. }
  100. }
  101.  
  102. @Override
  103. public void run()
  104. {
  105. Message message;
  106.  
  107. try // To process incoming messages...
  108. {
  109. while(socket.isClosed() == false) // If the socket is open...
  110. {
  111. // While there are more messages...
  112. // Also assigns to the message var.
  113. while((message = (Message)oInStream.readObject()) != null)
  114. {
  115. // Passes on the message and sender info.
  116. if(message.getType() == Message.TYPE.NAME_REQUEST)
  117. {
  118. changer = ipMap.get(socket.getInetAddress());
  119. System.out.println(socket.getInetAddress());
  120. System.out.println(changer.toString());
  121. handleNameRequests(message);
  122. }
  123. else
  124. {
  125. tellEveryone(message, ip); // TEST CHANGE- DELETED IF TEST
  126. }
  127. }
  128. // TEST TEST TEST
  129. synchronized(clientOutputStreams)
  130. {
  131. int index =
  132. clientOutputStreams.indexOf(
  133. socket.getOutputStream());
  134. clientOutputStreams.remove(index);
  135. System.out.println("Removed the client in sync");
  136. }
  137. }
  138. // TEST TEST TEST
  139. socket.close(); // TEST CLOSING SOCKET WHEN DONE.
  140. System.out.println("Sock closed after while loop in ch run()");
  141. }
  142. catch(IOException e)
  143. {
  144. System.out.println("IOException caught when "
  145. + "reading message.");
  146. }
  147. catch (ClassNotFoundException e)
  148. {
  149. System.out.println("Some poor sap is going to have to debug"
  150. + "this!");
  151. }
  152. finally
  153. {
  154. // THIS WHOLE BLOCK: TEST TEST TEST
  155. try
  156. {
  157. oInStream.close();
  158. System.out.println("just closed oinStream");
  159. }
  160. catch(IOException e)
  161. {
  162. e.printStackTrace();
  163. }
  164. }
  165. }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement