use of won.bot.framework.eventbot.listener.EventListener in project webofneeds by researchstudio-sat.
the class ExecuteDeactivateAtomCommandAction method doRun.
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
if (!(event instanceof DeactivateAtomCommandEvent)) {
return;
}
DeactivateAtomCommandEvent deactivateAtomCommandEvent = (DeactivateAtomCommandEvent) event;
EventListenerContext ctx = getEventListenerContext();
EventBus bus = ctx.getEventBus();
final URI atomUri = deactivateAtomCommandEvent.getAtomUri();
Dataset atomRDF = ctx.getLinkedDataSource().getDataForPublicResource(atomUri);
final URI wonNodeUri = WonRdfUtils.ConnectionUtils.getWonNodeURIFromAtom(atomRDF, atomUri);
WonNodeInformationService wonNodeInformationService = ctx.getWonNodeInformationService();
WonMessage deactivateAtomMessage = createWonMessage(wonNodeInformationService, atomUri, wonNodeUri);
deactivateAtomMessage = ctx.getWonMessageSender().prepareMessage(deactivateAtomMessage);
EventListener successCallback = event12 -> {
logger.debug("atom creation successful, new atom URI is {}", atomUri);
bus.publish(new DeactivateAtomCommandSuccessEvent(atomUri, deactivateAtomCommandEvent));
};
EventListener failureCallback = event1 -> {
String textMessage = WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) event1).getFailureMessage());
logger.debug("atom creation failed for atom URI {}, original message URI {}: {}", new Object[] { atomUri, ((FailureResponseEvent) event1).getOriginalMessageURI(), textMessage });
bus.publish(new DeactivateAtomCommandFailureEvent(atomUri, deactivateAtomCommandEvent, textMessage));
};
EventBotActionUtils.makeAndSubscribeResponseListener(deactivateAtomMessage, successCallback, failureCallback, ctx);
logger.debug("registered listeners for response to message URI {}", deactivateAtomMessage.getMessageURI());
ctx.getWonMessageSender().sendMessage(deactivateAtomMessage);
logger.debug("atom creation message sent with message URI {}", deactivateAtomMessage.getMessageURI());
}
use of won.bot.framework.eventbot.listener.EventListener in project webofneeds by researchstudio-sat.
the class BehaviourBarrier method onActivate.
@Override
protected void onActivate(Optional<Object> message) {
Set<BotBehaviour> deactivatedBehaviours = Collections.synchronizedSet(new HashSet<>());
subscribeWithAutoCleanup(BotBehaviourDeactivatedEvent.class, new ActionOnEventListener(context, event -> {
if (!(event instanceof BotBehaviourDeactivatedEvent)) {
return false;
}
return behavioursToWaitFor.contains(((BotBehaviourDeactivatedEvent) event).getBehaviour());
}, new BaseEventBotAction(context) {
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
synchronized (behavioursToWaitFor) {
deactivatedBehaviours.add(((BotBehaviourDeactivatedEvent) event).getBehaviour());
if (deactivatedBehaviours.containsAll(behavioursToWaitFor)) {
behavioursToStart.forEach(BotBehaviour::activate);
deactivate();
}
}
}
}));
}
use of won.bot.framework.eventbot.listener.EventListener in project webofneeds by researchstudio-sat.
the class BotTrigger method activate.
public synchronized void activate() {
if (active.get())
return;
// make the stop listener
this.stopListener = new ActionOnFirstEventListener(this.context, new BotTriggerFilter(this), new BaseEventBotAction(BotTrigger.this.context) {
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
// unregister all listeners
BotTrigger.this.context.getEventBus().unsubscribe(BotTrigger.this.startListener);
BotTrigger.this.context.getEventBus().unsubscribe(BotTrigger.this.stopListener);
BotTrigger.this.cancelableTask.cancel(true);
BotTrigger.this.active.set(false);
}
});
// make the start listener
this.startListener = new ActionOnFirstEventListener(this.context, new BotTriggerFilter(this), new BaseEventBotAction(BotTrigger.this.context) {
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
reschedule();
}
});
// register both listeners
context.getEventBus().subscribe(StopBotTriggerCommandEvent.class, stopListener);
context.getEventBus().subscribe(StartBotTriggerCommandEvent.class, startListener);
active.set(true);
}
use of won.bot.framework.eventbot.listener.EventListener in project webofneeds by researchstudio-sat.
the class MatchingLoadTestMonitorAction method doRun.
@Override
protected void doRun(final Event event, EventListener executingListener) throws Exception {
Stopwatch stopwatch = SimonManager.getStopwatch("atomHintFullRoundtrip");
if (event instanceof AtomCreatedEvent) {
Split split = stopwatch.start();
atomSplits.put(((AtomCreatedEvent) event).getAtomURI().toString(), split);
logger.info("RECEIVED EVENT {} for uri {}", event, ((AtomCreatedEvent) event).getAtomURI().toString());
long startTime = System.currentTimeMillis();
String atomUri = ((AtomCreatedEvent) event).getAtomURI().toString();
atomEventStartTime.put(atomUri, startTime);
} else if (event instanceof AtomHintFromMatcherEvent) {
String atomUri = ((AtomHintFromMatcherEvent) event).getRecipientAtom().toString();
logger.info("RECEIVED EVENT {} for uri {}", event, atomUri);
long hintReceivedTime = System.currentTimeMillis();
atomSplits.get(atomUri).stop();
hintEventReceivedTime.computeIfAbsent(atomUri, k -> new LinkedList<>());
hintEventReceivedTime.get(atomUri).add(hintReceivedTime);
} else if (event instanceof SocketHintFromMatcherEvent) {
String atomUri = ((SocketHintFromMatcherEvent) event).getRecipientSocket().toString();
logger.info("RECEIVED EVENT {} for uri {}", event, atomUri);
long hintReceivedTime = System.currentTimeMillis();
atomSplits.get(atomUri).stop();
hintEventReceivedTime.computeIfAbsent(atomUri, k -> new LinkedList<>());
hintEventReceivedTime.get(atomUri).add(hintReceivedTime);
}
if (startTestTime == -1) {
startTestTime = System.currentTimeMillis();
}
logger.info("Number of Atoms: {}", atomEventStartTime.size());
logger.info("Number of Hints: {}", getTotalHints());
logger.info("Number of Atoms with Hints: {}", getAtomsWithHints());
logger.info("Average Duration: {}", getAverageHintDuration());
logger.info("Minimum Duration: {}", getMinHintDuration());
logger.info("Maximum Duration: {}", getMaxHintDuration());
logger.info("Atoms with Hints per Second: {}", getAtomsWithAtomsPerSecond(startTestTime));
logger.info("Hints per Second: {}", getHintsPerSecondThroughput(startTestTime));
}
use of won.bot.framework.eventbot.listener.EventListener in project webofneeds by researchstudio-sat.
the class CoordinationBehaviour method onActivate.
@Override
protected void onActivate(Optional<Object> message) {
EventBotAction actionToExecute;
if (typeB == CoordinationType.ACTIVATE) {
actionToExecute = new BaseEventBotAction(context) {
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
BotBehaviourEvent botBehaviourEvent = (BotBehaviourEvent) event;
behaviourB.activate(botBehaviourEvent.getMessage());
deactivate(botBehaviourEvent.getMessage());
}
};
} else {
actionToExecute = new BaseEventBotAction(context) {
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
BotBehaviourEvent botBehaviourEvent = (BotBehaviourEvent) event;
behaviourB.deactivate(botBehaviourEvent.getMessage());
deactivate(botBehaviourEvent.getMessage());
}
};
}
Class<? extends Event> eventClazz;
if (typeA == CoordinationType.ACTIVATE) {
eventClazz = BotBehaviourActivatedEvent.class;
} else {
eventClazz = BotBehaviourDeactivatedEvent.class;
}
subscribeWithAutoCleanup(eventClazz, new ActionOnEventListener(context, event -> ((BotBehaviourEvent) event).getBehaviour() == behaviourA, actionToExecute));
}
Aggregations