Big update

Rounding things out
This commit is contained in:
Paldiu 2022-11-19 18:20:07 -06:00
parent 115e8fcb20
commit 6257edb19b
6 changed files with 60 additions and 41 deletions

View File

@ -6,7 +6,7 @@ public abstract class ExecutableService implements Service {
private final int serviceID;
private final Plugin plugin;
public ExecutableService(Plugin plugin, int serviceID) {
public ExecutableService(Plugin plugin, int serviceID, long delay, long period, boolean delayed, boolean repeating) {
this.serviceID = serviceID;
this.plugin = plugin;
}

View File

@ -1,12 +1,13 @@
package io.github.simplex.api;
import io.github.simplex.simplexss.ServiceManager;
import io.github.simplex.simplexss.ServicePool;
import reactor.core.publisher.Mono;
public interface Scheduler {
Mono<ServiceManager> getServiceManager();
Mono<Void> queue(Service service);
Mono<ServicePool> queue(Service service);
Mono<Void> runOnce(Service service);

View File

@ -1,22 +1,30 @@
package io.github.simplex.api;
import io.github.simplex.simplexss.ServiceManager;
import io.github.simplex.simplexss.ServicePool;
import org.bukkit.plugin.Plugin;
import reactor.core.publisher.Mono;
public interface Service {
import java.util.concurrent.RunnableScheduledFuture;
public interface Service extends RunnableScheduledFuture<Service> {
int getServiceID();
boolean isDelayed();
boolean isRepeating();
long getPeriod();
long getDelay();
Mono<Void> start();
Mono<Void> stop();
Plugin getProvidingPlugin();
ServicePool getServicePool();
@Override
default void run() {
start().subscribe();
}
}

View File

@ -35,24 +35,11 @@ public final class SchedulingSystem implements Scheduler {
}
@Override
public Mono<Void> queue(Service service) {
getServiceManager().doOnNext(m -> {
Mono<ServicePool> pool = Mono.justOrEmpty(m.getAssociatedServicePool(service));
pool.defaultIfEmpty(m.createServicePool(service))
.map(p -> p.getService(service.getServiceID()))
.doOnNext(s -> {
if (s.getServicePool().isPoolDelayed()) {
getDelayedPools().add(s.getServicePool());
}
if (s.getServicePool().isPoolRepeating()) {
getRepeatingPools().add(s.getServicePool());
}
else {
runOnce(s).block();
}
});
public Mono<ServicePool> queue(Service service) {
return getServiceManager().flatMap(serviceManager -> {
Mono<ServicePool> pool = Mono.justOrEmpty(serviceManager.getAssociatedServicePool(service));
return pool.defaultIfEmpty(serviceManager.createServicePool(service));
});
return Mono.empty();
}
@Override

View File

@ -49,7 +49,7 @@ public final class ServiceManager {
public @Nullable ServicePool getAssociatedServicePool(Service service) {
if (!locateServiceWithinPools(service)) return null;
return servicePools
return getServicePools()
.stream()
.filter(p -> p.getAssociatedServices().contains(service))
.findFirst()

View File

@ -1,47 +1,63 @@
package io.github.simplex.simplexss;
import io.github.simplex.api.Service;
import org.jetbrains.annotations.NotNull;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public final class ServicePool {
private final Set<Service> associatedServices;
private final Scheduler scheduler;
private final ExecutorService executor;
private boolean delayed = false;
private boolean repeating = false;
public ServicePool() {
this.associatedServices = new HashSet<>();
this.executor = Executors.newSingleThreadExecutor();
this.scheduler = Schedulers.fromExecutorService(executor);
}
public void setDelayed(boolean delayed) {
this.delayed = delayed;
}
public void setRepeating(boolean repeating) {
this.repeating = repeating;
}
public boolean isPoolDelayed() {
return delayed;
}
public boolean isPoolRepeating() {
return repeating;
}
public void addService(Service service) {
void addService(Service service) {
getAssociatedServices().add(service);
}
public boolean isValidService(Service service) {
boolean isValidService(Service service) {
return getAssociatedServices().contains(service);
}
@NotNull
public Set<Service> getAssociatedServices() {
return associatedServices;
}
public Mono<Void> startServices() {
return Mono.just(getAssociatedServices()).doOnNext(services -> {
for (Service service : services) {
if (service.isRepeating()) {
scheduler.schedulePeriodically(service, service.getDelay() * 5, service.getPeriod() * 5, TimeUnit.MILLISECONDS);
} else if (service.isDelayed()) {
scheduler.schedule(service, service.getDelay() * 5, TimeUnit.MILLISECONDS);
}
}
}).then();
}
public Mono<Void> stopServices() {
return Mono.just(getAssociatedServices()).doOnNext(services -> {
for (Service service : services) {
service.stop();
}
}).then();
}
public Service getService(int serviceID) {
return getAssociatedServices()
.stream()
@ -53,4 +69,11 @@ public final class ServicePool {
public void removeService(Service service) {
getAssociatedServices().remove(service);
}
public ServicePool recycle() {
this.getAssociatedServices().clear();
this.repeating = false;
this.delayed = false;
return this;
}
}