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
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
void readClass( final Class listAs, final 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-09-25 23:10:56 +02:00
|
|
|
for( final Method m : c.getMethods() )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final 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
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final 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-09-25 23:10:56 +02:00
|
|
|
catch( final 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
|
|
|
}
|
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
MENetworkEvent postEvent( final Grid g, final MENetworkEvent e )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final 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-09-25 23:10:56 +02:00
|
|
|
for( final Entry<Class, MENetworkEventInfo> subscriber : subscribers.entrySet() )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final MENetworkEventInfo target = subscriber.getValue();
|
|
|
|
final 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++;
|
2015-10-08 15:42:42 +02:00
|
|
|
target.invoke( cache.getCache(), e );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
for( final IGridNode obj : g.getMachines( subscriber.getKey() ) )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
x++;
|
|
|
|
target.invoke( obj.getMachine(), e );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-25 23:10:56 +02:00
|
|
|
catch( final NetworkEventDone done )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
// Early out.
|
|
|
|
}
|
|
|
|
|
|
|
|
e.setVisitedObjects( x );
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
MENetworkEvent postEventTo( final Grid grid, final GridNode node, final MENetworkEvent e )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final 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-09-25 23:10:56 +02:00
|
|
|
final 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-09-25 23:10:56 +02:00
|
|
|
catch( final 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
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
private static class NetworkEventDone extends Throwable
|
2014-11-30 04:58:08 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
private static final long serialVersionUID = -3079021487019171205L;
|
|
|
|
}
|
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
private class EventMethod
|
2014-11-30 04:58:08 +01:00
|
|
|
{
|
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
private final Class objClass;
|
|
|
|
private final Method objMethod;
|
|
|
|
private final Class objEvent;
|
2014-11-30 04:58:08 +01:00
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
public EventMethod( final Class Event, final Class ObjClass, final Method ObjMethod )
|
2014-11-30 04:58:08 +01:00
|
|
|
{
|
|
|
|
this.objClass = ObjClass;
|
|
|
|
this.objMethod = ObjMethod;
|
|
|
|
this.objEvent = Event;
|
|
|
|
}
|
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
private void invoke( final Object obj, final MENetworkEvent e ) throws NetworkEventDone
|
2014-11-30 04:58:08 +01:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
this.objMethod.invoke( obj, e );
|
|
|
|
}
|
2015-09-25 23:10:56 +02:00
|
|
|
catch( final 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
private class MENetworkEventInfo
|
2014-11-30 04:58:08 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
private final List<EventMethod> methods = new ArrayList<EventMethod>();
|
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
private void Add( final Class Event, final Class ObjClass, final Method ObjMethod )
|
2014-11-30 04:58:08 +01:00
|
|
|
{
|
|
|
|
this.methods.add( new EventMethod( Event, ObjClass, ObjMethod ) );
|
|
|
|
}
|
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
private void invoke( final Object obj, final MENetworkEvent e ) throws NetworkEventDone
|
2014-11-30 04:58:08 +01:00
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
for( final 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
|
|
|
}
|