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;
|
2014-09-24 02:26:27 +02:00
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.Map.Entry;
|
|
|
|
|
|
|
|
import appeng.api.networking.events.MENetworkStorageEvent;
|
|
|
|
import appeng.api.networking.security.BaseActionSource;
|
|
|
|
import appeng.api.storage.IMEInventoryHandler;
|
|
|
|
import appeng.api.storage.IMEMonitorHandlerReceiver;
|
|
|
|
import appeng.api.storage.MEMonitorHandler;
|
|
|
|
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
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
public class NetworkMonitor<T extends IAEStack<T>> extends MEMonitorHandler<T>
|
|
|
|
{
|
|
|
|
|
2015-04-06 00:35:42 +02:00
|
|
|
private static final Deque<NetworkMonitor<?>> DEPTH = new LinkedList<NetworkMonitor<?>>();
|
|
|
|
private final GridStorageCache myGridCache;
|
|
|
|
private final StorageChannel myChannel;
|
2014-09-24 02:26:27 +02:00
|
|
|
boolean sendEvent = false;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
public NetworkMonitor( GridStorageCache cache, StorageChannel chan )
|
|
|
|
{
|
|
|
|
super( null, chan );
|
|
|
|
this.myGridCache = cache;
|
|
|
|
this.myChannel = chan;
|
|
|
|
}
|
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
public void forceUpdate()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.hasChanged = true;
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
Iterator<Entry<IMEMonitorHandlerReceiver<T>, Object>> i = this.getListeners();
|
2015-04-03 08:54:31 +02:00
|
|
|
while( i.hasNext() )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
Entry<IMEMonitorHandlerReceiver<T>, Object> o = i.next();
|
2014-09-28 11:47:17 +02:00
|
|
|
IMEMonitorHandlerReceiver<T> receiver = o.getKey();
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( receiver.isValid( o.getValue() ) )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-28 11:47:17 +02:00
|
|
|
receiver.onListUpdate();
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
else
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-24 02:26:27 +02:00
|
|
|
i.remove();
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
public void onTick()
|
|
|
|
{
|
|
|
|
if( this.sendEvent )
|
|
|
|
{
|
|
|
|
this.sendEvent = false;
|
|
|
|
this.myGridCache.myGrid.postEvent( new MENetworkStorageEvent( this, this.myChannel ) );
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
@Override
|
|
|
|
protected IMEInventoryHandler getHandler()
|
|
|
|
{
|
|
|
|
switch( this.myChannel )
|
|
|
|
{
|
|
|
|
case ITEMS:
|
|
|
|
return this.myGridCache.getItemInventoryHandler();
|
|
|
|
case FLUIDS:
|
|
|
|
return this.myGridCache.getFluidInventoryHandler();
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2015-02-03 12:04:13 +01:00
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
protected void postChangesToListeners( Iterable<T> changes, 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-05-08 23:25:19 +02:00
|
|
|
protected void postChange( boolean add, Iterable<T> changes, BaseActionSource src )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( 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-01-01 22:13:10 +01:00
|
|
|
DEPTH.push( this );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
this.sendEvent = true;
|
|
|
|
this.notifyListenersOfChange( changes, src );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
IItemList<T> myStorageList = this.getStorageList();
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
for( 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-04-03 08:54:31 +02:00
|
|
|
( difference = changedItem.copy() ).setStackSize( -changedItem.getStackSize() );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.myGridCache.interestManager.containsKey( changedItem ) )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
Collection<ItemWatcher> list = this.myGridCache.interestManager.get( changedItem );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( !list.isEmpty() )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
IAEStack fullStack = myStorageList.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 );
|
|
|
|
}
|
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
this.myGridCache.interestManager.enableTransactions();
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
for( ItemWatcher iw : list )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
iw.getHost().onStackChange( myStorageList, fullStack, difference, src, this.getChannel() );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
this.myGridCache.interestManager.disableTransactions();
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 11:14:34 +01:00
|
|
|
final NetworkMonitor<?> last = DEPTH.pop();
|
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
|
|
|
}
|
|
|
|
}
|