mpetazzoni / ttorrent

BitTorrent Java library with tracker and download client
http://mpetazzoni.github.com/ttorrent/
Apache License 2.0
1.38k stars 502 forks source link

TorrentListenerWrapper downloadComplete not called if file already exists #228

Closed asadsm closed 5 years ago

asadsm commented 5 years ago

The following code fails to complete if the file already exists.

                SimpleClient client = new SimpleClient();

                System.out.println("Started download...");
                client.downloadTorrent(torrentFile.getAbsolutePath(), destFolder.getAbsolutePath(), InetAddress.getLocalHost());
                System.out.println("Finished download...");

Tried to debug it and it seems like the downloadComplete isn't called if the file already exists. It is called otherwise.

Following logs for when it works:

[0928 14:37:34,123 main]   INFO - rent.tracker.Tracker - Starting BitTorrent tracker on http://asad.test.com:6969/announce...
[0928 14:37:34,215 main]   INFO - rent.tracker.Tracker - Started torrent tracker on /0.0.0.0:6969
Tracker Started...
/home/asmalik/test.csv /home/tmp/asmalik/
[0928 14:37:37,897 main]  DEBUG - ommon.TorrentCreator - Analyzing local data for test.csv with 24 threads...
[0928 14:37:37,918 main]  DEBUG - ommon.TorrentCreator - Hashed 1 file(s) (3679 bytes) in 1 pieces (1 expected) in 20.2ms.
Created new torrent
[0928 14:37:37,945 main]   INFO - rent.tracker.Tracker - Registered new torrent with hash 034ADE4DA7A3120925EE0DF07FD8952CF1146C4C.
Hex Info is 034ADE4DA7A3120925EE0DF07FD8952CF1146C4C
Added torrent to tracker
[0928 14:37:37,963 main]  DEBUG - ileCollectionStorage - Initialized torrent byte storage on 1 file(s) (3679 total byte(s)).
[0928 14:37:37,971 main]  DEBUG - nounce.TrackerClient - Announcing  COMPLETED to tracker with 0U/0D/0L bytes...
[0928 14:37:37,971 main]  DEBUG - CommunicationManager - Added torrent LoadedTorrentImpl{piece storage='com.turn.ttorrent.client.storage.PieceStorageImpl@567d299b', metadata provider='com.turn.ttorrent.client.FileMetadataProvider@2eafffde'} (034ADE4DA7A3120925EE0DF07FD8952CF1146C4C)
[0928 14:37:38,001 main]   INFO - CommunicationManager - BitTorrent client [..336535] started and listening at 0.0.0.0:6881...
Started source file/folder seeder
[0928 14:37:38,002 torrent tracker announce thread]   INFO - nt.announce.Announce - Starting announce loop...
[0928 14:37:38,002 torrent tracker announce thread]  DEBUG - nt.announce.Announce - Starting announce for 1 torrents
[0928 14:37:38,002 torrent tracker announce thread]  DEBUG - nt.announce.Announce - Started multi announce. Event NONE, torrents [com.turn.ttorrent.client.AnnounceableInformationImpl@79347a98]
Started download...
[0928 14:37:38,003 main]  DEBUG - irstAvailableChannel - Could not bind to port 6881, trying next port...
[0928 14:37:38,003 main]   INFO - CommunicationManager - BitTorrent client [..373361] started and listening at 0.0.0.0:6882...
[0928 14:37:38,004 torrent tracker announce thread]   INFO - nt.announce.Announce - Starting announce loop...
[0928 14:37:38,004 torrent tracker announce thread]  DEBUG - nt.announce.Announce - Starting announce for 0 torrents
[0928 14:37:38,004 torrent tracker announce thread]  DEBUG - nt.announce.Announce - Started multi announce. Event NONE, torrents []
[0928 14:37:38,007 main]  DEBUG - ileCollectionStorage - Initialized torrent byte storage on 1 file(s) (3679 total byte(s)).
[0928 14:37:38,007 main]  DEBUG - .storage.FileStorage - Downloading new file to /home/tmp/asmalik/test.csv.part...
[0928 14:37:38,008 main]  DEBUG - .storage.FileStorage - Opened byte storage file at /home/tmp/asmalik/test.csv.part (0+3679 byte(s)).
[0928 14:37:38,009 main]  DEBUG - .storage.FileStorage - Closing file channel to test.csv.part. Channel open: true
[0928 14:37:38,023 main]  DEBUG - nounce.TrackerClient - Announcing  STARTED to tracker with 0U/0D/3679L bytes...
[0928 14:37:38,072 main]  DEBUG - CommunicationManager - Added torrent LoadedTorrentImpl{piece storage='com.turn.ttorrent.client.storage.PieceStorageImpl@2fc14f68', metadata provider='com.turn.ttorrent.client.FileMetadataProvider@591f989e'} (034ADE4DA7A3120925EE0DF07FD8952CF1146C4C)
[0928 14:37:53,005 torrent tracker announce thread]  DEBUG - nt.announce.Announce - Starting announce for 1 torrents
[0928 14:37:53,005 torrent tracker announce thread]  DEBUG - nt.announce.Announce - Started multi announce. Event NONE, torrents [com.turn.ttorrent.client.AnnounceableInformationImpl@32f06a2d]
[0928 14:37:53,010 torrent tracker announce thread]   INFO - CommunicationManager - Got 1 peer(s) ([Peer /10.88.211.37:6881 for torrent null]) for 034ADE4DA7A3120925EE0DF07FD8952CF1146C4C in tracker response
[0928 14:37:53,011 torrent tracker announce thread]  DEBUG - CommunicationManager - trying to connect to the peer Peer /10.88.211.37:6881 for torrent null
[0928 14:37:53,031 Torrent channels manager thread]  DEBUG - ork.ConnectionWorker - try connect to peer. Connect task is ConnectTask{myHost='10.88.211.37', myPort=6881}
[0928 14:37:53,052 Torrent channels manager thread]  DEBUG - nectableKeyProcessor - setup new TCP connection with java.nio.channels.SocketChannel[connected local=/10.88.211.37:60637 remote=/10.88.211.37:6881]
[0928 14:37:53,068 Torrent channels manager thread]  DEBUG - rk.HandshakeReceiver - setup new connection with Peer /10.88.211.37:60637 for torrent 034ADE4DA7A3120925EE0DF07FD8952CF1146C4C
[0928 14:37:53,069 pool-1-thread-2]  DEBUG - client.SharedTorrent - Analyzing local data for test.csv with 24 threads...
[0928 14:37:53,070 Torrent channels manager thread]  DEBUG - rk.HandshakeReceiver - setup new connection with Peer /10.88.211.37:6881 for torrent 034ADE4DA7A3120925EE0DF07FD8952CF1146C4C
[0928 14:37:53,071 pool-3-thread-2]  DEBUG - client.SharedTorrent - Analyzing local data for test.csv with 24 threads...
[0928 14:37:53,075 pool-3-thread-3 handle message for torrent 034ADE4DA7A3120925EE0DF07FD8952CF1146C4C peer: /10.88.211.37:6881]  DEBUG - client.SharedTorrent - Peer Peer /10.88.211.37:6881 for torrent 034ADE4DA7A3120925EE0DF07FD8952CF1146C4C contributes 1 piece(s), total pieces count: 1.
[0928 14:37:53,081 pool-1-thread-4 handle message for torrent 034ADE4DA7A3120925EE0DF07FD8952CF1146C4C peer: /10.88.211.37:60637]  DEBUG - .storage.FileStorage - Opened byte storage file at /home/asmalik/test.csv (0+3679 byte(s)).
[0928 14:37:53,083 pool-3-thread-5 handle message for torrent 034ADE4DA7A3120925EE0DF07FD8952CF1146C4C peer: /10.88.211.37:6881]   INFO - client.SharedTorrent - all pieces are received for torrent SharedTorrent{[test.csv]}. Validating...
[0928 14:37:53,084 pool-4-thread-1]  DEBUG - .storage.FileStorage - Partial download found at /home/tmp/asmalik/test.csv.part. Continuing...
[0928 14:37:53,084 pool-4-thread-1]  DEBUG - .storage.FileStorage - Opened byte storage file at /home/tmp/asmalik/test.csv.part (0+3679 byte(s)).
[0928 14:37:53,084 pool-4-thread-1]  DEBUG - .storage.FileStorage - Closing file channel to test.csv.part (download complete).
[0928 14:37:53,093 pool-4-thread-1]  DEBUG - .storage.FileStorage - Moved torrent data from test.csv.part to test.csv.
[0928 14:37:53,094 pool-4-thread-1]  DEBUG - .storage.FileStorage - Opened byte storage file at /home/tmp/asmalik/test.csv (0+3679 byte(s)).
[0928 14:37:53,094 pool-4-thread-1]  DEBUG - CommunicationManager - Download of test.csv complete.
Finished download...
[0928 14:37:53,094 pool-4-thread-1]  DEBUG - nounce.TrackerClient - Announcing  COMPLETED to tracker with 0U/3679D/0L bytes...
[0928 14:37:53,099 Torrent channels manager thread]  DEBUG - work.WorkingReceiver - channel java.nio.channels.SocketChannel[connected local=/10.88.211.37:60637 remote=/10.88.211.37:6881] is closed by other peer
[0928 14:37:53,100 pool-1-thread-5 handle message for torrent 034ADE4DA7A3120925EE0DF07FD8952CF1146C4C peer: /10.88.211.37:60637]  DEBUG - client.SharedTorrent - Peer Peer /10.88.211.37:60637 for torrent 034ADE4DA7A3120925EE0DF07FD8952CF1146C4C went away with 0 piece(s) [1/1].
[0928 14:37:53,100 pool-1-thread-5 handle message for torrent 034ADE4DA7A3120925EE0DF07FD8952CF1146C4C peer: /10.88.211.37:60637]  DEBUG - .storage.FileStorage - Closing file channel to test.csv. Channel open: true
[0928 14:37:53,101 Torrent channels manager thread]  DEBUG - client.SharedTorrent - Peer Peer /10.88.211.37:6881 for torrent 034ADE4DA7A3120925EE0DF07FD8952CF1146C4C went away with 1 piece(s) [1/1].
[0928 14:37:53,101 Torrent channels manager thread]  DEBUG - .storage.FileStorage - Closing file channel to test.csv. Channel open: true

Following logs for when it doesn't:

[0928 14:38:38,882 main]   INFO - rent.tracker.Tracker - Starting BitTorrent tracker on http://asad.test.com:6969/announce...
[0928 14:38:38,985 main]   INFO - rent.tracker.Tracker - Started torrent tracker on /0.0.0.0:6969
Tracker Started...
/home/asmalik/test.csv /home/tmp/asmalik/
[0928 14:38:40,105 main]  DEBUG - ommon.TorrentCreator - Analyzing local data for test.csv with 24 threads...
[0928 14:38:40,128 main]  DEBUG - ommon.TorrentCreator - Hashed 1 file(s) (3679 bytes) in 1 pieces (1 expected) in 21.9ms.
Created new torrent
[0928 14:38:40,156 main]   INFO - rent.tracker.Tracker - Registered new torrent with hash 034ADE4DA7A3120925EE0DF07FD8952CF1146C4C.
Hex Info is 034ADE4DA7A3120925EE0DF07FD8952CF1146C4C
Added torrent to tracker
[0928 14:38:40,173 main]  DEBUG - ileCollectionStorage - Initialized torrent byte storage on 1 file(s) (3679 total byte(s)).
[0928 14:38:40,181 main]  DEBUG - nounce.TrackerClient - Announcing  COMPLETED to tracker with 0U/0D/0L bytes...
[0928 14:38:40,181 main]  DEBUG - CommunicationManager - Added torrent LoadedTorrentImpl{piece storage='com.turn.ttorrent.client.storage.PieceStorageImpl@567d299b', metadata provider='com.turn.ttorrent.client.FileMetadataProvider@2eafffde'} (034ADE4DA7A3120925EE0DF07FD8952CF1146C4C)
[0928 14:38:40,213 main]   INFO - CommunicationManager - BitTorrent client [..633438] started and listening at 0.0.0.0:6881...
Started source file/folder seeder
[0928 14:38:40,214 torrent tracker announce thread]   INFO - nt.announce.Announce - Starting announce loop...
[0928 14:38:40,214 torrent tracker announce thread]  DEBUG - nt.announce.Announce - Starting announce for 1 torrents
[0928 14:38:40,214 torrent tracker announce thread]  DEBUG - nt.announce.Announce - Started multi announce. Event NONE, torrents [com.turn.ttorrent.client.AnnounceableInformationImpl@79347a98]
Started download...
[0928 14:38:40,215 main]  DEBUG - irstAvailableChannel - Could not bind to port 6881, trying next port...
[0928 14:38:40,215 main]   INFO - CommunicationManager - BitTorrent client [..653561] started and listening at 0.0.0.0:6882...
[0928 14:38:40,216 torrent tracker announce thread]   INFO - nt.announce.Announce - Starting announce loop...
[0928 14:38:40,216 torrent tracker announce thread]  DEBUG - nt.announce.Announce - Starting announce for 0 torrents
[0928 14:38:40,216 torrent tracker announce thread]  DEBUG - nt.announce.Announce - Started multi announce. Event NONE, torrents []
[0928 14:38:40,218 main]  DEBUG - ileCollectionStorage - Initialized torrent byte storage on 1 file(s) (3679 total byte(s)).
[0928 14:38:40,219 main]  DEBUG - .storage.FileStorage - Using existing file /home/tmp/asmalik/test.csv.
[0928 14:38:40,219 main]  DEBUG - .storage.FileStorage - Opened byte storage file at /home/tmp/asmalik/test.csv (0+3679 byte(s)).
[0928 14:38:40,221 main]  DEBUG - .storage.FileStorage - Closing file channel to test.csv. Channel open: true
[0928 14:38:40,231 main]  DEBUG - nounce.TrackerClient - Announcing  COMPLETED to tracker with 0U/0D/0L bytes...
[0928 14:38:40,279 main]  DEBUG - CommunicationManager - Added torrent LoadedTorrentImpl{piece storage='com.turn.ttorrent.client.storage.PieceStorageImpl@2fc14f68', metadata provider='com.turn.ttorrent.client.FileMetadataProvider@591f989e'} (034ADE4DA7A3120925EE0DF07FD8952CF1146C4C)
[0928 14:38:55,216 torrent tracker announce thread]  DEBUG - nt.announce.Announce - Starting announce for 1 torrents
[0928 14:38:55,216 torrent tracker announce thread]  DEBUG - nt.announce.Announce - Started multi announce. Event NONE, torrents [com.turn.ttorrent.client.AnnounceableInformationImpl@65250955]
Dead-off commented 5 years ago

all right, downloadComplete listener is invoked when last piece is received and validated. I think we need listener like validationComplete(int validpieces, int totalpieces) and if this method was invoked with validpieces == totalpieces thenclient.downloadTorrent() method must finished. I will try to fix it

Dead-off commented 5 years ago

fixed