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-09-24 02:26:27 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.PriorityQueue;
|
|
|
|
|
|
|
|
import net.minecraft.crash.CrashReport;
|
|
|
|
import net.minecraft.crash.CrashReportCategory;
|
|
|
|
import net.minecraft.util.ReportedException;
|
2014-12-29 21:59:05 +01:00
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
import appeng.api.networking.IGrid;
|
|
|
|
import appeng.api.networking.IGridHost;
|
|
|
|
import appeng.api.networking.IGridNode;
|
|
|
|
import appeng.api.networking.IGridStorage;
|
|
|
|
import appeng.api.networking.ticking.IGridTickable;
|
|
|
|
import appeng.api.networking.ticking.ITickManager;
|
|
|
|
import appeng.api.networking.ticking.TickRateModulation;
|
|
|
|
import appeng.api.networking.ticking.TickingRequest;
|
|
|
|
import appeng.me.cache.helpers.TickTracker;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2014-09-24 02:26:27 +02:00
|
|
|
public class TickManagerCache implements ITickManager
|
|
|
|
{
|
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
private final IGrid myGrid;
|
|
|
|
private final HashMap<IGridNode, TickTracker> alertable = new HashMap<IGridNode, TickTracker>();
|
|
|
|
private final HashMap<IGridNode, TickTracker> sleeping = new HashMap<IGridNode, TickTracker>();
|
|
|
|
private final HashMap<IGridNode, TickTracker> awake = new HashMap<IGridNode, TickTracker>();
|
|
|
|
private final PriorityQueue<TickTracker> upcomingTicks = new PriorityQueue<TickTracker>();
|
2015-04-03 08:54:31 +02:00
|
|
|
private long currentTick = 0;
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
public TickManagerCache( final IGrid g )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
|
|
|
this.myGrid = g;
|
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
|
|
|
public long getCurrentTick()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.currentTick;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
public long getAvgNanoTime( final IGridNode node )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
TickTracker tt = this.awake.get( node );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( tt == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
tt = this.sleeping.get( node );
|
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( tt == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-24 02:26:27 +02:00
|
|
|
return -1;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
|
|
|
|
return tt.getAvgNanos();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onUpdateTick()
|
|
|
|
{
|
|
|
|
TickTracker tt = null;
|
|
|
|
try
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.currentTick++;
|
2015-04-03 08:54:31 +02:00
|
|
|
while( !this.upcomingTicks.isEmpty() )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
tt = this.upcomingTicks.peek();
|
2015-10-08 15:42:42 +02:00
|
|
|
final int diff = (int) ( this.currentTick - tt.getLastTick() );
|
|
|
|
if( diff >= tt.getCurrentRate() )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
|
|
|
// remove tt..
|
2014-12-29 15:13:47 +01:00
|
|
|
this.upcomingTicks.poll();
|
2015-10-08 15:42:42 +02:00
|
|
|
final TickRateModulation mod = tt.getGridTickable().tickingRequest( tt.getNode(), diff );
|
2015-02-03 12:04:13 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
switch( mod )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
case FASTER:
|
2015-10-08 15:42:42 +02:00
|
|
|
tt.setRate( tt.getCurrentRate() - 2 );
|
2015-04-03 08:54:31 +02:00
|
|
|
break;
|
|
|
|
case IDLE:
|
2015-10-08 15:42:42 +02:00
|
|
|
tt.setRate( tt.getRequest().maxTickRate );
|
2015-04-03 08:54:31 +02:00
|
|
|
break;
|
|
|
|
case SAME:
|
|
|
|
break;
|
|
|
|
case SLEEP:
|
2015-10-08 15:42:42 +02:00
|
|
|
this.sleepDevice( tt.getNode() );
|
2015-04-03 08:54:31 +02:00
|
|
|
break;
|
|
|
|
case SLOWER:
|
2015-10-08 15:42:42 +02:00
|
|
|
tt.setRate( tt.getCurrentRate() + 1 );
|
2015-04-03 08:54:31 +02:00
|
|
|
break;
|
|
|
|
case URGENT:
|
|
|
|
tt.setRate( 0 );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
2015-02-03 12:04:13 +01:00
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
if( this.awake.containsKey( tt.getNode() ) )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.addToQueue( tt );
|
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
|
|
|
return; // done!
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-25 23:10:56 +02:00
|
|
|
catch( final Throwable t )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final CrashReport crashreport = CrashReport.makeCrashReport( t, "Ticking GridNode" );
|
2015-10-08 15:42:42 +02:00
|
|
|
final CrashReportCategory crashreportcategory = crashreport.makeCategory( tt.getGridTickable().getClass().getSimpleName() + " being ticked." );
|
2015-04-03 08:54:31 +02:00
|
|
|
tt.addEntityCrashInfo( crashreportcategory );
|
|
|
|
throw new ReportedException( crashreport );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
private void addToQueue( final TickTracker tt )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-10-08 15:42:42 +02:00
|
|
|
tt.setLastTick( this.currentTick );
|
2014-12-29 15:13:47 +01:00
|
|
|
this.upcomingTicks.add( tt );
|
2014-09-24 02:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public void removeNode( final IGridNode gridNode, final IGridHost machine )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
|
|
|
if( machine instanceof IGridTickable )
|
|
|
|
{
|
|
|
|
this.alertable.remove( gridNode );
|
|
|
|
this.sleeping.remove( gridNode );
|
|
|
|
this.awake.remove( gridNode );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public void addNode( final IGridNode gridNode, final IGridHost machine )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
|
|
|
if( machine instanceof IGridTickable )
|
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final TickingRequest tr = ( (IGridTickable) machine ).getTickingRequest( gridNode );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( tr != null )
|
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final TickTracker tt = new TickTracker( tr, gridNode, (IGridTickable) machine, this.currentTick, this );
|
2015-04-03 08:54:31 +02:00
|
|
|
|
|
|
|
if( tr.canBeAlerted )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
this.alertable.put( gridNode, tt );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
|
|
|
|
if( tr.isSleeping )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
this.sleeping.put( gridNode, tt );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
this.awake.put( gridNode, tt );
|
|
|
|
this.addToQueue( tt );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@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 )
|
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
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public boolean alertDevice( final IGridNode node )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final TickTracker tt = this.alertable.get( node );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( tt == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-09-24 02:26:27 +02:00
|
|
|
return false;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-09-24 02:26:27 +02:00
|
|
|
// throw new RuntimeException(
|
|
|
|
// "Invalid alerted device, this node is not marked as alertable, or part of this grid." );
|
|
|
|
|
|
|
|
// set to awake, this is for sanity.
|
2014-12-29 15:13:47 +01:00
|
|
|
this.sleeping.remove( node );
|
|
|
|
this.awake.put( node, tt );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
|
|
|
// configure sort.
|
2015-10-08 15:42:42 +02:00
|
|
|
tt.setLastTick( tt.getLastTick() - tt.getRequest().maxTickRate );
|
|
|
|
tt.setCurrentRate( tt.getRequest().minTickRate );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
|
|
|
// prevent dupes and tick build up.
|
2014-12-29 15:13:47 +01:00
|
|
|
this.upcomingTicks.remove( tt );
|
|
|
|
this.upcomingTicks.add( tt );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public boolean sleepDevice( final IGridNode node )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.awake.containsKey( node ) )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final TickTracker gt = this.awake.get( node );
|
2014-12-29 15:13:47 +01:00
|
|
|
this.awake.remove( node );
|
|
|
|
this.sleeping.put( node, gt );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public boolean wakeDevice( final IGridNode node )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.sleeping.containsKey( node ) )
|
2014-09-24 02:26:27 +02:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final TickTracker gt = this.sleeping.get( node );
|
2014-12-29 15:13:47 +01:00
|
|
|
this.sleeping.remove( node );
|
|
|
|
this.awake.put( node, gt );
|
|
|
|
this.addToQueue( gt );
|
2014-09-24 02:26:27 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|