Search in sources :

Example 1 with BATestBotScript

use of won.bot.framework.eventbot.listener.baStateBots.BATestBotScript in project webofneeds by researchstudio-sat.

the class BAAtomicAdditionalParticipantsBaseBot method initializeEventListeners.

@Override
protected void initializeEventListeners() {
    final EventListenerContext ctx = getEventListenerContext();
    final AdditionalParticipantCoordinatorBotContextWrapper botContextWrapper = (AdditionalParticipantCoordinatorBotContextWrapper) getBotContextWrapper();
    final EventBus bus = getEventBus();
    logger.info("info1: No of needs: " + noOfNeeds);
    // wait for all needs to be created
    WaitForNEventsListener allNeedsCreatedListener = new WaitForNEventsListener(ctx, "waitForAllNeedsCreated", noOfNeeds);
    bus.subscribe(NeedCreatedEvent.class, allNeedsCreatedListener);
    // create needs every trigger execution until noOfNeeds are created
    this.participantNeedCreator = new ActionOnEventListener(ctx, "participantCreator", new CreateNeedWithFacetsAction(ctx, botContextWrapper.getParticipantListName(), getParticipantFacetType().getURI()), noOfNonDelayedNeeds - 1);
    bus.subscribe(ActEvent.class, this.participantNeedCreator);
    // create needs every trigger execution until noOfNeeds are created
    this.delayedParticipantNeedCreator = new ActionOnEventListener(ctx, "delayedParticipantCreator", new CreateNeedWithFacetsAction(ctx, botContextWrapper.getParticipantDelayedListName(), getParticipantFacetType().getURI()), noOfDelayedNeeds);
    bus.subscribe(ActEvent.class, this.delayedParticipantNeedCreator);
    // when done, create one coordinator need
    this.coordinatorNeedCreator = new ActionOnEventListener(ctx, "coordinatorCreator", new FinishedEventFilter(participantNeedCreator), new CreateNeedWithFacetsAction(ctx, botContextWrapper.getCoordinatorListName(), getCoordinatorFacetType().getURI()), 1);
    bus.subscribe(FinishedEvent.class, this.coordinatorNeedCreator);
    final Iterator<BATestBotScript> firstPhasescriptIterator = firstPhaseScripts.iterator();
    final Iterator<BATestBotScript> firstPhaseScriptWithDelayIterator = firstPhaseScriptsWithDelay.iterator();
    final Iterator<BATestBotScript> secondPhasescriptIterator = secondPhaseScripts.iterator();
    // final Iterator<BATestBotScript> secondPhasescriptWithDelayIterator = secondPhaseScripts.iterator();
    // make a composite filter, with one filter for each testScriptListener that wait
    // for the FinishedEvents the they emit. That filter will be used to shut
    // down all needs after all the scriptListeners have finished.
    final OrFilter firstPhaseScriptListenerFilter = new OrFilter();
    final OrFilter firstPhaseScriptWithDelayListenerFilter = new OrFilter();
    final OrFilter secondPhaseScriptListenerFilter = new OrFilter();
    // final OrFilter secondPhaseScriptWithDelayListenerFilter = new OrFilter();
    // create a callback that gets called immediately before the connection is established
    ConnectFromListToListAction.ConnectHook scriptConnectHook = new ConnectFromListToListAction.ConnectHook() {

        @Override
        public void onConnect(final URI fromNeedURI, final URI toNeedURI) {
            // create the listener that will execute the script actions
            BATestScriptListener testScriptListener = new BATestScriptListener(ctx, firstPhasescriptIterator.next(), fromNeedURI, toNeedURI, MILLIS_BETWEEN_MESSAGES);
            // remember it so we can check its state later
            firstPhasetestScriptListeners.add(testScriptListener);
            // subscribe it to the relevant events.
            bus.subscribe(ConnectFromOtherNeedEvent.class, testScriptListener);
            bus.subscribe(OpenFromOtherNeedEvent.class, testScriptListener);
            bus.subscribe(MessageFromOtherNeedEvent.class, testScriptListener);
            // add a filter that will wait for the FinishedEvent emitted by that listener
            // wrap it in an acceptance filter to make extra sure we count each listener only once.
            firstPhaseScriptListenerFilter.addFilter(new AcceptOnceFilter(new FinishedEventFilter(testScriptListener)));
            // now we create the listener that is only active in the second phase
            // remember it so we can check its state later
            BATestScriptListener secondPhaseTestScriptListener = new BATestScriptListener(ctx, secondPhasescriptIterator.next(), fromNeedURI, toNeedURI, MILLIS_BETWEEN_MESSAGES);
            secondPhasetestScriptListeners.add(secondPhaseTestScriptListener);
            secondPhaseScriptListenerFilter.addFilter(new AcceptOnceFilter(new FinishedEventFilter(secondPhaseTestScriptListener)));
        }
    };
    ConnectFromListToListAction.ConnectHook scriptConnectWithDelayHook = new ConnectFromListToListAction.ConnectHook() {

        @Override
        public void onConnect(final URI fromNeedURI, final URI toNeedURI) {
            // create the listener that will execute the script actions
            BATestScriptListener testScriptListener = new BATestScriptListener(ctx, firstPhaseScriptWithDelayIterator.next(), fromNeedURI, toNeedURI, MILLIS_BETWEEN_MESSAGES);
            // remember it so we can check its state later
            firstPhasetestScriptWithDelayListeners.add(testScriptListener);
            // subscribe it to the relevant events.
            bus.subscribe(ConnectFromOtherNeedEvent.class, testScriptListener);
            bus.subscribe(OpenFromOtherNeedEvent.class, testScriptListener);
            bus.subscribe(MessageFromOtherNeedEvent.class, testScriptListener);
            // add a filter that will wait for the FinishedEvent emitted by that listener
            // wrap it in an acceptance filter to make extra sure we count each listener only once.
            firstPhaseScriptWithDelayListenerFilter.addFilter(new AcceptOnceFilter(new FinishedEventFilter(testScriptListener)));
            // now we create the listener that is only active in the second phase
            // remember it so we can check its state later
            BATestScriptListener secondPhaseTestScriptListener = new BATestScriptListener(ctx, secondPhasescriptIterator.next(), fromNeedURI, toNeedURI, MILLIS_BETWEEN_MESSAGES);
            secondPhasetestScriptListeners.add(secondPhaseTestScriptListener);
            secondPhaseScriptListenerFilter.addFilter(new AcceptOnceFilter(new FinishedEventFilter(secondPhaseTestScriptListener)));
        }
    };
    // when done, connect the participants to the coordinator
    this.needConnector = new ActionOnEventListener(ctx, "needConnector", new FinishedEventFilter(allNeedsCreatedListener), new ConnectFromListToListAction(ctx, botContextWrapper.getCoordinatorListName(), botContextWrapper.getParticipantListName(), getCoordinatorFacetType().getURI(), getParticipantFacetType().getURI(), MILLIS_BETWEEN_MESSAGES, scriptConnectHook, "Hi!"), 1);
    bus.subscribe(FinishedEvent.class, this.needConnector);
    // wait until the non-delayed participants are connected and done with their scripts
    BaseEventListener waitForNonDelayedConnectsListener = new WaitForNEventsListener(ctx, firstPhaseScriptListenerFilter, noOfNonDelayedNeeds - 1);
    bus.subscribe(FinishedEvent.class, waitForNonDelayedConnectsListener);
    FinishedEventFilter allNonDelayedConnectedFilter = new FinishedEventFilter(waitForNonDelayedConnectsListener);
    this.needConnectorWithDelay = new ActionOnEventListener(ctx, "needConnectorWithDelay", allNonDelayedConnectedFilter, new ConnectFromListToListAction(ctx, botContextWrapper.getCoordinatorListName(), botContextWrapper.getParticipantDelayedListName(), getCoordinatorFacetType().getURI(), getParticipantFacetType().getURI(), MILLIS_BETWEEN_MESSAGES, scriptConnectWithDelayHook, "Hi!"), 1);
    // TODO: MAKE THIS SO URI_LIST_NAME_PARTICIPANT_DELAYED "delayedParticipants" works again
    bus.subscribe(FinishedEvent.class, this.needConnectorWithDelay);
    // for each group member, there are 2 listeners waiting for messages. when they are all finished, we're done.
    this.firstPhaseWithDelayDoneListener = new ActionOnceAfterNEventsListener(ctx, "firstPhaseDoneWithDelayListener", firstPhaseScriptWithDelayListenerFilter, noOfDelayedNeeds, new BaseEventBotAction(ctx) {

        @Override
        protected void doRun(final Event event, EventListener executingListener) throws Exception {
            logger.debug("starting second phase");
            logger.debug("non-delayed listeners: {}", firstPhasetestScriptListeners.size());
            logger.debug("delayed listeners: {}", firstPhasetestScriptWithDelayListeners.size());
            Iterator<BATestScriptListener> firstPhaseListeners = firstPhasetestScriptListeners.iterator();
            Iterator<BATestScriptListener> firstPhaseWithDelayListeners = firstPhasetestScriptWithDelayListeners.iterator();
            Iterator<BATestScriptListener> combinedFirstPhaseListeners = Iterators.concat(firstPhaseListeners, firstPhaseWithDelayListeners);
            logger.debug("# of listeners in second phase: {}", secondPhasetestScriptListeners.size());
            for (BATestScriptListener listener : secondPhasetestScriptListeners) {
                logger.debug("subscribing second phase listener {}", listener);
                // subscribe it to the relevant events.
                bus.subscribe(MessageFromOtherNeedEvent.class, listener);
                bus.subscribe(SecondPhaseStartedEvent.class, listener);
                BATestScriptListener correspondingFirstPhaseListener = combinedFirstPhaseListeners.next();
                listener.setCoordinatorSideConnectionURI(correspondingFirstPhaseListener.getCoordinatorSideConnectionURI());
                listener.setParticipantSideConnectionURI(correspondingFirstPhaseListener.getParticipantSideConnectionURI());
                listener.updateFilterForBothConnectionURIs();
                bus.publish(new SecondPhaseStartedEvent(correspondingFirstPhaseListener.getCoordinatorURI(), correspondingFirstPhaseListener.getCoordinatorSideConnectionURI(), correspondingFirstPhaseListener.getParticipantURI()));
            }
        }
    });
    bus.subscribe(FinishedEvent.class, this.firstPhaseWithDelayDoneListener);
    // for each group member, there are 2 listeners waiting for messages. when they are all finished, we're done.
    this.scriptsDoneListener = new ActionOnceAfterNEventsListener(ctx, "scriptsDoneListener", secondPhaseScriptListenerFilter, noOfNeeds - 1, new DeactivateAllNeedsOfListAction(ctx, botContextWrapper.getParticipantListName()));
    bus.subscribe(FinishedEvent.class, this.scriptsDoneListener);
    // When the needs are deactivated, all connections are closed. wait for the close events and signal work done.
    this.workDoneSignaller = new ActionOnceAfterNEventsListener(ctx, "workDoneSignaller", noOfNeeds - 1, new SignalWorkDoneAction(ctx));
    bus.subscribe(CloseFromOtherNeedEvent.class, this.workDoneSignaller);
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) ActionOnceAfterNEventsListener(won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener) AdditionalParticipantCoordinatorBotContextWrapper(won.bot.framework.bot.context.AdditionalParticipantCoordinatorBotContextWrapper) BaseEventListener(won.bot.framework.eventbot.listener.BaseEventListener) EventBus(won.bot.framework.eventbot.bus.EventBus) URI(java.net.URI) DeactivateAllNeedsOfListAction(won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsOfListAction) ConnectFromListToListAction(won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction) BaseEventListener(won.bot.framework.eventbot.listener.BaseEventListener) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener) WaitForNEventsListener(won.bot.framework.eventbot.listener.impl.WaitForNEventsListener) BATestBotScript(won.bot.framework.eventbot.listener.baStateBots.BATestBotScript) CreateNeedWithFacetsAction(won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction) OrFilter(won.bot.framework.eventbot.filter.impl.OrFilter) SignalWorkDoneAction(won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction) SecondPhaseStartedEvent(won.bot.framework.eventbot.listener.baStateBots.baCCMessagingBots.atomicBots.SecondPhaseStartedEvent) BATestScriptListener(won.bot.framework.eventbot.listener.baStateBots.BATestScriptListener) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) MessageFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.MessageFromOtherNeedEvent) SecondPhaseStartedEvent(won.bot.framework.eventbot.listener.baStateBots.baCCMessagingBots.atomicBots.SecondPhaseStartedEvent) NeedCreatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent) FinishedEvent(won.bot.framework.eventbot.event.impl.listener.FinishedEvent) ConnectFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent) OpenFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.OpenFromOtherNeedEvent) Event(won.bot.framework.eventbot.event.Event) ActEvent(won.bot.framework.eventbot.event.impl.lifecycle.ActEvent) CloseFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.CloseFromOtherNeedEvent) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) AcceptOnceFilter(won.bot.framework.eventbot.filter.impl.AcceptOnceFilter) FinishedEventFilter(won.bot.framework.eventbot.filter.impl.FinishedEventFilter)

Example 2 with BATestBotScript

use of won.bot.framework.eventbot.listener.baStateBots.BATestBotScript in project webofneeds by researchstudio-sat.

the class BAAtomicCCAdditionalParticipants method getSecondPhaseScripts.

protected List<BATestBotScript> getSecondPhaseScripts() {
    List<BATestBotScript> scripts = new ArrayList<BATestBotScript>(4);
    scripts.add(new CompletedSPClosingAdditionalParticipantsBot("CompletedSPClosingBot-1"));
    scripts.add(new CompletedSPClosingAdditionalParticipantsBot("CompletedSPClosingBot-2"));
    scripts.add(new CompletedSPClosingAdditionalParticipantsBot("CompletedSPClosingBot-3"));
    scripts.add(new CompletedSPClosingAdditionalParticipantsBot("CompletedSPClosingBot-4"));
    return scripts;
}
Also used : ArrayList(java.util.ArrayList) CompletedSPClosingAdditionalParticipantsBot(won.bot.framework.eventbot.listener.baStateBots.baCCMessagingBots.atomicBots.coordinationMessageAsTextBot.CompletedSPClosingAdditionalParticipantsBot) BATestBotScript(won.bot.framework.eventbot.listener.baStateBots.BATestBotScript)

Example 3 with BATestBotScript

use of won.bot.framework.eventbot.listener.baStateBots.BATestBotScript in project webofneeds by researchstudio-sat.

the class AutomaticBAMessageResponderListener method handleOpenEvent.

/**
 * React to open event by sending a message.
 *
 * @param openEvent
 */
private void handleOpenEvent(final OpenFromOtherNeedEvent openEvent) {
    logger.debug("handleOpen: got open event for need: {}, connection state is: {}", openEvent.getCon().getNeedURI(), openEvent.getCon().getState());
    if (openEvent.getCon().getState() != ConnectionState.CONNECTED) {
        logger.warn("connection state must be CONNECTED when open is received. We don't expect open of connections created through hints.");
    }
    logger.debug("replying to open with message");
    // register a WS-BA state machine for this need-need combination:
    boolean weAreOnCoordinatorSide = areWeOnCoordinatorSide(openEvent.getCon());
    logger.info("Are we on Coordinator side? " + weAreOnCoordinatorSide);
    BATestBotScript script = setupStateMachine(openEvent, weAreOnCoordinatorSide);
    // now, getGeneric the action from the state machine and make the right need send the message
    if (isScriptFinished(script))
        return;
    BATestScriptAction action = script.getNextAction();
    // logger.info("State of the sender before sending: {} ", action.getStateOfSenderBeforeSending());
    // decide from which need to send the initial WS-BA protocol message
    URI connectionToSendMessageFrom = determineConnectionToSendFrom(openEvent.getCon(), weAreOnCoordinatorSide, action);
    // 2 sendMessageFromConnection(action, connectionToSendMessageFrom);
    sendModelFromConnection(action, connectionToSendMessageFrom);
}
Also used : BATestScriptAction(won.bot.framework.eventbot.listener.baStateBots.BATestScriptAction) URI(java.net.URI) BATestBotScript(won.bot.framework.eventbot.listener.baStateBots.BATestBotScript)

Example 4 with BATestBotScript

use of won.bot.framework.eventbot.listener.baStateBots.BATestBotScript in project webofneeds by researchstudio-sat.

the class BAAtomicCCAdditionalParticipants method getFirstPhaseScripts.

protected List<BATestBotScript> getFirstPhaseScripts() {
    List<BATestBotScript> scripts = new ArrayList<BATestBotScript>(2);
    // Coordination message is sent as TEXT
    scripts.add(new CompletedFPBot());
    scripts.add(new CompletedFPBot());
    return scripts;
}
Also used : ArrayList(java.util.ArrayList) CompletedFPBot(won.bot.framework.eventbot.listener.baStateBots.baCCMessagingBots.atomicBots.coordinationMessageAsTextBot.CompletedFPBot) BATestBotScript(won.bot.framework.eventbot.listener.baStateBots.BATestBotScript)

Example 5 with BATestBotScript

use of won.bot.framework.eventbot.listener.baStateBots.BATestBotScript in project webofneeds by researchstudio-sat.

the class BAAtomicCCAdditionalParticipants method getFirstPhaseScriptsWithDelay.

protected List<BATestBotScript> getFirstPhaseScriptsWithDelay() {
    List<BATestBotScript> scripts = new ArrayList<BATestBotScript>(2);
    scripts.add(new CompletedFPBot());
    scripts.add(new CompletedFPBot());
    return scripts;
}
Also used : ArrayList(java.util.ArrayList) CompletedFPBot(won.bot.framework.eventbot.listener.baStateBots.baCCMessagingBots.atomicBots.coordinationMessageAsTextBot.CompletedFPBot) BATestBotScript(won.bot.framework.eventbot.listener.baStateBots.BATestBotScript)

Aggregations

BATestBotScript (won.bot.framework.eventbot.listener.baStateBots.BATestBotScript)10 URI (java.net.URI)5 ArrayList (java.util.ArrayList)5 ParticipantCoordinatorBotContextWrapper (won.bot.framework.bot.context.ParticipantCoordinatorBotContextWrapper)2 EventListenerContext (won.bot.framework.eventbot.EventListenerContext)2 SignalWorkDoneAction (won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction)2 CreateNeedWithFacetsAction (won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction)2 DeactivateAllNeedsOfListAction (won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsOfListAction)2 ConnectFromListToListAction (won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction)2 EventBus (won.bot.framework.eventbot.bus.EventBus)2 AcceptOnceFilter (won.bot.framework.eventbot.filter.impl.AcceptOnceFilter)2 FinishedEventFilter (won.bot.framework.eventbot.filter.impl.FinishedEventFilter)2 OrFilter (won.bot.framework.eventbot.filter.impl.OrFilter)2 BATestScriptAction (won.bot.framework.eventbot.listener.baStateBots.BATestScriptAction)2 BATestScriptListener (won.bot.framework.eventbot.listener.baStateBots.BATestScriptListener)2 CompletedFPBot (won.bot.framework.eventbot.listener.baStateBots.baCCMessagingBots.atomicBots.coordinationMessageAsTextBot.CompletedFPBot)2 ActionOnEventListener (won.bot.framework.eventbot.listener.impl.ActionOnEventListener)2 ActionOnceAfterNEventsListener (won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener)2 AdditionalParticipantCoordinatorBotContextWrapper (won.bot.framework.bot.context.AdditionalParticipantCoordinatorBotContextWrapper)1 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)1