/* * This file is part of Applied Energistics 2. Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved. Applied * Energistics 2 is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General * Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any * later version. Applied Energistics 2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General * Public License for more details. You should have received a copy of the GNU Lesser General Public License along with * Applied Energistics 2. If not, see . */ package appeng.me.cache; import java.util.Collection; import java.util.Collections; import java.util.Deque; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Optional; import java.util.Set; import java.util.WeakHashMap; import java.util.function.Predicate; import javax.annotation.Nonnegative; import javax.annotation.Nonnull; import javax.annotation.Nullable; import org.jetbrains.annotations.NotNull; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import appeng.api.config.AccessRestriction; import appeng.api.config.Actionable; import appeng.api.networking.IGrid; import appeng.api.networking.events.MENetworkStorageEvent; import appeng.api.networking.security.BaseActionSource; import appeng.api.networking.security.ReshuffleActionSource; import appeng.api.networking.storage.IStorageInterceptor; import appeng.api.storage.IMEInventoryHandler; import appeng.api.storage.IMEMonitor; import appeng.api.storage.IMEMonitorHandlerReceiver; import appeng.api.storage.IMENetworkInventory; import appeng.api.storage.StorageChannel; import appeng.api.storage.data.IAEStack; import appeng.api.storage.data.IAEStackType; import appeng.api.storage.data.IItemList; import appeng.me.storage.ItemWatcher; import appeng.util.IterationCounter; import appeng.util.item.LazyItemList; import appeng.util.item.NetworkItemList; import appeng.util.item.PrioritizedNetworkItemList; public class NetworkMonitor> implements IMEMonitor { @Nonnull private static final Deque> GLOBAL_DEPTH = Lists.newLinkedList(); @Nonnull private final GridStorageCache myGridCache; private final IAEStackType stackType; @Nonnull private final IItemList cachedList; @Nonnull private final Map listeners; private boolean sendEvent = false; private boolean hasChanged = false; @Nonnegative private int localDepthSemaphore = 0; private volatile boolean locked = false; private final Set storageInterceptors = Collections.newSetFromMap(new WeakHashMap<>()); public NetworkMonitor(final GridStorageCache cache, final IAEStackType type) { this.myGridCache = cache; this.stackType = type; this.cachedList = type.createList(); this.listeners = new HashMap<>(); } public void setLocked(boolean locked) { this.locked = locked; } public boolean isLocked() { return this.locked; } private boolean isReshuffleSource(BaseActionSource src) { return src instanceof ReshuffleActionSource; } @Override public void addListener(final IMEMonitorHandlerReceiver l, final Object verificationToken) { this.listeners.put(l, verificationToken); } @Override public boolean canAccept(final T input) { return this.getHandler().canAccept(input); } @Override public T extractItems(final T request, final Actionable mode, final BaseActionSource src) { if (this.locked && !isReshuffleSource(src)) { return null; } if (mode == Actionable.SIMULATE) { return this.getHandler().extractItems(request, mode, src); } localDepthSemaphore++; final T leftover = this.getHandler().extractItems(request, mode, src); localDepthSemaphore--; if (localDepthSemaphore == 0) { this.monitorDifference(request.copy(), leftover, true, src); } return leftover; } @Override public AccessRestriction getAccess() { return this.getHandler().getAccess(); } @Override public IItemList getAvailableItems(final IItemList out, int iteration) { return this.getHandler().getAvailableItems(out, iteration); } @Override public IItemList getAvailableItems(IItemList out, int iteration, Optional> filter) { return this.getHandler().getAvailableItems(out, iteration, filter); } @Override public PrioritizedNetworkItemList getAvailableItemsWithPriority(int iteration) { return this.getHandler().getAvailableItemsWithPriority(iteration); } @Override public StorageChannel getChannel() { return this.getHandler().getChannel(); } @Override @SuppressWarnings({ "unchecked" }) public @NotNull IAEStackType getStackType() { return (IAEStackType) this.getHandler().getStackType(); } @Override public int getPriority() { return this.getHandler().getPriority(); } @Override public int getSlot() { return this.getHandler().getSlot(); } @Nonnull @Override public IItemList getStorageList() { if (this.hasChanged) { this.hasChanged = false; this.cachedList.resetStatus(); final IItemList ret = this.getAvailableItems(this.cachedList, IterationCounter.fetchNewId()); if (ret instanceof NetworkItemList) { for (T item : ret) { this.cachedList.add(item); } } return this.cachedList; } return this.cachedList; } @Override public T injectItems(T input, final Actionable mode, final BaseActionSource src) { if (this.locked && !isReshuffleSource(src)) { return input; } for (Iterator iterator = storageInterceptors.iterator(); iterator.hasNext();) { final IStorageInterceptor isi = iterator.next(); if (isi.canAccept(input)) { input = (T) isi.injectItems(input, mode, src); if (mode == Actionable.MODULATE && isi.shouldRemoveInterceptor(input)) iterator.remove(); if (input == null) return null; } } if (mode == Actionable.SIMULATE) { return this.getHandler().injectItems(input, mode, src); } localDepthSemaphore++; final T leftover = this.getHandler().injectItems(input, mode, src); localDepthSemaphore--; if (localDepthSemaphore == 0) { this.monitorDifference(input.copy(), leftover, false, src); } return leftover; } @Override public boolean isPrioritized(final T input) { return this.getHandler().isPrioritized(input); } @Override public void removeListener(final IMEMonitorHandlerReceiver l) { this.listeners.remove(l); } @Override public boolean validForPass(final int i) { return this.getHandler().validForPass(i); } @Override @SuppressWarnings("unchecked") public IMENetworkInventory getExternalNetworkInventory() { IMEInventoryHandler handler = this.getHandler(); if (handler instanceof IMENetworkInventorynetworkInventory) { return (IMENetworkInventory) networkInventory; } return handler.getExternalNetworkInventory(); } @Nullable @SuppressWarnings("unchecked") public IMEInventoryHandler getHandler() { return (IMEInventoryHandler) this.myGridCache.getInventoryHandler(this.stackType); } public IGrid getGrid() { return this.myGridCache.getGrid(); } private Iterator> getListeners() { return this.listeners.entrySet().iterator(); } private T monitorDifference(final T diff, final T leftOvers, final boolean extraction, final BaseActionSource src) { if (extraction) { diff.setStackSize(leftOvers == null ? 0 : -leftOvers.getStackSize()); } else if (leftOvers != null) { diff.decStackSize(leftOvers.getStackSize()); } if (diff.getStackSize() != 0) { this.postChangesToListeners(ImmutableList.of(diff), src); final ItemFlowGridCache flowCache = this.myGridCache.getGrid().getCache(ItemFlowGridCache.class); flowCache.recordFlow(diff, src); } return leftOvers; } private void notifyListenersOfChange(final Iterable> diff, final BaseActionSource src) { this.hasChanged = true; final Iterator> i = this.getListeners(); while (i.hasNext()) { final Entry o = i.next(); final IMEMonitorHandlerReceiver receiver = o.getKey(); if (receiver.isValid(o.getValue())) { receiver.postChange(this, diff, src); } else { i.remove(); } } } private void postChangesToListeners(final Iterable> changes, final BaseActionSource src) { this.postChange(true, changes, src); } protected void postChange(final boolean add, final Iterable> changes, final BaseActionSource src) { if (localDepthSemaphore > 0 || GLOBAL_DEPTH.contains(this)) { return; } GLOBAL_DEPTH.push(this); localDepthSemaphore++; this.sendEvent = true; this.notifyListenersOfChange(changes, src); for (final IAEStack changedItem : changes) { if (changedItem == null) { continue; } T difference = (T) changedItem; if (!add) { difference = (T) changedItem.copy(); difference.setStackSize(-changedItem.getStackSize()); } if (this.myGridCache.getInterestManager().containsKey(changedItem)) { final Collection list = this.myGridCache.getInterestManager().get(changedItem); if (!list.isEmpty()) { IAEStack fullStack = this.getHandler() .getAvailableItem((T) changedItem, IterationCounter.fetchNewId()); if (fullStack == null) { fullStack = changedItem.copy(); fullStack.setStackSize(0); } this.myGridCache.getInterestManager().enableTransactions(); IItemList itemList = new LazyItemList<>(this::getStorageList); for (final ItemWatcher iw : list) { iw.getHost().onStackChange(itemList, fullStack, difference, src, this.getChannel()); } this.myGridCache.getInterestManager().disableTransactions(); } } } final NetworkMonitor last = GLOBAL_DEPTH.pop(); localDepthSemaphore--; if (last != this) { throw new IllegalStateException("Invalid Access to Networked Storage API detected."); } } void forceUpdate() { this.hasChanged = true; final Iterator> i = this.getListeners(); while (i.hasNext()) { final Entry o = i.next(); final IMEMonitorHandlerReceiver receiver = o.getKey(); if (receiver.isValid(o.getValue())) { receiver.onListUpdate(); } else { i.remove(); } } } void onTick() { if (this.sendEvent) { this.sendEvent = false; this.myGridCache.getGrid().postEvent(new MENetworkStorageEvent(this, this.stackType)); } } public void addStorageInterceptor(IStorageInterceptor storageInterceptor) { this.storageInterceptors.add(storageInterceptor); } public void removeStorageInterceptor(IStorageInterceptor storageInterceptor) { this.storageInterceptors.remove(storageInterceptor); } }