org.jabber.jabberbeans.util
Class SyncPacketListener

java.lang.Object
  |
  +--org.jabber.jabberbeans.util.SyncPacketListener

public class SyncPacketListener
extends java.lang.Object
implements PacketListener, java.io.Serializable

This is an interface that extends a PacketListener, so that it will filter out messages that are not the response being looked for. This is an adapter.

The main use for this class is to 'wait' on the main thread until the response for an IQ packet comes back.

Version:
$Revision: 1.1.1.1 $
Author:
David Waite <dwaite@jabber.com>, $Author: lucky $
See Also:
Serialized Form

Field Summary
private  ConnectionBean connectionbean
          Parent ConnectionBean
private  java.lang.Class waitClass
          internal placeholder value for a Class type representing a packet type to watch out for.
private static char WAITFORID
           
private static char WAITFORPACKETTYPE
           
private  Packet waitPacket
          internal packet value, used for reporting a Packet type to watch for, or as a placeholder for returning a packet after a incoming match is found.
private  boolean waitSuccessful
          boolean set to indicate to the main thread that the condition was met (as opposed to just timing out
private  int waitType
          type indicator for a single listener to be able to listen to and respond to different methods of use.
private static char WAITUNKNOWN
           
 
Constructor Summary
SyncPacketListener(ConnectionBean cbean)
          Creates a new IQPacketListener instance.
 
Method Summary
 void receivedPacket(PacketEvent p)
          Monitor receivedPacket listener method.
 void reset()
          reset restores the object to a known state, it should always be called if the object is reused.
 Packet sendAndWait(ContentPacket p)
          sends a packet, then waits for a response, blocking indefinately until the response comes over the wire, or there is an exception
 Packet sendAndWait(ContentPacket p, int timeout)
          sends a packet, then waits for a response, blocking until the response comes over the wire, there is a timeout, or there is an exception
 void sendFailed(PacketEvent pe)
          called if a packet is not successfully sent (for instance, if the connection dies while the packet is queued, or a packet is sent while disconnected).
 void sentPacket(PacketEvent p)
          Monitor sentPacket listener method
 Packet waitForType(java.lang.Class c)
          wait indefinately for a packet of type Class 'c' to come in.
 Packet waitForType(java.lang.Class c, int timeout)
          wait for a specified period of time for a packet of the same type as Class 'c' to come in
 Packet waitForType(Packet p)
          wait indefinately for another packet of the same type to come in.
 Packet waitForType(Packet p, int timeout)
          wait for a specified period of time for a packet of the same type as Packet p to come in
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

connectionbean

private ConnectionBean connectionbean
Parent ConnectionBean

waitPacket

private Packet waitPacket
internal packet value, used for reporting a Packet type to watch for, or as a placeholder for returning a packet after a incoming match is found.

waitClass

private java.lang.Class waitClass
internal placeholder value for a Class type representing a packet type to watch out for.

waitType

private int waitType
type indicator for a single listener to be able to listen to and respond to different methods of use.

WAITUNKNOWN

private static final char WAITUNKNOWN

WAITFORID

private static final char WAITFORID

WAITFORPACKETTYPE

private static final char WAITFORPACKETTYPE

waitSuccessful

private boolean waitSuccessful
boolean set to indicate to the main thread that the condition was met (as opposed to just timing out
Constructor Detail

SyncPacketListener

public SyncPacketListener(ConnectionBean cbean)
Creates a new IQPacketListener instance.
Parameters:
cbean - parent ConnectionBean
Method Detail

reset

public void reset()
reset restores the object to a known state, it should always be called if the object is reused.

sendAndWait

public Packet sendAndWait(ContentPacket p)
                   throws java.lang.InterruptedException
sends a packet, then waits for a response, blocking indefinately until the response comes over the wire, or there is an exception
Parameters:
p - a ContentPacket which is sent. The identifier field is used for matching the response, so a ContentPacket is required.
Returns:
matched Packet if successful

sendAndWait

public Packet sendAndWait(ContentPacket p,
                          int timeout)
                   throws java.lang.InterruptedException
sends a packet, then waits for a response, blocking until the response comes over the wire, there is a timeout, or there is an exception
Parameters:
p - a ContentPacket which is sent. The identifier field is used for matching the response, so a ContentPacket is required.
Returns:
matched Packet if successful, otherwise null

waitForType

public Packet waitForType(Packet p)
                   throws java.lang.InterruptedException
wait indefinately for another packet of the same type to come in.
Parameters:
p - packet which incoming packets will be compared
Returns:
matched Packet if successful

waitForType

public Packet waitForType(java.lang.Class c)
                   throws java.lang.InterruptedException
wait indefinately for a packet of type Class 'c' to come in.
Parameters:
c - class which incoming packets will be compared.
Returns:
matched Packet if successful

waitForType

public Packet waitForType(Packet p,
                          int timeout)
                   throws java.lang.InterruptedException
wait for a specified period of time for a packet of the same type as Packet p to come in
Parameters:
p - packet which incoming packets will be compared.
timeout - maximum time to wait
Returns:
matched Packet if successful, otherwise null

waitForType

public Packet waitForType(java.lang.Class c,
                          int timeout)
                   throws java.lang.InterruptedException
wait for a specified period of time for a packet of the same type as Class 'c' to come in
Parameters:
class - class which incoming packets will be compared.
timeout - maximum time to wait
Returns:
matched Packet if successful, otherwise null

sentPacket

public void sentPacket(PacketEvent p)
Monitor sentPacket listener method
Specified by:
sentPacket in interface PacketListener
Parameters:
p - corresponding PacketEvent value

receivedPacket

public void receivedPacket(PacketEvent p)
Monitor receivedPacket listener method. Will fire when a packet matching the sent packet's identifier is found
Specified by:
receivedPacket in interface PacketListener
Parameters:
p - a PacketEvent value

sendFailed

public void sendFailed(PacketEvent pe)
Description copied from interface: PacketListener
called if a packet is not successfully sent (for instance, if the connection dies while the packet is queued, or a packet is sent while disconnected).
Specified by:
sendFailed in interface PacketListener
Tags copied from interface: PacketListener
Parameters:
pe - PacketEvent for the failed send