You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by rw...@apache.org on 2008/02/19 23:42:18 UTC

svn commit: r629259 - /commons/proper/net/trunk/src/java/org/apache/commons/net/tftp/TFTPClient.java

Author: rwinston
Date: Tue Feb 19 14:42:05 2008
New Revision: 629259

URL: http://svn.apache.org/viewvc?rev=629259&view=rev
Log:
NET-68, NET-161

Modified:
    commons/proper/net/trunk/src/java/org/apache/commons/net/tftp/TFTPClient.java

Modified: commons/proper/net/trunk/src/java/org/apache/commons/net/tftp/TFTPClient.java
URL: http://svn.apache.org/viewvc/commons/proper/net/trunk/src/java/org/apache/commons/net/tftp/TFTPClient.java?rev=629259&r1=629258&r2=629259&view=diff
==============================================================================
--- commons/proper/net/trunk/src/java/org/apache/commons/net/tftp/TFTPClient.java (original)
+++ commons/proper/net/trunk/src/java/org/apache/commons/net/tftp/TFTPClient.java Tue Feb 19 14:42:05 2008
@@ -1,10 +1,9 @@
 /*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
+ * Copyright 2001-2005 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
@@ -361,7 +360,7 @@
     public void sendFile(String filename, int mode, InputStream input,
                          InetAddress host, int port) throws IOException
     {
-        int bytesRead, timeouts, lastBlock, block, hostPort, dataLength, offset;
+        int bytesRead, timeouts, lastBlock, block, hostPort, dataLength, offset, totalThisPacket;
         TFTPPacket sent, received = null;
         TFTPErrorPacket error;
         TFTPDataPacket data =
@@ -369,10 +368,13 @@
         ;
         TFTPAckPacket ack;
 
+        boolean justStarted = true;
+        
         beginBufferedOps();
 
-        dataLength = lastBlock = hostPort = bytesRead = 0;
+        dataLength = lastBlock = hostPort = bytesRead = totalThisPacket = 0;
         block = 0;
+        boolean lastAckWait = false;
 
         if (mode == TFTP.ASCII_MODE)
             input = new ToNetASCIIInputStream(input);
@@ -383,11 +385,16 @@
 _sendPacket:
         do
         {
+            // first time: block is 0, lastBlock is 0, send a request packet.
+            // subsequent: block is integer starting at 1, send data packet.
             bufferedSend(sent);
-
+            
+            // this is trying to receive an ACK
 _receivePacket:
             while (true)
             {
+                
+
                 timeouts = 0;
                 while (timeouts < __maxTimeouts)
                 {
@@ -419,12 +426,13 @@
                         endBufferedOps();
                         throw new IOException("Bad packet: " + e.getMessage());
                     }
-                }
+                } // end of while loop over tries to receive
 
                 // The first time we receive we get the port number and
         // answering host address (for hosts with multiple IPs)
-                if (lastBlock == 0)
+                if (justStarted)
                 {
+                    justStarted = false;
                     hostPort = received.getPort();
                     data.setPort(hostPort);
                     if(!host.equals(received.getAddress()))
@@ -456,7 +464,13 @@
                         if (lastBlock == block)
                         {
                             ++block;
-                            break _receivePacket;
+                            if (lastAckWait) {
+                                
+                              break _sendPacket;
+                            }
+                            else {
+                              break _receivePacket;
+                            }
                         }
                         else
                         {
@@ -489,22 +503,33 @@
                 //break;
             }
 
+            // OK, we have just gotten ACK about the last data we sent. Make another
+            // and send it            
+
             dataLength = TFTPPacket.SEGMENT_SIZE;
             offset = 4;
+            totalThisPacket = 0;
             while (dataLength > 0 &&
                     (bytesRead = input.read(_sendBuffer, offset, dataLength)) > 0)
             {
                 offset += bytesRead;
                 dataLength -= bytesRead;
+                totalThisPacket += bytesRead;
             }
 
+            if( totalThisPacket < TFTPPacket.SEGMENT_SIZE ) {
+                /* this will be our last packet -- send, wait for ack, stop */
+                lastAckWait = true;
+            }
             data.setBlockNumber(block);
-            data.setData(_sendBuffer, 4, offset - 4);
+            data.setData(_sendBuffer, 4, totalThisPacket);
             sent = data;
         }
-        while (dataLength == 0);
-
-        bufferedSend(sent);
+        while ( totalThisPacket > 0 || lastAckWait );
+        // Note: this was looping while dataLength == 0 || lastAckWait,
+        // which was discarding the last packet if it was not full size
+        // Should send the packet. 
+        
         endBufferedOps();
     }