| 5 | |
| 6 | |
| 7 | public class BroadCast extends Thread { |
| 8 | |
| 9 | ClientThread clientThread; |
| 10 | ServerThread serverThread; |
| 11 | String str; |
| 12 | private boolean flag_exit = false; |
| 13 | public BroadCast(ServerThread serverThread){ |
| 14 | this.serverThread = serverThread; |
| 15 | } |
| 16 | |
| 17 | @Override |
| 18 | public void run() { |
| 19 | boolean flag = true; |
| 20 | while(flag_exit){ |
| 21 | synchronized (serverThread.messages) { |
| 22 | if(serverThread.messages.isEmpty()){ |
| 23 | continue; |
| 24 | }else{ |
| 25 | str = (String)serverThread.messages.firstElement(); |
| 26 | serverThread.messages.removeElement(str); |
| 27 | if(str.contains("@clientThread")){ |
| 28 | flag = false; |
| 29 | } |
| 30 | } |
| 31 | } |
| 32 | synchronized (serverThread.clients) { |
| 33 | for(int i=0; i < serverThread.clients.size(); i++) |
| 34 | { |
| 35 | clientThread = serverThread.clients.elementAt(i); |
| 36 | if(flag){ |
| 37 | try |
| 38 | { |
| 39 | //���¼��ÿһ���ͻ��˷���������Ϣ |
| 40 | if(str.contains("@exit")){ |
| 41 | serverThread.clients.remove(i); |
| 42 | clientThread.closeClienthread(clientThread); |
| 43 | clientThread.dos.writeUTF(str); |
| 44 | } |
| 45 | if(str.contains("@chat") || str.contains("@userlist") || str.contains("@serverexit")){ |
| 46 | clientThread.dos.writeUTF(str); |
| 47 | } |
| 48 | if(str.contains("@single")){ |
| 49 | String[] info = str.split("@single"); |
| 50 | int id_thread = Integer.parseInt(info[2]); |
| 51 | for(int j = 0; j < serverThread.clients.size(); j++){ |
| 52 | if(id_thread == serverThread.clients.get(j).getId()){ |
| 53 | serverThread.clients.get(j).dos.writeUTF(str); |
| 54 | i = serverThread.clients.size(); |
| 55 | break; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | catch(IOException E){} |
| 61 | }else{ |
| 62 | String value = serverThread.users.get((int)clientThread.getId()); |
| 63 | if(value.equals("@login@")){ |
| 64 | flag = true; |
nothing calls this directly
no outgoing calls
no test coverage detected