From 3269d655910a403a011bfb47a8cd6d67f1d336ba Mon Sep 17 00:00:00 2001 From: Alex Wenckus Date: Thu, 23 Nov 2006 01:51:00 +0000 Subject: [PATCH] Initial work on ChatState. git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@6215 b35dd754-fafc-0310-a699-88a17e54d16e --- build/resources/META-INF/smack.providers | 7 ++ source/org/jivesoftware/smack/Chat.java | 34 +++++----- .../org/jivesoftware/smack/ChatListener.java | 25 ------- .../org/jivesoftware/smack/ChatManager.java | 62 +++++++++-------- .../smack/ChatManagerListener.java | 37 +++++++++++ source/org/jivesoftware/smackx/ChatState.java | 50 ++++++++++++++ .../smackx/ChatStateListener.java | 31 +++++++++ .../jivesoftware/smackx/ChatStateManager.java | 41 ++++++++++++ .../smackx/packet/ChatStateExtension.java | 66 +++++++++++++++++++ .../smackx/muc/MultiUserChatTest.java | 2 +- 10 files changed, 288 insertions(+), 67 deletions(-) delete mode 100644 source/org/jivesoftware/smack/ChatListener.java create mode 100644 source/org/jivesoftware/smack/ChatManagerListener.java create mode 100644 source/org/jivesoftware/smackx/ChatState.java create mode 100644 source/org/jivesoftware/smackx/ChatStateListener.java create mode 100644 source/org/jivesoftware/smackx/ChatStateManager.java create mode 100644 source/org/jivesoftware/smackx/packet/ChatStateExtension.java diff --git a/build/resources/META-INF/smack.providers b/build/resources/META-INF/smack.providers index 3e3a66f00..37cbe6f98 100644 --- a/build/resources/META-INF/smack.providers +++ b/build/resources/META-INF/smack.providers @@ -29,6 +29,13 @@ jabber:x:event org.jivesoftware.smackx.provider.MessageEventProvider + + + + + + + diff --git a/source/org/jivesoftware/smack/Chat.java b/source/org/jivesoftware/smack/Chat.java index 478ec3cec..9e11c69ba 100644 --- a/source/org/jivesoftware/smack/Chat.java +++ b/source/org/jivesoftware/smack/Chat.java @@ -22,9 +22,9 @@ package org.jivesoftware.smack; import org.jivesoftware.smack.packet.Message; -import java.lang.ref.WeakReference; -import java.util.Iterator; import java.util.Set; +import java.util.Collection; +import java.util.Collections; import java.util.concurrent.CopyOnWriteArraySet; /** @@ -42,8 +42,7 @@ public class Chat { private ChatManager chatManager; private String threadID; private String participant; - private final Set> listeners = - new CopyOnWriteArraySet>(); + private final Set listeners = new CopyOnWriteArraySet(); /** * Creates a new chat with the specified user and thread ID. @@ -124,7 +123,20 @@ public class Chat { if(listener == null) { return; } - listeners.add(new WeakReference(listener)); + listeners.add(listener); + } + + public void removeMessageListener(PacketListener listener) { + 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. + */ + public Collection getListeners() { + return Collections.unmodifiableCollection(listeners); } /** @@ -152,16 +164,8 @@ public class Chat { // probably never had one. message.setThread(threadID); - for (Iterator> i = listeners.iterator(); i.hasNext();) { - WeakReference listenerRef = i.next(); - PacketListener listener; - if ((listener = listenerRef.get()) != null) { - listener.processPacket(message); - } - // If the reference was cleared, remove it from the set. - else { - i.remove(); - } + for (PacketListener listener : listeners) { + listener.processPacket(message); } } } \ No newline at end of file diff --git a/source/org/jivesoftware/smack/ChatListener.java b/source/org/jivesoftware/smack/ChatListener.java deleted file mode 100644 index 9e055b4bd..000000000 --- a/source/org/jivesoftware/smack/ChatListener.java +++ /dev/null @@ -1,25 +0,0 @@ -/** - * $RCSfile: $ - * $Revision: $ - * $Date: $ - * - * Copyright (C) 2006 Jive Software. All rights reserved. - * This software is the proprietary information of Jive Software. Use is subject to license terms. - */ -package org.jivesoftware.smack; - -/** - * A listener for chat related events. - * - * @author Alexander Wenckus - */ -public interface ChatListener { - - /** - * Event fired when a new chat is created. - * - * @param chat the chat that was created. - * @param createdLocally true if the chat was created by the local user and false if it wasn't. - */ - void chatCreated(Chat chat, boolean createdLocally); -} diff --git a/source/org/jivesoftware/smack/ChatManager.java b/source/org/jivesoftware/smack/ChatManager.java index 6052216d7..69cfd4d09 100644 --- a/source/org/jivesoftware/smack/ChatManager.java +++ b/source/org/jivesoftware/smack/ChatManager.java @@ -1,11 +1,23 @@ /** - * $RCSfile: $ - * $Revision: $ - * $Date: $ + * $RCSfile$ + * $Revision: 2407 $ + * $Date: 2004-11-02 15:37:00 -0800 (Tue, 02 Nov 2004) $ * - * Copyright (C) 2006 Jive Software. All rights reserved. - * This software is the proprietary information of Jive Software. Use is subject to license terms. + * Copyright 2003-2004 Jive Software. + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. */ + package org.jivesoftware.smack; import org.jivesoftware.smack.util.StringUtils; @@ -20,7 +32,7 @@ import java.util.concurrent.CopyOnWriteArraySet; /** * The chat manager keeps track of references to all current chats. It will not hold any references * in memory on its own so it is neccesary to keep a reference to the chat object itself. To be - * made aware of new chats, register a listener by calling {@link #addChatListener(ChatListener)}. + * made aware of new chats, register a listener by calling {@link #addChatListener(ChatManagerListener)}. * * @author Alexander Wenckus */ @@ -58,25 +70,23 @@ public class ChatManager { private Map jidChats = new ReferenceMap(ReferenceMap.HARD, ReferenceMap.WEAK); - private Set chatListeners = new CopyOnWriteArraySet(); + private Set chatManagerListeners = new CopyOnWriteArraySet(); private XMPPConnection connection; ChatManager(XMPPConnection connection) { this.connection = connection; - PacketFilter filter = new AndFilter(new PacketTypeFilter(Message.class), - new PacketFilter() { - - public boolean accept(Packet packet) { - if (!(packet instanceof Message)) { - return false; - } - Message.Type messageType = ((Message) packet).getType(); - return messageType != Message.Type.groupchat && - messageType != Message.Type.headline; - } - }); + PacketFilter filter = new PacketFilter() { + public boolean accept(Packet packet) { + if (!(packet instanceof Message)) { + return false; + } + Message.Type messageType = ((Message) packet).getType(); + return messageType != Message.Type.groupchat && + messageType != Message.Type.headline; + } + }; // Add a listener for all message packets so that we can deliver errant // messages to the best Chat instance available. connection.addPacketListener(new PacketListener() { @@ -119,7 +129,7 @@ public class ChatManager { threadChats.put(threadID, chat); jidChats.put(userJID, chat); - for(ChatListener listener : chatListeners) { + for(ChatManagerListener listener : chatManagerListeners) { listener.chatCreated(chat, createdLocally); } @@ -146,8 +156,8 @@ public class ChatManager { * * @param listener the listener. */ - public void addChatListener(ChatListener listener) { - chatListeners.add(listener); + public void addChatListener(ChatManagerListener listener) { + chatManagerListeners.add(listener); } /** @@ -155,8 +165,8 @@ public class ChatManager { * * @param listener the listener that is being removed */ - public void removeChatListener(ChatListener listener) { - chatListeners.remove(listener); + public void removeChatListener(ChatManagerListener listener) { + chatManagerListeners.remove(listener); } /** @@ -166,8 +176,8 @@ public class ChatManager { * @return an unmodifiable collection of all chat listeners currently registered with this * manager. */ - public Collection getChatListeners() { - return Collections.unmodifiableCollection(chatListeners); + public Collection getChatListeners() { + return Collections.unmodifiableCollection(chatManagerListeners); } private void deliverMessage(Chat chat, Message message) { diff --git a/source/org/jivesoftware/smack/ChatManagerListener.java b/source/org/jivesoftware/smack/ChatManagerListener.java new file mode 100644 index 000000000..5d145d67a --- /dev/null +++ b/source/org/jivesoftware/smack/ChatManagerListener.java @@ -0,0 +1,37 @@ +/** + * $RCSfile$ + * $Revision: 2407 $ + * $Date: 2004-11-02 15:37:00 -0800 (Tue, 02 Nov 2004) $ + * + * Copyright 2003-2004 Jive Software. + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +package org.jivesoftware.smack; + +/** + * A listener for chat related events. + * + * @author Alexander Wenckus + */ +public interface ChatManagerListener { + + /** + * Event fired when a new chat is created. + * + * @param chat the chat that was created. + * @param createdLocally true if the chat was created by the local user and false if it wasn't. + */ + void chatCreated(Chat chat, boolean createdLocally); +} diff --git a/source/org/jivesoftware/smackx/ChatState.java b/source/org/jivesoftware/smackx/ChatState.java new file mode 100644 index 000000000..382e05a82 --- /dev/null +++ b/source/org/jivesoftware/smackx/ChatState.java @@ -0,0 +1,50 @@ +/** + * $RCSfile$ + * $Revision: 2407 $ + * $Date: 2004-11-02 15:37:00 -0800 (Tue, 02 Nov 2004) $ + * + * Copyright 2003-2004 Jive Software. + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +package org.jivesoftware.smackx; + +/** + * Represents the current state of a users interaction with another user. Implemented according to + * XEP-0085. + * + * @author Alexander Wenckus + */ +public enum ChatState { + /** + * User is actively participating in the chat session. + */ + active, + /** + * User is composing a message. + */ + composing, + /** + * User had been composing but now has stopped. + */ + paused, + /** + * User has not been actively participating in the chat session. + */ + inactive, + /** + * User has effectively ended their participation in the chat session. + */ + gone +} diff --git a/source/org/jivesoftware/smackx/ChatStateListener.java b/source/org/jivesoftware/smackx/ChatStateListener.java new file mode 100644 index 000000000..3265c0d20 --- /dev/null +++ b/source/org/jivesoftware/smackx/ChatStateListener.java @@ -0,0 +1,31 @@ +/** + * $RCSfile$ + * $Revision: 2407 $ + * $Date: 2004-11-02 15:37:00 -0800 (Tue, 02 Nov 2004) $ + * + * Copyright 2003-2004 Jive Software. + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +package org.jivesoftware.smackx; + +import org.jivesoftware.smack.Chat; +import org.jivesoftware.smack.PacketListener; + +/** + * + */ +public interface ChatStateListener extends PacketListener { + void participantActive(Chat chat); +} diff --git a/source/org/jivesoftware/smackx/ChatStateManager.java b/source/org/jivesoftware/smackx/ChatStateManager.java new file mode 100644 index 000000000..1a0208184 --- /dev/null +++ b/source/org/jivesoftware/smackx/ChatStateManager.java @@ -0,0 +1,41 @@ +/** + * $RCSfile$ + * $Revision: 2407 $ + * $Date: 2004-11-02 15:37:00 -0800 (Tue, 02 Nov 2004) $ + * + * Copyright 2003-2004 Jive Software. + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +package org.jivesoftware.smackx; + +import org.jivesoftware.smack.Chat; +import org.jivesoftware.smack.packet.Message; + +/** + * + */ +public class ChatStateManager { + + /** + * + * @param currentState + * @param chat + */ + public void setCurrentState(ChatState currentState, Chat chat) { + Message message = new Message(); + } + + +} diff --git a/source/org/jivesoftware/smackx/packet/ChatStateExtension.java b/source/org/jivesoftware/smackx/packet/ChatStateExtension.java new file mode 100644 index 000000000..2df204c85 --- /dev/null +++ b/source/org/jivesoftware/smackx/packet/ChatStateExtension.java @@ -0,0 +1,66 @@ +/** + * $RCSfile$ + * $Revision: 2407 $ + * $Date: 2004-11-02 15:37:00 -0800 (Tue, 02 Nov 2004) $ + * + * Copyright 2003-2004 Jive Software. + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + */ + +package org.jivesoftware.smackx.packet; + +import org.jivesoftware.smackx.ChatState; +import org.jivesoftware.smack.packet.PacketExtension; +import org.jivesoftware.smack.provider.PacketExtensionProvider; +import org.xmlpull.v1.XmlPullParser; + +/** + * Represents a chat state which is an extension to message packets which is used to indicate + * the current status of a chat participant. + * + * @see org.jivesoftware.smackx.ChatState + * @author Alexander Wenckus + */ +public class ChatStateExtension implements PacketExtension { + + private ChatState state; + + /** + * Default constructor. The argument provided is the state that the extension will represent. + * + * @param state the state that the extension represents. + */ + public ChatStateExtension(ChatState state) { + this.state = state; + } + + public String getElementName() { + return state.name(); + } + + public String getNamespace() { + return "http://jabber.org/protocol/chatstates"; + } + + public String toXML() { + return "<" + getElementName() + " xmlns=\"" + getNamespace() + "\" />"; + } + + public class Provider implements PacketExtensionProvider { + + public PacketExtension parseExtension(XmlPullParser parser) throws Exception { + return null; + } + } +} diff --git a/test/org/jivesoftware/smackx/muc/MultiUserChatTest.java b/test/org/jivesoftware/smackx/muc/MultiUserChatTest.java index aebf90eed..378f2bdd8 100644 --- a/test/org/jivesoftware/smackx/muc/MultiUserChatTest.java +++ b/test/org/jivesoftware/smackx/muc/MultiUserChatTest.java @@ -454,7 +454,7 @@ public class MultiUserChatTest extends SmackTestCase { MultiUserChat muc2 = new MultiUserChat(getConnection(1), room); muc2.join("testbot2"); - getConnection(0).getChatManager().addChatListener(new ChatListener() { + getConnection(0).getChatManager().addChatListener(new ChatManagerListener() { public void chatCreated(Chat chat2, boolean createdLocally) { assertEquals( "Sender of chat is incorrect",