Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement application state persistence #35

Merged
merged 33 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
bb8ffd6
add Sha1Hash splitHashes method
Ashuh May 12, 2024
1996ca1
Implement RxObservableCollectionBase toString
Ashuh May 12, 2024
70a37bc
Ditch java 9 modules
Ashuh May 12, 2024
714a881
Add hibernate ORM & h2 dependencies
Ashuh May 12, 2024
5b7451b
Add hibernate.properties
Ashuh May 12, 2024
d97d789
Remove jul.properties
Ashuh May 12, 2024
64cb862
Implement database models & dao
Ashuh May 12, 2024
eb11b69
Persist torrents to database
Ashuh May 12, 2024
b27d861
Override equals & hashcode in HttpTracker
Ashuh May 12, 2024
c069c3c
Override equals & hashcode in torrent entity models
Ashuh May 23, 2024
9bf76e1
Add argument validity check
Ashuh May 23, 2024
0f0f9de
Fix bug in TorrentProgress.createExisting
Ashuh May 23, 2024
7a11d57
Remove FileProgressComponent
Ashuh May 23, 2024
832718c
Add tests
Ashuh May 23, 2024
11c5669
Move bencoded models to new package
Ashuh May 24, 2024
59da535
Refactor repository classes
Ashuh May 25, 2024
f8ecdd5
Rename method
Ashuh May 25, 2024
55eca3c
Modify TorrentMetadata to model tiers of trackers
Ashuh May 25, 2024
8a7c3f5
Fix typo
Ashuh May 25, 2024
a38b69f
Remove FileMetadata end member since it is derivable
Ashuh May 25, 2024
d7df6c8
Change FileMetadata constructor argument order
Ashuh May 25, 2024
7954716
Fix bug in FileProgress.createExisting
Ashuh May 26, 2024
b5e8f33
Fix verifiedPieces column length exceeded
Ashuh May 26, 2024
037962c
Store piece hashes in db as concatenated byte array instead of list o…
Ashuh Jun 1, 2024
b7b50c8
Store piece to received blocks map as serialized bytes in db
Ashuh Jun 2, 2024
bc33484
Format logback.xml
Ashuh Jun 2, 2024
fb26265
Parallelize file verification
Ashuh Jun 2, 2024
a345050
Update hibernate.properties
Ashuh Jun 2, 2024
b8ec2a0
Revert accidental change
Ashuh Jun 2, 2024
8477136
Fix DataStatusBar not updating on first flipped bit
Ashuh Jun 2, 2024
80021bf
Change db file nmae
Ashuh Jun 2, 2024
9e62a50
Update TorrentEntityTest
Ashuh Jun 2, 2024
6c4e7e3
Update TorrentProgressComponentTest
Ashuh Jun 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
id 'application'
id 'checkstyle'
id 'org.openjfx.javafxplugin' version '0.0.14'
id 'org.gradlex.extra-java-module-info' version '1.4.1'
id "org.hibernate.orm" version "6.5.0.Final"
}

group 'jtorrent'
Expand All @@ -13,13 +13,7 @@ sourceCompatibility = '17'
targetCompatibility = '17'

application {
mainModule = 'jtorrent'
mainClass = 'jtorrent.Main'
// Workaround for the following error:
// java.lang.IllegalAccessError: class ch.qos.logback.core.boolex.JaninoEventEvaluatorBase
// (in module ch.qos.logback.core) cannot access class org.codehaus.janino.ScriptEvaluator
// (in module org.codehaus.janino) because module ch.qos.logback.core does not read module org.codehaus.janino
applicationDefaultJvmArgs = ['--add-reads', 'ch.qos.logback.core=org.codehaus.janino']
}

repositories {
Expand All @@ -43,18 +37,13 @@ dependencies {
implementation 'org.kordamp.ikonli:ikonli-materialdesign2-pack:12.3.1'
implementation 'ch.qos.logback:logback-classic:1.5.6'
implementation 'org.codehaus.janino:janino:3.1.12'
implementation 'com.h2database:h2:2.2.224'

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.2'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.2'
}

extraJavaModuleInfo {
module('com.dampcake:bencode', 'com.dampcake.bencode', '1.4') {
exports('com.dampcake.bencode')
}
testImplementation 'org.instancio:instancio-junit:4.5.1'
}

test {
useJUnitPlatform()
jvmArgs = ['--add-reads', 'ch.qos.logback.core=org.codehaus.janino']
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import jtorrent.domain.torrent.model.Torrent;
import jtorrent.domain.torrent.repository.PieceRepository;

public class FilePieceRepository implements PieceRepository {
public class AppPieceRepository implements PieceRepository {

private static final Logger LOGGER = LoggerFactory.getLogger(FilePieceRepository.class);
private static final Logger LOGGER = LoggerFactory.getLogger(AppPieceRepository.class);
private static final String READ_ONLY_MODE = "r";
private static final String READ_WRITE_MODE = "rw";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package jtorrent.data.torrent.repository;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.util.List;

import jtorrent.data.torrent.source.file.filemanager.BencodedTorrentFileManager;
import jtorrent.data.torrent.source.file.model.BencodedTorrent;
import jtorrent.data.torrent.source.file.model.BencodedTorrentFactory;
import jtorrent.domain.torrent.model.TorrentMetadata;
import jtorrent.domain.torrent.repository.TorrentMetadataRepository;

public class AppTorrentMetadataRepository implements TorrentMetadataRepository {

private final BencodedTorrentFileManager torrentFileManager = new BencodedTorrentFileManager();

@Override
public TorrentMetadata getTorrentMetadata(File file) throws IOException {
return torrentFileManager.read(file).toDomain();
}

@Override
public TorrentMetadata getTorrentMetadata(URL url) throws IOException {
return torrentFileManager.read(url).toDomain();
}

@Override
public void saveTorrentMetadata(TorrentMetadata torrentMetadata, Path savePath) throws IOException {
BencodedTorrent bencodedTorrent = BencodedTorrent.fromDomain(torrentMetadata);
torrentFileManager.write(savePath, bencodedTorrent);
}

/**
* Creates a new {@link TorrentMetadata} instance with the current time as the creation date.
*
* @param trackerUrls list of tiers, each containing a list of tracker URLs
* @param comment comment about the torrent
* @param createdBy name and version of the program used to create the .torrent
* @param pieceSize size of each piece in bytes
* @return a new {@link TorrentMetadata} instance
*/
@Override
public TorrentMetadata createTOrrentMetadata(Path source, List<List<String>> trackerUrls, String comment,
String createdBy, int pieceSize) throws IOException {
return BencodedTorrentFactory.create(source, trackerUrls, comment, createdBy, pieceSize).toDomain();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package jtorrent.data.torrent.repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jtorrent.data.torrent.source.db.dao.TorrentDao;
import jtorrent.data.torrent.source.db.model.TorrentEntity;
import jtorrent.domain.common.util.Sha1Hash;
import jtorrent.domain.common.util.rx.MutableRxObservableList;
import jtorrent.domain.common.util.rx.RxObservableList;
import jtorrent.domain.torrent.model.Torrent;
import jtorrent.domain.torrent.repository.TorrentRepository;

public class AppTorrentRepository implements TorrentRepository {

private final MutableRxObservableList<Torrent> torrentsObservable;
private final Map<Sha1Hash, Torrent> infoHashToTorrent;
private final TorrentDao torrentDao = new TorrentDao();

public AppTorrentRepository() {
List<Torrent> torrents = new ArrayList<>();
torrentDao.readAll().stream()
.map(TorrentEntity::toDomain)
.forEach(torrents::add);
infoHashToTorrent = torrents.stream()
.collect(HashMap::new, (map, torrent) -> map.put(torrent.getInfoHash(), torrent), Map::putAll);
this.torrentsObservable = new MutableRxObservableList<>(torrents);
}

@Override
public RxObservableList<Torrent> getTorrents() {
return torrentsObservable;
}

@Override
public Torrent getTorrent(Sha1Hash infoHash) {
return infoHashToTorrent.get(infoHash);
}

@Override
public void addTorrent(Torrent torrent) {
if (isExistingTorrent(torrent)) {
// TODO: maybe throw exception if torrent already exists?
return;
}
torrentDao.create(TorrentEntity.fromDomain(torrent));
infoHashToTorrent.put(torrent.getInfoHash(), torrent);
torrentsObservable.add(torrent);
}

@Override
public void persistTorrents() {
infoHashToTorrent.values().stream()
.map(TorrentEntity::fromDomain)
.forEach(torrentDao::update);
}

@Override
public void removeTorrent(Torrent torrent) {
torrentDao.delete(torrent.getInfoHash().getBytes());
infoHashToTorrent.remove(torrent.getInfoHash());
torrentsObservable.remove(torrent);
}

private boolean isExistingTorrent(Torrent torrent) {
return infoHashToTorrent.containsKey(torrent.getInfoHash());
}
}

This file was deleted.

Loading
Loading