No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ReceiveThread.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package chat;
  2. import java.io.EOFException;
  3. import java.net.SocketException;
  4. import java.io.IOException;
  5. import java.io.ObjectInputStream;
  6. import java.io.ObjectOutputStream;
  7. import java.net.Socket;
  8. import java.util.Collections;
  9. import java.util.List;
  10. import javax.swing.JTextArea;
  11. class ReceiveThread extends Thread {
  12. User user;
  13. Socket socket;
  14. JTextArea displayArea;
  15. JTextArea knownUsersPanel;
  16. Boolean isOutdoor;
  17. List<User> known_users;
  18. List<User> known_outdoor_users;
  19. List<Socket> dest_sockets;
  20. List<Socket> outdoor_dest_sockets;
  21. ReceiveThread(User in_user, Socket in_socket, JTextArea in_displayArea, List<User> in_known_users, List<User> in_known_outdoor_users,
  22. JTextArea in_knownUsersPanel, List<Socket> in_dest_sockets, List<Socket> in_outdoor_dest_sockets, Boolean in_isOutdoor)
  23. {
  24. user = in_user;
  25. socket = in_socket;
  26. displayArea = in_displayArea;
  27. knownUsersPanel = in_knownUsersPanel;
  28. known_users = in_known_users;
  29. known_outdoor_users = in_known_outdoor_users;
  30. dest_sockets = in_dest_sockets;
  31. outdoor_dest_sockets = in_outdoor_dest_sockets;
  32. isOutdoor = in_isOutdoor;
  33. }
  34. public void run()
  35. {
  36. boolean exit = false;
  37. try
  38. {
  39. while(!exit)
  40. {
  41. ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
  42. Notification notif = (Notification) in.readObject();
  43. if(!(notif.getAuthor().equals(user)) && !(notif.getAuthor().isOutdoor() && !isOutdoor))
  44. {
  45. for(Socket s:outdoor_dest_sockets)
  46. {
  47. try
  48. {
  49. ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
  50. out.writeObject(notif);
  51. }
  52. catch(IOException e){}
  53. }
  54. }
  55. if(isOutdoor)
  56. {
  57. for(Socket s:dest_sockets)
  58. {
  59. try
  60. {
  61. ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
  62. out.writeObject(notif);
  63. }
  64. catch(IOException e){}
  65. }
  66. }
  67. else
  68. {
  69. if(notif instanceof Message)
  70. {
  71. Message m = (Message) notif;
  72. displayArea.append(m.getAuthor().getName() + " : " + m.getText() + "\n");
  73. }
  74. else if (notif instanceof DisconnectNotification)
  75. {
  76. DisconnectNotification dn = (DisconnectNotification) notif;
  77. displayArea.append(dn.getAuthor().getName() + " has left the chat.\n");
  78. known_users.remove(dn.getAuthor());
  79. known_outdoor_users.remove(dn.getAuthor());
  80. /*if(!dn.getAuthor().equals(user) && !(dn.getAuthor().isOutdoor() && !isOutdoor))
  81. {
  82. exit = true;
  83. socket.close();
  84. if(!isOutdoor)
  85. dest_sockets.remove(socket);
  86. else
  87. outdoor_dest_sockets.remove(socket);
  88. }*/
  89. }
  90. else if (notif instanceof ConnectNotification)
  91. {
  92. ConnectNotification cn = (ConnectNotification) notif;
  93. displayArea.append(cn.getAuthor().getName() + " has joined the chat.\n");
  94. if(cn.getAuthor().isOutdoor())
  95. {
  96. if(!known_outdoor_users.contains(cn.getAuthor()))
  97. {
  98. known_outdoor_users.add(cn.getAuthor());
  99. Collections.sort(known_outdoor_users);
  100. }
  101. }
  102. else
  103. {
  104. if(!known_users.contains(cn.getAuthor()))
  105. {
  106. known_users.add(cn.getAuthor());
  107. Collections.sort(known_users);
  108. }
  109. }
  110. }
  111. knownUsersPanel.setText("");
  112. knownUsersPanel.append("Indoor users:\n");
  113. for(User a:known_users)
  114. {
  115. knownUsersPanel.append(" " + a.getName() + " \n");
  116. }
  117. knownUsersPanel.append("\nOutdoor users:\n");
  118. for(User a:known_outdoor_users)
  119. {
  120. knownUsersPanel.append(" " + a.getName() + " \n");
  121. }
  122. displayArea.setCaretPosition(displayArea.getDocument().getLength());
  123. }
  124. }
  125. }
  126. catch(Exception e)
  127. {
  128. System.out.println("Socket closed");
  129. dest_sockets.remove(socket);
  130. outdoor_dest_sockets.remove(socket);
  131. }
  132. }
  133. }