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