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;
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
import java.lang.reflect.Method;
|
2014-01-05 09:46:38 +01:00
|
|
|
import java.util.ArrayList;
|
2014-11-30 04:58:08 +01:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashMap;
|
2013-12-27 23:59:59 +01:00
|
|
|
import java.util.HashSet;
|
2014-11-30 04:58:08 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2014-09-30 22:59:49 +02:00
|
|
|
import java.util.Map.Entry;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
import appeng.api.networking.IGridNode;
|
|
|
|
import appeng.api.networking.events.MENetworkEvent;
|
|
|
|
import appeng.api.networking.events.MENetworkEventSubscribe;
|
2014-02-07 21:37:22 +01:00
|
|
|
import appeng.core.AELog;
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
public class NetworkEventBus
|
|
|
|
{
|
2015-01-01 22:13:10 +01:00
|
|
|
private static final Collection<Class> READ_CLASSES = new HashSet<Class>();
|
|
|
|
private static final Map<Class<? extends MENetworkEvent>, Map<Class, MENetworkEventInfo>> EVENTS = new HashMap<Class<? extends MENetworkEvent>, Map<Class, MENetworkEventInfo>>();
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
public void readClass( Class listAs, Class c )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( READ_CLASSES.contains( c ) )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2013-12-27 23:59:59 +01:00
|
|
|
return;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-01-01 22:13:10 +01:00
|
|
|
READ_CLASSES.add( c );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-05-14 17:55:45 +02:00
|
|
|
try
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
for( Method m : c.getMethods() )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-05-14 17:55:45 +02:00
|
|
|
MENetworkEventSubscribe s = m.getAnnotation( MENetworkEventSubscribe.class );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( s != null )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
Class[] types = m.getParameterTypes();
|
2015-04-03 08:54:31 +02:00
|
|
|
if( types.length == 1 )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( MENetworkEvent.class.isAssignableFrom( types[0] ) )
|
2014-05-14 17:55:45 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
|
2015-01-01 22:13:10 +01:00
|
|
|
Map<Class, MENetworkEventInfo> classEvents = EVENTS.get( types[0] );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( classEvents == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-01-01 22:13:10 +01:00
|
|
|
EVENTS.put( types[0], classEvents = new HashMap<Class, MENetworkEventInfo>() );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-11-30 04:58:08 +01:00
|
|
|
|
2014-05-14 17:55:45 +02:00
|
|
|
MENetworkEventInfo thisEvent = classEvents.get( listAs );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( thisEvent == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-05-14 17:55:45 +02:00
|
|
|
thisEvent = new MENetworkEventInfo();
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-11-30 04:58:08 +01:00
|
|
|
|
2014-05-14 17:55:45 +02:00
|
|
|
thisEvent.Add( types[0], c, m );
|
2014-11-30 04:58:08 +01:00
|
|
|
|
2014-05-14 17:55:45 +02:00
|
|
|
classEvents.put( listAs, thisEvent );
|
|
|
|
}
|
|
|
|
else
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-03-23 10:14:35 +01:00
|
|
|
throw new IllegalStateException( "Invalid ME Network Event Subscriber, " + m.getName() + "s Parameter must extend MENetworkEvent." );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
else
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-03-23 10:14:35 +01:00
|
|
|
throw new IllegalStateException( "Invalid ME Network Event Subscriber, " + m.getName() + " must have exactly 1 parameter." );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
catch( Throwable t )
|
2014-05-14 17:55:45 +02:00
|
|
|
{
|
2015-03-23 10:14:35 +01:00
|
|
|
throw new IllegalStateException( "Error while adding " + c.getName() + " to event bus", t );
|
2014-05-14 17:55:45 +02:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
public MENetworkEvent postEvent( Grid g, MENetworkEvent e )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-01-01 22:13:10 +01:00
|
|
|
Map<Class, MENetworkEventInfo> subscribers = EVENTS.get( e.getClass() );
|
2013-12-27 23:59:59 +01:00
|
|
|
int x = 0;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( subscribers != null )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
for( Entry<Class, MENetworkEventInfo> subscriber : subscribers.entrySet() )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-09-30 22:59:49 +02:00
|
|
|
MENetworkEventInfo target = subscriber.getValue();
|
2014-11-30 04:58:08 +01:00
|
|
|
GridCacheWrapper cache = g.getCaches().get( subscriber.getKey() );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( cache != null )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
x++;
|
|
|
|
target.invoke( cache.myCache, e );
|
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
for( IGridNode obj : g.getMachines( subscriber.getKey() ) )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
x++;
|
|
|
|
target.invoke( obj.getMachine(), e );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
catch( NetworkEventDone done )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
// Early out.
|
|
|
|
}
|
|
|
|
|
|
|
|
e.setVisitedObjects( x );
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2014-11-30 04:58:08 +01:00
|
|
|
public MENetworkEvent postEventTo( Grid grid, GridNode node, MENetworkEvent e )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-01-01 22:13:10 +01:00
|
|
|
Map<Class, MENetworkEventInfo> subscribers = EVENTS.get( e.getClass() );
|
2013-12-27 23:59:59 +01:00
|
|
|
int x = 0;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( subscribers != null )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
MENetworkEventInfo target = subscribers.get( node.getMachineClass() );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( target != null )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
x++;
|
|
|
|
target.invoke( node.getMachine(), e );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
catch( NetworkEventDone done )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
// Early out.
|
|
|
|
}
|
|
|
|
|
|
|
|
e.setVisitedObjects( x );
|
|
|
|
return e;
|
|
|
|
}
|
2014-11-30 04:58:08 +01:00
|
|
|
|
|
|
|
static class NetworkEventDone extends Throwable
|
|
|
|
{
|
|
|
|
|
|
|
|
private static final long serialVersionUID = -3079021487019171205L;
|
|
|
|
}
|
|
|
|
|
|
|
|
class EventMethod
|
|
|
|
{
|
|
|
|
|
|
|
|
public final Class objClass;
|
|
|
|
public final Method objMethod;
|
|
|
|
public final Class objEvent;
|
|
|
|
|
|
|
|
public EventMethod( Class Event, Class ObjClass, Method ObjMethod )
|
|
|
|
{
|
|
|
|
this.objClass = ObjClass;
|
|
|
|
this.objMethod = ObjMethod;
|
|
|
|
this.objEvent = Event;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void invoke( Object obj, MENetworkEvent e ) throws NetworkEventDone
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
this.objMethod.invoke( obj, e );
|
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
catch( Throwable e1 )
|
2014-11-30 04:58:08 +01:00
|
|
|
{
|
|
|
|
AELog.severe( "[AppEng] Network Event caused exception:" );
|
|
|
|
AELog.severe( "Offending Class: " + obj.getClass().getName() );
|
|
|
|
AELog.severe( "Offending Object: " + obj.toString() );
|
|
|
|
AELog.error( e1 );
|
2015-03-23 10:14:35 +01:00
|
|
|
throw new IllegalStateException( e1 );
|
2014-11-30 04:58:08 +01:00
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( e.isCanceled() )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
throw new NetworkEventDone();
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-11-30 04:58:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MENetworkEventInfo
|
|
|
|
{
|
|
|
|
|
|
|
|
private final List<EventMethod> methods = new ArrayList<EventMethod>();
|
|
|
|
|
|
|
|
public void Add( Class Event, Class ObjClass, Method ObjMethod )
|
|
|
|
{
|
|
|
|
this.methods.add( new EventMethod( Event, ObjClass, ObjMethod ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public void invoke( Object obj, MENetworkEvent e ) throws NetworkEventDone
|
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
for( EventMethod em : this.methods )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-11-30 04:58:08 +01:00
|
|
|
em.invoke( obj, e );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-11-30 04:58:08 +01:00
|
|
|
}
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|