mirror of
https://github.com/SimplexDevelopment/SimplexSS.git
synced 2024-11-14 22:03:31 +00:00
Big update
Rounding things out
This commit is contained in:
parent
115e8fcb20
commit
6257edb19b
@ -6,7 +6,7 @@ public abstract class ExecutableService implements Service {
|
|||||||
private final int serviceID;
|
private final int serviceID;
|
||||||
private final Plugin plugin;
|
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.serviceID = serviceID;
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,13 @@
|
|||||||
package io.github.simplex.api;
|
package io.github.simplex.api;
|
||||||
|
|
||||||
import io.github.simplex.simplexss.ServiceManager;
|
import io.github.simplex.simplexss.ServiceManager;
|
||||||
|
import io.github.simplex.simplexss.ServicePool;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
public interface Scheduler {
|
public interface Scheduler {
|
||||||
Mono<ServiceManager> getServiceManager();
|
Mono<ServiceManager> getServiceManager();
|
||||||
|
|
||||||
Mono<Void> queue(Service service);
|
Mono<ServicePool> queue(Service service);
|
||||||
|
|
||||||
Mono<Void> runOnce(Service service);
|
Mono<Void> runOnce(Service service);
|
||||||
|
|
||||||
|
@ -1,22 +1,30 @@
|
|||||||
package io.github.simplex.api;
|
package io.github.simplex.api;
|
||||||
|
|
||||||
import io.github.simplex.simplexss.ServiceManager;
|
|
||||||
import io.github.simplex.simplexss.ServicePool;
|
import io.github.simplex.simplexss.ServicePool;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
import reactor.core.publisher.Mono;
|
import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
public interface Service {
|
import java.util.concurrent.RunnableScheduledFuture;
|
||||||
|
|
||||||
|
public interface Service extends RunnableScheduledFuture<Service> {
|
||||||
int getServiceID();
|
int getServiceID();
|
||||||
|
|
||||||
boolean isDelayed();
|
boolean isDelayed();
|
||||||
|
|
||||||
boolean isRepeating();
|
boolean isRepeating();
|
||||||
|
|
||||||
|
long getPeriod();
|
||||||
|
|
||||||
|
long getDelay();
|
||||||
|
|
||||||
Mono<Void> start();
|
Mono<Void> start();
|
||||||
|
|
||||||
Mono<Void> stop();
|
Mono<Void> stop();
|
||||||
|
|
||||||
Plugin getProvidingPlugin();
|
Plugin getProvidingPlugin();
|
||||||
|
|
||||||
ServicePool getServicePool();
|
@Override
|
||||||
|
default void run() {
|
||||||
|
start().subscribe();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,24 +35,11 @@ public final class SchedulingSystem implements Scheduler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Mono<Void> queue(Service service) {
|
public Mono<ServicePool> queue(Service service) {
|
||||||
getServiceManager().doOnNext(m -> {
|
return getServiceManager().flatMap(serviceManager -> {
|
||||||
Mono<ServicePool> pool = Mono.justOrEmpty(m.getAssociatedServicePool(service));
|
Mono<ServicePool> pool = Mono.justOrEmpty(serviceManager.getAssociatedServicePool(service));
|
||||||
pool.defaultIfEmpty(m.createServicePool(service))
|
return pool.defaultIfEmpty(serviceManager.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();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
return Mono.empty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -49,7 +49,7 @@ public final class ServiceManager {
|
|||||||
public @Nullable ServicePool getAssociatedServicePool(Service service) {
|
public @Nullable ServicePool getAssociatedServicePool(Service service) {
|
||||||
if (!locateServiceWithinPools(service)) return null;
|
if (!locateServiceWithinPools(service)) return null;
|
||||||
|
|
||||||
return servicePools
|
return getServicePools()
|
||||||
.stream()
|
.stream()
|
||||||
.filter(p -> p.getAssociatedServices().contains(service))
|
.filter(p -> p.getAssociatedServices().contains(service))
|
||||||
.findFirst()
|
.findFirst()
|
||||||
|
@ -1,47 +1,63 @@
|
|||||||
package io.github.simplex.simplexss;
|
package io.github.simplex.simplexss;
|
||||||
|
|
||||||
import io.github.simplex.api.Service;
|
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.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
public final class ServicePool {
|
public final class ServicePool {
|
||||||
private final Set<Service> associatedServices;
|
private final Set<Service> associatedServices;
|
||||||
|
private final Scheduler scheduler;
|
||||||
|
private final ExecutorService executor;
|
||||||
private boolean delayed = false;
|
private boolean delayed = false;
|
||||||
private boolean repeating = false;
|
private boolean repeating = false;
|
||||||
|
|
||||||
public ServicePool() {
|
public ServicePool() {
|
||||||
this.associatedServices = new HashSet<>();
|
this.associatedServices = new HashSet<>();
|
||||||
|
this.executor = Executors.newSingleThreadExecutor();
|
||||||
|
this.scheduler = Schedulers.fromExecutorService(executor);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDelayed(boolean delayed) {
|
void addService(Service service) {
|
||||||
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) {
|
|
||||||
getAssociatedServices().add(service);
|
getAssociatedServices().add(service);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isValidService(Service service) {
|
boolean isValidService(Service service) {
|
||||||
return getAssociatedServices().contains(service);
|
return getAssociatedServices().contains(service);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
public Set<Service> getAssociatedServices() {
|
public Set<Service> getAssociatedServices() {
|
||||||
return associatedServices;
|
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) {
|
public Service getService(int serviceID) {
|
||||||
return getAssociatedServices()
|
return getAssociatedServices()
|
||||||
.stream()
|
.stream()
|
||||||
@ -53,4 +69,11 @@ public final class ServicePool {
|
|||||||
public void removeService(Service service) {
|
public void removeService(Service service) {
|
||||||
getAssociatedServices().remove(service);
|
getAssociatedServices().remove(service);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ServicePool recycle() {
|
||||||
|
this.getAssociatedServices().clear();
|
||||||
|
this.repeating = false;
|
||||||
|
this.delayed = false;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user