Skip to content

Commit

Permalink
Merge pull request #35 from Ashuh/persistence
Browse files Browse the repository at this point in the history
Implement application state persistence
  • Loading branch information
Ashuh authored Jun 2, 2024
2 parents d77353c + 6c4e7e3 commit cbff116
Show file tree
Hide file tree
Showing 51 changed files with 2,006 additions and 411 deletions.
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

0 comments on commit cbff116

Please sign in to comment.