1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-30 02:02:06 +01:00

References to PacketWriterListener changed to PacketListener

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2172 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Gaston Dombiak 2003-11-18 17:06:47 +00:00 committed by gdombiak
parent 4a8060c209
commit 7d7c02b750
6 changed files with 75 additions and 110 deletions

View file

@ -55,6 +55,7 @@ package org.jivesoftware.smack;
import java.util.*; import java.util.*;
import java.io.*; import java.io.*;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.Packet;
/** /**
@ -122,25 +123,27 @@ class PacketWriter {
} }
/** /**
* Registers a packet writer listener with this writer. The listener will be * Registers a packet listener with this writer. The listener will be
* notified of every packet that this writer sends. * notified of every packet that this writer sends. A packet filter determines
* which packets will be delivered to the listener.
* *
* @param packetWriterListener the packet writer listener to notify of sent packets. * @param packetListener the packet listener to notify of sent packets.
* @param packetFilter the packet filter to use.
*/ */
public void addPacketListener(PacketWriterListener packetWriterListener) { public void addPacketListener(PacketListener packetListener, PacketFilter packetFilter) {
synchronized (listeners) { synchronized (listeners) {
listeners.add(packetWriterListener); listeners.add(new ListenerWrapper(packetListener, packetFilter));
} }
} }
/** /**
* Removes a packet writer listener. * Removes a packet listener.
* *
* @param packetWriterListener the packet writer listener to remove. * @param packetListener the packet listener to remove.
*/ */
public void removePacketListener(PacketWriterListener packetWriterListener) { public void removePacketListener(PacketListener packetListener) {
synchronized (listeners) { synchronized (listeners) {
listeners.remove(packetWriterListener); listeners.remove(packetListener);
} }
} }
@ -236,11 +239,46 @@ class PacketWriter {
// Notify the listeners of the new sent packet // Notify the listeners of the new sent packet
int size = listeners.size(); int size = listeners.size();
for (int i=0; i<size; i++) { for (int i=0; i<size; i++) {
PacketWriterListener packetWriterListener = (PacketWriterListener)listeners.get(i); ListenerWrapper listenerWrapper = (ListenerWrapper)listeners.get(i);
if (packetWriterListener != null) { if (listenerWrapper != null) {
packetWriterListener.processPacket(sentPacket); listenerWrapper.notifyListener(sentPacket);
} }
} }
} }
} }
/**
* A wrapper class to associate a packet filter with a listener.
*/
private static class ListenerWrapper {
private PacketListener packetListener;
private PacketFilter packetFilter;
public ListenerWrapper(PacketListener packetListener,
PacketFilter packetFilter)
{
this.packetListener = packetListener;
this.packetFilter = packetFilter;
}
public boolean equals(Object object) {
if (object == null) {
return false;
}
if (object instanceof ListenerWrapper) {
return ((ListenerWrapper)object).packetListener.equals(this.packetListener);
}
else if (object instanceof PacketListener) {
return object.equals(this.packetListener);
}
return false;
}
public void notifyListener(Packet packet) {
if (packetFilter == null || packetFilter.accept(packet)) {
packetListener.processPacket(packet);
}
}
}
} }

View file

@ -1,75 +0,0 @@
/**
* $RCSfile$
* $Revision$
* $Date$
*
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
* ====================================================================
* The Jive Software License (based on Apache Software License, Version 1.1)
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by
* Jive Software (http://www.jivesoftware.com)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Smack" and "Jive Software" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please
* contact webmaster@jivesoftware.com.
*
* 5. Products derived from this software may not be called "Smack",
* nor may "Smack" appear in their name, without prior written
* permission of Jive Software.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*/
package org.jivesoftware.smack;
import org.jivesoftware.smack.packet.Packet;
/**
* Provides a mechanism to listen for packets that are written to a XMPP server.
* This allows event-style programming -- every time a new packet is written,
* the {@link #processPacket(Packet)} method will be called.
*
* @see XMPPConnection#addPacketListener(PacketWriterListener)
*
* @author Gaston Dombiak
*/
public interface PacketWriterListener {
/**
* Process the next packet sent to this packet listener.<p>
*
* @param packet the packet to process.
*/
public void processPacket(Packet packet);
}

View file

@ -570,25 +570,27 @@ public class XMPPConnection {
} }
/** /**
* Registers a packet writer listener with this connection. The listener will be * Registers a packet listener with this connection. The listener will be
* notified of every packet that this connection sends. * notified of every packet that this connection sends. A packet filter determines
* which packets will be delivered to the listener.
* *
* @param packetWriterListener the packet writer listener to notify of sent packets. * @param packetListener the packet listener to notify of sent packets.
* @param packetFilter the packet filter to use.
*/ */
public void addPacketListener(PacketWriterListener packetWriterListener) { public void addPacketWriterListener(PacketListener packetListener, PacketFilter packetFilter) {
if (!isConnected()) { if (!isConnected()) {
throw new IllegalStateException("Not connected to server."); throw new IllegalStateException("Not connected to server.");
} }
packetWriter.addPacketListener(packetWriterListener); packetWriter.addPacketListener(packetListener, packetFilter);
} }
/** /**
* Removes a packet writer listener from this connection. * Removes a packet listener from this connection.
* *
* @param packetWriterListener the packet writer listener to remove. * @param packetListener the packet listener to remove.
*/ */
public void removePacketListener(PacketWriterListener packetWriterListener) { public void removePacketWriterListener(PacketListener packetListener) {
packetWriter.removePacketListener(packetWriterListener); packetWriter.removePacketListener(packetListener);
} }
/** /**
@ -689,9 +691,9 @@ public class XMPPConnection {
// If debugging is enabled, we should start the thread that will listen for // If debugging is enabled, we should start the thread that will listen for
// all packets and then log them. // all packets and then log them.
if (DEBUG_ENABLED) { if (DEBUG_ENABLED) {
packetReader.addPacketListener(debugger.getListener(), null); packetReader.addPacketListener(debugger.getReaderListener(), null);
if (debugger.getWriterListener() != null) { if (debugger.getWriterListener() != null) {
packetWriter.addPacketListener(debugger.getWriterListener()); packetWriter.addPacketListener(debugger.getWriterListener(), null);
} }
} }
// Start the packet writer. This will open a XMPP stream to the server // Start the packet writer. This will open a XMPP stream to the server

View file

@ -415,11 +415,11 @@ public class LiteDebugger implements SmackDebugger {
return writer; return writer;
} }
public PacketListener getListener() { public PacketListener getReaderListener() {
return listener; return listener;
} }
public PacketWriterListener getWriterListener() { public PacketListener getWriterListener() {
return null; return null;
} }
} }

View file

@ -98,13 +98,13 @@ public interface SmackDebugger {
* @return the PacketListener that will listen for all incoming packets and write them to * @return the PacketListener that will listen for all incoming packets and write them to
* the GUI * the GUI
*/ */
public abstract PacketListener getListener(); public abstract PacketListener getReaderListener();
/** /**
* Returns the thread that will listen for all outgoing packets and write them to the GUI. * Returns the thread that will listen for all outgoing packets and write them to the GUI.
* *
* @return the PacketWriterListener that will listen for all sent packets and write them to * @return the PacketListener that will listen for all sent packets and write them to
* the GUI * the GUI
*/ */
public abstract PacketWriterListener getWriterListener(); public abstract PacketListener getWriterListener();
} }

View file

@ -89,8 +89,8 @@ public class EnhancedDebugger implements SmackDebugger {
private XMPPConnection connection = null; private XMPPConnection connection = null;
private PacketListener listener = null; private PacketListener readerListener = null;
private PacketWriterListener writerListener = null; private PacketListener writerListener = null;
private ConnectionListener connListener = null; private ConnectionListener connListener = null;
private Writer writer; private Writer writer;
@ -150,7 +150,7 @@ public class EnhancedDebugger implements SmackDebugger {
// Create a thread that will listen for all incoming packets and write them to // Create a thread that will listen for all incoming packets and write them to
// the GUI. This is what we call "interpreted" packet data, since it's the packet // the GUI. This is what we call "interpreted" packet data, since it's the packet
// data as Smack sees it and not as it's coming in as raw XML. // data as Smack sees it and not as it's coming in as raw XML.
listener = new PacketListener() { readerListener = new PacketListener() {
SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm:ss aaa"); SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm:ss aaa");
public void processPacket(Packet packet) { public void processPacket(Packet packet) {
addReadPacketToTable(dateFormatter, packet); addReadPacketToTable(dateFormatter, packet);
@ -159,7 +159,7 @@ public class EnhancedDebugger implements SmackDebugger {
// Create a thread that will listen for all outgoing packets and write them to // Create a thread that will listen for all outgoing packets and write them to
// the GUI. // the GUI.
writerListener = new PacketWriterListener() { writerListener = new PacketListener() {
SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm:ss aaa"); SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm:ss aaa");
public void processPacket(Packet packet) { public void processPacket(Packet packet) {
addSentPacketToTable(dateFormatter, packet); addSentPacketToTable(dateFormatter, packet);
@ -624,11 +624,11 @@ public class EnhancedDebugger implements SmackDebugger {
return writer; return writer;
} }
public PacketListener getListener() { public PacketListener getReaderListener() {
return listener; return readerListener;
} }
public PacketWriterListener getWriterListener() { public PacketListener getWriterListener() {
return writerListener; return writerListener;
} }