2013-12-27 23:59:59 +01:00
|
|
|
package appeng.me;
|
|
|
|
|
|
|
|
import java.lang.reflect.Method;
|
2014-01-05 09:46:38 +01:00
|
|
|
import java.util.ArrayList;
|
2013-12-27 23:59:59 +01:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Hashtable;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
public class NetworkEventBus
|
|
|
|
{
|
|
|
|
|
|
|
|
class NetworkEventDone extends Throwable
|
|
|
|
{
|
|
|
|
|
|
|
|
private static final long serialVersionUID = -3079021487019171205L;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2014-01-05 09:46:38 +01:00
|
|
|
class EventMethod
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
public final Class objClass;
|
|
|
|
public final Method objMethod;
|
|
|
|
public final Class objEvent;
|
|
|
|
|
2014-01-05 09:46:38 +01:00
|
|
|
public EventMethod(Class Event, Class ObjClass, Method ObjMethod) {
|
2013-12-27 23:59:59 +01:00
|
|
|
this.objClass = ObjClass;
|
|
|
|
this.objMethod = ObjMethod;
|
|
|
|
this.objEvent = Event;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void invoke(Object obj, MENetworkEvent e) throws NetworkEventDone
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
objMethod.invoke( obj, e );
|
|
|
|
}
|
|
|
|
catch (Throwable e1)
|
|
|
|
{
|
2014-02-07 21:37:22 +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 );
|
2013-12-27 23:59:59 +01:00
|
|
|
throw new RuntimeException( e1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( e.isCanceled() )
|
|
|
|
throw new NetworkEventDone();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-05 09:46:38 +01:00
|
|
|
class MENetworkEventInfo
|
|
|
|
{
|
|
|
|
|
|
|
|
private ArrayList<EventMethod> methods = new ArrayList();
|
|
|
|
|
|
|
|
public void Add(Class Event, Class ObjClass, Method ObjMethod)
|
|
|
|
{
|
|
|
|
methods.add( new EventMethod( Event, ObjClass, ObjMethod ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public void invoke(Object obj, MENetworkEvent e) throws NetworkEventDone
|
|
|
|
{
|
|
|
|
for (EventMethod em : methods)
|
|
|
|
em.invoke( obj, e );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
private static Set<Class> readClasses = new HashSet();
|
|
|
|
private static Hashtable<Class<? extends MENetworkEvent>, Hashtable<Class, MENetworkEventInfo>> events = new Hashtable();
|
|
|
|
|
|
|
|
public void readClass(Class listAs, Class c)
|
|
|
|
{
|
|
|
|
if ( readClasses.contains( c ) )
|
|
|
|
return;
|
|
|
|
readClasses.add( c );
|
|
|
|
|
|
|
|
for (Method m : c.getMethods())
|
|
|
|
{
|
|
|
|
MENetworkEventSubscribe s = m.getAnnotation( MENetworkEventSubscribe.class );
|
|
|
|
if ( s != null )
|
|
|
|
{
|
|
|
|
Class types[] = m.getParameterTypes();
|
|
|
|
if ( types.length == 1 )
|
|
|
|
{
|
|
|
|
if ( MENetworkEvent.class.isAssignableFrom( types[0] ) )
|
|
|
|
{
|
|
|
|
|
|
|
|
Hashtable<Class, MENetworkEventInfo> classEvents = events.get( types[0] );
|
|
|
|
if ( classEvents == null )
|
|
|
|
events.put( types[0], classEvents = new Hashtable() );
|
|
|
|
|
2014-01-05 09:46:38 +01:00
|
|
|
MENetworkEventInfo thisEvent = classEvents.get( listAs );
|
|
|
|
if ( thisEvent == null )
|
|
|
|
thisEvent = new MENetworkEventInfo();
|
|
|
|
|
|
|
|
thisEvent.Add( types[0], c, m );
|
|
|
|
|
|
|
|
classEvents.put( listAs, thisEvent );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
throw new RuntimeException( "Invalid ME Network Event Subscriber, " + m.getName() + "s Parameter must extend MENetworkEvent." );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw new RuntimeException( "Invalid ME Network Event Subscriber, " + m.getName() + " must have exactly 1 parameter." );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public MENetworkEvent postEvent(Grid g, MENetworkEvent e)
|
|
|
|
{
|
|
|
|
Hashtable<Class, MENetworkEventInfo> subscribers = events.get( e.getClass() );
|
|
|
|
int x = 0;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if ( subscribers != null )
|
|
|
|
{
|
|
|
|
for (Class o : subscribers.keySet())
|
|
|
|
{
|
|
|
|
MENetworkEventInfo target = subscribers.get( o );
|
|
|
|
GridCacheWrapper cache = g.caches.get( o );
|
|
|
|
if ( cache != null )
|
|
|
|
{
|
|
|
|
x++;
|
|
|
|
target.invoke( cache.myCache, e );
|
|
|
|
}
|
|
|
|
|
|
|
|
for (IGridNode obj : g.getMachines( o ))
|
|
|
|
{
|
|
|
|
x++;
|
|
|
|
target.invoke( obj.getMachine(), e );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (NetworkEventDone done)
|
|
|
|
{
|
|
|
|
// Early out.
|
|
|
|
}
|
|
|
|
|
|
|
|
e.setVisitedObjects( x );
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
public MENetworkEvent postEventTo(Grid grid, GridNode node, MENetworkEvent e)
|
|
|
|
{
|
|
|
|
Hashtable<Class, MENetworkEventInfo> subscribers = events.get( e.getClass() );
|
|
|
|
int x = 0;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if ( subscribers != null )
|
|
|
|
{
|
|
|
|
MENetworkEventInfo target = subscribers.get( node.getMachineClass() );
|
|
|
|
if ( target != null )
|
|
|
|
{
|
|
|
|
x++;
|
|
|
|
target.invoke( node.getMachine(), e );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (NetworkEventDone done)
|
|
|
|
{
|
|
|
|
// Early out.
|
|
|
|
}
|
|
|
|
|
|
|
|
e.setVisitedObjects( x );
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|