atomashpolskiy / bt

BitTorrent library and client with DHT, magnet links, encryption and more
https://atomashpolskiy.github.io/bt/
Apache License 2.0
2.4k stars 382 forks source link

Dear, friend, i find some trouble when i use this bt gropram in my project to download a magnetUrl, pls help me. #231

Open wkder opened 1 month ago

wkder commented 1 month ago

this is my develop tool intelij idea show:

2024-07-25 22:30:00.598 INFO 21076 --- [ scheduling-1] bt.runtime.BtRuntimeBuilder : Loading standard extension module bt.peerexchange.PeerExchangeModule 2024-07-25 22:30:00.599 INFO 21076 --- [ scheduling-1] bt.runtime.BtRuntimeBuilder : Loading standard extension module bt.peer.lan.LocalServiceDiscoveryModule 2024-07-25 22:30:00.599 INFO 21076 --- [ scheduling-1] bt.runtime.BtRuntimeBuilder : Overriding auto-loaded module bt.dht.DHTModule 2024-07-25 22:30:00.599 INFO 21076 --- [ scheduling-1] bt.runtime.BtRuntimeBuilder : Auto-loading module bt.tracker.http.HttpTrackerModule with default configuration 2024-07-25 22:30:01.515 INFO 21076 --- [ scheduling-1] b.n.p.impl.PortMappingInitializer : Mapping application's acceptor port on startup. 2024-07-25 22:30:01.570 INFO 21076 --- [ming-acceptor-0] bt.net.SocketChannelConnectionAcceptor : Opening server channel for incoming connections @ /172.20.10.5:6891 2024-07-25 22:30:16.586 WARN 21076 --- [.peer-collector] bt.peer.ScheduledPeerSource : Peer collection finished with exception in peer source: bt.dht.DHTPeerSource@75b65b9e

java.util.concurrent.TimeoutException: null at java.util.concurrent.FutureTask.get(FutureTask.java:205) ~[na:1.8.0_291] at bt.peer.ScheduledPeerSource.waitForFutureAndLogExceptions(ScheduledPeerSource.java:101) [bt-core-1.10.jar:1.10] at bt.peer.ScheduledPeerSource.schedulePeerCollection(ScheduledPeerSource.java:86) [bt-core-1.10.jar:1.10] at bt.peer.ScheduledPeerSource.update(ScheduledPeerSource.java:66) [bt-core-1.10.jar:1.10] at bt.peer.PeerRegistry.queryPeerSource(PeerRegistry.java:199) [bt-core-1.10.jar:1.10] at bt.peer.PeerRegistry.lambda$collectPeersForTorrent$4(PeerRegistry.java:150) [bt-core-1.10.jar:1.10] at java.lang.Iterable.forEach(Iterable.java:75) ~[na:1.8.0_291] at bt.peer.PeerRegistry.collectPeersForTorrent(PeerRegistry.java:149) [bt-core-1.10.jar:1.10] at java.util.concurrent.ConcurrentHashMap$KeySetView.forEach(ConcurrentHashMap.java:4649) ~[na:1.8.0_291] at java.util.Collections$UnmodifiableCollection.forEach(Collections.java:1082) ~[na:1.8.0_291] at bt.peer.PeerRegistry.collectAndVisitPeers(PeerRegistry.java:114) [bt-core-1.10.jar:1.10] at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[na:1.8.0_291] at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) ~[na:1.8.0_291] at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) ~[na:1.8.0_291] at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) ~[na:1.8.0_291] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_291] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_291] at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_291]

my code is just try to Download some file by a magnet link guide by the usage

my code is as follow:

package com.xxx.autonetplay.task;

import bt.Bt; import bt.data.Storage; import bt.data.file.FileSystemStorage; import bt.dht.DHTConfig; import bt.dht.DHTModule; import bt.runtime.BtClient; import bt.runtime.Config; import com.google.inject.Module; import com.xxx.autonetplay.mapper.URLVisitMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled;

import java.nio.file.Path; import java.nio.file.Paths; import java.util.logging.Logger;

@Configuration //1.主要用于标记配置类,兼备Component的效果。 @EnableScheduling // 2.开启定时任务 public class MagnetLinkSchedule {

private Logger log = Logger.getLogger(MagnetLinkSchedule.class.getName());

private int limit = 1;
private int maxThreads = 1;
String downloadPath = "F:\\urlDownload\\";
//String downloadPath = "/home/net/urlDownload/";;
@Autowired
URLVisitMapper urlVisitMapper;

//3.添加定时任务
@Scheduled(cron = "0 0/5 * * * ? ")
private void configureTasks() throws InterruptedException {
    try {
        Config config = new Config() {
            @Override
            public int getNumOfHashingThreads() {
                return Runtime.getRuntime().availableProcessors() * 2;
            }
        };

        // enable bootstrapping from public routers
        Module dhtModule = new DHTModule(new DHTConfig() {
            @Override
            public boolean shouldUseRouterBootstrap() {
                return true;
            }
        });

        // get download directory
        Path targetDirectory = Paths.get(downloadPath, "Downloads");

        // create file system based backend for torrent data
        Storage storage = new FileSystemStorage(targetDirectory);

        // create client with a private runtime
        BtClient client = Bt.client()
                .config(config)
                .storage(storage)
                .magnet("magnet:?xt=urn:btih:af0d9aa01a9ae123a73802cfa58ccaf355eb19f1")
                .autoLoadModules()
                .module(dhtModule)
                .stopWhenDownloaded()
                .build();

        // launch
        client.startAsync().join();
     } catch (Exception e) {
        e.printStackTrace();
    }
}

}

it looks like i have some trouble to connected with the magnet-link server, but i can use this magnet link to download the movie by tool Client of BitComet with fast download speed。