001: /*
002: * Copyright 2001-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.net.tftp;
017:
018: import java.net.DatagramPacket;
019: import java.net.InetAddress;
020:
021: /***
022: * A final class derived from TFTPPacket definiing the TFTP Acknowledgement
023: * packet type.
024: * <p>
025: * Details regarding the TFTP protocol and the format of TFTP packets can
026: * be found in RFC 783. But the point of these classes is to keep you
027: * from having to worry about the internals. Additionally, only very
028: * few people should have to care about any of the TFTPPacket classes
029: * or derived classes. Almost all users should only be concerned with the
030: * {@link org.apache.commons.net.tftp.TFTPClient} class
031: * {@link org.apache.commons.net.tftp.TFTPClient#receiveFile receiveFile()}
032: * and
033: * {@link org.apache.commons.net.tftp.TFTPClient#sendFile sendFile()}
034: * methods.
035: * <p>
036: * <p>
037: * @author Daniel F. Savarese
038: * @see TFTPPacket
039: * @see TFTPPacketException
040: * @see TFTP
041: ***/
042:
043: public final class TFTPAckPacket extends TFTPPacket {
044: /*** The block number being acknowledged by the packet. ***/
045: int _blockNumber;
046:
047: /***
048: * Creates an acknowledgment packet to be sent to a host at a given port
049: * acknowledging receipt of a block.
050: * <p>
051: * @param destination The host to which the packet is going to be sent.
052: * @param port The port to which the packet is going to be sent.
053: * @param blockNumber The block number being acknowledged.
054: ***/
055: public TFTPAckPacket(InetAddress destination, int port,
056: int blockNumber) {
057: super (TFTPPacket.ACKNOWLEDGEMENT, destination, port);
058: _blockNumber = blockNumber;
059: }
060:
061: /***
062: * Creates an acknowledgement packet based from a received
063: * datagram. Assumes the datagram is at least length 4, else an
064: * ArrayIndexOutOfBoundsException may be thrown.
065: * <p>
066: * @param datagram The datagram containing the received acknowledgement.
067: * @throws TFTPPacketException If the datagram isn't a valid TFTP
068: * acknowledgement packet.
069: ***/
070: TFTPAckPacket(DatagramPacket datagram) throws TFTPPacketException {
071: super (TFTPPacket.ACKNOWLEDGEMENT, datagram.getAddress(),
072: datagram.getPort());
073: byte[] data;
074:
075: data = datagram.getData();
076:
077: if (getType() != data[1])
078: throw new TFTPPacketException(
079: "TFTP operator code does not match type.");
080:
081: _blockNumber = (((data[2] & 0xff) << 8) | (data[3] & 0xff));
082: }
083:
084: /***
085: * This is a method only available within the package for
086: * implementing efficient datagram transport by elminating buffering.
087: * It takes a datagram as an argument, and a byte buffer in which
088: * to store the raw datagram data. Inside the method, the data
089: * is set as the datagram's data and the datagram returned.
090: * <p>
091: * @param datagram The datagram to create.
092: * @param data The buffer to store the packet and to use in the datagram.
093: * @return The datagram argument.
094: ***/
095: DatagramPacket _newDatagram(DatagramPacket datagram, byte[] data) {
096: data[0] = 0;
097: data[1] = (byte) _type;
098: data[2] = (byte) ((_blockNumber & 0xffff) >> 8);
099: data[3] = (byte) (_blockNumber & 0xff);
100:
101: datagram.setAddress(_address);
102: datagram.setPort(_port);
103: datagram.setData(data);
104: datagram.setLength(4);
105:
106: return datagram;
107: }
108:
109: /***
110: * Creates a UDP datagram containing all the TFTP
111: * acknowledgement packet data in the proper format.
112: * This is a method exposed to the programmer in case he
113: * wants to implement his own TFTP client instead of using
114: * the {@link org.apache.commons.net.tftp.TFTPClient}
115: * class. Under normal circumstances, you should not have a need to call this
116: * method.
117: * <p>
118: * @return A UDP datagram containing the TFTP acknowledgement packet.
119: ***/
120: public DatagramPacket newDatagram() {
121: byte[] data;
122:
123: data = new byte[4];
124: data[0] = 0;
125: data[1] = (byte) _type;
126: data[2] = (byte) ((_blockNumber & 0xffff) >> 8);
127: data[3] = (byte) (_blockNumber & 0xff);
128:
129: return new DatagramPacket(data, data.length, _address, _port);
130: }
131:
132: /***
133: * Returns the block number of the acknowledgement.
134: * <p>
135: * @return The block number of the acknowledgement.
136: ***/
137: public int getBlockNumber() {
138: return _blockNumber;
139: }
140:
141: /*** Sets the block number of the acknowledgement. ***/
142: public void setBlockNumber(int blockNumber) {
143: _blockNumber = blockNumber;
144: }
145: }
|