|
|
@ -6,9 +6,9 @@ import java.util.concurrent.Delayed; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
|
|
|
|
import mightypork.utils.control.bus.events.Event; |
|
|
|
import mightypork.utils.control.bus.events.Event; |
|
|
|
|
|
|
|
import mightypork.utils.control.bus.events.types.DelayedEvent; |
|
|
|
import mightypork.utils.control.bus.events.types.ImmediateEvent; |
|
|
|
import mightypork.utils.control.bus.events.types.ImmediateEvent; |
|
|
|
import mightypork.utils.control.bus.events.types.QueuedEvent; |
|
|
|
import mightypork.utils.control.bus.events.types.SingleReceiverEvent; |
|
|
|
import mightypork.utils.control.bus.events.types.SingularEvent; |
|
|
|
|
|
|
|
import mightypork.utils.control.interf.Destroyable; |
|
|
|
import mightypork.utils.control.interf.Destroyable; |
|
|
|
import mightypork.utils.logging.Log; |
|
|
|
import mightypork.utils.logging.Log; |
|
|
|
|
|
|
|
|
|
|
@ -27,7 +27,7 @@ final public class EventBus implements Destroyable { |
|
|
|
private final BufferedHashSet<Object> clients = new BufferedHashSet<Object>(); |
|
|
|
private final BufferedHashSet<Object> clients = new BufferedHashSet<Object>(); |
|
|
|
|
|
|
|
|
|
|
|
/** Messages queued for delivery */ |
|
|
|
/** Messages queued for delivery */ |
|
|
|
private final DelayQueue<DelayedEvent> sendQueue = new DelayQueue<DelayedEvent>(); |
|
|
|
private final DelayQueue<DelayQueueEntry> sendQueue = new DelayQueue<DelayQueueEntry>(); |
|
|
|
|
|
|
|
|
|
|
|
/** Queue polling thread */ |
|
|
|
/** Queue polling thread */ |
|
|
|
private final QueuePollingThread busThread; |
|
|
|
private final QueuePollingThread busThread; |
|
|
@ -110,17 +110,18 @@ final public class EventBus implements Destroyable { |
|
|
|
{ |
|
|
|
{ |
|
|
|
assertLive(); |
|
|
|
assertLive(); |
|
|
|
|
|
|
|
|
|
|
|
if(event.getClass().isAnnotationPresent(QueuedEvent.class)) { |
|
|
|
DelayedEvent adelay = event.getClass().getAnnotation(DelayedEvent.class); |
|
|
|
sendQueued(event); |
|
|
|
if (adelay != null) { |
|
|
|
|
|
|
|
sendDelayed(event, adelay.delay()); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if(event.getClass().isAnnotationPresent(ImmediateEvent.class)) { |
|
|
|
if (event.getClass().isAnnotationPresent(ImmediateEvent.class)) { |
|
|
|
sendDirect(event); |
|
|
|
sendDirect(event); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
dispatch(event); |
|
|
|
sendQueued(event); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -147,9 +148,9 @@ final public class EventBus implements Destroyable { |
|
|
|
{ |
|
|
|
{ |
|
|
|
assertLive(); |
|
|
|
assertLive(); |
|
|
|
|
|
|
|
|
|
|
|
final DelayedEvent dm = new DelayedEvent(delay, event); |
|
|
|
final DelayQueueEntry dm = new DelayQueueEntry(delay, event); |
|
|
|
|
|
|
|
|
|
|
|
if(logSending) Log.f3("<bus> Q "+Log.str(event)+", t = +"+delay+"s"); |
|
|
|
if (logSending) Log.f3("<bus> Q " + Log.str(event) + ", t = +" + delay + "s"); |
|
|
|
|
|
|
|
|
|
|
|
sendQueue.add(dm); |
|
|
|
sendQueue.add(dm); |
|
|
|
} |
|
|
|
} |
|
|
@ -166,7 +167,7 @@ final public class EventBus implements Destroyable { |
|
|
|
{ |
|
|
|
{ |
|
|
|
assertLive(); |
|
|
|
assertLive(); |
|
|
|
|
|
|
|
|
|
|
|
if(logSending) Log.f3("<bus> D "+Log.str(event)); |
|
|
|
if (logSending) Log.f3("<bus> D " + Log.str(event)); |
|
|
|
|
|
|
|
|
|
|
|
dispatch(event); |
|
|
|
dispatch(event); |
|
|
|
} |
|
|
|
} |
|
|
@ -190,7 +191,7 @@ final public class EventBus implements Destroyable { |
|
|
|
boolean sent = false; |
|
|
|
boolean sent = false; |
|
|
|
boolean accepted = false; |
|
|
|
boolean accepted = false; |
|
|
|
|
|
|
|
|
|
|
|
final boolean singular = event.getClass().isAnnotationPresent(SingularEvent.class); |
|
|
|
final boolean singular = event.getClass().isAnnotationPresent(SingleReceiverEvent.class); |
|
|
|
|
|
|
|
|
|
|
|
for (final EventChannel<?, ?> b : channels) { |
|
|
|
for (final EventChannel<?, ?> b : channels) { |
|
|
|
if (b.canBroadcast(event)) { |
|
|
|
if (b.canBroadcast(event)) { |
|
|
@ -201,7 +202,7 @@ final public class EventBus implements Destroyable { |
|
|
|
if (sent && singular) break; |
|
|
|
if (sent && singular) break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if(!accepted) Log.e("<bus> Not accepted by any channel: " + Log.str(event)); |
|
|
|
if (!accepted) Log.e("<bus> Not accepted by any channel: " + Log.str(event)); |
|
|
|
|
|
|
|
|
|
|
|
channels.setBuffering(false); |
|
|
|
channels.setBuffering(false); |
|
|
|
clients.setBuffering(false); |
|
|
|
clients.setBuffering(false); |
|
|
@ -253,13 +254,13 @@ final public class EventBus implements Destroyable { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private class DelayedEvent implements Delayed { |
|
|
|
private class DelayQueueEntry implements Delayed { |
|
|
|
|
|
|
|
|
|
|
|
private final long due; |
|
|
|
private final long due; |
|
|
|
private Event<?> evt = null; |
|
|
|
private Event<?> evt = null; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public DelayedEvent(double seconds, Event<?> event) { |
|
|
|
public DelayQueueEntry(double seconds, Event<?> event) { |
|
|
|
super(); |
|
|
|
super(); |
|
|
|
this.due = System.currentTimeMillis() + (long) (seconds * 1000); |
|
|
|
this.due = System.currentTimeMillis() + (long) (seconds * 1000); |
|
|
|
this.evt = event; |
|
|
|
this.evt = event; |
|
|
@ -300,7 +301,7 @@ final public class EventBus implements Destroyable { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void run() |
|
|
|
public void run() |
|
|
|
{ |
|
|
|
{ |
|
|
|
DelayedEvent evt; |
|
|
|
DelayQueueEntry evt; |
|
|
|
|
|
|
|
|
|
|
|
while (!stopped) { |
|
|
|
while (!stopped) { |
|
|
|
evt = null; |
|
|
|
evt = null; |
|
|
|