Search in sources :

Example 6 with ConnectFromListToListAction

use of won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction in project webofneeds by researchstudio-sat.

the class CommentBot method initializeEventListeners.

@Override
protected void initializeEventListeners() {
    EventListenerContext ctx = getEventListenerContext();
    final EventBus bus = getEventBus();
    CommentBotContextWrapper botContextWrapper = (CommentBotContextWrapper) getBotContextWrapper();
    // create needs every trigger execution until 2 needs are created
    this.needCreator = new ActionOnEventListener(ctx, new CreateNeedWithFacetsAction(ctx, botContextWrapper.getNeedCreateListName()), NO_OF_NEEDS);
    bus.subscribe(ActEvent.class, this.needCreator);
    // count until 1 need is created, then create a comment facet
    this.commentFacetCreator = new ActionOnEventListener(ctx, new CreateNeedWithFacetsAction(ctx, botContextWrapper.getCommentListName(), FacetType.CommentFacet.getURI()), 1);
    bus.subscribe(NeedCreatedEvent.class, this.commentFacetCreator);
    this.needConnector = new ActionOnceAfterNEventsListener(ctx, 2, new ConnectFromListToListAction(ctx, botContextWrapper.getNeedCreateListName(), botContextWrapper.getCommentListName(), FacetType.OwnerFacet.getURI(), FacetType.CommentFacet.getURI(), MILLIS_BETWEEN_MESSAGES, "Hi, I am the " + "CommentBot."));
    bus.subscribe(NeedCreatedEvent.class, this.needConnector);
    this.autoOpener = new ActionOnEventListener(ctx, new OpenConnectionAction(ctx, "Hi!"));
    bus.subscribe(OpenFromOtherNeedEvent.class, this.autoOpener);
    bus.subscribe(ConnectFromOtherNeedEvent.class, this.autoOpener);
    BaseEventListener assertionRunner = new ActionOnceAfterNEventsListener(ctx, 1, new BaseEventBotAction(ctx) {

        @Override
        protected void doRun(final Event event, EventListener executingListener) throws Exception {
            executeAssertionsForEstablishedConnectionInternal(bus);
        }
    });
    bus.subscribe(OpenFromOtherNeedEvent.class, assertionRunner);
    // deactivate all needs when the assertion was executed
    this.allNeedsDeactivator = new ActionOnEventListener(ctx, new DeactivateAllNeedsAction(ctx), 1);
    bus.subscribe(AssertionsExecutedEvent.class, this.allNeedsDeactivator);
    // add a listener that counts two NeedDeactivatedEvents and then tells the
    // framework that the bot's work is done
    this.workDoneSignaller = new ActionOnceAfterNEventsListener(ctx, 2, new SignalWorkDoneAction(ctx));
    bus.subscribe(NeedDeactivatedEvent.class, this.workDoneSignaller);
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) ActionOnceAfterNEventsListener(won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener) CreateNeedWithFacetsAction(won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction) BaseEventListener(won.bot.framework.eventbot.listener.BaseEventListener) EventBus(won.bot.framework.eventbot.bus.EventBus) OpenConnectionAction(won.bot.framework.eventbot.action.impl.wonmessage.OpenConnectionAction) SignalWorkDoneAction(won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction) DeactivateAllNeedsAction(won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsAction) ConnectFromListToListAction(won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction) CommentBotContextWrapper(won.bot.framework.bot.context.CommentBotContextWrapper) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) BaseEvent(won.bot.framework.eventbot.event.BaseEvent) NeedDeactivatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedDeactivatedEvent) 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) NeedCreatedEvent(won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) BaseEventListener(won.bot.framework.eventbot.listener.BaseEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener)

Example 7 with ConnectFromListToListAction

use of won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction in project webofneeds by researchstudio-sat.

the class ConversationBot method initializeEventListeners.

@Override
protected void initializeEventListeners() {
    EventListenerContext ctx = getEventListenerContext();
    EventBus bus = getEventBus();
    // create needs every trigger execution until 2 needs are created
    this.needCreator = new ActionOnEventListener(ctx, new CreateNeedWithFacetsAction(ctx, getBotContextWrapper().getNeedCreateListName()), NO_OF_NEEDS);
    bus.subscribe(ActEvent.class, this.needCreator);
    // count until 2 needs were created, then
    // * connect the 2 needs
    this.needConnector = new ActionOnceAfterNEventsListener(ctx, "needConnector", NO_OF_NEEDS, new ConnectFromListToListAction(ctx, ctx.getBotContextWrapper().getNeedCreateListName(), ctx.getBotContextWrapper().getNeedCreateListName(), FacetType.OwnerFacet.getURI(), FacetType.OwnerFacet.getURI(), MILLIS_BETWEEN_MESSAGES, "Hi, I am the ConversationBot."));
    bus.subscribe(NeedCreatedEvent.class, this.needConnector);
    // add a listener that is informed of the connect/open events and that auto-opens
    // subscribe it to:
    // * connect events - so it responds with open
    // * open events - so it responds with open (if the open received was the first open, and we still need to accept the connection)
    this.autoOpener = new ActionOnEventListener(ctx, new OpenConnectionAction(ctx, "Hi, I " + "am the ConversationBot, too!"));
    bus.subscribe(ConnectFromOtherNeedEvent.class, this.autoOpener);
    // add a listener that auto-responds to messages by a message
    // after 10 messages, it unsubscribes from all events
    // subscribe it to:
    // * message events - so it responds
    // * open events - so it initiates the chain reaction of responses
    this.autoResponder = new AutomaticMessageResponderListener(ctx, NO_OF_MESSAGES, MILLIS_BETWEEN_MESSAGES);
    bus.subscribe(OpenFromOtherNeedEvent.class, this.autoResponder);
    bus.subscribe(MessageFromOtherNeedEvent.class, this.autoResponder);
    // add a listener that closes the connection after it has seen 10 messages
    this.connectionCloser = new ActionOnceAfterNEventsListener(ctx, NO_OF_MESSAGES, new CloseConnectionAction(ctx, "Bye!"));
    bus.subscribe(MessageFromOtherNeedEvent.class, this.connectionCloser);
    // add a listener that closes the connection when a failureEvent occurs
    EventListener onFailureConnectionCloser = new ActionOnEventListener(ctx, new CloseConnectionAction(ctx, "Bye!"));
    bus.subscribe(FailureResponseEvent.class, onFailureConnectionCloser);
    // add a listener that auto-responds to a close message with a deactivation of both needs.
    // subscribe it to:
    // * close events
    this.needDeactivator = new ActionOnEventListener(ctx, new DeactivateAllNeedsAction(ctx), 1);
    bus.subscribe(CloseFromOtherNeedEvent.class, this.needDeactivator);
    // add a listener that counts two NeedDeactivatedEvents and then tells the
    // framework that the bot's work is done
    this.workDoneSignaller = new ActionOnceAfterNEventsListener(ctx, NO_OF_NEEDS, new SignalWorkDoneAction(ctx));
    bus.subscribe(NeedDeactivatedEvent.class, this.workDoneSignaller);
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) ActionOnceAfterNEventsListener(won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener) CloseConnectionAction(won.bot.framework.eventbot.action.impl.wonmessage.CloseConnectionAction) DeactivateAllNeedsAction(won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsAction) ConnectFromListToListAction(won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction) CreateNeedWithFacetsAction(won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction) EventBus(won.bot.framework.eventbot.bus.EventBus) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) OpenConnectionAction(won.bot.framework.eventbot.action.impl.wonmessage.OpenConnectionAction) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) BaseEventListener(won.bot.framework.eventbot.listener.BaseEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener) SignalWorkDoneAction(won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction) AutomaticMessageResponderListener(won.bot.framework.eventbot.listener.impl.AutomaticMessageResponderListener)

Example 8 with ConnectFromListToListAction

use of won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction in project webofneeds by researchstudio-sat.

the class BAAtomicBaseBot method initializeEventListeners.

@Override
protected void initializeEventListeners() {
    final EventListenerContext ctx = getEventListenerContext();
    final EventBus bus = getEventBus();
    ParticipantCoordinatorBotContextWrapper botContextWrapper = (ParticipantCoordinatorBotContextWrapper) getBotContextWrapper();
    // create needs every trigger execution until noOfNeeds are created
    this.participantNeedCreator = new ActionOnEventListener(ctx, "participantCreator", new CreateNeedWithFacetsAction(ctx, botContextWrapper.getParticipantListName(), getParticipantFacetType().getURI()), noOfNeeds - 1);
    bus.subscribe(ActEvent.class, this.participantNeedCreator);
    // 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);
    FinishedEventFilter coordinatorCreatorFilter = new FinishedEventFilter(coordinatorNeedCreator);
    final Iterator<TwoPhaseScript> scriptIterator = scripts.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 secondPhaseScriptListenerFilter = 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) {
            TwoPhaseScript script = scriptIterator.next();
            // create the listener that will execute the script actions
            BATestScriptListener testScriptListener = new BATestScriptListener(ctx, script.getFirstPhaseScript(), fromNeedURI, toNeedURI, MILLIS_BETWEEN_MESSAGES);
            // 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, script.getSecondPhaseScript(), fromNeedURI, toNeedURI, MILLIS_BETWEEN_MESSAGES);
            // remember both listeners as a pair as we'll need them together later
            TwoPhaseScriptListener twoPhaseScriptListener = new TwoPhaseScriptListener(testScriptListener, secondPhaseTestScriptListener);
            scriptListeners.add(twoPhaseScriptListener);
            secondPhaseScriptListenerFilter.addFilter(new AcceptOnceFilter(new FinishedEventFilter(secondPhaseTestScriptListener)));
        }
    };
    // when done, connect the participants to the coordinator
    this.needConnector = new ActionOnceAfterNEventsListener(ctx, "needConnector", noOfNeeds, new ConnectFromListToListAction(ctx, botContextWrapper.getCoordinatorListName(), botContextWrapper.getParticipantListName(), getCoordinatorFacetType().getURI(), getParticipantFacetType().getURI(), MILLIS_BETWEEN_MESSAGES, scriptConnectHook, "Hi!"));
    bus.subscribe(NeedCreatedEvent.class, this.needConnector);
    // for each group member, there are 2 listeners waiting for messages. when they are all finished, we're done.
    this.firstPhaseDoneListener = new ActionOnceAfterNEventsListener(ctx, "firstPhaseDoneListener", firstPhaseScriptListenerFilter, noOfNeeds - 1, new BaseEventBotAction(ctx) {

        @Override
        protected void doRun(final Event event, EventListener executingListener) throws Exception {
            logger.debug("starting second phase");
            for (TwoPhaseScriptListener listener : scriptListeners) {
                logger.debug("subscribing second phase listener {}", listener);
                // subscribe it to the relevant events.
                bus.subscribe(MessageFromOtherNeedEvent.class, listener.getSecondPhaseListener());
                bus.subscribe(SecondPhaseStartedEvent.class, listener.getSecondPhaseListener());
                listener.getSecondPhaseListener().setCoordinatorSideConnectionURI(listener.getFirstPhaseListener().getCoordinatorSideConnectionURI());
                listener.getSecondPhaseListener().setParticipantSideConnectionURI(listener.getFirstPhaseListener().getParticipantSideConnectionURI());
                listener.getSecondPhaseListener().updateFilterForBothConnectionURIs();
                bus.publish(new SecondPhaseStartedEvent(listener.getFirstPhaseListener().getCoordinatorURI(), listener.getFirstPhaseListener().getCoordinatorSideConnectionURI(), listener.getFirstPhaseListener().getParticipantURI()));
            }
        }
    });
    bus.subscribe(FinishedEvent.class, this.firstPhaseDoneListener);
    // 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) CreateNeedWithFacetsAction(won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction) EventBus(won.bot.framework.eventbot.bus.EventBus) OrFilter(won.bot.framework.eventbot.filter.impl.OrFilter) SignalWorkDoneAction(won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction) URI(java.net.URI) DeactivateAllNeedsOfListAction(won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsOfListAction) SecondPhaseStartedEvent(won.bot.framework.eventbot.listener.baStateBots.baCCMessagingBots.atomicBots.SecondPhaseStartedEvent) ConnectFromListToListAction(won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction) 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) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener) AcceptOnceFilter(won.bot.framework.eventbot.filter.impl.AcceptOnceFilter) FinishedEventFilter(won.bot.framework.eventbot.filter.impl.FinishedEventFilter) ParticipantCoordinatorBotContextWrapper(won.bot.framework.bot.context.ParticipantCoordinatorBotContextWrapper)

Example 9 with ConnectFromListToListAction

use of won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction in project webofneeds by researchstudio-sat.

the class StandardTwoPhaseCommitNoVoteBot method initializeEventListeners.

@Override
protected void initializeEventListeners() {
    EventListenerContext ctx = getEventListenerContext();
    EventBus bus = getEventBus();
    ParticipantCoordinatorBotContextWrapper botContextWrapper = (ParticipantCoordinatorBotContextWrapper) getBotContextWrapper();
    // create needs every trigger execution until noOfNeeds are created
    this.participantNeedCreator = new ActionOnEventListener(ctx, "participantCreator", new CreateNeedWithFacetsAction(ctx, botContextWrapper.getParticipantListName(), FacetType.ParticipantFacet.getURI()), noOfNeeds - 1);
    bus.subscribe(ActEvent.class, this.participantNeedCreator);
    // when done, create one coordinator need
    this.coordinatorNeedCreator = new ActionOnEventListener(ctx, "coordinatorCreator", new FinishedEventFilter(participantNeedCreator), new CreateNeedWithFacetsAction(ctx, botContextWrapper.getCoordinatorListName(), FacetType.CoordinatorFacet.getURI()), 1);
    bus.subscribe(FinishedEvent.class, this.coordinatorNeedCreator);
    // when done, connect the participants to the coordinator
    this.needConnector = new ActionOnceAfterNEventsListener(ctx, "needConnector", noOfNeeds, new ConnectFromListToListAction(ctx, botContextWrapper.getCoordinatorListName(), botContextWrapper.getParticipantListName(), FacetType.CoordinatorFacet.getURI(), FacetType.ParticipantFacet.getURI(), MILLIS_BETWEEN_MESSAGES, "Hi!"));
    bus.subscribe(NeedCreatedEvent.class, this.needConnector);
    // add a listener that is informed of the connect/open events and that auto-opens
    // subscribe it to:
    // * connect events - so it responds with open
    // * open events - so it responds with open (if the open received was the first open, and we still need to accept the connection)
    this.autoOpener = new ActionOnEventListener(ctx, "autoOpener", new OpenConnectionAction(ctx, "Hi!"));
    bus.subscribe(OpenFromOtherNeedEvent.class, this.autoOpener);
    bus.subscribe(ConnectFromOtherNeedEvent.class, this.autoOpener);
    this.autoCloser = new ActionOnceAfterNEventsListener(ctx, "autoCloser", noOfNeeds - 3, new CloseConnectionAction(ctx, "Bye!"));
    bus.subscribe(ConnectFromOtherNeedEvent.class, this.autoCloser);
    // after the last connect event, all connections are closed!
    this.needDeactivator = new ActionOnEventListener(ctx, new TwoPhaseCommitNoVoteDeactivateAllNeedsAction(ctx), 1);
    bus.subscribe(CloseFromOtherNeedEvent.class, this.needDeactivator);
    // add a listener that counts two NeedDeactivatedEvents and then tells the
    // framework that the bot's work is done
    this.workDoneSignaller = new ActionOnceAfterNEventsListener(ctx, noOfNeeds, new SignalWorkDoneAction(ctx));
    bus.subscribe(NeedDeactivatedEvent.class, this.workDoneSignaller);
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) ActionOnceAfterNEventsListener(won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener) CloseConnectionAction(won.bot.framework.eventbot.action.impl.wonmessage.CloseConnectionAction) ConnectFromListToListAction(won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction) CreateNeedWithFacetsAction(won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction) EventBus(won.bot.framework.eventbot.bus.EventBus) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) OpenConnectionAction(won.bot.framework.eventbot.action.impl.wonmessage.OpenConnectionAction) TwoPhaseCommitNoVoteDeactivateAllNeedsAction(won.bot.framework.eventbot.action.impl.facet.TwoPhaseCommitNoVoteDeactivateAllNeedsAction) SignalWorkDoneAction(won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction) FinishedEventFilter(won.bot.framework.eventbot.filter.impl.FinishedEventFilter) ParticipantCoordinatorBotContextWrapper(won.bot.framework.bot.context.ParticipantCoordinatorBotContextWrapper)

Example 10 with ConnectFromListToListAction

use of won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction in project webofneeds by researchstudio-sat.

the class BABaseBot method initializeEventListeners.

@Override
protected void initializeEventListeners() {
    final EventListenerContext ctx = getEventListenerContext();
    final EventBus bus = getEventBus();
    ParticipantCoordinatorBotContextWrapper botContextWrapper = (ParticipantCoordinatorBotContextWrapper) getBotContextWrapper();
    // create needs every trigger execution until noOfNeeds are created
    this.participantNeedCreator = new ActionOnEventListener(ctx, "participantCreator", new CreateNeedWithFacetsAction(ctx, botContextWrapper.getParticipantListName(), getParticipantFacetType().getURI()), noOfNeeds - 1);
    bus.subscribe(ActEvent.class, this.participantNeedCreator);
    // 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> scriptIterator = scripts.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 mainScriptListenerFilter = 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, scriptIterator.next(), fromNeedURI, toNeedURI, MILLIS_BETWEEN_MESSAGES);
            // remember it so we can check its state later
            testScriptListeners.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.
            mainScriptListenerFilter.addFilter(new AcceptOnceFilter(new FinishedEventFilter(testScriptListener)));
        }
    };
    // when done, connect the participants to the coordinator
    this.needConnector = new ActionOnceAfterNEventsListener(ctx, "needConnector", noOfNeeds, new ConnectFromListToListAction(ctx, botContextWrapper.getCoordinatorListName(), botContextWrapper.getParticipantListName(), getCoordinatorFacetType().getURI(), getParticipantFacetType().getURI(), MILLIS_BETWEEN_MESSAGES, scriptConnectHook, "Hi!"));
    bus.subscribe(NeedCreatedEvent.class, this.needConnector);
    // 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", mainScriptListenerFilter, 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) CreateNeedWithFacetsAction(won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction) EventBus(won.bot.framework.eventbot.bus.EventBus) OrFilter(won.bot.framework.eventbot.filter.impl.OrFilter) SignalWorkDoneAction(won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction) URI(java.net.URI) DeactivateAllNeedsOfListAction(won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsOfListAction) ConnectFromListToListAction(won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction) BATestScriptListener(won.bot.framework.eventbot.listener.baStateBots.BATestScriptListener) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) AcceptOnceFilter(won.bot.framework.eventbot.filter.impl.AcceptOnceFilter) FinishedEventFilter(won.bot.framework.eventbot.filter.impl.FinishedEventFilter) ParticipantCoordinatorBotContextWrapper(won.bot.framework.bot.context.ParticipantCoordinatorBotContextWrapper) BATestBotScript(won.bot.framework.eventbot.listener.baStateBots.BATestBotScript)

Aggregations

EventListenerContext (won.bot.framework.eventbot.EventListenerContext)10 SignalWorkDoneAction (won.bot.framework.eventbot.action.impl.lifecycle.SignalWorkDoneAction)10 CreateNeedWithFacetsAction (won.bot.framework.eventbot.action.impl.needlifecycle.CreateNeedWithFacetsAction)10 ConnectFromListToListAction (won.bot.framework.eventbot.action.impl.wonmessage.ConnectFromListToListAction)10 EventBus (won.bot.framework.eventbot.bus.EventBus)10 ActionOnEventListener (won.bot.framework.eventbot.listener.impl.ActionOnEventListener)10 ActionOnceAfterNEventsListener (won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener)10 OpenConnectionAction (won.bot.framework.eventbot.action.impl.wonmessage.OpenConnectionAction)7 EventListener (won.bot.framework.eventbot.listener.EventListener)7 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)6 Event (won.bot.framework.eventbot.event.Event)6 ActEvent (won.bot.framework.eventbot.event.impl.lifecycle.ActEvent)6 NeedCreatedEvent (won.bot.framework.eventbot.event.impl.needlifecycle.NeedCreatedEvent)6 BaseEventListener (won.bot.framework.eventbot.listener.BaseEventListener)6 DeactivateAllNeedsOfListAction (won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsOfListAction)5 FinishedEventFilter (won.bot.framework.eventbot.filter.impl.FinishedEventFilter)5 ParticipantCoordinatorBotContextWrapper (won.bot.framework.bot.context.ParticipantCoordinatorBotContextWrapper)4 DeactivateAllNeedsAction (won.bot.framework.eventbot.action.impl.needlifecycle.DeactivateAllNeedsAction)4 CloseConnectionAction (won.bot.framework.eventbot.action.impl.wonmessage.CloseConnectionAction)4 ConnectFromOtherNeedEvent (won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent)4