Example usage for org.apache.commons.net.tftp TFTPWriteRequestPacket getPort

List of usage examples for org.apache.commons.net.tftp TFTPWriteRequestPacket getPort

Introduction

In this page you can find the example usage for org.apache.commons.net.tftp TFTPWriteRequestPacket getPort.

Prototype

public final int getPort() 

Source Link

Document

Returns the port where the packet is going to be sent or where it came from.

Usage

From source file:org.mobicents.slee.resource.tftp.TFTPTransfer.java

private void handleWrite(TFTPWriteRequestPacket twrp) throws Exception {
    long totalBytesReceived = 0;
    try {//from  w  w w.j a v  a  2 s.  c  om
        if (mode_ == ServerMode.GET_ONLY) {
            transferTftp_.bufferedSend(new TFTPErrorPacket(twrp.getAddress(), twrp.getPort(),
                    TFTPErrorPacket.ILLEGAL_OPERATION, "Write not allowed by server."));
            return;
        }
        if (trc.isFineEnabled())
            trc.fine("WRITE request received, get cracking");
        fireEvent(twrp);
        suspend(0);

        if (twrp.getMode() == TFTP.NETASCII_MODE) {
            os_ = new FromNetASCIIOutputStream(os_);
        }
        TFTPAckPacket lastSentAck = new TFTPAckPacket(twrp.getAddress(), twrp.getPort(), 0);
        transferTftp_.bufferedSend(lastSentAck);

        int lastBlock = 0;
        while (true) {
            // get the response - ensure it is from the right place.
            TFTPPacket dataPacket = null;
            int timeoutCount = 0;

            while (!shutdownTransfer
                    && (dataPacket == null || !dataPacket.getAddress().equals(twrp.getAddress())
                            || dataPacket.getPort() != twrp.getPort())) {
                // listen for an answer.
                if (dataPacket != null) {
                    // The data that we got didn't come from the
                    // expected source, fire back an error, and continue
                    // listening.
                    trc.warning("TFTP Server ignoring message from unexpected source.");
                    transferTftp_.bufferedSend(new TFTPErrorPacket(dataPacket.getAddress(),
                            dataPacket.getPort(), TFTPErrorPacket.UNKNOWN_TID, "Unexpected Host or Port"));
                }
                try {
                    dataPacket = transferTftp_.bufferedReceive();
                } catch (SocketTimeoutException e) {
                    if (timeoutCount >= maxTimeoutRetries_) {
                        throw e;
                    }
                    // It didn't get our ack. Resend it.
                    transferTftp_.bufferedSend(lastSentAck);
                    timeoutCount++;
                    continue;
                }
            }
            if (dataPacket != null && dataPacket instanceof TFTPWriteRequestPacket) {
                // it must have missed our initial ack. Send another.
                lastSentAck = new TFTPAckPacket(twrp.getAddress(), twrp.getPort(), 0);
                transferTftp_.bufferedSend(lastSentAck);
            } else if (dataPacket == null || !(dataPacket instanceof TFTPDataPacket)) {
                if (!shutdownTransfer) {
                    trc.severe("Unexpected response from tftp client during transfer (" + dataPacket
                            + ").  Transfer aborted.");
                }
                break;
            } else {
                int block = ((TFTPDataPacket) dataPacket).getBlockNumber();
                byte[] data = ((TFTPDataPacket) dataPacket).getData();
                int dataLength = ((TFTPDataPacket) dataPacket).getDataLength();
                int dataOffset = ((TFTPDataPacket) dataPacket).getDataOffset();

                if (block > lastBlock || (lastBlock == 65535 && block == 0)) {
                    // it might resend a data block if it missed our ack
                    // - don't rewrite the block.
                    os_.write(data, dataOffset, dataLength);
                    lastBlock = block;
                    fireEvent(dataPacket);
                    totalBytesReceived += dataLength;
                }
                lastSentAck = new TFTPAckPacket(twrp.getAddress(), twrp.getPort(), block);
                transferTftp_.bufferedSend(lastSentAck);
                if (dataLength < TFTPDataPacket.MAX_DATA_LENGTH) {
                    // end of stream signal - The tranfer is complete.
                    if (trc.isFineEnabled())
                        trc.fine("Bytes received = " + totalBytesReceived);
                    os_.close();

                    // But my ack may be lost - so listen to see if I
                    // need to resend the ack.
                    for (int i = 0; i < maxTimeoutRetries_; i++) {
                        try {
                            dataPacket = transferTftp_.bufferedReceive();
                        } catch (SocketTimeoutException e) {
                            // this is the expected route - the client
                            // shouldn't be sending any more packets.
                            break;
                        }
                        if (dataPacket != null && (!dataPacket.getAddress().equals(twrp.getAddress())
                                || dataPacket.getPort() != twrp.getPort())) {
                            // make sure it was from the right client...
                            transferTftp_.bufferedSend(
                                    new TFTPErrorPacket(dataPacket.getAddress(), dataPacket.getPort(),
                                            TFTPErrorPacket.UNKNOWN_TID, "Unexpected Host or Port"));
                        } else {
                            // This means they sent us the last
                            // datapacket again, must have missed our
                            // ack. resend it.
                            transferTftp_.bufferedSend(lastSentAck);
                        }
                    }
                    // all done.
                    break;
                }
            }
        }
    } finally {
        if (sbbIs != null)
            sbbIs.close();
        if (os_ != null)
            os_.close();
    }
}