2003-01-13 17:58:47 +01:00
|
|
|
/**
|
|
|
|
*
|
2007-02-12 01:59:05 +01:00
|
|
|
* Copyright 2003-2007 Jive Software.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* All rights reserved. Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-11-03 00:53:30 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
package org.jivesoftware.smack;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.packet.Message;
|
|
|
|
|
2006-07-18 07:14:33 +02:00
|
|
|
import java.util.Set;
|
2006-11-23 02:51:00 +01:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
2006-11-22 23:55:37 +01:00
|
|
|
import java.util.concurrent.CopyOnWriteArraySet;
|
2005-09-05 22:00:45 +02:00
|
|
|
|
2003-01-13 17:58:47 +01:00
|
|
|
/**
|
2005-09-05 22:00:45 +02:00
|
|
|
* A chat is a series of messages sent between two users. Each chat has a unique
|
|
|
|
* thread ID, which is used to track which messages are part of a particular
|
|
|
|
* conversation. Some messages are sent without a thread ID, and some clients
|
|
|
|
* don't send thread IDs at all. Therefore, if a message without a thread ID
|
|
|
|
* arrives it is routed to the most recently created Chat with the message
|
|
|
|
* sender.
|
2006-11-22 23:55:37 +01:00
|
|
|
*
|
2003-01-13 17:58:47 +01:00
|
|
|
* @author Matt Tucker
|
|
|
|
*/
|
|
|
|
public class Chat {
|
|
|
|
|
2006-11-22 23:55:37 +01:00
|
|
|
private ChatManager chatManager;
|
2004-03-11 16:43:21 +01:00
|
|
|
private String threadID;
|
2003-01-13 17:58:47 +01:00
|
|
|
private String participant;
|
2006-11-23 22:10:59 +01:00
|
|
|
private final Set<MessageListener> listeners = new CopyOnWriteArraySet<MessageListener>();
|
2003-01-13 17:58:47 +01:00
|
|
|
|
|
|
|
/**
|
2004-03-11 16:43:21 +01:00
|
|
|
* Creates a new chat with the specified user and thread ID.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2006-11-22 23:55:37 +01:00
|
|
|
* @param chatManager the chatManager the chat will use.
|
2003-01-13 17:58:47 +01:00
|
|
|
* @param participant the user to chat with.
|
2004-03-11 16:43:21 +01:00
|
|
|
* @param threadID the thread ID to use.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
2006-11-22 23:55:37 +01:00
|
|
|
Chat(ChatManager chatManager, String participant, String threadID) {
|
|
|
|
this.chatManager = chatManager;
|
2003-01-13 17:58:47 +01:00
|
|
|
this.participant = participant;
|
2004-03-11 16:43:21 +01:00
|
|
|
this.threadID = threadID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the thread id associated with this chat, which corresponds to the
|
|
|
|
* <tt>thread</tt> field of XMPP messages. This method may return <tt>null</tt>
|
|
|
|
* if there is no thread ID is associated with this Chat.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2004-03-11 16:43:21 +01:00
|
|
|
* @return the thread ID of this chat.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
2004-03-11 16:43:21 +01:00
|
|
|
public String getThreadID() {
|
|
|
|
return threadID;
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the name of the user the chat is with.
|
|
|
|
*
|
|
|
|
* @return the name of the user the chat is occuring with.
|
|
|
|
*/
|
|
|
|
public String getParticipant() {
|
|
|
|
return participant;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends the specified text as a message to the other chat participant.
|
|
|
|
* This is a convenience method for:
|
|
|
|
*
|
|
|
|
* <pre>
|
|
|
|
* Message message = chat.createMessage();
|
|
|
|
* message.setBody(messageText);
|
|
|
|
* chat.sendMessage(message);
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* @param text the text to send.
|
|
|
|
* @throws XMPPException if sending the message fails.
|
|
|
|
*/
|
|
|
|
public void sendMessage(String text) throws XMPPException {
|
2006-11-22 23:55:37 +01:00
|
|
|
Message message = new Message(participant, Message.Type.chat);
|
2004-03-11 16:43:21 +01:00
|
|
|
message.setThread(threadID);
|
2006-11-22 23:55:37 +01:00
|
|
|
message.setBody(text);
|
|
|
|
chatManager.sendMessage(this, message);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2003-05-27 17:20:00 +02:00
|
|
|
* Sends a message to the other chat participant. The thread ID, recipient,
|
2006-11-22 23:55:37 +01:00
|
|
|
* and message type of the message will automatically set to those of this chat.
|
2003-01-13 17:58:47 +01:00
|
|
|
*
|
2003-05-27 17:20:00 +02:00
|
|
|
* @param message the message to send.
|
|
|
|
* @throws XMPPException if an error occurs sending the message.
|
2003-01-13 17:58:47 +01:00
|
|
|
*/
|
|
|
|
public void sendMessage(Message message) throws XMPPException {
|
2003-05-27 17:20:00 +02:00
|
|
|
// Force the recipient, message type, and thread ID since the user elected
|
|
|
|
// to send the message through this chat object.
|
|
|
|
message.setTo(participant);
|
2006-11-22 23:55:37 +01:00
|
|
|
message.setType(Message.Type.chat);
|
2004-03-11 16:43:21 +01:00
|
|
|
message.setThread(threadID);
|
2006-11-22 23:55:37 +01:00
|
|
|
chatManager.sendMessage(this, message);
|
2003-01-13 17:58:47 +01:00
|
|
|
}
|
2003-04-03 00:52:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a packet listener that will be notified of any new messages in the
|
|
|
|
* chat.
|
|
|
|
*
|
|
|
|
* @param listener a packet listener.
|
|
|
|
*/
|
2006-11-23 22:10:59 +01:00
|
|
|
public void addMessageListener(MessageListener listener) {
|
2006-11-22 23:55:37 +01:00
|
|
|
if(listener == null) {
|
|
|
|
return;
|
2005-09-05 22:00:45 +02:00
|
|
|
}
|
2006-11-23 22:10:59 +01:00
|
|
|
// TODO these references should be weak.
|
2006-11-23 02:51:00 +01:00
|
|
|
listeners.add(listener);
|
|
|
|
}
|
|
|
|
|
2006-11-23 22:10:59 +01:00
|
|
|
public void removeMessageListener(MessageListener listener) {
|
2006-11-23 02:51:00 +01:00
|
|
|
listeners.remove(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an unmodifiable collection of all of the listeners registered with this chat.
|
|
|
|
*
|
|
|
|
* @return an unmodifiable collection of all of the listeners registered with this chat.
|
|
|
|
*/
|
2006-11-23 22:10:59 +01:00
|
|
|
public Collection<MessageListener> getListeners() {
|
2006-11-23 02:51:00 +01:00
|
|
|
return Collections.unmodifiableCollection(listeners);
|
2006-11-22 23:55:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a {@link org.jivesoftware.smack.PacketCollector} which will accumulate the Messages
|
|
|
|
* for this chat. Always cancel PacketCollectors when finished with them as they will accumulate
|
|
|
|
* messages indefinitely.
|
|
|
|
*
|
|
|
|
* @return the PacketCollector which returns Messages for this chat.
|
|
|
|
*/
|
|
|
|
public PacketCollector createCollector() {
|
|
|
|
return chatManager.createPacketCollector(this);
|
2005-09-05 22:00:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delivers a message directly to this chat, which will add the message
|
|
|
|
* to the collector and deliver it to all listeners registered with the
|
2010-02-09 12:55:56 +01:00
|
|
|
* Chat. This is used by the Connection class to deliver messages
|
2005-09-05 22:00:45 +02:00
|
|
|
* without a thread ID.
|
|
|
|
*
|
|
|
|
* @param message the message.
|
|
|
|
*/
|
|
|
|
void deliver(Message message) {
|
|
|
|
// Because the collector and listeners are expecting a thread ID with
|
|
|
|
// a specific value, set the thread ID on the message even though it
|
|
|
|
// probably never had one.
|
|
|
|
message.setThread(threadID);
|
|
|
|
|
2006-11-23 22:10:59 +01:00
|
|
|
for (MessageListener listener : listeners) {
|
|
|
|
listener.processMessage(this, message);
|
2004-07-16 22:46:16 +02:00
|
|
|
}
|
|
|
|
}
|
2007-01-17 20:36:45 +01:00
|
|
|
|
2014-02-01 23:22:30 +01:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "Chat [(participant=" + participant + "), (thread=" + threadID + ")]";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
int hash = 1;
|
|
|
|
hash = hash * 31 + threadID.hashCode();
|
|
|
|
hash = hash * 31 + participant.hashCode();
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2007-01-17 20:36:45 +01:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object obj) {
|
|
|
|
return obj instanceof Chat
|
|
|
|
&& threadID.equals(((Chat)obj).getThreadID())
|
|
|
|
&& participant.equals(((Chat)obj).getParticipant());
|
|
|
|
}
|
2003-04-03 00:52:07 +02:00
|
|
|
}
|