1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-29 23:42:06 +01:00

Initial work on ChatState.

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@6215 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Alex Wenckus 2006-11-23 01:51:00 +00:00 committed by alex
parent ae6065d7cc
commit 3269d65591
10 changed files with 288 additions and 67 deletions

View file

@ -30,6 +30,13 @@
<className>org.jivesoftware.smackx.provider.MessageEventProvider</className> <className>org.jivesoftware.smackx.provider.MessageEventProvider</className>
</extensionProvider> </extensionProvider>
<!-- Chat State -->
<!--<extensionProvider>-->
<!--<elementName>active</elementName>-->
<!--<namespace>http://jabber.org/protocol/chatstates</namespace>-->
<!--<className></className>-->
<!--</extensionProvider>-->
<!-- XHTML --> <!-- XHTML -->
<extensionProvider> <extensionProvider>
<elementName>html</elementName> <elementName>html</elementName>

View file

@ -22,9 +22,9 @@ package org.jivesoftware.smack;
import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Message;
import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.Set; import java.util.Set;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.CopyOnWriteArraySet;
/** /**
@ -42,8 +42,7 @@ public class Chat {
private ChatManager chatManager; private ChatManager chatManager;
private String threadID; private String threadID;
private String participant; private String participant;
private final Set<WeakReference<PacketListener>> listeners = private final Set<PacketListener> listeners = new CopyOnWriteArraySet<PacketListener>();
new CopyOnWriteArraySet<WeakReference<PacketListener>>();
/** /**
* Creates a new chat with the specified user and thread ID. * Creates a new chat with the specified user and thread ID.
@ -124,7 +123,20 @@ public class Chat {
if(listener == null) { if(listener == null) {
return; return;
} }
listeners.add(new WeakReference<PacketListener>(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<PacketListener> getListeners() {
return Collections.unmodifiableCollection(listeners);
} }
/** /**
@ -152,16 +164,8 @@ public class Chat {
// probably never had one. // probably never had one.
message.setThread(threadID); message.setThread(threadID);
for (Iterator<WeakReference<PacketListener>> i = listeners.iterator(); i.hasNext();) { for (PacketListener listener : listeners) {
WeakReference<PacketListener> listenerRef = i.next();
PacketListener listener;
if ((listener = listenerRef.get()) != null) {
listener.processPacket(message); listener.processPacket(message);
} }
// If the reference was cleared, remove it from the set.
else {
i.remove();
}
}
} }
} }

View file

@ -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);
}

View file

@ -1,11 +1,23 @@
/** /**
* $RCSfile: $ * $RCSfile$
* $Revision: $ * $Revision: 2407 $
* $Date: $ * $Date: 2004-11-02 15:37:00 -0800 (Tue, 02 Nov 2004) $
* *
* Copyright (C) 2006 Jive Software. All rights reserved. * Copyright 2003-2004 Jive Software.
* This software is the proprietary information of Jive Software. Use is subject to license terms. *
* 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; package org.jivesoftware.smack;
import org.jivesoftware.smack.util.StringUtils; 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 * 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 * 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 * @author Alexander Wenckus
*/ */
@ -58,16 +70,14 @@ public class ChatManager {
private Map<String, Chat> jidChats = new ReferenceMap<String, Chat>(ReferenceMap.HARD, private Map<String, Chat> jidChats = new ReferenceMap<String, Chat>(ReferenceMap.HARD,
ReferenceMap.WEAK); ReferenceMap.WEAK);
private Set<ChatListener> chatListeners = new CopyOnWriteArraySet<ChatListener>(); private Set<ChatManagerListener> chatManagerListeners = new CopyOnWriteArraySet<ChatManagerListener>();
private XMPPConnection connection; private XMPPConnection connection;
ChatManager(XMPPConnection connection) { ChatManager(XMPPConnection connection) {
this.connection = connection; this.connection = connection;
PacketFilter filter = new AndFilter(new PacketTypeFilter(Message.class), PacketFilter filter = new PacketFilter() {
new PacketFilter() {
public boolean accept(Packet packet) { public boolean accept(Packet packet) {
if (!(packet instanceof Message)) { if (!(packet instanceof Message)) {
return false; return false;
@ -76,7 +86,7 @@ public class ChatManager {
return messageType != Message.Type.groupchat && return messageType != Message.Type.groupchat &&
messageType != Message.Type.headline; messageType != Message.Type.headline;
} }
}); };
// Add a listener for all message packets so that we can deliver errant // Add a listener for all message packets so that we can deliver errant
// messages to the best Chat instance available. // messages to the best Chat instance available.
connection.addPacketListener(new PacketListener() { connection.addPacketListener(new PacketListener() {
@ -119,7 +129,7 @@ public class ChatManager {
threadChats.put(threadID, chat); threadChats.put(threadID, chat);
jidChats.put(userJID, chat); jidChats.put(userJID, chat);
for(ChatListener listener : chatListeners) { for(ChatManagerListener listener : chatManagerListeners) {
listener.chatCreated(chat, createdLocally); listener.chatCreated(chat, createdLocally);
} }
@ -146,8 +156,8 @@ public class ChatManager {
* *
* @param listener the listener. * @param listener the listener.
*/ */
public void addChatListener(ChatListener listener) { public void addChatListener(ChatManagerListener listener) {
chatListeners.add(listener); chatManagerListeners.add(listener);
} }
/** /**
@ -155,8 +165,8 @@ public class ChatManager {
* *
* @param listener the listener that is being removed * @param listener the listener that is being removed
*/ */
public void removeChatListener(ChatListener listener) { public void removeChatListener(ChatManagerListener listener) {
chatListeners.remove(listener); chatManagerListeners.remove(listener);
} }
/** /**
@ -166,8 +176,8 @@ public class ChatManager {
* @return an unmodifiable collection of all chat listeners currently registered with this * @return an unmodifiable collection of all chat listeners currently registered with this
* manager. * manager.
*/ */
public Collection<ChatListener> getChatListeners() { public Collection<ChatManagerListener> getChatListeners() {
return Collections.unmodifiableCollection(chatListeners); return Collections.unmodifiableCollection(chatManagerListeners);
} }
private void deliverMessage(Chat chat, Message message) { private void deliverMessage(Chat chat, Message message) {

View file

@ -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);
}

View file

@ -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
* <a href="http://www.xmpp.org/extensions/xep-0085.html">XEP-0085</a>.
*
* @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
}

View file

@ -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);
}

View file

@ -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();
}
}

View file

@ -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;
}
}
}

View file

@ -454,7 +454,7 @@ public class MultiUserChatTest extends SmackTestCase {
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room); MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2"); muc2.join("testbot2");
getConnection(0).getChatManager().addChatListener(new ChatListener() { getConnection(0).getChatManager().addChatListener(new ChatManagerListener() {
public void chatCreated(Chat chat2, boolean createdLocally) { public void chatCreated(Chat chat2, boolean createdLocally) {
assertEquals( assertEquals(
"Sender of chat is incorrect", "Sender of chat is incorrect",