Applied-Energistics-2-tiler.../me/cache/NetworkMonitor.java

114 lines
2.7 KiB
Java
Raw Normal View History

package appeng.me.cache;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map.Entry;
2014-01-05 09:43:49 +01:00
import java.util.Set;
import appeng.api.networking.events.MENetworkStorageEvent;
2014-01-05 09:43:49 +01:00
import appeng.api.networking.security.BaseActionSource;
import appeng.api.storage.IMEInventoryHandler;
import appeng.api.storage.IMEMonitorHandlerReceiver;
2014-01-01 22:46:16 +01:00
import appeng.api.storage.MEMonitorHandler;
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;
2014-01-01 22:46:16 +01:00
public class NetworkMonitor<T extends IAEStack<T>> extends MEMonitorHandler<T>
{
final private GridStorageCache myGridCache;
final private StorageChannel myChannel;
boolean sendEvent = false;
2014-01-05 09:43:49 +01:00
public void forceUpdate()
{
hasChanged = true;
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
}
public NetworkMonitor(GridStorageCache cache, StorageChannel chan) {
2014-02-21 22:04:51 +01:00
super( null, chan );
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)
{
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
}
}
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 ) );
}
}
@Override
protected IMEInventoryHandler getHandler()
{
switch (myChannel)
{
case ITEMS:
return myGridCache.getItemInventoryHandler();
case FLUIDS:
return myGridCache.getFluidInventoryHandler();
default:
}
return null;
}
}