2014-11-14 12:02:52 +01:00
|
|
|
/*
|
|
|
|
* 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 <http://www.gnu.org/licenses/lgpl>.
|
|
|
|
*/
|
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
package appeng.me.cache;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2014-10-05 15:38:56 +02:00
|
|
|
import java.util.Collection;
|
2015-03-26 11:13:24 +01:00
|
|
|
import java.util.Deque;
|
2015-12-04 00:31:25 +01:00
|
|
|
import java.util.HashMap;
|
2014-09-24 02:26:27 +02:00
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.LinkedList;
|
2015-12-04 00:31:25 +01:00
|
|
|
import java.util.Map;
|
2014-09-24 02:26:27 +02:00
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
2015-12-04 00:31:25 +01:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
|
|
|
import com.google.common.collect.ImmutableList;
|
|
|
|
|
|
|
|
import appeng.api.config.AccessRestriction;
|
|
|
|
import appeng.api.config.Actionable;
|
2014-09-24 02:26:27 +02:00
|
|
|
import appeng.api.networking.events.MENetworkStorageEvent;
|
|
|
|
import appeng.api.networking.security.BaseActionSource;
|
|
|
|
import appeng.api.storage.IMEInventoryHandler;
|
2015-12-04 00:31:25 +01:00
|
|
|
import appeng.api.storage.IMEMonitor;
|
2014-09-24 02:26:27 +02:00
|
|
|
import appeng.api.storage.IMEMonitorHandlerReceiver;
|
|
|
|
import appeng.api.storage.StorageChannel;
|
|
|
|
import appeng.api.storage.data.IAEStack;
|
|
|
|
import appeng.api.storage.data.IItemList;
|
|
|
|
import appeng.me.storage.ItemWatcher;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2015-12-04 00:31:25 +01:00
|
|
|
public class NetworkMonitor<T extends IAEStack<T>> implements IMEMonitor<T>
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-12-04 00:31:25 +01:00
|
|
|
@Nonnull
|
|
|
|
private static final Deque<NetworkMonitor<?>> GLOBAL_DEPTH = new LinkedList<NetworkMonitor<?>>();
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-12-04 00:31:25 +01:00
|
|
|
@Nonnull
|
2015-04-06 00:35:42 +02:00
|
|
|
private final GridStorageCache myGridCache;
|
2015-12-04 00:31:25 +01:00
|
|
|
@Nonnull
|
2015-04-06 00:35:42 +02:00
|
|
|
private final StorageChannel myChannel;
|
2015-12-04 00:31:25 +01:00
|
|
|
@Nonnull
|
|
|
|
private final IItemList<T> cachedList;
|
|
|
|
@Nonnull
|
|
|
|
private final Map<IMEMonitorHandlerReceiver<T>, Object> listeners = new HashMap<IMEMonitorHandlerReceiver<T>, Object>();
|
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
private boolean sendEvent = false;
|
2015-12-04 00:31:25 +01:00
|
|
|
private boolean hasChanged = false;
|
|
|
|
private int localDepthSemaphore = 0;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
public NetworkMonitor( final GridStorageCache cache, final StorageChannel chan )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
|
|
|
this.myGridCache = cache;
|
|
|
|
this.myChannel = chan;
|
2015-12-04 00:31:25 +01:00
|
|
|
this.cachedList = (IItemList<T>) chan.createList();
|
2015-04-03 08:54:31 +02:00
|
|
|
}
|
|
|
|
|
2015-12-04 00:31:25 +01:00
|
|
|
@Override
|
|
|
|
public void addListener( final IMEMonitorHandlerReceiver<T> l, final Object verificationToken )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-12-04 00:31:25 +01:00
|
|
|
this.listeners.put( l, verificationToken );
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-12-04 00:31:25 +01:00
|
|
|
@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( mode == Actionable.SIMULATE )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-12-04 00:31:25 +01:00
|
|
|
return this.getHandler().extractItems( request, mode, src );
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-12-04 00:31:25 +01:00
|
|
|
localDepthSemaphore++;
|
|
|
|
final T leftover = this.getHandler().extractItems( request, mode, src );
|
|
|
|
localDepthSemaphore--;
|
|
|
|
|
|
|
|
if( localDepthSemaphore == 0 )
|
|
|
|
{
|
|
|
|
this.monitorDifference( request.copy(), leftover, true, src );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
2015-12-04 00:31:25 +01:00
|
|
|
|
|
|
|
return leftover;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2015-12-04 00:31:25 +01:00
|
|
|
@Override
|
|
|
|
public AccessRestriction getAccess()
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
2015-12-04 00:31:25 +01:00
|
|
|
return this.getHandler().getAccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IItemList<T> getAvailableItems( final IItemList out )
|
|
|
|
{
|
|
|
|
return this.getHandler().getAvailableItems( out );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public StorageChannel getChannel()
|
|
|
|
{
|
|
|
|
return this.getHandler().getChannel();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getPriority()
|
|
|
|
{
|
|
|
|
return this.getHandler().getPriority();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getSlot()
|
|
|
|
{
|
|
|
|
return this.getHandler().getSlot();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nonnull
|
|
|
|
@Override
|
|
|
|
public IItemList<T> getStorageList()
|
|
|
|
{
|
|
|
|
if( this.hasChanged )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
2015-12-04 00:31:25 +01:00
|
|
|
this.hasChanged = false;
|
|
|
|
this.cachedList.resetStatus();
|
|
|
|
return this.getAvailableItems( this.cachedList );
|
2015-04-03 08:54:31 +02:00
|
|
|
}
|
2015-12-04 00:31:25 +01:00
|
|
|
|
|
|
|
return this.cachedList;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
@Override
|
2015-12-04 00:31:25 +01:00
|
|
|
public T injectItems( final T input, final Actionable mode, final BaseActionSource src )
|
|
|
|
{
|
|
|
|
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<T> l )
|
|
|
|
{
|
|
|
|
this.listeners.remove( l );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean validForPass( final int i )
|
|
|
|
{
|
|
|
|
return this.getHandler().validForPass( i );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@SuppressWarnings( "unchecked" )
|
|
|
|
private IMEInventoryHandler<T> getHandler()
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
|
|
|
switch( this.myChannel )
|
|
|
|
{
|
|
|
|
case ITEMS:
|
2015-12-04 00:31:25 +01:00
|
|
|
return (IMEInventoryHandler<T>) this.myGridCache.getItemInventoryHandler();
|
2015-04-03 08:54:31 +02:00
|
|
|
case FLUIDS:
|
2015-12-04 00:31:25 +01:00
|
|
|
return (IMEInventoryHandler<T>) this.myGridCache.getFluidInventoryHandler();
|
2015-04-03 08:54:31 +02:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2015-02-03 12:04:13 +01:00
|
|
|
|
2015-12-04 00:31:25 +01:00
|
|
|
private Iterator<Entry<IMEMonitorHandlerReceiver<T>, Object>> getListeners()
|
|
|
|
{
|
|
|
|
return this.listeners.entrySet().iterator();
|
|
|
|
}
|
|
|
|
|
|
|
|
private T monitorDifference( final IAEStack original, final T leftOvers, final boolean extraction, final BaseActionSource src )
|
|
|
|
{
|
|
|
|
final T diff = (T) original.copy();
|
|
|
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
return leftOvers;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void notifyListenersOfChange( final Iterable<T> diff, final BaseActionSource src )
|
|
|
|
{
|
|
|
|
final Iterator<Entry<IMEMonitorHandlerReceiver<T>, Object>> i = this.getListeners();
|
|
|
|
|
|
|
|
while( i.hasNext() )
|
|
|
|
{
|
|
|
|
final Entry<IMEMonitorHandlerReceiver<T>, Object> o = i.next();
|
|
|
|
final IMEMonitorHandlerReceiver<T> receiver = o.getKey();
|
|
|
|
if( receiver.isValid( o.getValue() ) )
|
|
|
|
{
|
|
|
|
receiver.postChange( this, diff, src );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
i.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void postChangesToListeners( final Iterable<T> changes, final BaseActionSource src )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.postChange( true, changes, src );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
2015-02-03 12:04:13 +01:00
|
|
|
|
2015-12-04 00:31:25 +01:00
|
|
|
private void updateCache( Iterable<T> changes, boolean add )
|
|
|
|
{
|
|
|
|
for( final T changedItem : changes )
|
|
|
|
{
|
|
|
|
T difference = changedItem;
|
|
|
|
|
|
|
|
if( !add && changedItem != null )
|
|
|
|
{
|
|
|
|
difference = changedItem.copy();
|
|
|
|
difference.setStackSize( -changedItem.getStackSize() );
|
|
|
|
}
|
|
|
|
|
|
|
|
this.cachedList.add( difference );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
protected void postChange( final boolean add, final Iterable<T> changes, final BaseActionSource src )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-12-04 00:31:25 +01:00
|
|
|
if( localDepthSemaphore > 0 || GLOBAL_DEPTH.contains( this ) )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-24 02:26:27 +02:00
|
|
|
return;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-12-04 00:31:25 +01:00
|
|
|
GLOBAL_DEPTH.push( this );
|
|
|
|
localDepthSemaphore++;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
this.sendEvent = true;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-12-04 00:31:25 +01:00
|
|
|
this.updateCache( changes, add );
|
|
|
|
this.notifyListenersOfChange( changes, src );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
for( final T changedItem : changes )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
T difference = changedItem;
|
|
|
|
|
2015-05-08 23:25:19 +02:00
|
|
|
if( !add && changedItem != null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-12-04 00:31:25 +01:00
|
|
|
difference = changedItem.copy();
|
|
|
|
difference.setStackSize( -changedItem.getStackSize() );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
if( this.myGridCache.getInterestManager().containsKey( changedItem ) )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-10-08 15:42:42 +02:00
|
|
|
final Collection<ItemWatcher> list = this.myGridCache.getInterestManager().get( changedItem );
|
2015-12-04 00:31:25 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( !list.isEmpty() )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-12-04 00:31:25 +01:00
|
|
|
IAEStack fullStack = this.getStorageList().findPrecise( changedItem );
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( fullStack == null )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
fullStack = changedItem.copy();
|
|
|
|
fullStack.setStackSize( 0 );
|
|
|
|
}
|
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
this.myGridCache.getInterestManager().enableTransactions();
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
for( final ItemWatcher iw : list )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-12-04 00:31:25 +01:00
|
|
|
iw.getHost().onStackChange( this.getStorageList(), fullStack, difference, src, this.getChannel() );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
this.myGridCache.getInterestManager().disableTransactions();
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-04 00:31:25 +01:00
|
|
|
final NetworkMonitor<?> last = GLOBAL_DEPTH.pop();
|
|
|
|
localDepthSemaphore--;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( last != this )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-03-23 10:14:35 +01:00
|
|
|
throw new IllegalStateException( "Invalid Access to Networked Storage API detected." );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
2015-12-04 00:31:25 +01:00
|
|
|
|
|
|
|
void forceUpdate( boolean reset )
|
|
|
|
{
|
|
|
|
if( reset )
|
|
|
|
{
|
|
|
|
this.hasChanged = true;
|
|
|
|
this.cachedList.resetStatus();
|
|
|
|
this.getAvailableItems( this.cachedList );
|
|
|
|
}
|
|
|
|
|
|
|
|
final Iterator<Entry<IMEMonitorHandlerReceiver<T>, Object>> i = this.getListeners();
|
|
|
|
|
|
|
|
while( i.hasNext() )
|
|
|
|
{
|
|
|
|
final Entry<IMEMonitorHandlerReceiver<T>, Object> o = i.next();
|
|
|
|
final IMEMonitorHandlerReceiver<T> 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.myChannel ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|