Search in sources :

Example 1 with TaskEntity

use of com.acgist.snail.pojo.entity.TaskEntity in project snail by acgist.

the class TorrentStreamTest method testReadWrite.

@Test
void testReadWrite() throws DownloadException, IOException {
    final var path = "E:/snail/902FFAA29EE632C8DC966ED9AB573409BA9A518E.torrent";
    final var session = TorrentContext.getInstance().newTorrentSession(path);
    final var entity = new TaskEntity();
    entity.setFile("E:/snail/tmp");
    entity.setType(Type.TORRENT);
    entity.setName("acgist");
    entity.setStatus(Status.COMPLETED);
    long pos = 0L;
    TorrentFile torrentFile = null;
    final var files = session.torrent().getInfo().files();
    for (TorrentFile file : files) {
        if (file.path().endsWith("Scans/Vol.1/Box_1.png")) {
            torrentFile = file;
            break;
        }
        pos += file.getLength();
    }
    final List<String> list = new ArrayList<>();
    entity.setDescription(DescriptionWrapper.newEncoder(list).serialize());
    session.upload(TaskSession.newInstance(entity));
    final var group = session.torrentStreamGroup();
    final long pieceLength = session.torrent().getInfo().getPieceLength();
    final String sourceFile = "E:/snail/server/Scans/Vol.1/Box_1.png";
    final var oldStream = TorrentStream.newInstance(pieceLength, sourceFile, torrentFile.getLength(), pos, true, group);
    oldStream.install();
    oldStream.verify();
    final String targetFile = "E:/snail/tmp/server/Scans/Vol.1/Box_1.png";
    final var newStream = TorrentStream.newInstance(pieceLength, targetFile, torrentFile.getLength(), pos, false, group);
    newStream.install();
    newStream.verify();
    int pieceBeginIndex = (int) (pos / pieceLength);
    int pieceEndIndex = (int) ((pos + torrentFile.getLength()) / pieceLength) + 1;
    final BitSet peerPieces = new BitSet();
    peerPieces.set(pieceBeginIndex, pieceEndIndex);
    this.cost();
    for (int index = pieceBeginIndex; index < pieceEndIndex; index++) {
        var torrentPiece = newStream.pick(0, peerPieces, peerPieces);
        if (torrentPiece != null) {
            torrentPiece.write(torrentPiece.getBegin(), oldStream.read(index));
            newStream.write(torrentPiece);
        }
    }
    this.costed();
    oldStream.release();
    newStream.release();
    final var sourceHash = FileUtils.sha1(sourceFile);
    final var targetHash = FileUtils.sha1(targetFile);
    assertEquals(targetHash, sourceHash);
}
Also used : TaskEntity(com.acgist.snail.pojo.entity.TaskEntity) ArrayList(java.util.ArrayList) BitSet(java.util.BitSet) TorrentFile(com.acgist.snail.pojo.bean.TorrentFile) Test(org.junit.jupiter.api.Test)

Example 2 with TaskEntity

use of com.acgist.snail.pojo.entity.TaskEntity in project snail by acgist.

the class PeerClientTest method testMagnet.

@Test
void testMagnet() throws DownloadException {
    final var hash = "1f4f28a6df2ea7899328cbef1dfaaeec9920cdb3";
    final var torrentSession = TorrentSession.newInstance(InfoHash.newInstance(hash), null);
    final var entity = new TaskEntity();
    entity.setFile("E:/snail/tmp/torrent/");
    entity.setType(Type.TORRENT);
    entity.setUrl(hash);
    torrentSession.magnet(TaskSession.newInstance(entity));
    final String host = "127.0.0.1";
    // final Integer port = 15000; // 迅雷测试端口
    // final Integer port = 18888; // 蜗牛测试端口
    // FDM测试端口
    final Integer port = 49160;
    final var peerSession = PeerSession.newInstance(new StatisticsSession(), host, port);
    final var downloader = PeerDownloader.newInstance(peerSession, torrentSession);
    downloader.handshake();
    ThreadUtils.sleep(100000);
    assertNotNull(downloader);
}
Also used : TaskEntity(com.acgist.snail.pojo.entity.TaskEntity) StatisticsSession(com.acgist.snail.pojo.session.StatisticsSession) Test(org.junit.jupiter.api.Test)

Example 3 with TaskEntity

use of com.acgist.snail.pojo.entity.TaskEntity in project snail by acgist.

the class PropertyDescriptorTest method testCosted.

@Test
void testCosted() {
    final TaskEntity task = new TaskEntity();
    final var descriptor = PropertyDescriptor.newInstance(task);
    this.costed(100000, () -> descriptor.get("id"));
    this.costed(100000, () -> descriptor.set("id", "1234"));
    assertNotNull(task);
    this.costed(1000, () -> {
        final var costed = PropertyDescriptor.newInstance(new TaskEntity());
        costed.get("id");
        costed.set("id", "1234");
    // System.gc();
    });
    System.gc();
}
Also used : TaskEntity(com.acgist.snail.pojo.entity.TaskEntity) Test(org.junit.jupiter.api.Test)

Example 4 with TaskEntity

use of com.acgist.snail.pojo.entity.TaskEntity in project snail by acgist.

the class PropertyDescriptorTest method testWeakMap.

@Test
void testWeakMap() {
    TaskEntity task = new TaskEntity();
    final Map<TaskEntity, Map<Integer, Integer>> taskMap = new WeakHashMap<>();
    taskMap.put(task, Map.of(1, 2));
    System.out.println(taskMap);
    task = null;
    System.gc();
    System.out.println(taskMap);
    Class<TaskEntity> clazz = TaskEntity.class;
    final Map<Class<?>, Map<Integer, Integer>> clazzMap = new WeakHashMap<>();
    clazzMap.put(clazz, Map.of(1, 2));
    System.out.println(clazzMap);
    clazz = null;
    System.gc();
    System.out.println(clazzMap);
}
Also used : TaskEntity(com.acgist.snail.pojo.entity.TaskEntity) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap) WeakHashMap(java.util.WeakHashMap) Test(org.junit.jupiter.api.Test)

Example 5 with TaskEntity

use of com.acgist.snail.pojo.entity.TaskEntity in project snail by acgist.

the class PropertyDescriptorTest method testGetter.

@Test
void testGetter() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    final String id = "1234";
    final TaskEntity task = new TaskEntity();
    task.setId(id);
    assertEquals(id, PropertyDescriptor.newInstance(task).get("id"));
}
Also used : TaskEntity(com.acgist.snail.pojo.entity.TaskEntity) Test(org.junit.jupiter.api.Test)

Aggregations

TaskEntity (com.acgist.snail.pojo.entity.TaskEntity)15 Test (org.junit.jupiter.api.Test)14 ArrayList (java.util.ArrayList)3 BitSet (java.util.BitSet)2 Date (java.util.Date)2 Order (org.junit.jupiter.api.Order)2 NetException (com.acgist.snail.context.exception.NetException)1 ITaskSession (com.acgist.snail.pojo.ITaskSession)1 M3u8 (com.acgist.snail.pojo.bean.M3u8)1 TorrentFile (com.acgist.snail.pojo.bean.TorrentFile)1 TorrentPiece (com.acgist.snail.pojo.bean.TorrentPiece)1 StatisticsSession (com.acgist.snail.pojo.session.StatisticsSession)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 WeakHashMap (java.util.WeakHashMap)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1