Skip to content

Commit

Permalink
refactor: remove StorageChannel segregation per type
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulvdberge committed Mar 9, 2024
1 parent 5ff90cf commit 75311f9
Show file tree
Hide file tree
Showing 272 changed files with 1,906 additions and 3,075 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.refinedmods.refinedstorage2.api.grid.watcher;

import com.refinedmods.refinedstorage2.api.resource.ResourceKey;
import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType;
import com.refinedmods.refinedstorage2.api.storage.tracked.TrackedResource;

import javax.annotation.Nullable;
Expand All @@ -23,17 +22,11 @@ public interface GridWatcher {
/**
* Called when a resource is changed.
*
* @param storageChannelType the relevant storage channel type
* @param resource the resource
* @param change the changed amount
* @param trackedResource the tracked resource, if present
* @param resource the resource
* @param change the changed amount
* @param trackedResource the tracked resource, if present
*/
void onChanged(
StorageChannelType storageChannelType,
ResourceKey resource,
long change,
@Nullable TrackedResource trackedResource
);
void onChanged(ResourceKey resource, long change, @Nullable TrackedResource trackedResource);

/**
* Usually called when the grid network has been changed.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.refinedmods.refinedstorage2.api.grid.watcher;

import com.refinedmods.refinedstorage2.api.storage.Actor;
import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannel;

import javax.annotation.Nullable;

import org.apiguardian.api.API;

Expand All @@ -10,17 +13,15 @@
*/
@API(status = API.Status.STABLE, since = "2.0.0-milestone.3.3")
public interface GridWatcherManager {
void addWatcher(
GridWatcher watcher,
Class<? extends Actor> actorType,
GridStorageChannelProvider storageChannelProvider
);
void addWatcher(GridWatcher watcher,
Class<? extends Actor> actorType,
@Nullable StorageChannel storageChannel);

void attachAll(GridStorageChannelProvider storageChannelProvider);
void attachAll(@Nullable StorageChannel storageChannel);

void removeWatcher(GridWatcher watcher, GridStorageChannelProvider storageChannelProvider);
void removeWatcher(GridWatcher watcher, @Nullable StorageChannel storageChannel);

void detachAll(GridStorageChannelProvider storageChannelProvider);
void detachAll(StorageChannel storageChannel);

void activeChanged(boolean active);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.refinedmods.refinedstorage2.api.grid.watcher;

import com.refinedmods.refinedstorage2.api.storage.Actor;
import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType;
import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannel;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;

import org.apiguardian.api.API;
import org.slf4j.Logger;
Expand All @@ -20,87 +21,68 @@ public class GridWatcherManagerImpl implements GridWatcherManager {
public void addWatcher(
final GridWatcher watcher,
final Class<? extends Actor> actorType,
final GridStorageChannelProvider storageChannelProvider
@Nullable final StorageChannel storageChannel
) {
if (watchers.containsKey(watcher)) {
throw new IllegalArgumentException("Watcher is already registered");
}
final GridWatcherRegistration registration = new GridWatcherRegistration(watcher, actorType);
attachAll(registration, storageChannelProvider, false);
if (storageChannel != null) {
attach(registration, storageChannel, false);
}
watchers.put(watcher, registration);
LOGGER.info("Added watcher {}, new count is {}", watcher, watchers.size());
}

@Override
public void attachAll(final GridStorageChannelProvider storageChannelProvider) {
public void attachAll(@Nullable final StorageChannel storageChannel) {
// If we get here we are affected by a network split or network merge.
// At this point, all the storages that are affected by the split or merge have not yet been processed
// as the grid has the highest priority.
watchers.forEach((watcher, registration) -> {
// Invalidate all watcher data, the resources that were synced earlier are no longer valid because we have
// a brand-new network.
watcher.invalidate();
// Re-attach the watcher to the new network, and send all the resources from the new network.
// Resources from the old network are not part of the new network yet, as mentioned above,
// but those will be synced when the storages are re-added.
attachAll(registration, storageChannelProvider, true);
if (storageChannel != null) {
// Re-attach the watcher to the new network, and send all the resources from the new network.
// Resources from the old network are not part of the new network yet, as mentioned above,
// but those will be synced when the storages are re-added.
attach(registration, storageChannel, true);
}
});
}

private void attachAll(final GridWatcherRegistration registration,
final GridStorageChannelProvider storageChannelProvider,
final boolean replay) {
storageChannelProvider.getStorageChannelTypes().forEach(storageChannelType -> attach(
registration,
storageChannelType,
storageChannelProvider,
replay
));
}

private void attach(
final GridWatcherRegistration registration,
final StorageChannelType storageChannelType,
final GridStorageChannelProvider storageChannelProvider,
final StorageChannel storageChannel,
final boolean replay
) {
LOGGER.info("Attaching {} to {}", registration, storageChannelType);
registration.attach(storageChannelProvider.getStorageChannel(storageChannelType), storageChannelType, replay);
LOGGER.info("Attaching {} to {}", registration, storageChannel);
registration.attach(storageChannel, replay);
}

@Override
public void removeWatcher(final GridWatcher watcher, final GridStorageChannelProvider storageChannelProvider) {
public void removeWatcher(final GridWatcher watcher, @Nullable final StorageChannel storageChannel) {
final GridWatcherRegistration registration = watchers.get(watcher);
if (registration == null) {
throw new IllegalArgumentException("Watcher is not registered");
}
detachAll(registration, storageChannelProvider);
if (storageChannel != null) {
detach(registration, storageChannel);
}
watchers.remove(watcher);
LOGGER.info("Removed watcher {}, remaining {}", watcher, watchers.size());
}

@Override
public void detachAll(final GridStorageChannelProvider storageChannelProvider) {
public void detachAll(final StorageChannel storageChannel) {
LOGGER.info("Detaching {} watchers", watchers.size());
watchers.values().forEach(w -> detachAll(w, storageChannelProvider));
watchers.values().forEach(watcher -> detach(watcher, storageChannel));
}

private void detachAll(final GridWatcherRegistration registration,
final GridStorageChannelProvider storageChannelProvider) {
storageChannelProvider.getStorageChannelTypes().forEach(storageChannelType -> detach(
registration,
storageChannelType,
storageChannelProvider
));
}

private void detach(
final GridWatcherRegistration registration,
final StorageChannelType storageChannelType,
final GridStorageChannelProvider storageChannelProvider
) {
LOGGER.info("Detaching {} from {}", registration, storageChannelType);
registration.detach(storageChannelProvider.getStorageChannel(storageChannelType), storageChannelType);
private void detach(final GridWatcherRegistration registration, final StorageChannel storageChannel) {
LOGGER.info("Detaching {} from {}", registration, storageChannel);
registration.detach(storageChannel);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,22 @@
import com.refinedmods.refinedstorage2.api.resource.list.listenable.ResourceListListener;
import com.refinedmods.refinedstorage2.api.storage.Actor;
import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannel;
import com.refinedmods.refinedstorage2.api.storage.channel.StorageChannelType;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;

class GridWatcherRegistration {
private final GridWatcher watcher;
private final Class<? extends Actor> actorType;
private final Map<StorageChannelType, ResourceListListener> listeners = new HashMap<>();
@Nullable
private ResourceListListener listener;

GridWatcherRegistration(final GridWatcher watcher, final Class<? extends Actor> actorType) {
this.watcher = watcher;
this.actorType = actorType;
}

void attach(final StorageChannel storageChannel,
final StorageChannelType storageChannelType,
final boolean replay) {
final ResourceListListener listener = change -> watcher.onChanged(
storageChannelType,
void attach(final StorageChannel storageChannel, final boolean replay) {
this.listener = change -> watcher.onChanged(
change.resourceAmount().getResource(),
change.change(),
storageChannel.findTrackedResourceByActorType(
Expand All @@ -31,10 +27,8 @@ void attach(final StorageChannel storageChannel,
).orElse(null)
);
storageChannel.addListener(listener);
listeners.put(storageChannelType, listener);
if (replay) {
storageChannel.getAll().forEach(resourceAmount -> watcher.onChanged(
storageChannelType,
resourceAmount.getResource(),
resourceAmount.getAmount(),
storageChannel.findTrackedResourceByActorType(
Expand All @@ -45,9 +39,11 @@ void attach(final StorageChannel storageChannel,
}
}

void detach(final StorageChannel storageChannel, final StorageChannelType storageChannelType) {
final ResourceListListener listener = listeners.get(storageChannelType);
void detach(final StorageChannel storageChannel) {
if (listener == null) {
return;
}
storageChannel.removeListener(listener);
listeners.remove(storageChannelType);
listener = null;
}
}
Loading

0 comments on commit 75311f9

Please sign in to comment.