pick 97420a31d The big reformat of 2020
This commit is contained in:
@@ -18,7 +18,6 @@
|
||||
|
||||
package appeng.me;
|
||||
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@@ -34,196 +33,153 @@ import appeng.api.networking.events.MENetworkEvent;
|
||||
import appeng.api.networking.events.MENetworkEventSubscribe;
|
||||
import appeng.core.AELog;
|
||||
|
||||
public class NetworkEventBus {
|
||||
private static final Collection<Class> READ_CLASSES = new HashSet<>();
|
||||
private static final Map<Class<? extends MENetworkEvent>, Map<Class, MENetworkEventInfo>> EVENTS = new HashMap<>();
|
||||
|
||||
public class NetworkEventBus
|
||||
{
|
||||
private static final Collection<Class> READ_CLASSES = new HashSet<>();
|
||||
private static final Map<Class<? extends MENetworkEvent>, Map<Class, MENetworkEventInfo>> EVENTS = new HashMap<>();
|
||||
void readClass(final Class listAs, final Class c) {
|
||||
if (READ_CLASSES.contains(c)) {
|
||||
return;
|
||||
}
|
||||
READ_CLASSES.add(c);
|
||||
|
||||
void readClass( final Class listAs, final Class c )
|
||||
{
|
||||
if( READ_CLASSES.contains( c ) )
|
||||
{
|
||||
return;
|
||||
}
|
||||
READ_CLASSES.add( c );
|
||||
try {
|
||||
for (final Method m : c.getMethods()) {
|
||||
final MENetworkEventSubscribe s = m.getAnnotation(MENetworkEventSubscribe.class);
|
||||
if (s != null) {
|
||||
final Class[] types = m.getParameterTypes();
|
||||
if (types.length == 1) {
|
||||
if (MENetworkEvent.class.isAssignableFrom(types[0])) {
|
||||
|
||||
try
|
||||
{
|
||||
for( final Method m : c.getMethods() )
|
||||
{
|
||||
final MENetworkEventSubscribe s = m.getAnnotation( MENetworkEventSubscribe.class );
|
||||
if( s != null )
|
||||
{
|
||||
final Class[] types = m.getParameterTypes();
|
||||
if( types.length == 1 )
|
||||
{
|
||||
if( MENetworkEvent.class.isAssignableFrom( types[0] ) )
|
||||
{
|
||||
Map<Class, MENetworkEventInfo> classEvents = EVENTS.get(types[0]);
|
||||
if (classEvents == null) {
|
||||
EVENTS.put(types[0], classEvents = new HashMap<>());
|
||||
}
|
||||
|
||||
Map<Class, MENetworkEventInfo> classEvents = EVENTS.get( types[0] );
|
||||
if( classEvents == null )
|
||||
{
|
||||
EVENTS.put( types[0], classEvents = new HashMap<>() );
|
||||
}
|
||||
MENetworkEventInfo thisEvent = classEvents.get(listAs);
|
||||
if (thisEvent == null) {
|
||||
thisEvent = new MENetworkEventInfo();
|
||||
}
|
||||
|
||||
MENetworkEventInfo thisEvent = classEvents.get( listAs );
|
||||
if( thisEvent == null )
|
||||
{
|
||||
thisEvent = new MENetworkEventInfo();
|
||||
}
|
||||
thisEvent.Add(types[0], c, m);
|
||||
|
||||
thisEvent.Add( types[0], c, m );
|
||||
classEvents.put(listAs, thisEvent);
|
||||
} else {
|
||||
throw new IllegalStateException("Invalid ME Network Event Subscriber, " + m.getName()
|
||||
+ "s Parameter must extend MENetworkEvent.");
|
||||
}
|
||||
} else {
|
||||
throw new IllegalStateException("Invalid ME Network Event Subscriber, " + m.getName()
|
||||
+ " must have exactly 1 parameter.");
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (final Throwable t) {
|
||||
throw new IllegalStateException("Error while adding " + c.getName() + " to event bus", t);
|
||||
}
|
||||
}
|
||||
|
||||
classEvents.put( listAs, thisEvent );
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalStateException( "Invalid ME Network Event Subscriber, " + m
|
||||
.getName() + "s Parameter must extend MENetworkEvent." );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalStateException( "Invalid ME Network Event Subscriber, " + m.getName() + " must have exactly 1 parameter." );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch( final Throwable t )
|
||||
{
|
||||
throw new IllegalStateException( "Error while adding " + c.getName() + " to event bus", t );
|
||||
}
|
||||
}
|
||||
MENetworkEvent postEvent(final Grid g, final MENetworkEvent e) {
|
||||
final Map<Class, MENetworkEventInfo> subscribers = EVENTS.get(e.getClass());
|
||||
int x = 0;
|
||||
|
||||
MENetworkEvent postEvent( final Grid g, final MENetworkEvent e )
|
||||
{
|
||||
final Map<Class, MENetworkEventInfo> subscribers = EVENTS.get( e.getClass() );
|
||||
int x = 0;
|
||||
try {
|
||||
if (subscribers != null) {
|
||||
for (final Entry<Class, MENetworkEventInfo> subscriber : subscribers.entrySet()) {
|
||||
final MENetworkEventInfo target = subscriber.getValue();
|
||||
final GridCacheWrapper cache = g.getCaches().get(subscriber.getKey());
|
||||
if (cache != null) {
|
||||
x++;
|
||||
target.invoke(cache.getCache(), e);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if( subscribers != null )
|
||||
{
|
||||
for( final Entry<Class, MENetworkEventInfo> subscriber : subscribers.entrySet() )
|
||||
{
|
||||
final MENetworkEventInfo target = subscriber.getValue();
|
||||
final GridCacheWrapper cache = g.getCaches().get( subscriber.getKey() );
|
||||
if( cache != null )
|
||||
{
|
||||
x++;
|
||||
target.invoke( cache.getCache(), e );
|
||||
}
|
||||
// events may create or remove grid nodes in rare cases
|
||||
final IMachineSet machines = g.getMachines(subscriber.getKey());
|
||||
final List<IGridNode> work = new ArrayList<>(machines.size());
|
||||
machines.forEach(work::add);
|
||||
|
||||
// events may create or remove grid nodes in rare cases
|
||||
final IMachineSet machines = g.getMachines( subscriber.getKey() );
|
||||
final List<IGridNode> work = new ArrayList<>( machines.size() );
|
||||
machines.forEach( work::add );
|
||||
for (final IGridNode obj : work) {
|
||||
// stil part of grid?
|
||||
if (machines.contains(obj)) {
|
||||
x++;
|
||||
target.invoke(obj.getMachine(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (final NetworkEventDone done) {
|
||||
// Early out.
|
||||
}
|
||||
|
||||
for( final IGridNode obj : work )
|
||||
{
|
||||
// stil part of grid?
|
||||
if( machines.contains( obj ) )
|
||||
{
|
||||
x++;
|
||||
target.invoke( obj.getMachine(), e );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch( final NetworkEventDone done )
|
||||
{
|
||||
// Early out.
|
||||
}
|
||||
e.setVisitedObjects(x);
|
||||
return e;
|
||||
}
|
||||
|
||||
e.setVisitedObjects( x );
|
||||
return e;
|
||||
}
|
||||
MENetworkEvent postEventTo(final Grid grid, final GridNode node, final MENetworkEvent e) {
|
||||
final Map<Class, MENetworkEventInfo> subscribers = EVENTS.get(e.getClass());
|
||||
int x = 0;
|
||||
|
||||
MENetworkEvent postEventTo( final Grid grid, final GridNode node, final MENetworkEvent e )
|
||||
{
|
||||
final Map<Class, MENetworkEventInfo> subscribers = EVENTS.get( e.getClass() );
|
||||
int x = 0;
|
||||
try {
|
||||
if (subscribers != null) {
|
||||
final MENetworkEventInfo target = subscribers.get(node.getMachineClass());
|
||||
if (target != null) {
|
||||
x++;
|
||||
target.invoke(node.getMachine(), e);
|
||||
}
|
||||
}
|
||||
} catch (final NetworkEventDone done) {
|
||||
// Early out.
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if( subscribers != null )
|
||||
{
|
||||
final MENetworkEventInfo target = subscribers.get( node.getMachineClass() );
|
||||
if( target != null )
|
||||
{
|
||||
x++;
|
||||
target.invoke( node.getMachine(), e );
|
||||
}
|
||||
}
|
||||
}
|
||||
catch( final NetworkEventDone done )
|
||||
{
|
||||
// Early out.
|
||||
}
|
||||
e.setVisitedObjects(x);
|
||||
return e;
|
||||
}
|
||||
|
||||
e.setVisitedObjects( x );
|
||||
return e;
|
||||
}
|
||||
private static class NetworkEventDone extends Throwable {
|
||||
|
||||
private static class NetworkEventDone extends Throwable
|
||||
{
|
||||
private static final long serialVersionUID = -3079021487019171205L;
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = -3079021487019171205L;
|
||||
}
|
||||
private class EventMethod {
|
||||
|
||||
private class EventMethod
|
||||
{
|
||||
private final Class objClass;
|
||||
private final Method objMethod;
|
||||
private final Class objEvent;
|
||||
|
||||
private final Class objClass;
|
||||
private final Method objMethod;
|
||||
private final Class objEvent;
|
||||
public EventMethod(final Class Event, final Class ObjClass, final Method ObjMethod) {
|
||||
this.objClass = ObjClass;
|
||||
this.objMethod = ObjMethod;
|
||||
this.objEvent = Event;
|
||||
}
|
||||
|
||||
public EventMethod( final Class Event, final Class ObjClass, final Method ObjMethod )
|
||||
{
|
||||
this.objClass = ObjClass;
|
||||
this.objMethod = ObjMethod;
|
||||
this.objEvent = Event;
|
||||
}
|
||||
private void invoke(final Object obj, final MENetworkEvent e) throws NetworkEventDone {
|
||||
try {
|
||||
this.objMethod.invoke(obj, e);
|
||||
} catch (final Throwable e1) {
|
||||
AELog.error("[AppEng] Network Event caused exception:");
|
||||
AELog.error("Class: %1s, Object: %2s", obj.getClass().getName(), obj.toString());
|
||||
AELog.info(e1);
|
||||
throw new IllegalStateException(e1);
|
||||
}
|
||||
|
||||
private void invoke( final Object obj, final MENetworkEvent e ) throws NetworkEventDone
|
||||
{
|
||||
try
|
||||
{
|
||||
this.objMethod.invoke( obj, e );
|
||||
}
|
||||
catch( final Throwable e1 )
|
||||
{
|
||||
AELog.error( "[AppEng] Network Event caused exception:" );
|
||||
AELog.error( "Class: %1s, Object: %2s", obj.getClass().getName(), obj.toString() );
|
||||
AELog.info( e1 );
|
||||
throw new IllegalStateException( e1 );
|
||||
}
|
||||
if (e.isCanceled()) {
|
||||
throw new NetworkEventDone();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if( e.isCanceled() )
|
||||
{
|
||||
throw new NetworkEventDone();
|
||||
}
|
||||
}
|
||||
}
|
||||
private class MENetworkEventInfo {
|
||||
|
||||
private class MENetworkEventInfo
|
||||
{
|
||||
private final List<EventMethod> methods = new ArrayList<>();
|
||||
|
||||
private final List<EventMethod> methods = new ArrayList<>();
|
||||
private void Add(final Class Event, final Class ObjClass, final Method ObjMethod) {
|
||||
this.methods.add(new EventMethod(Event, ObjClass, ObjMethod));
|
||||
}
|
||||
|
||||
private void Add( final Class Event, final Class ObjClass, final Method ObjMethod )
|
||||
{
|
||||
this.methods.add( new EventMethod( Event, ObjClass, ObjMethod ) );
|
||||
}
|
||||
|
||||
private void invoke( final Object obj, final MENetworkEvent e ) throws NetworkEventDone
|
||||
{
|
||||
for( final EventMethod em : this.methods )
|
||||
{
|
||||
em.invoke( obj, e );
|
||||
}
|
||||
}
|
||||
}
|
||||
private void invoke(final Object obj, final MENetworkEvent e) throws NetworkEventDone {
|
||||
for (final EventMethod em : this.methods) {
|
||||
em.invoke(obj, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user