Initial commit
Some checks failed
Gradle Build / build (push) Has been cancelled

This commit is contained in:
CaiXiang
2024-11-30 18:36:13 +08:00
commit aa56926258
2134 changed files with 232943 additions and 0 deletions

View File

@@ -0,0 +1,338 @@
// SPDX-FileCopyrightText: The openTCS Authors
// SPDX-License-Identifier: MIT
package org.opentcs.kernel;
import com.google.inject.assistedinject.FactoryModuleBuilder;
import com.google.inject.multibindings.MapBinder;
import jakarta.inject.Singleton;
import java.io.File;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import org.opentcs.access.Kernel;
import org.opentcs.access.LocalKernel;
import org.opentcs.access.SslParameterSet;
import org.opentcs.common.LoggingScheduledThreadPoolExecutor;
import org.opentcs.components.kernel.ObjectNameProvider;
import org.opentcs.components.kernel.services.DispatcherService;
import org.opentcs.components.kernel.services.InternalPeripheralJobService;
import org.opentcs.components.kernel.services.InternalPeripheralService;
import org.opentcs.components.kernel.services.InternalPlantModelService;
import org.opentcs.components.kernel.services.InternalQueryService;
import org.opentcs.components.kernel.services.InternalTransportOrderService;
import org.opentcs.components.kernel.services.InternalVehicleService;
import org.opentcs.components.kernel.services.NotificationService;
import org.opentcs.components.kernel.services.PeripheralDispatcherService;
import org.opentcs.components.kernel.services.PeripheralJobService;
import org.opentcs.components.kernel.services.PeripheralService;
import org.opentcs.components.kernel.services.PlantModelService;
import org.opentcs.components.kernel.services.QueryService;
import org.opentcs.components.kernel.services.RouterService;
import org.opentcs.components.kernel.services.TCSObjectService;
import org.opentcs.components.kernel.services.TransportOrderService;
import org.opentcs.components.kernel.services.VehicleService;
import org.opentcs.customizations.ApplicationEventBus;
import org.opentcs.customizations.ApplicationHome;
import org.opentcs.customizations.kernel.GlobalSyncObject;
import org.opentcs.customizations.kernel.KernelExecutor;
import org.opentcs.customizations.kernel.KernelInjectionModule;
import org.opentcs.drivers.peripherals.PeripheralControllerPool;
import org.opentcs.drivers.vehicle.VehicleControllerPool;
import org.opentcs.kernel.extensions.controlcenter.vehicles.AttachmentManager;
import org.opentcs.kernel.extensions.controlcenter.vehicles.VehicleEntryPool;
import org.opentcs.kernel.extensions.watchdog.Watchdog;
import org.opentcs.kernel.extensions.watchdog.WatchdogConfiguration;
import org.opentcs.kernel.peripherals.DefaultPeripheralControllerPool;
import org.opentcs.kernel.peripherals.LocalPeripheralControllerPool;
import org.opentcs.kernel.peripherals.PeripheralAttachmentManager;
import org.opentcs.kernel.peripherals.PeripheralCommAdapterRegistry;
import org.opentcs.kernel.peripherals.PeripheralControllerFactory;
import org.opentcs.kernel.peripherals.PeripheralEntryPool;
import org.opentcs.kernel.persistence.ModelPersister;
import org.opentcs.kernel.persistence.XMLFileModelPersister;
import org.opentcs.kernel.services.StandardDispatcherService;
import org.opentcs.kernel.services.StandardNotificationService;
import org.opentcs.kernel.services.StandardPeripheralDispatcherService;
import org.opentcs.kernel.services.StandardPeripheralJobService;
import org.opentcs.kernel.services.StandardPeripheralService;
import org.opentcs.kernel.services.StandardPlantModelService;
import org.opentcs.kernel.services.StandardQueryService;
import org.opentcs.kernel.services.StandardRouterService;
import org.opentcs.kernel.services.StandardTCSObjectService;
import org.opentcs.kernel.services.StandardTransportOrderService;
import org.opentcs.kernel.services.StandardVehicleService;
import org.opentcs.kernel.vehicles.DefaultVehicleControllerPool;
import org.opentcs.kernel.vehicles.LocalVehicleControllerPool;
import org.opentcs.kernel.vehicles.VehicleCommAdapterRegistry;
import org.opentcs.kernel.vehicles.VehicleControllerComponentsFactory;
import org.opentcs.kernel.vehicles.VehicleControllerFactory;
import org.opentcs.kernel.vehicles.transformers.CoordinateSystemTransformerFactory;
import org.opentcs.kernel.vehicles.transformers.DefaultVehicleDataTransformerFactory;
import org.opentcs.kernel.workingset.CreationTimeThreshold;
import org.opentcs.kernel.workingset.NotificationBuffer;
import org.opentcs.kernel.workingset.PeripheralJobPoolManager;
import org.opentcs.kernel.workingset.PlantModelManager;
import org.opentcs.kernel.workingset.PrefixedUlidObjectNameProvider;
import org.opentcs.kernel.workingset.TCSObjectManager;
import org.opentcs.kernel.workingset.TCSObjectRepository;
import org.opentcs.kernel.workingset.TransportOrderPoolManager;
import org.opentcs.util.event.EventBus;
import org.opentcs.util.event.EventHandler;
import org.opentcs.util.event.SimpleEventBus;
import org.opentcs.util.logging.UncaughtExceptionLogger;
/**
* A Guice module for the openTCS kernel application.
*/
public class DefaultKernelInjectionModule
extends
KernelInjectionModule {
/**
* Creates a new instance.
*/
public DefaultKernelInjectionModule() {
}
@Override
protected void configure() {
configureEventHub();
configureKernelExecutor();
// Ensure that the application's home directory can be used everywhere.
File applicationHome = new File(System.getProperty("opentcs.home", "."));
bind(File.class)
.annotatedWith(ApplicationHome.class)
.toInstance(applicationHome);
// A single global synchronization object for the kernel.
bind(Object.class)
.annotatedWith(GlobalSyncObject.class)
.to(Object.class)
.in(Singleton.class);
// The kernel's data pool structures.
bind(TCSObjectRepository.class).in(Singleton.class);
bind(TCSObjectManager.class).in(Singleton.class);
bind(PlantModelManager.class).in(Singleton.class);
bind(TransportOrderPoolManager.class).in(Singleton.class);
bind(PeripheralJobPoolManager.class).in(Singleton.class);
bind(NotificationBuffer.class).in(Singleton.class);
bind(ObjectNameProvider.class)
.to(PrefixedUlidObjectNameProvider.class)
.in(Singleton.class);
configurePersistence();
bind(VehicleCommAdapterRegistry.class)
.in(Singleton.class);
configureVehicleControllers();
bind(AttachmentManager.class)
.in(Singleton.class);
bind(VehicleEntryPool.class)
.in(Singleton.class);
configurePeripheralControllers();
bind(PeripheralCommAdapterRegistry.class)
.in(Singleton.class);
bind(PeripheralAttachmentManager.class)
.in(Singleton.class);
bind(PeripheralEntryPool.class)
.in(Singleton.class);
bind(StandardKernel.class)
.in(Singleton.class);
bind(LocalKernel.class)
.to(StandardKernel.class);
vehicleDataTransformersBinder().addBinding().to(DefaultVehicleDataTransformerFactory.class);
// tag::documentation_registerTransformerFactory[]
vehicleDataTransformersBinder().addBinding().to(CoordinateSystemTransformerFactory.class);
// end::documentation_registerTransformerFactory[]
configureKernelStatesDependencies();
configureKernelStarterDependencies();
configureSslParameters();
configureKernelServicesDependencies();
// Ensure all of these binders are initialized.
extensionsBinderAllModes();
extensionsBinderModelling();
extensionsBinderOperating();
vehicleCommAdaptersBinder();
peripheralCommAdaptersBinder();
configureWatchdogExtension();
}
private void configureKernelServicesDependencies() {
bind(StandardPlantModelService.class).in(Singleton.class);
bind(PlantModelService.class).to(StandardPlantModelService.class);
bind(InternalPlantModelService.class).to(StandardPlantModelService.class);
bind(StandardTransportOrderService.class).in(Singleton.class);
bind(TransportOrderService.class).to(StandardTransportOrderService.class);
bind(InternalTransportOrderService.class).to(StandardTransportOrderService.class);
bind(StandardVehicleService.class).in(Singleton.class);
bind(VehicleService.class).to(StandardVehicleService.class);
bind(InternalVehicleService.class).to(StandardVehicleService.class);
bind(StandardTCSObjectService.class).in(Singleton.class);
bind(TCSObjectService.class).to(StandardTCSObjectService.class);
bind(StandardNotificationService.class).in(Singleton.class);
bind(NotificationService.class).to(StandardNotificationService.class);
bind(StandardRouterService.class).in(Singleton.class);
bind(RouterService.class).to(StandardRouterService.class);
bind(StandardDispatcherService.class).in(Singleton.class);
bind(DispatcherService.class).to(StandardDispatcherService.class);
bind(StandardQueryService.class).in(Singleton.class);
bind(QueryService.class).to(StandardQueryService.class);
bind(InternalQueryService.class).to(StandardQueryService.class);
bind(StandardPeripheralService.class).in(Singleton.class);
bind(PeripheralService.class).to(StandardPeripheralService.class);
bind(InternalPeripheralService.class).to(StandardPeripheralService.class);
bind(StandardPeripheralJobService.class).in(Singleton.class);
bind(PeripheralJobService.class).to(StandardPeripheralJobService.class);
bind(InternalPeripheralJobService.class).to(StandardPeripheralJobService.class);
bind(StandardPeripheralDispatcherService.class).in(Singleton.class);
bind(PeripheralDispatcherService.class).to(StandardPeripheralDispatcherService.class);
}
private void configureVehicleControllers() {
install(new FactoryModuleBuilder().build(VehicleControllerFactory.class));
install(new FactoryModuleBuilder().build(VehicleControllerComponentsFactory.class));
bind(DefaultVehicleControllerPool.class)
.in(Singleton.class);
bind(VehicleControllerPool.class)
.to(DefaultVehicleControllerPool.class);
bind(LocalVehicleControllerPool.class)
.to(DefaultVehicleControllerPool.class);
}
private void configurePeripheralControllers() {
install(new FactoryModuleBuilder().build(PeripheralControllerFactory.class));
bind(DefaultPeripheralControllerPool.class)
.in(Singleton.class);
bind(PeripheralControllerPool.class)
.to(DefaultPeripheralControllerPool.class);
bind(LocalPeripheralControllerPool.class)
.to(DefaultPeripheralControllerPool.class);
}
private void configurePersistence() {
bind(ModelPersister.class).to(XMLFileModelPersister.class);
}
private void configureEventHub() {
EventBus newEventBus = new SimpleEventBus();
bind(EventHandler.class)
.annotatedWith(ApplicationEventBus.class)
.toInstance(newEventBus);
bind(org.opentcs.util.event.EventSource.class)
.annotatedWith(ApplicationEventBus.class)
.toInstance(newEventBus);
bind(EventBus.class)
.annotatedWith(ApplicationEventBus.class)
.toInstance(newEventBus);
}
private void configureKernelStatesDependencies() {
// A map for KernelState instances to be provided at runtime.
MapBinder<Kernel.State, KernelState> stateMapBinder
= MapBinder.newMapBinder(binder(), Kernel.State.class, KernelState.class);
stateMapBinder.addBinding(Kernel.State.SHUTDOWN).to(KernelStateShutdown.class);
stateMapBinder.addBinding(Kernel.State.MODELLING).to(KernelStateModelling.class);
stateMapBinder.addBinding(Kernel.State.OPERATING).to(KernelStateOperating.class);
bind(OrderPoolConfiguration.class)
.toInstance(
getConfigBindingProvider().get(
OrderPoolConfiguration.PREFIX,
OrderPoolConfiguration.class
)
);
bind(CreationTimeThreshold.class)
.in(Singleton.class);
transportOrderCleanupApprovalBinder();
orderSequenceCleanupApprovalBinder();
peripheralJobCleanupApprovalBinder();
}
private void configureKernelStarterDependencies() {
bind(KernelApplicationConfiguration.class)
.toInstance(
getConfigBindingProvider().get(
KernelApplicationConfiguration.PREFIX,
KernelApplicationConfiguration.class
)
);
}
private void configureSslParameters() {
SslConfiguration configuration
= getConfigBindingProvider().get(
SslConfiguration.PREFIX,
SslConfiguration.class
);
SslParameterSet sslParamSet = new SslParameterSet(
SslParameterSet.DEFAULT_KEYSTORE_TYPE,
new File(configuration.keystoreFile()),
configuration.keystorePassword(),
new File(configuration.truststoreFile()),
configuration.truststorePassword()
);
bind(SslParameterSet.class).toInstance(sslParamSet);
}
private void configureKernelExecutor() {
ScheduledExecutorService executor
= new LoggingScheduledThreadPoolExecutor(
1,
runnable -> {
Thread thread = new Thread(runnable, "kernelExecutor");
thread.setUncaughtExceptionHandler(new UncaughtExceptionLogger(false));
return thread;
}
);
bind(ScheduledExecutorService.class)
.annotatedWith(KernelExecutor.class)
.toInstance(executor);
bind(ExecutorService.class)
.annotatedWith(KernelExecutor.class)
.toInstance(executor);
bind(Executor.class)
.annotatedWith(KernelExecutor.class)
.toInstance(executor);
}
private void configureWatchdogExtension() {
extensionsBinderOperating().addBinding()
.to(Watchdog.class)
.in(Singleton.class);
bind(WatchdogConfiguration.class)
.toInstance(
getConfigBindingProvider().get(
WatchdogConfiguration.PREFIX,
WatchdogConfiguration.class
)
);
}
}

View File

@@ -0,0 +1,138 @@
// SPDX-FileCopyrightText: The openTCS Authors
// SPDX-License-Identifier: MIT
package org.opentcs.kernel;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.util.Modules;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ServiceLoader;
import org.opentcs.configuration.ConfigurationBindingProvider;
import org.opentcs.configuration.gestalt.GestaltConfigurationBindingProvider;
import org.opentcs.customizations.kernel.KernelInjectionModule;
import org.opentcs.strategies.basic.dispatching.DefaultDispatcherModule;
import org.opentcs.strategies.basic.peripherals.dispatching.DefaultPeripheralJobDispatcherModule;
import org.opentcs.strategies.basic.routing.DefaultRouterModule;
import org.opentcs.strategies.basic.scheduling.DefaultSchedulerModule;
import org.opentcs.util.Environment;
import org.opentcs.util.logging.UncaughtExceptionLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The kernel process's default entry point.
*/
public class RunKernel {
/**
* This class's Logger.
*/
private static final Logger LOG = LoggerFactory.getLogger(RunKernel.class);
/**
* Prevents external instantiation.
*/
private RunKernel() {
}
/**
* Initializes the system and starts the openTCS kernel including modules.
*
* @param args The command line arguments.
* @throws Exception If there was a problem starting the kernel.
*/
public static void main(String[] args)
throws Exception {
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionLogger(false));
Environment.logSystemInfo();
LOG.debug("Setting up openTCS kernel {}...", Environment.getBaselineVersion());
Injector injector = Guice.createInjector(customConfigurationModule());
injector.getInstance(KernelStarter.class).startKernel();
}
/**
* Builds and returns a Guice module containing the custom configuration for the kernel
* application, including additions and overrides by the user.
*
* @return The custom configuration module.
*/
private static Module customConfigurationModule() {
List<KernelInjectionModule> defaultModules
= Arrays.asList(
new DefaultKernelInjectionModule(),
new DefaultDispatcherModule(),
new DefaultRouterModule(),
new DefaultSchedulerModule(),
new DefaultPeripheralJobDispatcherModule()
);
ConfigurationBindingProvider bindingProvider = configurationBindingProvider();
for (KernelInjectionModule defaultModule : defaultModules) {
defaultModule.setConfigBindingProvider(bindingProvider);
}
return Modules.override(defaultModules)
.with(findRegisteredModules(bindingProvider));
}
/**
* Finds and returns all Guice modules registered via ServiceLoader.
*
* @return The registered/found modules.
*/
private static List<KernelInjectionModule> findRegisteredModules(
ConfigurationBindingProvider bindingProvider
) {
List<KernelInjectionModule> registeredModules = new ArrayList<>();
for (KernelInjectionModule module : ServiceLoader.load(KernelInjectionModule.class)) {
LOG.info(
"Integrating injection module {} (source: {})",
module.getClass().getName(),
module.getClass().getProtectionDomain().getCodeSource()
);
module.setConfigBindingProvider(bindingProvider);
registeredModules.add(module);
}
return registeredModules;
}
private static ConfigurationBindingProvider configurationBindingProvider() {
String chosenProvider = System.getProperty("opentcs.configuration.provider", "gestalt");
switch (chosenProvider) {
case "gestalt":
default:
LOG.info("Using gestalt as the configuration provider.");
return gestaltConfigurationBindingProvider();
}
}
private static ConfigurationBindingProvider gestaltConfigurationBindingProvider() {
return new GestaltConfigurationBindingProvider(
Paths.get(
System.getProperty("opentcs.base", "."),
"config",
"opentcs-kernel-defaults-baseline.properties"
)
.toAbsolutePath(),
Paths.get(
System.getProperty("opentcs.base", "."),
"config",
"opentcs-kernel-defaults-custom.properties"
)
.toAbsolutePath(),
Paths.get(
System.getProperty("opentcs.home", "."),
"config",
"opentcs-kernel.properties"
)
.toAbsolutePath()
);
}
}

View File

@@ -0,0 +1,48 @@
// SPDX-FileCopyrightText: The openTCS Authors
// SPDX-License-Identifier: MIT
package org.opentcs.kernel;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
/**
* Shuts down a running kernel via its administration interface.
*/
public class ShutdownKernel {
private ShutdownKernel() {
}
/**
* Java main.
*
* @param args command line args
*/
public static void main(String[] args) {
if (args.length > 2) {
System.err.println("ShutdownKernel [<host>] [<port>]");
return;
}
String hostName = args.length > 0 ? args[0] : "localhost";
int port = args.length > 1 ? Integer.parseInt(args[1]) : 55001;
try {
URL url = new URI("http://" + hostName + ":" + port + "/v1/kernel").toURL();
System.err.println("Calling to " + url + "...");
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setRequestMethod("DELETE");
httpCon.connect();
httpCon.getInputStream();
}
catch (IOException | URISyntaxException exc) {
System.err.println("Exception accessing admin interface:");
exc.printStackTrace();
}
}
}

View File

@@ -0,0 +1,2 @@
# SPDX-FileCopyrightText: The openTCS Authors
# SPDX-License-Identifier: MIT