123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- package chat;
-
- import java.io.EOFException;
- import java.net.SocketException;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.net.Socket;
- import java.util.Collections;
- import java.util.List;
-
- import javax.swing.JTextArea;
-
- class ReceiveThread extends Thread {
- User user;
- Socket socket;
- JTextArea displayArea;
- JTextArea knownUsersPanel;
- Boolean isOutdoor;
- List<User> known_users;
- List<User> known_outdoor_users;
- List<Socket> dest_sockets;
- List<Socket> outdoor_dest_sockets;
-
- ReceiveThread(User in_user, Socket in_socket, JTextArea in_displayArea, List<User> in_known_users, List<User> in_known_outdoor_users,
- JTextArea in_knownUsersPanel, List<Socket> in_dest_sockets, List<Socket> in_outdoor_dest_sockets, Boolean in_isOutdoor)
- {
- user = in_user;
- socket = in_socket;
- displayArea = in_displayArea;
- knownUsersPanel = in_knownUsersPanel;
- known_users = in_known_users;
- known_outdoor_users = in_known_outdoor_users;
- dest_sockets = in_dest_sockets;
- outdoor_dest_sockets = in_outdoor_dest_sockets;
- isOutdoor = in_isOutdoor;
- }
- public void run()
- {
- boolean exit = false;
- while(!exit)
- {
- try
- {
- ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
- Notification notif = (Notification) in.readObject();
-
- if(!(notif.getAuthor().equals(user)) && !(notif.getAuthor().isOutdoor() && !isOutdoor))
- {
- for(Socket s:outdoor_dest_sockets)
- {
- try
- {
- ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
-
- out.writeObject(notif);
-
- }
- catch(IOException e){}
- }
- }
-
- if(isOutdoor)
- {
- for(Socket s:dest_sockets)
- {
- try
- {
- ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
-
- out.writeObject(notif);
- }
- catch(IOException e){}
- }
-
- }
- else
- {
- if(notif instanceof Message)
- {
- Message m = (Message) notif;
- displayArea.append(m.getAuthor().getName() + " : " + m.getText() + "\n");
- }
- else if (notif instanceof DisconnectNotification)
- {
- DisconnectNotification dn = (DisconnectNotification) notif;
-
- displayArea.append(dn.getAuthor().getName() + " has left the chat.\n");
-
- known_users.remove(dn.getAuthor());
- known_outdoor_users.remove(dn.getAuthor());
-
- if(!dn.getAuthor().equals(user) && !(dn.getAuthor().isOutdoor() && !isOutdoor))
- {
- exit = true;
-
- socket.close();
- if(!isOutdoor)
- dest_sockets.remove(socket);
- else
- outdoor_dest_sockets.remove(socket);
- }
- }
-
- else if (notif instanceof ConnectNotification)
- {
- ConnectNotification cn = (ConnectNotification) notif;
-
- displayArea.append(cn.getAuthor().getName() + " has joined the chat.\n");
- if(cn.getAuthor().isOutdoor())
- {
- if(!known_outdoor_users.contains(cn.getAuthor()))
- {
- known_outdoor_users.add(cn.getAuthor());
- Collections.sort(known_outdoor_users);
- }
- }
- else
- {
- if(!known_users.contains(cn.getAuthor()))
- {
- known_users.add(cn.getAuthor());
- Collections.sort(known_users);
- }
- }
- }
- knownUsersPanel.setText("");
- knownUsersPanel.append("Indoor users:\n");
- for(User a:known_users)
- {
- knownUsersPanel.append(" " + a.getName() + " \n");
- }
- knownUsersPanel.append("\nOutdoor users:\n");
- for(User a:known_outdoor_users)
- {
- knownUsersPanel.append(" " + a.getName() + " \n");
- }
- displayArea.setCaretPosition(displayArea.getDocument().getLength());
- }
- }
- catch(Exception e)
- {
- e.printStackTrace();
- /*System.out.println("Socket closed");
- dest_sockets.remove(socket);
- outdoor_dest_sockets.remove(socket);*/
- }
- }
- }
- }
|