use of won.bot.framework.eventbot.bus.EventBus 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);
}
Aggregations