NetworkFile: change waitForFilePiece behaviour to only wait once - consumers must handle looping behaviour themselves.

This commit is contained in:
Nekojimi 2025-10-02 00:09:55 +01:00
parent 1131d47dc0
commit 6685e6fd7e

View file

@ -132,7 +132,9 @@ public class NetworkFile extends NetworkFSNode
/** /**
* Retreives the local file used for storing file data, or if it doesn't exist, creates it. * Retreives the local file used for storing file data, or if it doesn't exist, creates it.
*
* @return a File object for the (potentially new) file storage. * @return a File object for the (potentially new) file storage.
*
* @throws IOException if the file could not be created due to an IO error. * @throws IOException if the file could not be created due to an IO error.
*/ */
public File getOrCreateLocalFile() throws IOException public File getOrCreateLocalFile() throws IOException
@ -146,7 +148,9 @@ public class NetworkFile extends NetworkFSNode
{ {
localFile = new File(tempDirectory, getName()); localFile = new File(tempDirectory, getName());
// localFile = File.createTempFile("FriendCloud", getNetworkPath()); // localFile = File.createTempFile("FriendCloud", getNetworkPath());
localFile.createNewFile(); boolean created = localFile.createNewFile();
if (created)
System.out.println("NetworkFile: Created local file " + localFile.getAbsolutePath() + " for " + getObjectID());
} }
return localFile; return localFile;
} }
@ -237,21 +241,19 @@ public class NetworkFile extends NetworkFSNode
/** /**
* Waits for a certain piece of the file to become available, or for a timeout to expire. * Waits for a certain piece of the file to become available, or for a timeout to expire.
*
* @param pieceIdx the index of the piece to wait for. Note: <=0 means "don't wait at all", not "wait forever". * @param pieceIdx the index of the piece to wait for. Note: <=0 means "don't wait at all", not "wait forever".
* @param timeoutMs the amount of time, in milliseconds, to wait. * @param timeoutMs the amount of time, in milliseconds, to wait.
* @return true if the piece is available (haspiece(pieceIdx) will return true), false if the timeout was reached. * @throws InterruptedException if the waiting was interrupted.
*
* @return true if the piece is available (hasPiece(pieceIdx) will return true), false if the timeout was reached.
*/ */
public boolean waitForFilePiece(int pieceIdx, long timeoutMs) public boolean waitForFilePiece(int pieceIdx, long timeoutMs) throws InterruptedException
{ {
synchronized (pieces) synchronized (pieces)
{ {
if (hasPiece(pieceIdx)) if (hasPiece(pieceIdx))
return true; return true;
while (timeoutMs > 0)
{
long startTime = System.currentTimeMillis();
try
{
System.out.println("NetworkFile: waiting " + timeoutMs + "ms for piece " + pieceIdx + " of file " + name); System.out.println("NetworkFile: waiting " + timeoutMs + "ms for piece " + pieceIdx + " of file " + name);
pieces.wait(timeoutMs); pieces.wait(timeoutMs);
if (hasPiece(pieceIdx)) if (hasPiece(pieceIdx))
@ -259,13 +261,6 @@ public class NetworkFile extends NetworkFSNode
System.out.println("NetworkFile: got piece we were waiting for."); System.out.println("NetworkFile: got piece we were waiting for.");
return true; return true;
} }
} catch (InterruptedException ignored)
{
}
long endTime = System.currentTimeMillis();
long timeWaited = (endTime - startTime);
timeoutMs -= timeWaited;
}
} }
System.err.println("Timed out waiting for piece!"); System.err.println("Timed out waiting for piece!");
return false; return false;
@ -286,11 +281,17 @@ public class NetworkFile extends NetworkFSNode
public enum StorageType public enum StorageType
{ {
/** The file will be stored as a complete file in the storage directory under it's own name and file path. */ /**
* The file will be stored as a complete file in the storage directory under it's own name and file path.
*/
COMPLETE, COMPLETE,
/** The file will be stored as a complete file in a temporary directory. It will be deleted when the computer restarts. */ /**
* The file will be stored as a complete file in a temporary directory. It will be deleted when the computer restarts.
*/
TEMPORARY_COMPLETE, TEMPORARY_COMPLETE,
/** Each piece of the file will be stored permanently in an individual piece file, in the pieces subdirectory. */ /**
* Each piece of the file will be stored permanently in an individual piece file, in the pieces subdirectory.
*/
PIECES, PIECES,
} }
} }