mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-13 11:09:39 +02:00
ChatState mostly code complete.
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@6217 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
10eda70a40
commit
88ea6cf037
7 changed files with 223 additions and 32 deletions
|
@ -42,7 +42,7 @@ public class Chat {
|
|||
private ChatManager chatManager;
|
||||
private String threadID;
|
||||
private String participant;
|
||||
private final Set<PacketListener> listeners = new CopyOnWriteArraySet<PacketListener>();
|
||||
private final Set<MessageListener> listeners = new CopyOnWriteArraySet<MessageListener>();
|
||||
|
||||
/**
|
||||
* Creates a new chat with the specified user and thread ID.
|
||||
|
@ -119,14 +119,15 @@ public class Chat {
|
|||
*
|
||||
* @param listener a packet listener.
|
||||
*/
|
||||
public void addMessageListener(PacketListener listener) {
|
||||
public void addMessageListener(MessageListener listener) {
|
||||
if(listener == null) {
|
||||
return;
|
||||
}
|
||||
// TODO these references should be weak.
|
||||
listeners.add(listener);
|
||||
}
|
||||
|
||||
public void removeMessageListener(PacketListener listener) {
|
||||
public void removeMessageListener(MessageListener listener) {
|
||||
listeners.remove(listener);
|
||||
}
|
||||
|
||||
|
@ -135,7 +136,7 @@ public class Chat {
|
|||
*
|
||||
* @return an unmodifiable collection of all of the listeners registered with this chat.
|
||||
*/
|
||||
public Collection<PacketListener> getListeners() {
|
||||
public Collection<MessageListener> getListeners() {
|
||||
return Collections.unmodifiableCollection(listeners);
|
||||
}
|
||||
|
||||
|
@ -164,8 +165,8 @@ public class Chat {
|
|||
// probably never had one.
|
||||
message.setThread(threadID);
|
||||
|
||||
for (PacketListener listener : listeners) {
|
||||
listener.processPacket(message);
|
||||
for (MessageListener listener : listeners) {
|
||||
listener.processMessage(this, message);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -37,6 +37,7 @@ import java.util.concurrent.CopyOnWriteArraySet;
|
|||
* @author Alexander Wenckus
|
||||
*/
|
||||
public class ChatManager {
|
||||
|
||||
/**
|
||||
* Returns the next unique id. Each id made up of a short alphanumeric
|
||||
* prefix along with a unique numeric value.
|
||||
|
@ -70,7 +71,11 @@ public class ChatManager {
|
|||
private Map<String, Chat> jidChats = new ReferenceMap<String, Chat>(ReferenceMap.HARD,
|
||||
ReferenceMap.WEAK);
|
||||
|
||||
private Set<ChatManagerListener> chatManagerListeners = new CopyOnWriteArraySet<ChatManagerListener>();
|
||||
private Set<ChatManagerListener> chatManagerListeners
|
||||
= new CopyOnWriteArraySet<ChatManagerListener>();
|
||||
|
||||
private Map<PacketInterceptor, PacketFilter> interceptors
|
||||
= new WeakHashMap<PacketInterceptor, PacketFilter>();
|
||||
|
||||
private XMPPConnection connection;
|
||||
|
||||
|
@ -115,7 +120,7 @@ public class ChatManager {
|
|||
* @param listener the listener which will listen for new messages from this chat.
|
||||
* @return the created chat.
|
||||
*/
|
||||
public Chat createChat(String userJID, PacketListener listener) {
|
||||
public Chat createChat(String userJID, MessageListener listener) {
|
||||
String threadID = nextID();
|
||||
|
||||
Chat chat = createChat(userJID, threadID, true);
|
||||
|
@ -186,7 +191,12 @@ public class ChatManager {
|
|||
}
|
||||
|
||||
void sendMessage(Chat chat, Message message) {
|
||||
// Here we will run any interceptors
|
||||
for(Map.Entry<PacketInterceptor, PacketFilter> interceptor : interceptors.entrySet()) {
|
||||
PacketFilter filter = interceptor.getValue();
|
||||
if(filter != null && filter.accept(message)) {
|
||||
interceptor.getKey().interceptPacket(message);
|
||||
}
|
||||
}
|
||||
connection.sendPacket(message);
|
||||
}
|
||||
|
||||
|
@ -195,4 +205,18 @@ public class ChatManager {
|
|||
new FromContainsFilter(chat.getParticipant())));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an interceptor which intercepts any messages sent through chats.
|
||||
*
|
||||
* @param packetInterceptor the interceptor.
|
||||
*/
|
||||
public void addOutgoingMessageInterceptor(PacketInterceptor packetInterceptor) {
|
||||
addOutgoingMessageInterceptor(packetInterceptor, null);
|
||||
}
|
||||
|
||||
public void addOutgoingMessageInterceptor(PacketInterceptor packetInterceptor, PacketFilter filter) {
|
||||
if (packetInterceptor != null) {
|
||||
interceptors.put(packetInterceptor, filter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
30
source/org/jivesoftware/smack/MessageListener.java
Normal file
30
source/org/jivesoftware/smack/MessageListener.java
Normal file
|
@ -0,0 +1,30 @@
|
|||
/**
|
||||
* $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;
|
||||
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public interface MessageListener {
|
||||
void processMessage(Chat chat, Message message);
|
||||
}
|
|
@ -45,6 +45,16 @@ public class PacketExtensionFilter implements PacketFilter {
|
|||
this.namespace = namespace;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new packet extension filter. Packets will pass the filter if they have a packet
|
||||
* extension that matches the specified namespace.
|
||||
*
|
||||
* @param namespace the XML namespace of the packet extension.
|
||||
*/
|
||||
public PacketExtensionFilter(String namespace) {
|
||||
this(null, namespace);
|
||||
}
|
||||
|
||||
public boolean accept(Packet packet) {
|
||||
return packet.getExtension(elementName, namespace) != null;
|
||||
}
|
||||
|
|
|
@ -26,6 +26,7 @@ import java.io.ByteArrayOutputStream;
|
|||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
/**
|
||||
* Base class for XMPP packets. Every packet has a unique ID (which is automatically
|
||||
|
@ -72,7 +73,9 @@ public abstract class Packet {
|
|||
private String packetID = null;
|
||||
private String to = null;
|
||||
private String from = null;
|
||||
private List<PacketExtension> packetExtensions = null;
|
||||
private final List<PacketExtension> packetExtensions
|
||||
= new CopyOnWriteArrayList<PacketExtension>();
|
||||
|
||||
private Map<String,Object> properties = null;
|
||||
private XMPPError error = null;
|
||||
|
||||
|
@ -179,9 +182,20 @@ public abstract class Packet {
|
|||
return Collections.unmodifiableList(new ArrayList<PacketExtension>(packetExtensions));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first extension of this packet that has the given namespace.
|
||||
*
|
||||
* @param namespace the namespace of the extension that is desired.
|
||||
* @return the packet extension with the given namespace.
|
||||
*/
|
||||
public PacketExtension getExtension(String namespace) {
|
||||
return getExtension(null, namespace);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the first packet extension that matches the specified element name and
|
||||
* namespace, or <tt>null</tt> if it doesn't exist. Packet extensions are
|
||||
* namespace, or <tt>null</tt> if it doesn't exist. If the provided elementName is null
|
||||
* than only the provided namespace is attempted to be matched. Packet extensions are
|
||||
* are arbitrary XML sub-documents in standard XMPP packets. By default, a
|
||||
* DefaultPacketExtension instance will be returned for each extension. However,
|
||||
* PacketExtensionProvider instances can be registered with the
|
||||
|
@ -189,16 +203,18 @@ public abstract class Packet {
|
|||
* class to handle custom parsing. In that case, the type of the Object
|
||||
* will be determined by the provider.
|
||||
*
|
||||
* @param elementName the XML element name of the packet extension.
|
||||
* @param elementName the XML element name of the packet extension. (May be null)
|
||||
* @param namespace the XML element namespace of the packet extension.
|
||||
* @return the extension, or <tt>null</tt> if it doesn't exist.
|
||||
*/
|
||||
public synchronized PacketExtension getExtension(String elementName, String namespace) {
|
||||
if (packetExtensions == null || elementName == null || namespace == null) {
|
||||
public PacketExtension getExtension(String elementName, String namespace) {
|
||||
if (namespace == null) {
|
||||
return null;
|
||||
}
|
||||
for (PacketExtension ext : packetExtensions) {
|
||||
if (elementName.equals(ext.getElementName()) && namespace.equals(ext.getNamespace())) {
|
||||
if ((elementName == null || elementName.equals(ext.getElementName()))
|
||||
&& namespace.equals(ext.getNamespace()))
|
||||
{
|
||||
return ext;
|
||||
}
|
||||
}
|
||||
|
@ -210,10 +226,7 @@ public abstract class Packet {
|
|||
*
|
||||
* @param extension a packet extension.
|
||||
*/
|
||||
public synchronized void addExtension(PacketExtension extension) {
|
||||
if (packetExtensions == null) {
|
||||
packetExtensions = new ArrayList<PacketExtension>();
|
||||
}
|
||||
public void addExtension(PacketExtension extension) {
|
||||
packetExtensions.add(extension);
|
||||
}
|
||||
|
||||
|
@ -222,10 +235,8 @@ public abstract class Packet {
|
|||
*
|
||||
* @param extension the packet extension to remove.
|
||||
*/
|
||||
public synchronized void removeExtension(PacketExtension extension) {
|
||||
if (packetExtensions != null) {
|
||||
packetExtensions.remove(extension);
|
||||
}
|
||||
public void removeExtension(PacketExtension extension) {
|
||||
packetExtensions.remove(extension);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue