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);
}
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);
}
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();
}
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);
}
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"));
}
Aggregations