2013-12-27 23:59:59 +01:00
|
|
|
package appeng.me.cache;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
2014-01-22 05:52:33 +01:00
|
|
|
import appeng.api.networking.GridFlags;
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.networking.IGrid;
|
|
|
|
import appeng.api.networking.IGridCache;
|
|
|
|
import appeng.api.networking.IGridHost;
|
|
|
|
import appeng.api.networking.IGridNode;
|
|
|
|
import appeng.api.networking.IGridStorage;
|
2014-01-22 05:52:33 +01:00
|
|
|
import appeng.api.networking.events.MENetworkBootingStatusChange;
|
|
|
|
import appeng.api.networking.events.MENetworkEventSubscribe;
|
|
|
|
import appeng.api.networking.events.MENetworkPowerStatusChange;
|
|
|
|
import appeng.api.networking.ticking.ITickManager;
|
2014-01-20 17:41:37 +01:00
|
|
|
import appeng.me.cache.helpers.TunnelCollection;
|
|
|
|
import appeng.parts.p2p.PartP2PTunnel;
|
2014-01-22 05:52:33 +01:00
|
|
|
import appeng.parts.p2p.PartP2PTunnelME;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
import com.google.common.collect.LinkedHashMultimap;
|
|
|
|
import com.google.common.collect.Multimap;
|
|
|
|
|
|
|
|
public class P2PCache implements IGridCache
|
|
|
|
{
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
final private HashMap<Long, PartP2PTunnel> inputs = new HashMap();
|
|
|
|
final private Multimap<Long, PartP2PTunnel> outputs = LinkedHashMultimap.create();
|
|
|
|
final private TunnelCollection NullColl = new TunnelCollection<PartP2PTunnel>( null, null );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
final IGrid myGrid;
|
|
|
|
|
|
|
|
public P2PCache(IGrid g) {
|
|
|
|
myGrid = g;
|
|
|
|
}
|
|
|
|
|
2014-01-22 05:52:33 +01:00
|
|
|
@MENetworkEventSubscribe
|
2014-09-28 11:47:17 +02:00
|
|
|
public void bootComplete(MENetworkBootingStatusChange bootStatus)
|
2014-01-22 05:52:33 +01:00
|
|
|
{
|
|
|
|
ITickManager tm = myGrid.getCache( ITickManager.class );
|
|
|
|
for (PartP2PTunnel me : inputs.values())
|
|
|
|
{
|
|
|
|
if ( me instanceof PartP2PTunnelME )
|
|
|
|
tm.wakeDevice( me.getGridNode() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@MENetworkEventSubscribe
|
|
|
|
public void bootComplete(MENetworkPowerStatusChange power)
|
|
|
|
{
|
|
|
|
ITickManager tm = myGrid.getCache( ITickManager.class );
|
|
|
|
for (PartP2PTunnel me : inputs.values())
|
|
|
|
{
|
|
|
|
if ( me instanceof PartP2PTunnelME )
|
|
|
|
tm.wakeDevice( me.getGridNode() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-04 17:11:29 +01:00
|
|
|
public void onUpdateTick()
|
2014-01-22 05:52:33 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
public void updateFreq(PartP2PTunnel t, long NewFreq)
|
|
|
|
{
|
2014-07-15 06:47:07 +02:00
|
|
|
if ( outputs.containsValue( t ) )
|
|
|
|
outputs.remove( t.freq, t );
|
|
|
|
|
|
|
|
if ( inputs.containsValue( t ) )
|
|
|
|
inputs.remove( t.freq );
|
2014-01-20 17:41:37 +01:00
|
|
|
|
|
|
|
t.freq = NewFreq;
|
|
|
|
|
|
|
|
if ( t.output )
|
|
|
|
outputs.put( t.freq, t );
|
|
|
|
else
|
|
|
|
inputs.put( t.freq, t );
|
|
|
|
|
2014-02-04 17:11:29 +01:00
|
|
|
// AELog.info( "update-" + (t.output ? "output: " : "input: ") + t.freq
|
|
|
|
// );
|
2014-06-09 01:10:19 +02:00
|
|
|
updateTunnel( t.freq, t.output, true );
|
|
|
|
updateTunnel( t.freq, !t.output, true );
|
2014-01-20 17:41:37 +01:00
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
@Override
|
2014-02-04 17:11:29 +01:00
|
|
|
public void addNode(IGridNode node, IGridHost machine)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
if ( machine instanceof PartP2PTunnel )
|
2014-01-20 17:41:37 +01:00
|
|
|
{
|
2014-01-22 05:52:33 +01:00
|
|
|
if ( machine instanceof PartP2PTunnelME )
|
|
|
|
{
|
2014-01-31 22:12:15 +01:00
|
|
|
if ( !node.hasFlag( GridFlags.REQUIRE_CHANNEL ) )
|
2014-01-22 05:52:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
PartP2PTunnel t = (PartP2PTunnel) machine;
|
2014-02-04 17:11:29 +01:00
|
|
|
// AELog.info( "add-" + (t.output ? "output: " : "input: ") + t.freq
|
|
|
|
// );
|
2014-01-20 17:41:37 +01:00
|
|
|
|
|
|
|
if ( t.output )
|
|
|
|
outputs.put( t.freq, t );
|
|
|
|
else
|
|
|
|
inputs.put( t.freq, t );
|
|
|
|
|
2014-06-09 01:10:19 +02:00
|
|
|
updateTunnel( t.freq, !t.output, false );
|
2014-01-20 17:41:37 +01:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-02-04 17:11:29 +01:00
|
|
|
public void removeNode(IGridNode node, IGridHost machine)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
if ( machine instanceof PartP2PTunnel )
|
2014-01-20 17:41:37 +01:00
|
|
|
{
|
2014-01-22 05:52:33 +01:00
|
|
|
if ( machine instanceof PartP2PTunnelME )
|
|
|
|
{
|
2014-01-31 22:12:15 +01:00
|
|
|
if ( !node.hasFlag( GridFlags.REQUIRE_CHANNEL ) )
|
2014-01-22 05:52:33 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
PartP2PTunnel t = (PartP2PTunnel) machine;
|
2014-02-04 17:11:29 +01:00
|
|
|
// AELog.info( "rmv-" + (t.output ? "output: " : "input: ") + t.freq
|
|
|
|
// );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
if ( t.output )
|
|
|
|
outputs.remove( t.freq, t );
|
|
|
|
else
|
|
|
|
inputs.remove( t.freq );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-06-09 01:10:19 +02:00
|
|
|
updateTunnel( t.freq, !t.output, false );
|
2014-01-20 17:41:37 +01:00
|
|
|
}
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-06-09 01:10:19 +02:00
|
|
|
private void updateTunnel(long freq, boolean updateOutputs, boolean configChange)
|
2014-01-20 17:41:37 +01:00
|
|
|
{
|
2014-01-26 07:49:06 +01:00
|
|
|
for (PartP2PTunnel p : outputs.get( freq ))
|
2014-06-09 01:10:19 +02:00
|
|
|
{
|
|
|
|
if ( configChange )
|
|
|
|
p.onTunnelConfigChange();
|
|
|
|
p.onTunnelNetworkChange();
|
|
|
|
}
|
2014-01-26 07:49:06 +01:00
|
|
|
|
|
|
|
PartP2PTunnel in = inputs.get( freq );
|
|
|
|
if ( in != null )
|
2014-06-09 01:10:19 +02:00
|
|
|
{
|
|
|
|
if ( configChange )
|
|
|
|
in.onTunnelConfigChange();
|
|
|
|
in.onTunnelNetworkChange();
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
public TunnelCollection<PartP2PTunnel> getOutputs(long freq, Class<? extends PartP2PTunnel> c)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-01-20 17:41:37 +01:00
|
|
|
PartP2PTunnel in = inputs.get( freq );
|
|
|
|
if ( in == null )
|
|
|
|
return NullColl;
|
|
|
|
|
2014-07-15 06:47:07 +02:00
|
|
|
TunnelCollection<PartP2PTunnel> out = inputs.get( freq ).getCollection( outputs.get( freq ), c );
|
|
|
|
if ( out == null )
|
|
|
|
return NullColl;
|
|
|
|
|
|
|
|
return out;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public PartP2PTunnel getInput(long freq)
|
|
|
|
{
|
|
|
|
return inputs.get( freq );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSplit(IGridStorage storageB)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onJoin(IGridStorage storageB)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void populateGridStorage(IGridStorage storage)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|