org.jabber.jabberbeans.util
Class SyncPacketListener

java.lang.Object
  |
  +--org.jabber.jabberbeans.util.SyncPacketListener
All Implemented Interfaces:
PacketListener, java.io.Serializable

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.

See Also:
Serialized Form

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, toString, wait, wait, wait
 

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
java.lang.InterruptedException

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
java.lang.InterruptedException

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
java.lang.InterruptedException

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
java.lang.InterruptedException

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
java.lang.InterruptedException

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:
timeout - maximum time to wait
Returns:
matched Packet if successful, otherwise null
java.lang.InterruptedException

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
Parameters:
pe - PacketEvent for the failed send