jSyncManager

org.jSyncManager.API.Protocol
Class PADP

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--org.jSyncManager.API.Protocol.PADP
All Implemented Interfaces:
CMPDLPTransferInterface, PacketTimerInterface, java.lang.Runnable

public class PADP
extends java.lang.Thread
implements PacketTimerInterface, CMPDLPTransferInterface

The Packet Assembly/Disassembly Protocol Handler class. This class provides the black-box for handling PADP packets.

Version:
$Revision: 1.12 $
Author:
Brad BARCLAY <bbarclay@jsyncmanager.org>
Last modified by: $Author: yaztromo $ on $Date: 2003/07/08 20:48:31 $.

Field Summary
static long ACK_TIMEOUT
          The default packet acknowledgement timeout.
static long IP_TIMEOUT
          The default inter-packet timeout.
static int STATE_CONNECTED
          The connected state value.
static int STATE_DISCONNECTED
          The disconnected state value.
static int STATE_SUSPENDED
          The suspended state value.
 
Fields inherited from class java.lang.Thread
inheritableThreadLocals, MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY, threadLocals
 
Constructor Summary
PADP(SLP slp)
          Creates a new instance of the PADP handler.
 
Method Summary
(package private) static void ()
           
 void connect()
          Connects to the underlying protocol stack to start a communication session, if we're not already connected.
 void disconnect()
          Disconnect from the remote end, putting us into suspended mode.
 void enableDebugMode()
          Enable packet debug mode.
protected  void handleAbort(PADP_Packet pkt)
          Handle an abort packet received from the remote end.
protected  void handleACK(PADP_Packet pkt)
          Handle an ACKnowledgement packet received from the remote end.
protected  void handleData(PADP_Packet pkt)
          Handle a data packet received from the remote end.
protected  void handleNACK(PADP_Packet pkt)
          Handle a Negative ACKnowledgement packet received from the remote end.
protected  void handleTickle(PADP_Packet pkt)
          Handle a tickle packet received from the remote end.
protected  int incReadyQueueEnd()
          Increment the end pointer for the ready queue.
protected  int incReadyQueueStart()
          Increment the start pointer for the ready queue.
protected  byte incTransID()
          Increments the transaction ID.
 boolean isConnected()
          Checks to see if this protocol is currently connected.
protected  PADP_Packet parsePADPacket(GenericPacket data)
          Converts an incoming GenericData packet into a PAD_Packet instance.
protected  void processPacket(PADP_Packet pkt)
          Processes an incoming packet.
 GenericPacket readPacket()
          Reads a packet from the ready queue.
 void run()
          Runs the input queue thread.
protected  void selfDisconnect()
          An internal method for handling disconnects that are caused by inter-packet timeouts or by a retry overrun.
protected  void sendAck(PADP_Packet pkt)
          Send an ACKnowledgement packet for a received packet.
protected  void sendPacket(PADP_Packet pkt)
          Sends a packet to the underlying SLP protocol handler.
protected  void sendTickle()
          Sends a tickle packet.
 void setDebugOutputStream(java.io.PrintStream stream)
          Sets the print stream that debug packet data should be sent to, if enabled.
 void startTickles()
          Start the tickle packet sending process.
 void stopTickles()
          Stop the tickle packet sending process.
 void suspendConnection()
          Suspend the connection.
 void timerExpired(PacketTimer packettimer)
          A timer expirey handler.
 void transmitPacket(byte[] data, byte hostSocket, byte serverSocket)
          Transmit a packet to the underlying SLP protocol layer.
protected  void transmitPacket(byte[] data, byte hostSocket, byte serverSocket, boolean flag)
          Transmit a packet to the underlying SLP protocol layer.
 void transmitPacket(GenericPacket pkt)
          Transmit a packet to the underlying SLP protocol layer.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ACK_TIMEOUT

public static long ACK_TIMEOUT
The default packet acknowledgement timeout. If a sent packet isn't acknowledged via an ACK packet within this time, an attampt will be made to resend it.

IP_TIMEOUT

public static long IP_TIMEOUT
The default inter-packet timeout. If no packets are received within this time, the connection is assumed to have been lost. To prevent the other end from timing out, use tickle packets.

STATE_DISCONNECTED

public static final int STATE_DISCONNECTED
The disconnected state value.

STATE_SUSPENDED

public static final int STATE_SUSPENDED
The suspended state value.

STATE_CONNECTED

public static final int STATE_CONNECTED
The connected state value.
Constructor Detail

PADP

public PADP(SLP slp)
Creates a new instance of the PADP handler.
Parameters:
slp - the underlying SLP packet handler.
Method Detail

static void ()

connect

public void connect()
Connects to the underlying protocol stack to start a communication session, if we're not already connected. Initially, we'll be put into "suspended" mode, until a connection is initialized from the remote end.
Specified by:
connect in interface CMPDLPTransferInterface

disconnect

public void disconnect()
Disconnect from the remote end, putting us into suspended mode.
Specified by:
disconnect in interface CMPDLPTransferInterface

enableDebugMode

public void enableDebugMode()
Enable packet debug mode.

handleAbort

protected void handleAbort(PADP_Packet pkt)
Handle an abort packet received from the remote end.
Parameters:
pkt - the packet that was received.

handleACK

protected void handleACK(PADP_Packet pkt)
Handle an ACKnowledgement packet received from the remote end.
Parameters:
pkt - the packet that was received.

handleData

protected void handleData(PADP_Packet pkt)
Handle a data packet received from the remote end.
Parameters:
pkt - the packet that was received.

handleNACK

protected void handleNACK(PADP_Packet pkt)
Handle a Negative ACKnowledgement packet received from the remote end. Note that NACK packets are no longer supported by the HotSync protocol. Thus, we don't actually process this packet type.
Parameters:
pkt - the packet that was received.

handleTickle

protected void handleTickle(PADP_Packet pkt)
Handle a tickle packet received from the remote end.
Parameters:
pkt - the packet that was received.

incReadyQueueEnd

protected int incReadyQueueEnd()
                        throws PADP_QueueOverrunException
Increment the end pointer for the ready queue. The input queue in this class is treated as a circular array. When we hit the end of the array, we advance back to the beginning, if it isn't being used already. If the end pointer's incrementation would cause it to overwrite the packet pointed to by the start pointer, a PADP_QueueOverrunException will occur.
Returns:
the new value for the ready queue end pointer.
Throws:
PADP_QueueOverrunException - if the ready queue is overrun.

incReadyQueueStart

protected int incReadyQueueStart()
                          throws PADP_QueueOverrunException
Increment the start pointer for the ready queue. The I/O queue in this class is treated as a circular array. When we hit the end of the array, we advance back to the beginning, if it isn't being used already. If the start pointer's incrementation would cause it to pass the cell referenced by to by the end pointer (ie: an underrun occurs), a PADP_QueueOverrunException will occur.
Returns:
the new value for the ready queue start pointer.
Throws:
PADP_QueueOverrunException - if the ready queue is underrun.

incTransID

protected byte incTransID()
Increments the transaction ID. Note that the transaction ID wraps. If we hit 0xFE, we wrap back to 0x01.
Returns:
the new transaction ID.

isConnected

public boolean isConnected()
Checks to see if this protocol is currently connected.
Returns:
true if we're in STATE_SUSPENDED or STATE_CONNECTED, false otherwise.

parsePADPacket

protected PADP_Packet parsePADPacket(GenericPacket data)
Converts an incoming GenericData packet into a PAD_Packet instance.
Parameters:
data - the incoming GenericPacket.
Returns:
the PADP_Packet object representing the data from the GenericPacket object.

processPacket

protected void processPacket(PADP_Packet pkt)
Processes an incoming packet.
Parameters:
pkt - the incoming packet to be processed.

readPacket

public GenericPacket readPacket()
                         throws NotConnectedException
Reads a packet from the ready queue. Calls to this method will block until a packet is available in the ready queue.
Specified by:
readPacket in interface CMPDLPTransferInterface
Returns:
the GenericPacket containing the data within the next PAD Packet.
Throws:
NotConnectedException - thrown if we're not connected, or the connection is lost.

run

public void run()
Runs the input queue thread.
Overrides:
run in class java.lang.Thread
See Also:
java.lang.Thread.run()

selfDisconnect

protected void selfDisconnect()
An internal method for handling disconnects that are caused by inter-packet timeouts or by a retry overrun.

sendAck

protected void sendAck(PADP_Packet pkt)
Send an ACKnowledgement packet for a received packet.
Parameters:
pkt - the packet that we wish to acknowledge.

sendPacket

protected void sendPacket(PADP_Packet pkt)
                   throws NotConnectedException
Sends a packet to the underlying SLP protocol handler.
Parameters:
pkt - the packet object to send.
Throws:
NotConnectedException - thrown if we're not connected, or if the connection is lost during processing.

sendTickle

protected void sendTickle()
Sends a tickle packet.

setDebugOutputStream

public void setDebugOutputStream(java.io.PrintStream stream)
Sets the print stream that debug packet data should be sent to, if enabled.
Parameters:
stream - the PrintStream object to use for debug data output.

startTickles

public void startTickles()
Start the tickle packet sending process.

stopTickles

public void stopTickles()
Stop the tickle packet sending process.

suspendConnection

public void suspendConnection()
Suspend the connection. Suspending the connection causes the protocol stack to disconnect from the remote end, but allows it to remain an active listener for new incoming connections.
Specified by:
suspendConnection in interface CMPDLPTransferInterface

timerExpired

public void timerExpired(PacketTimer packettimer)
A timer expirey handler. This method is called by a PacketTimer if it expires.
Specified by:
timerExpired in interface PacketTimerInterface
Parameters:
packettimer - the timer object that expired.
See Also:
PacketTimerInterface.timerExpired(org.jSyncManager.API.Protocol.PacketTimer)

transmitPacket

public void transmitPacket(GenericPacket pkt)
                    throws NotConnectedException,
                           PADP_PacketTooBigException
Transmit a packet to the underlying SLP protocol layer. Calls to this method causes the data to be fragmented (if required) and transmitted via PADP.
Parameters:
pkt - the packet of data to be wrapped in a PADP packet (or packets) and transmitted.
Throws:
NotConnectedException - thrown if we're not connected, or the connection is lost.
PADP_PacketTooBigException - if the packet being sent exceeds the maximum packet size.

transmitPacket

public void transmitPacket(byte[] data,
                           byte hostSocket,
                           byte serverSocket)
                    throws NotConnectedException,
                           PADP_PacketTooBigException
Transmit a packet to the underlying SLP protocol layer. Calls to this method causes the data to be fragmented (if required) and transmitted via PADP.
Specified by:
transmitPacket in interface CMPDLPTransferInterface
Parameters:
data - the packet of data to be wrapped in a PADP packet (or packets) and transmitted.
hostSocket - the host socket the data is being sent from.
serverSocket - the server socket to send this data to.
Throws:
NotConnectedException - thrown if we're not connected, or the connection is lost.
PADP_PacketTooBigException - if the packet being sent exceeds the maximum packet size.

transmitPacket

protected void transmitPacket(byte[] data,
                              byte hostSocket,
                              byte serverSocket,
                              boolean flag)
                       throws NotConnectedException,
                              PADP_PacketTooBigException
Transmit a packet to the underlying SLP protocol layer. Calls to this method causes the data to be fragmented (if required) and transmitted via PADP.
Parameters:
data - the packet of data to be wrapped in a PADP packet (or packets) and transmitted.
hostSocket - the host socket the data is being sent from.
serverSocket - the server socket to send this data to.
flag - true if this is the first packet, false otherwise.
Throws:
NotConnectedException - thrown if we're not connected, or the connection is lost.
PADP_PacketTooBigException - if the packet being sent exceeds the maximum packet size.

jSyncManager

Copyright (c) 1999 - 2003 Brad BARCLAY and others. All Rights Reserved.