Search in sources :

Example 41 with EventListener

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());
}
Also used : WonNodeInformationService(won.protocol.service.WonNodeInformationService) Logger(org.slf4j.Logger) WonMessageBuilderException(won.protocol.exception.WonMessageBuilderException) DeactivateAtomCommandFailureEvent(won.bot.framework.eventbot.event.impl.command.deactivate.DeactivateAtomCommandFailureEvent) MethodHandles(java.lang.invoke.MethodHandles) LoggerFactory(org.slf4j.LoggerFactory) EventBus(won.bot.framework.eventbot.bus.EventBus) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) EventBotActionUtils(won.bot.framework.eventbot.action.EventBotActionUtils) DeactivateAtomCommandEvent(won.bot.framework.eventbot.event.impl.command.deactivate.DeactivateAtomCommandEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) WonMessage(won.protocol.message.WonMessage) WonRdfUtils(won.protocol.util.WonRdfUtils) WonMessageBuilder(won.protocol.message.builder.WonMessageBuilder) Event(won.bot.framework.eventbot.event.Event) DeactivateAtomCommandSuccessEvent(won.bot.framework.eventbot.event.impl.command.deactivate.DeactivateAtomCommandSuccessEvent) EventListener(won.bot.framework.eventbot.listener.EventListener) URI(java.net.URI) Dataset(org.apache.jena.query.Dataset) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) Dataset(org.apache.jena.query.Dataset) WonNodeInformationService(won.protocol.service.WonNodeInformationService) EventBus(won.bot.framework.eventbot.bus.EventBus) URI(java.net.URI) DeactivateAtomCommandSuccessEvent(won.bot.framework.eventbot.event.impl.command.deactivate.DeactivateAtomCommandSuccessEvent) WonMessage(won.protocol.message.WonMessage) DeactivateAtomCommandFailureEvent(won.bot.framework.eventbot.event.impl.command.deactivate.DeactivateAtomCommandFailureEvent) EventListener(won.bot.framework.eventbot.listener.EventListener) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) DeactivateAtomCommandEvent(won.bot.framework.eventbot.event.impl.command.deactivate.DeactivateAtomCommandEvent)

Example 42 with EventListener

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();
                }
            }
        }
    }));
}
Also used : java.util(java.util) Event(won.bot.framework.eventbot.event.Event) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) Event(won.bot.framework.eventbot.event.Event) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener)

Example 43 with EventListener

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);
}
Also used : BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) Event(won.bot.framework.eventbot.event.Event) ActionOnFirstEventListener(won.bot.framework.eventbot.listener.impl.ActionOnFirstEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener) ActionOnFirstEventListener(won.bot.framework.eventbot.listener.impl.ActionOnFirstEventListener)

Example 44 with EventListener

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));
}
Also used : AtomCreatedEvent(won.bot.framework.eventbot.event.impl.atomlifecycle.AtomCreatedEvent) java.util(java.util) Logger(org.slf4j.Logger) Split(org.javasimon.Split) MethodHandles(java.lang.invoke.MethodHandles) LoggerFactory(org.slf4j.LoggerFactory) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) SocketHintFromMatcherEvent(won.bot.framework.eventbot.event.impl.wonmessage.SocketHintFromMatcherEvent) Stopwatch(org.javasimon.Stopwatch) SimonManager(org.javasimon.SimonManager) Event(won.bot.framework.eventbot.event.Event) EventListener(won.bot.framework.eventbot.listener.EventListener) AtomHintFromMatcherEvent(won.bot.framework.eventbot.event.impl.wonmessage.AtomHintFromMatcherEvent) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) SocketHintFromMatcherEvent(won.bot.framework.eventbot.event.impl.wonmessage.SocketHintFromMatcherEvent) Stopwatch(org.javasimon.Stopwatch) Split(org.javasimon.Split) AtomCreatedEvent(won.bot.framework.eventbot.event.impl.atomlifecycle.AtomCreatedEvent) AtomHintFromMatcherEvent(won.bot.framework.eventbot.event.impl.wonmessage.AtomHintFromMatcherEvent)

Example 45 with EventListener

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));
}
Also used : Event(won.bot.framework.eventbot.event.Event) EventBotAction(won.bot.framework.eventbot.action.EventBotAction) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) Optional(java.util.Optional) EventListener(won.bot.framework.eventbot.listener.EventListener) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) EventListenerContext(won.bot.framework.eventbot.EventListenerContext) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) Event(won.bot.framework.eventbot.event.Event) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) EventBotAction(won.bot.framework.eventbot.action.EventBotAction) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction)

Aggregations

EventListener (won.bot.framework.eventbot.listener.EventListener)57 Event (won.bot.framework.eventbot.event.Event)54 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)41 URI (java.net.URI)37 EventBus (won.bot.framework.eventbot.bus.EventBus)36 Dataset (org.apache.jena.query.Dataset)31 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)31 WonMessage (won.protocol.message.WonMessage)30 ActionOnceAfterNEventsListener (won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener)24 MethodHandles (java.lang.invoke.MethodHandles)23 Logger (org.slf4j.Logger)23 LoggerFactory (org.slf4j.LoggerFactory)23 EventBotActionUtils (won.bot.framework.eventbot.action.EventBotActionUtils)22 WonMessageBuilder (won.protocol.message.builder.WonMessageBuilder)20 ActionOnEventListener (won.bot.framework.eventbot.listener.impl.ActionOnEventListener)19 BotBehaviour (won.bot.framework.eventbot.behaviour.BotBehaviour)18 SuccessResponseEvent (won.bot.framework.eventbot.event.impl.wonmessage.SuccessResponseEvent)18 Optional (java.util.Optional)17 Test (org.junit.Test)17 EventBotAction (won.bot.framework.eventbot.action.EventBotAction)17