2013-12-27 23:59:59 +01:00
|
|
|
package appeng.me.cache;
|
|
|
|
|
2014-03-28 05:31:06 +01:00
|
|
|
import java.util.Iterator;
|
2013-12-27 23:59:59 +01:00
|
|
|
import java.util.LinkedList;
|
2014-03-28 05:31:06 +01:00
|
|
|
import java.util.Map.Entry;
|
2014-01-05 09:43:49 +01:00
|
|
|
import java.util.Set;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
import appeng.api.networking.events.MENetworkStorageEvent;
|
2014-01-05 09:43:49 +01:00
|
|
|
import appeng.api.networking.security.BaseActionSource;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.storage.IMEInventoryHandler;
|
2014-03-28 05:31:06 +01:00
|
|
|
import appeng.api.storage.IMEMonitorHandlerReceiver;
|
2014-01-01 22:46:16 +01:00
|
|
|
import appeng.api.storage.MEMonitorHandler;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.storage.StorageChannel;
|
|
|
|
import appeng.api.storage.data.IAEStack;
|
2014-01-05 09:43:49 +01:00
|
|
|
import appeng.api.storage.data.IItemList;
|
|
|
|
import appeng.me.storage.ItemWatcher;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-01-01 22:46:16 +01:00
|
|
|
public class NetworkMonitor<T extends IAEStack<T>> extends MEMonitorHandler<T>
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
final private GridStorageCache myGridCache;
|
|
|
|
final private StorageChannel myChannel;
|
|
|
|
|
|
|
|
boolean sendEvent = false;
|
|
|
|
|
2014-01-05 09:43:49 +01:00
|
|
|
public void forceUpdate()
|
|
|
|
{
|
|
|
|
hasChanged = true;
|
2014-03-28 05:31:06 +01:00
|
|
|
|
|
|
|
Iterator<Entry<IMEMonitorHandlerReceiver<T>, Object>> i = getListeners();
|
|
|
|
while (i.hasNext())
|
|
|
|
{
|
|
|
|
Entry<IMEMonitorHandlerReceiver<T>, Object> o = i.next();
|
|
|
|
IMEMonitorHandlerReceiver<T> recv = o.getKey();
|
|
|
|
|
|
|
|
if ( recv.isValid( o.getValue() ) )
|
|
|
|
recv.onListUpdate();
|
|
|
|
else
|
|
|
|
i.remove();
|
|
|
|
}
|
2014-01-05 09:43:49 +01:00
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
public NetworkMonitor(GridStorageCache cache, StorageChannel chan) {
|
2014-02-21 22:04:51 +01:00
|
|
|
super( null, chan );
|
2013-12-27 23:59:59 +01:00
|
|
|
myGridCache = cache;
|
|
|
|
myChannel = chan;
|
|
|
|
}
|
|
|
|
|
|
|
|
final static public LinkedList depth = new LinkedList();
|
|
|
|
|
|
|
|
@Override
|
2014-01-05 09:43:49 +01:00
|
|
|
protected void postChange(T diff, BaseActionSource src)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
if ( depth.contains( this ) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
depth.push( this );
|
|
|
|
|
|
|
|
sendEvent = true;
|
2014-01-05 09:43:49 +01:00
|
|
|
super.postChange( diff, src );
|
|
|
|
|
2014-06-10 19:16:14 +02:00
|
|
|
if ( myGridCache.interestManager.containsKey( diff ) )
|
2014-01-05 09:43:49 +01:00
|
|
|
{
|
2014-06-10 19:16:14 +02:00
|
|
|
Set<ItemWatcher> list = myGridCache.interestManager.get( diff );
|
2014-01-05 09:43:49 +01:00
|
|
|
if ( !list.isEmpty() )
|
|
|
|
{
|
|
|
|
IItemList<T> myStorageList = getStorageList();
|
|
|
|
|
|
|
|
IAEStack fullStack = myStorageList.findPrecise( diff );
|
|
|
|
if ( fullStack == null )
|
|
|
|
{
|
|
|
|
fullStack = diff.copy();
|
|
|
|
fullStack.setStackSize( 0 );
|
|
|
|
}
|
2014-06-10 19:16:14 +02:00
|
|
|
|
|
|
|
myGridCache.interestManager.enableTransactions();
|
|
|
|
|
2014-01-05 09:43:49 +01:00
|
|
|
for (ItemWatcher iw : list)
|
|
|
|
iw.getHost().onStackChange( myStorageList, fullStack, diff, src, getChannel() );
|
2014-06-10 19:16:14 +02:00
|
|
|
|
|
|
|
myGridCache.interestManager.disableTransactions();
|
2014-01-05 09:43:49 +01:00
|
|
|
}
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
Object last = depth.pop();
|
|
|
|
if ( last != this )
|
|
|
|
throw new RuntimeException( "Invalid Access to Networked Storage API detected." );
|
|
|
|
}
|
|
|
|
|
|
|
|
public void onTick()
|
|
|
|
{
|
|
|
|
if ( sendEvent )
|
|
|
|
{
|
|
|
|
sendEvent = false;
|
2014-01-20 17:41:37 +01:00
|
|
|
myGridCache.myGrid.postEvent( new MENetworkStorageEvent( this, myChannel ) );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected IMEInventoryHandler getHandler()
|
|
|
|
{
|
|
|
|
switch (myChannel)
|
|
|
|
{
|
|
|
|
case ITEMS:
|
|
|
|
return myGridCache.getItemInventoryHandler();
|
|
|
|
case FLUIDS:
|
|
|
|
return myGridCache.getFluidInventoryHandler();
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|