mirror of
https://github.com/SimplexDevelopment/SimplexSS.git
synced 2025-07-04 07:56:42 +00:00
SimplexSS (Scheduling Service)
A reactive non-blocking api for scheduling runnable tasks (now called Services) using Reactor (https://reactorproject.io)
This commit is contained in:
23
src/main/java/io/github/simplex/api/ExecutableService.java
Normal file
23
src/main/java/io/github/simplex/api/ExecutableService.java
Normal file
@ -0,0 +1,23 @@
|
||||
package io.github.simplex.api;
|
||||
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
public abstract class ExecutableService implements Service {
|
||||
private final int serviceID;
|
||||
private final Plugin plugin;
|
||||
|
||||
public ExecutableService(Plugin plugin, int serviceID) {
|
||||
this.serviceID = serviceID;
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getServiceID() {
|
||||
return serviceID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Plugin getProvidingPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
}
|
16
src/main/java/io/github/simplex/api/Scheduler.java
Normal file
16
src/main/java/io/github/simplex/api/Scheduler.java
Normal file
@ -0,0 +1,16 @@
|
||||
package io.github.simplex.api;
|
||||
|
||||
import io.github.simplex.simplexss.ServiceManager;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
public interface Scheduler {
|
||||
Mono<ServiceManager> getServiceManager();
|
||||
|
||||
Mono<Void> queue(Service service);
|
||||
|
||||
Mono<Void> runOnce(Service service);
|
||||
|
||||
Mono<Void> forceStop(Service service);
|
||||
|
||||
Mono<Void> forceStart(Service service);
|
||||
}
|
22
src/main/java/io/github/simplex/api/Service.java
Normal file
22
src/main/java/io/github/simplex/api/Service.java
Normal file
@ -0,0 +1,22 @@
|
||||
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 {
|
||||
int getServiceID();
|
||||
|
||||
boolean isDelayed();
|
||||
|
||||
boolean isRepeating();
|
||||
|
||||
Mono<Void> start();
|
||||
|
||||
Mono<Void> stop();
|
||||
|
||||
Plugin getProvidingPlugin();
|
||||
|
||||
ServicePool getServicePool();
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
package io.github.simplex.simplexss;
|
||||
|
||||
import io.github.simplex.api.Scheduler;
|
||||
import io.github.simplex.api.Service;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public final class SchedulingSystem implements Scheduler {
|
||||
private final ServiceManager serviceManager;
|
||||
private final Plugin plugin;
|
||||
private final Set<ServicePool> repeatingPools;
|
||||
private final Set<ServicePool> delayedPools;
|
||||
|
||||
public SchedulingSystem(ServiceManager serviceManager, Plugin plugin) {
|
||||
this.serviceManager = serviceManager;
|
||||
this.plugin = plugin;
|
||||
this.repeatingPools = new HashSet<>();
|
||||
this.delayedPools = new HashSet<>();
|
||||
}
|
||||
|
||||
public Set<ServicePool> getRepeatingPools() {
|
||||
return repeatingPools;
|
||||
}
|
||||
|
||||
public Set<ServicePool> getDelayedPools() {
|
||||
return delayedPools;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<ServiceManager> getServiceManager() {
|
||||
return Mono.just(serviceManager);
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
});
|
||||
});
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> runOnce(Service service) {
|
||||
service.start().block();
|
||||
service.stop().block();
|
||||
return Mono.empty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> forceStop(Service service) {
|
||||
return service.stop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> forceStart(Service service) {
|
||||
return service.start();
|
||||
}
|
||||
|
||||
public Plugin getProvidingPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
package io.github.simplex.simplexss;
|
||||
|
||||
import io.github.simplex.api.Service;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public final class ServiceManager {
|
||||
private final Set<ServicePool> servicePools;
|
||||
private final Plugin plugin;
|
||||
|
||||
public ServiceManager(Plugin plugin) {
|
||||
this.plugin = plugin;
|
||||
servicePools = new HashSet<>();
|
||||
}
|
||||
|
||||
@Contract(pure = true, value = "_ -> new")
|
||||
public @NotNull ServicePool createServicePool(Service... services) {
|
||||
ServicePool pool = new ServicePool();
|
||||
Stream.of(services).forEach(pool::addService);
|
||||
return pool;
|
||||
}
|
||||
|
||||
@Contract("_, _ -> param1")
|
||||
public ServicePool addToExistingPool(@NotNull ServicePool pool, Service... services) {
|
||||
Stream.of(services).forEach(pool::addService);
|
||||
return pool;
|
||||
}
|
||||
|
||||
@Contract("_, _ -> param1")
|
||||
public ServicePool takeFromExistingPool(@NotNull ServicePool pool, Service... services) {
|
||||
Stream.of(services).forEach(pool::removeService);
|
||||
return pool;
|
||||
}
|
||||
|
||||
public Set<ServicePool> getServicePools() {
|
||||
return servicePools;
|
||||
}
|
||||
|
||||
public boolean locateServiceWithinPools(Service service) {
|
||||
return servicePools.stream().map(p -> p.isValidService(service)).findFirst().orElseGet(() -> false);
|
||||
}
|
||||
|
||||
public @Nullable ServicePool getAssociatedServicePool(Service service) {
|
||||
if (!locateServiceWithinPools(service)) return null;
|
||||
|
||||
return servicePools
|
||||
.stream()
|
||||
.filter(p -> p.getAssociatedServices().contains(service))
|
||||
.findFirst()
|
||||
.orElseGet(() -> null);
|
||||
}
|
||||
|
||||
public Plugin getProvidingPlugin() {
|
||||
return plugin;
|
||||
}
|
||||
}
|
56
src/main/java/io/github/simplex/simplexss/ServicePool.java
Normal file
56
src/main/java/io/github/simplex/simplexss/ServicePool.java
Normal file
@ -0,0 +1,56 @@
|
||||
package io.github.simplex.simplexss;
|
||||
|
||||
import io.github.simplex.api.Service;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
public final class ServicePool {
|
||||
private final Set<Service> associatedServices;
|
||||
private boolean delayed = false;
|
||||
private boolean repeating = false;
|
||||
|
||||
public ServicePool() {
|
||||
this.associatedServices = new HashSet<>();
|
||||
}
|
||||
|
||||
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) {
|
||||
getAssociatedServices().add(service);
|
||||
}
|
||||
|
||||
public boolean isValidService(Service service) {
|
||||
return getAssociatedServices().contains(service);
|
||||
}
|
||||
|
||||
public Set<Service> getAssociatedServices() {
|
||||
return associatedServices;
|
||||
}
|
||||
|
||||
public Service getService(int serviceID) {
|
||||
return getAssociatedServices()
|
||||
.stream()
|
||||
.filter(s -> s.getServiceID() == serviceID)
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
public void removeService(Service service) {
|
||||
getAssociatedServices().remove(service);
|
||||
}
|
||||
}
|
17
src/main/java/io/github/simplex/simplexss/SimplexSS.java
Normal file
17
src/main/java/io/github/simplex/simplexss/SimplexSS.java
Normal file
@ -0,0 +1,17 @@
|
||||
package io.github.simplex.simplexss;
|
||||
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public final class SimplexSS extends JavaPlugin {
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
// Plugin startup logic
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
// Plugin shutdown logic
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user