Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storage transfer node #576

Merged
merged 3 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
</module>
<module name="FileLength">
<property name="max" value="700"/>
<property name="max" value="750"/>
</module>
<module name="NewlineAtEndOfFile"/>
<module name="JavadocPackage"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@
import com.refinedmods.refinedstorage2.api.network.impl.node.grid.GridNetworkNode;
import com.refinedmods.refinedstorage2.api.network.impl.node.iface.InterfaceNetworkNode;
import com.refinedmods.refinedstorage2.api.network.impl.node.importer.ImporterNetworkNode;
import com.refinedmods.refinedstorage2.api.network.impl.node.multistorage.MultiStorageNetworkNode;
import com.refinedmods.refinedstorage2.api.network.impl.node.relay.RelayInputNetworkNode;
import com.refinedmods.refinedstorage2.api.network.impl.node.relay.RelayOutputNetworkNode;
import com.refinedmods.refinedstorage2.api.network.impl.node.storage.StorageNetworkNode;
import com.refinedmods.refinedstorage2.api.network.impl.node.storagetransfer.StorageTransferNetworkNode;
import com.refinedmods.refinedstorage2.network.test.nodefactory.ControllerNetworkNodeFactory;
import com.refinedmods.refinedstorage2.network.test.nodefactory.DetectorNetworkNodeFactory;
import com.refinedmods.refinedstorage2.network.test.nodefactory.ExporterNetworkNodeFactory;
import com.refinedmods.refinedstorage2.network.test.nodefactory.ExternalStorageNetworkNodeFactory;
import com.refinedmods.refinedstorage2.network.test.nodefactory.GridNetworkNodeFactory;
import com.refinedmods.refinedstorage2.network.test.nodefactory.ImporterNetworkNodeFactory;
import com.refinedmods.refinedstorage2.network.test.nodefactory.InterfaceNetworkNodeFactory;
import com.refinedmods.refinedstorage2.network.test.nodefactory.MultiStorageNetworkNodeFactory;
import com.refinedmods.refinedstorage2.network.test.nodefactory.RelayInputNetworkNodeFactory;
import com.refinedmods.refinedstorage2.network.test.nodefactory.RelayOutputNetworkNodeFactory;
import com.refinedmods.refinedstorage2.network.test.nodefactory.SimpleNetworkNodeFactory;
import com.refinedmods.refinedstorage2.network.test.nodefactory.StorageNetworkNodeFactory;
import com.refinedmods.refinedstorage2.network.test.nodefactory.StorageTransferNetworkNodeFactory;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand All @@ -36,16 +36,16 @@
@Target(ElementType.TYPE)
@ExtendWith(NetworkTestExtension.class)
@RegisterNetworkNode(value = ControllerNetworkNodeFactory.class, clazz = ControllerNetworkNode.class)
@RegisterNetworkNode(value = MultiStorageNetworkNodeFactory.class, clazz = MultiStorageNetworkNode.class)
@RegisterNetworkNode(value = StorageNetworkNodeFactory.class, clazz = StorageNetworkNode.class)
@RegisterNetworkNode(value = ExporterNetworkNodeFactory.class, clazz = ExporterNetworkNode.class)
@RegisterNetworkNode(value = GridNetworkNodeFactory.class, clazz = GridNetworkNode.class)
@RegisterNetworkNode(value = ImporterNetworkNodeFactory.class, clazz = ImporterNetworkNode.class)
@RegisterNetworkNode(value = SimpleNetworkNodeFactory.class, clazz = SimpleNetworkNode.class)
@RegisterNetworkNode(value = StorageNetworkNodeFactory.class, clazz = StorageNetworkNode.class)
@RegisterNetworkNode(value = InterfaceNetworkNodeFactory.class, clazz = InterfaceNetworkNode.class)
@RegisterNetworkNode(value = ExternalStorageNetworkNodeFactory.class, clazz = ExternalStorageNetworkNode.class)
@RegisterNetworkNode(value = DetectorNetworkNodeFactory.class, clazz = DetectorNetworkNode.class)
@RegisterNetworkNode(value = RelayInputNetworkNodeFactory.class, clazz = RelayInputNetworkNode.class)
@RegisterNetworkNode(value = RelayOutputNetworkNodeFactory.class, clazz = RelayOutputNetworkNode.class)
@RegisterNetworkNode(value = StorageTransferNetworkNodeFactory.class, clazz = StorageTransferNetworkNode.class)
public @interface NetworkTest {
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import com.refinedmods.refinedstorage2.api.resource.list.ResourceListImpl;
import com.refinedmods.refinedstorage2.network.test.fake.FakePermissions;

import java.util.LinkedHashMap;

public final class NetworkTestFixtures {
public static final ComponentMapFactory<NetworkComponent, Network> NETWORK_COMPONENT_MAP_FACTORY =
new ComponentMapFactory<>();
Expand All @@ -30,7 +32,7 @@ public final class NetworkTestFixtures {
);
NETWORK_COMPONENT_MAP_FACTORY.addFactory(
StorageNetworkComponent.class,
network -> new StorageNetworkComponentImpl(new ResourceListImpl())
network -> new StorageNetworkComponentImpl(new ResourceListImpl(new LinkedHashMap<>()))
);
NETWORK_COMPONENT_MAP_FACTORY.addFactory(
SecurityNetworkComponent.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,17 @@
import java.util.Map;

public class StorageNetworkNodeFactory extends AbstractNetworkNodeFactory {
public static final String PROPERTY_ENERGY_USAGE_PER_STORAGE = "energy_usage_per_storage";
public static final String PROPERTY_SIZE = "size";

@Override
protected AbstractNetworkNode innerCreate(final AddNetworkNode ctx, final Map<String, Object> properties) {
return new StorageNetworkNode(getEnergyUsage(properties));
final long energyUsagePerStorage = (long) properties.getOrDefault(PROPERTY_ENERGY_USAGE_PER_STORAGE, 0L);
final int size = (int) properties.getOrDefault(PROPERTY_SIZE, 9);
return new StorageNetworkNode(
getEnergyUsage(properties),
energyUsagePerStorage,
size
);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package com.refinedmods.refinedstorage2.network.test.nodefactory;

import com.refinedmods.refinedstorage2.api.network.impl.node.AbstractNetworkNode;
import com.refinedmods.refinedstorage2.api.network.impl.node.multistorage.MultiStorageNetworkNode;
import com.refinedmods.refinedstorage2.api.network.impl.node.storagetransfer.StorageTransferNetworkNode;
import com.refinedmods.refinedstorage2.network.test.AddNetworkNode;

import java.util.Map;

public class MultiStorageNetworkNodeFactory extends AbstractNetworkNodeFactory {
public class StorageTransferNetworkNodeFactory extends AbstractNetworkNodeFactory {
public static final String PROPERTY_ENERGY_USAGE_PER_STORAGE = "energy_usage_per_storage";
public static final String PROPERTY_SIZE = "size";

@Override
protected AbstractNetworkNode innerCreate(final AddNetworkNode ctx, final Map<String, Object> properties) {
final long energyUsagePerStorage = (long) properties.getOrDefault(PROPERTY_ENERGY_USAGE_PER_STORAGE, 0L);
final int size = (int) properties.getOrDefault(PROPERTY_SIZE, 9);
return new MultiStorageNetworkNode(
final int size = (int) properties.getOrDefault(PROPERTY_SIZE, 6);
return new StorageTransferNetworkNode(
getEnergyUsage(properties),
energyUsagePerStorage,
size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import com.refinedmods.refinedstorage2.api.network.impl.node.grid.GridNetworkNode;
import com.refinedmods.refinedstorage2.api.network.impl.node.iface.InterfaceNetworkNode;
import com.refinedmods.refinedstorage2.api.network.impl.node.importer.ImporterNetworkNode;
import com.refinedmods.refinedstorage2.api.network.impl.node.multistorage.MultiStorageNetworkNode;
import com.refinedmods.refinedstorage2.api.network.impl.node.relay.RelayInputNetworkNode;
import com.refinedmods.refinedstorage2.api.network.impl.node.relay.RelayOutputNetworkNode;
import com.refinedmods.refinedstorage2.api.network.impl.node.storage.StorageNetworkNode;
import com.refinedmods.refinedstorage2.api.network.impl.node.storagetransfer.StorageTransferNetworkNode;

import org.junit.jupiter.api.Test;

Expand All @@ -21,8 +23,6 @@ class NetworkNodeFactoryTest {
@AddNetworkNode
ControllerNetworkNode controller;
@AddNetworkNode
MultiStorageNetworkNode multiStorage;
@AddNetworkNode
ExporterNetworkNode exporter;
@AddNetworkNode
GridNetworkNode grid;
Expand All @@ -38,6 +38,12 @@ class NetworkNodeFactoryTest {
ExternalStorageNetworkNode externalStorage;
@AddNetworkNode
DetectorNetworkNode detector;
@AddNetworkNode
RelayInputNetworkNode relayInput;
@AddNetworkNode
RelayOutputNetworkNode relayOutput;
@AddNetworkNode
StorageTransferNetworkNode storageTransfer;

@Test
void testInitialization() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
import com.refinedmods.refinedstorage2.api.network.Network;
import com.refinedmods.refinedstorage2.api.network.energy.EnergyNetworkComponent;
import com.refinedmods.refinedstorage2.api.network.impl.node.SimpleNetworkNode;
import com.refinedmods.refinedstorage2.api.network.impl.node.multistorage.MultiStorageNetworkNode;
import com.refinedmods.refinedstorage2.api.network.impl.node.storage.StorageNetworkNode;
import com.refinedmods.refinedstorage2.api.network.node.GraphNetworkComponent;
import com.refinedmods.refinedstorage2.api.network.node.container.NetworkNodeContainer;
import com.refinedmods.refinedstorage2.api.network.security.SecurityNetworkComponent;
import com.refinedmods.refinedstorage2.api.network.storage.StorageNetworkComponent;
import com.refinedmods.refinedstorage2.network.test.nodefactory.AbstractNetworkNodeFactory;
import com.refinedmods.refinedstorage2.network.test.nodefactory.MultiStorageNetworkNodeFactory;
import com.refinedmods.refinedstorage2.network.test.nodefactory.SimpleNetworkNodeFactory;
import com.refinedmods.refinedstorage2.network.test.nodefactory.StorageNetworkNodeFactory;

Expand All @@ -23,7 +21,7 @@
@ExtendWith({NetworkTestExtension.class})
@SetupNetwork(id = "a", energyCapacity = 100, energyStored = 50)
@SetupNetwork(id = "b")
@RegisterNetworkNode(value = MultiStorageNetworkNodeFactory.class, clazz = MultiStorageNetworkNode.class)
@RegisterNetworkNode(value = StorageNetworkNodeFactory.class, clazz = StorageNetworkNode.class)
@RegisterNetworkNode(value = StorageNetworkNodeFactory.class, clazz = StorageNetworkNode.class)
@RegisterNetworkNode(value = SimpleNetworkNodeFactory.class, clazz = SimpleNetworkNode.class)
class NetworkTestExtensionTest {
Expand All @@ -41,7 +39,7 @@ class NetworkTestExtensionTest {
@AddNetworkNode(networkId = "b", properties = {
@AddNetworkNode.Property(key = AbstractNetworkNodeFactory.PROPERTY_ACTIVE, boolValue = false)
})
MultiStorageNetworkNode storageInB;
StorageNetworkNode storageInB;

@AddNetworkNode(networkId = "nonexistent")
SimpleNetworkNode nonexistentNetworkNode;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.refinedmods.refinedstorage2.api.network.impl.node;

import com.refinedmods.refinedstorage2.api.network.impl.storage.AbstractStorageNetworkNode;
import com.refinedmods.refinedstorage2.api.storage.StateTrackedStorage;
import com.refinedmods.refinedstorage2.api.storage.Storage;
import com.refinedmods.refinedstorage2.api.storage.StorageState;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import javax.annotation.Nullable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractStorageContainerNetworkNode extends AbstractStorageNetworkNode {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractStorageContainerNetworkNode.class);

protected final StateTrackedStorage[] storages;

private final long energyUsage;
private final long energyUsagePerStorage;

@Nullable
private Provider provider;
@Nullable
private StateTrackedStorage.Listener listener;
private int activeStorages;

protected AbstractStorageContainerNetworkNode(final long energyUsage,
final long energyUsagePerStorage,
final int size) {
this.energyUsage = energyUsage;
this.energyUsagePerStorage = energyUsagePerStorage;
this.storages = new StateTrackedStorage[size];
}

public void setListener(final StateTrackedStorage.Listener listener) {
this.listener = listener;
}

public void setProvider(final Provider provider) {
this.provider = provider;
final List<StorageChange> changes = new ArrayList<>();
for (int i = 0; i < storages.length; ++i) {
changes.addAll(initializeStorage(i));
}
// If we are already initialized, update all the storages to keep the exposed storages in sync.
// If we are not initialized, update nothing as we have to wait for an activeness update.
if (activeStorages > 0) {
changes.forEach(this::onStorageChange);
}
updateActiveStorageCount();
}

public void onStorageChanged(final int index) {
if (index < 0 || index >= storages.length) {
LOGGER.warn("Invalid index {}", index);
return;
}
initializeStorage(index).forEach(this::onStorageChange);
updateActiveStorageCount();
}

protected void onStorageChange(final StorageChange change) {
// no op
}

private Set<StorageChange> initializeStorage(final int index) {
final Set<StorageChange> results = new HashSet<>();
if (storages[index] != null) {
results.add(new StorageChange(true, storages[index]));
}
if (provider != null) {
provider.resolve(index).ifPresentOrElse(resolved -> {
final StateTrackedStorage newStorage = new StateTrackedStorage(resolved, listener);
storages[index] = newStorage;
results.add(new StorageChange(false, newStorage));
}, () -> storages[index] = null);
}
return results;
}

private void updateActiveStorageCount() {
this.activeStorages = (int) Arrays.stream(storages).filter(Objects::nonNull).count();
}

@Override
public long getEnergyUsage() {
return energyUsage + (energyUsagePerStorage * activeStorages);
}

public int getSize() {
return storages.length;
}

public StorageState getState(final int index) {
final var storage = storages[index];
if (storage == null) {
return StorageState.NONE;
}
if (!isActive()) {
return StorageState.INACTIVE;
}
return storage.getState();
}

protected record StorageChange(boolean removed, StateTrackedStorage storage) {
}

@FunctionalInterface
public interface Provider {
Optional<Storage> resolve(int index);
}
}

This file was deleted.

Loading
Loading