From 961b355068349949303626588e190fbfbd16a9b2 Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Wed, 11 Nov 2020 00:47:21 +0100 Subject: [PATCH] Progress --- src/main/lombok/module-info.java | 7 -- .../warp/filesponge/BaseMirrorFileSource.java | 5 +- .../warp/filesponge/FileMirrorsManager.java | 77 +++++++++++++++++++ .../ReceiverAvailabilityManager.java | 28 +++++++ .../warp/filesponge/SecureFileAccessor.java | 7 +- 5 files changed, 113 insertions(+), 11 deletions(-) delete mode 100644 src/main/lombok/module-info.java create mode 100644 src/main/lombok/org/warp/filesponge/FileMirrorsManager.java create mode 100644 src/main/lombok/org/warp/filesponge/ReceiverAvailabilityManager.java diff --git a/src/main/lombok/module-info.java b/src/main/lombok/module-info.java deleted file mode 100644 index b5a91e4..0000000 --- a/src/main/lombok/module-info.java +++ /dev/null @@ -1,7 +0,0 @@ -module org.warp.filesponge { - exports org.warp.filesponge.api; - exports org.warp.filesponge.extra.api; - exports org.warp.filesponge.value; - requires lombok; - requires org.jetbrains.annotations; -} \ No newline at end of file diff --git a/src/main/lombok/org/warp/filesponge/BaseMirrorFileSource.java b/src/main/lombok/org/warp/filesponge/BaseMirrorFileSource.java index 8fc8db7..908e433 100644 --- a/src/main/lombok/org/warp/filesponge/BaseMirrorFileSource.java +++ b/src/main/lombok/org/warp/filesponge/BaseMirrorFileSource.java @@ -23,9 +23,10 @@ import lombok.AllArgsConstructor; import org.warp.filesponge.api.FileSource; import org.warp.filesponge.value.MirrorURI; -@AllArgsConstructor(access = AccessLevel.PRIVATE) +@AllArgsConstructor(access = AccessLevel.PROTECTED) public abstract class BaseMirrorFileSource implements FileSource { - private final MirrorURI mirrorURI; + protected final ReceiverAvailabilityManager receiverAvailabilityManager; + protected final MirrorURI mirrorURI; } diff --git a/src/main/lombok/org/warp/filesponge/FileMirrorsManager.java b/src/main/lombok/org/warp/filesponge/FileMirrorsManager.java new file mode 100644 index 0000000..6fdac23 --- /dev/null +++ b/src/main/lombok/org/warp/filesponge/FileMirrorsManager.java @@ -0,0 +1,77 @@ +/* + * FileSponge + * Copyright (C) 2020 Andrea Cavalli + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.warp.filesponge; + +import java.util.HashSet; +import java.util.Set; +import java.util.stream.Collectors; +import lombok.AllArgsConstructor; +import org.warp.commonutils.type.MultiAssociation; +import org.warp.filesponge.value.FileURI; +import org.warp.filesponge.value.MirrorURI; + +@AllArgsConstructor() +public class FileMirrorsManager { + + private final Set availableMirrors = new HashSet<>(); + /** + * This map must be persistent + */ + private final MultiAssociation fileMirrors; + + public synchronized Set getAvailableMirrors(FileURI fileURI) { + return fileMirrors + .getLinks(fileURI) + .stream() + .filter(this::isMirrorAvailable) + .collect(Collectors.toUnmodifiableSet()); + } + + public synchronized boolean hasAnyAvailableReceiver(FileURI uri) { + return fileMirrors.getLinks(uri).stream().anyMatch(this::isMirrorAvailable); + } + + public synchronized void addMirror(FileURI uri, MirrorURI mirrorURI) { + fileMirrors.link(uri, mirrorURI); + } + + public synchronized void removeMirror(FileURI uri, MirrorURI mirrorURI) { + fileMirrors.unlink(uri, mirrorURI); + } + + public synchronized void unsetAllFiles() { + fileMirrors.clear(); + } + + public synchronized void setAllMirrorsAsUnavailable() { + availableMirrors.clear(); + } + + public synchronized void setReceiverAvailability(MirrorURI mirrorURI, boolean available) { + if (available) { + availableMirrors.add(mirrorURI); + } else { + availableMirrors.remove(mirrorURI); + } + } + + public synchronized boolean isMirrorAvailable(MirrorURI mirrorURI) { + return this.availableMirrors.contains(mirrorURI); + } +} diff --git a/src/main/lombok/org/warp/filesponge/ReceiverAvailabilityManager.java b/src/main/lombok/org/warp/filesponge/ReceiverAvailabilityManager.java new file mode 100644 index 0000000..8e1b3ce --- /dev/null +++ b/src/main/lombok/org/warp/filesponge/ReceiverAvailabilityManager.java @@ -0,0 +1,28 @@ +/* + * FileSponge + * Copyright (C) 2020 Andrea Cavalli + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.warp.filesponge; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; + +@AllArgsConstructor(access = AccessLevel.PUBLIC) +public class ReceiverAvailabilityManager { + + private final FileMirrorsManager fileReceiversManager; +} diff --git a/src/main/lombok/org/warp/filesponge/SecureFileAccessor.java b/src/main/lombok/org/warp/filesponge/SecureFileAccessor.java index b33281d..75b20de 100644 --- a/src/main/lombok/org/warp/filesponge/SecureFileAccessor.java +++ b/src/main/lombok/org/warp/filesponge/SecureFileAccessor.java @@ -22,7 +22,6 @@ import java.util.Optional; import java.util.concurrent.CompletableFuture; import lombok.AllArgsConstructor; import lombok.EqualsAndHashCode; -import lombok.ToString; import org.jetbrains.annotations.NotNull; import org.warp.filesponge.api.FileAccessor; import org.warp.filesponge.value.FileContent; @@ -34,7 +33,6 @@ import org.warp.filesponge.value.FileURI; */ @AllArgsConstructor @EqualsAndHashCode -@ToString public class SecureFileAccessor implements FileAccessor { private final FileAccessor fileAccessor; @@ -53,4 +51,9 @@ public class SecureFileAccessor implements FileAccessor { public @NotNull FileStatus getStatus(@NotNull FileURI fileURI) { return fileAccessor.getStatus(fileURI); } + + @Override + public String toString() { + return fileAccessor.toString(); + } }