org.jSyncManager.API.Protocol

Class PADP

Implemented Interfaces:
CMPDLPTransferInterface, PacketTimerInterface

public class PADP
extends Thread
implements PacketTimerInterface, CMPDLPTransferInterface

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

Field Summary

static long
ACK_TIMEOUT
The default packet acknowledgement timeout.
static int
DEFAULT_QUEUE_LENGTH
The default queue length.
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.

Constructor Summary

PADP(SLP slp)
Creates a new instance of the PADP handler.

Method Summary

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
handleACK(PADP_Packet pkt)
Handle an ACKnowledgement packet received from the remote end.
protected void
handleAbort(PADP_Packet pkt)
Handle an abort 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 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(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(data[] , byte hostSocket, byte serverSocket)
Transmit a packet to the underlying SLP protocol layer.
protected void
transmitPacket(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.
void
useLongPackets(boolean flag)
Sets the use of long packets.

Field Details

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.

DEFAULT_QUEUE_LENGTH

public static final int DEFAULT_QUEUE_LENGTH
The default queue length.
Field Value:
4

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_CONNECTED

public static final int STATE_CONNECTED
The connected state value.
Field Value:
2

STATE_DISCONNECTED

public static final int STATE_DISCONNECTED
The disconnected state value.
Field Value:
0

STATE_SUSPENDED

public static final int STATE_SUSPENDED
The suspended state value.
Field Value:
1

Constructor Details

PADP

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

Method Details

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.

handleACK

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

handleAbort

protected void handleAbort(PADP_Packet pkt)
Handle an abort 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.

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.
Specified by:
isConnected in interface CMPDLPTransferInterface
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.

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(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.

transmitPacket

public void transmitPacket(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.
Parameters:
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(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:
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.

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.

useLongPackets

public void useLongPackets(boolean flag)
Sets the use of long packets.
Specified by:
useLongPackets in interface CMPDLPTransferInterface
Parameters:
flag - true if we should use long packet support, false otherwise.