1 20 21 package org.jivesoftware.smack; 22 23 import org.jivesoftware.smack.packet.Presence; 24 import org.jivesoftware.smack.packet.Message; 25 import org.jivesoftware.smack.packet.Packet; 26 import org.jivesoftware.smack.filter.*; 27 28 import java.util.*; 29 30 38 public class GroupChat { 39 40 private XMPPConnection connection; 41 private String room; 42 private String nickname = null; 43 private boolean joined = false; 44 private List participants = new ArrayList(); 45 private List connectionListeners = new ArrayList(); 46 47 private PacketFilter presenceFilter; 48 private PacketFilter messageFilter; 49 private PacketCollector messageCollector; 50 51 66 public GroupChat(XMPPConnection connection, String room) { 67 this.connection = connection; 68 this.room = room; 69 messageFilter = new AndFilter(new FromContainsFilter(room), 71 new PacketTypeFilter(Message.class)); 72 messageFilter = new AndFilter(messageFilter, new PacketFilter() { 73 public boolean accept(Packet packet) { 74 Message msg = (Message)packet; 75 return msg.getType() == Message.Type.GROUP_CHAT; 76 } 77 }); 78 messageCollector = connection.createPacketCollector(messageFilter); 79 presenceFilter = new AndFilter(new FromContainsFilter(room), 81 new PacketTypeFilter(Presence.class)); 82 connection.addPacketListener(new PacketListener() { 83 public void processPacket(Packet packet) { 84 Presence presence = (Presence)packet; 85 String from = presence.getFrom(); 86 if (presence.getType() == Presence.Type.AVAILABLE) { 87 synchronized (participants) { 88 if (!participants.contains(from)) { 89 participants.add(from); 90 } 91 } 92 } 93 else if (presence.getType() == Presence.Type.UNAVAILABLE) { 94 synchronized (participants) { 95 participants.remove(from); 96 } 97 } 98 } 99 }, presenceFilter); 100 } 101 102 107 public String getRoom() { 108 return room; 109 } 110 111 122 public synchronized void join(String nickname) throws XMPPException { 123 join(nickname, SmackConfiguration.getPacketReplyTimeout()); 124 } 125 126 138 public synchronized void join(String nickname, long timeout) throws XMPPException { 139 if (nickname == null || nickname.equals("")) { 140 throw new IllegalArgumentException ("Nickname must not be null or blank."); 141 } 142 if (joined) { 145 leave(); 146 } 147 Presence joinPresence = new Presence(Presence.Type.AVAILABLE); 150 joinPresence.setTo(room + "/" + nickname); 151 PacketFilter responseFilter = new AndFilter( 153 new FromContainsFilter(room + "/" + nickname), 154 new PacketTypeFilter(Presence.class)); 155 PacketCollector response = connection.createPacketCollector(responseFilter); 156 connection.sendPacket(joinPresence); 158 Presence presence = (Presence)response.nextResult(timeout); 160 response.cancel(); 161 if (presence == null) { 162 throw new XMPPException("No response from server."); 163 } 164 else if (presence.getError() != null) { 165 throw new XMPPException(presence.getError()); 166 } 167 this.nickname = nickname; 168 joined = true; 169 } 170 171 177 public boolean isJoined() { 178 return joined; 179 } 180 181 184 public synchronized void leave() { 185 if (!joined) { 187 return; 188 } 189 Presence leavePresence = new Presence(Presence.Type.UNAVAILABLE); 192 leavePresence.setTo(room + "/" + nickname); 193 connection.sendPacket(leavePresence); 194 participants = new ArrayList(); 196 nickname = null; 197 joined = false; 198 } 199 200 206 public String getNickname() { 207 return nickname; 208 } 209 210 220 public int getParticipantCount() { 221 synchronized (participants) { 222 return participants.size(); 223 } 224 } 225 226 237 public Iterator getParticipants() { 238 synchronized (participants) { 239 return Collections.unmodifiableList(new ArrayList(participants)).iterator(); 240 } 241 } 242 243 251 public void addParticipantListener(PacketListener listener) { 252 connection.addPacketListener(listener, presenceFilter); 253 connectionListeners.add(listener); 254 } 255 256 262 public void sendMessage(String text) throws XMPPException { 263 Message message = new Message(room, Message.Type.GROUP_CHAT); 264 message.setBody(text); 265 connection.sendPacket(message); 266 } 267 268 273 public Message createMessage() { 274 return new Message(room, Message.Type.GROUP_CHAT); 275 } 276 277 283 public void sendMessage(Message message) throws XMPPException { 284 connection.sendPacket(message); 285 } 286 287 298 public Message pollMessage() { 299 return (Message)messageCollector.pollResult(); 300 } 301 302 308 public Message nextMessage() { 309 return (Message)messageCollector.nextResult(); 310 } 311 312 321 public Message nextMessage(long timeout) { 322 return (Message)messageCollector.nextResult(timeout); 323 } 324 325 335 public void addMessageListener(PacketListener listener) { 336 connection.addPacketListener(listener, messageFilter); 337 connectionListeners.add(listener); 338 } 339 340 public void finalize() throws Throwable { 341 super.finalize(); 342 try { 343 if (messageCollector != null) { 344 messageCollector.cancel(); 345 } 346 for (Iterator it=connectionListeners.iterator(); it.hasNext();) { 348 connection.removePacketListener((PacketListener) it.next()); 349 } 350 } 351 catch (Exception e) {} 352 } 353 } | Popular Tags |