use of won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener 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);
}
use of won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener in project webofneeds by researchstudio-sat.
the class ConversationBotMonitored 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, "Hello," + "I am the ConversationBot, a simple bot that will exchange " + "messages and deactivate its needs after some time."));
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 ConverssationBot."));
bus.subscribe(ConnectFromOtherNeedEvent.class, this.autoOpener);
// add a listener that auto-responds to messages by a message and at different stages of messages processing fires
// different monitoring events. After specified number of 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 AutomaticMonitoredMessageResponderListener(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, "Farewell!"));
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, "Farewell!"));
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 messaging work is done and connection messages linked data can be crawled
this.crawlReadySignaller = new ActionOnceAfterNEventsListener(ctx, NO_OF_NEEDS, new BaseEventBotAction(ctx) {
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
bus.publish(new CrawlReadyEvent());
}
});
bus.subscribe(NeedDeactivatedEvent.class, this.crawlReadySignaller);
// add a listener that, when crawl is done, tells the
// framework that the bot's work is done
this.workDoneSignaller = new ActionOnEventListener(ctx, new SignalWorkDoneAction(ctx));
bus.subscribe(CrawlDoneEvent.class, this.workDoneSignaller);
// add a listener that reacts to monitoring events
this.monitor = new ActionOnEventListener(ctx, "msgMonitor", new MessageLifecycleMonitoringAction(ctx));
bus.subscribe(MessageDispatchStartedEvent.class, this.monitor);
bus.subscribe(MessageDispatchedEvent.class, this.monitor);
bus.subscribe(SuccessResponseEvent.class, this.monitor);
bus.subscribe(MessageFromOtherNeedEvent.class, this.monitor);
bus.subscribe(CrawlReadyEvent.class, this.monitor);
}
use of won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener in project webofneeds by researchstudio-sat.
the class GroupingBot method initializeEventListeners.
@Override
protected void initializeEventListeners() {
final EventListenerContext ctx = getEventListenerContext();
GroupBotContextWrapper botContextWrapper = (GroupBotContextWrapper) getBotContextWrapper();
EventBus bus = getEventBus();
// for each created need (in the group), add a listener that will auto-respond to messages directed at that need
// create a filter that only accepts events for needs in the group:
NeedUriInNamedListFilter groupMemberFilter = new NeedUriInNamedListFilter(ctx, botContextWrapper.getGroupMembersListName());
// remember the auto-responders in a list
this.autoResponders = new ArrayList<BaseEventListener>();
// remember the listeners that wait for all messages
this.messageCounters = new ArrayList<BaseEventListener>();
// make a composite filter, with one filter for each autoResponder that wait for the FinishedEvents the responders emit.
// that filter will be used to shut down all needs after all the autoResponders have finished.
final OrFilter mainAutoResponderFilter = new OrFilter();
// listen to NeedCreatedEvents
this.autoResponderCreator = new ActionOnEventListener(ctx, groupMemberFilter, new BaseEventBotAction(ctx) {
@Override
protected void doRun(final Event event, EventListener executingListener) throws Exception {
// create a listener that automatically answers messages, only for that need URI. We let it send NO_OF_MESSAGES messages
logger.debug("created auto responder");
AutomaticMessageResponderListener listener = new AutomaticMessageResponderListener(ctx, "autoResponder", NeedUriEventFilter.forEvent(event), NO_OF_MESSAGES, MILLIS_BETWEEN_MESSAGES);
// create a listener that publishes a FinishedEvent after having received all messages from the group
WaitForNEventsListener waitForMessagesListener = new WaitForNEventsListener(ctx, "messageCounter", NeedUriEventFilter.forEvent(event), NO_OF_MESSAGES * (NO_OF_GROUPMEMBERS - 1));
messageCounters.add(waitForMessagesListener);
// add a filter that will wait for the FinishedEvent emitted by that listener
// wrap it in an acceptonce filter to make extra sure we count each listener only once.
mainAutoResponderFilter.addFilter(new AcceptOnceFilter(new FinishedEventFilter(waitForMessagesListener)));
ActionOnEventListener debugger = new ActionOnEventListener(ctx, NeedUriEventFilter.forEvent(event), new BaseEventBotAction(ctx) {
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
if (event instanceof MessageFromOtherNeedEvent) {
MessageFromOtherNeedEvent msg = (MessageFromOtherNeedEvent) event;
logger.debug("processing event {} wonMessage {} - text message '{}', sent by {} to {}", new Object[] { event.toString(), msg.getWonMessage().getMessageURI(), WonRdfUtils.MessageUtils.getTextMessage(msg.getWonMessage()), msg.getRemoteNeedURI(), msg.getNeedURI() });
}
}
});
getEventBus().subscribe(MessageFromOtherNeedEvent.class, debugger);
// finally, subscribe to the message events
getEventBus().subscribe(MessageFromOtherNeedEvent.class, waitForMessagesListener);
getEventBus().subscribe(MessageFromOtherNeedEvent.class, listener);
}
});
getEventBus().subscribe(NeedCreatedEvent.class, this.autoResponderCreator);
// count until N needs were created, then create need with group facet (the others will connect to that facet)
this.groupCreator = new ActionOnceAfterNEventsListener(ctx, "groupCreator", NO_OF_GROUPMEMBERS, new CreateNeedWithFacetsAction(ctx, botContextWrapper.getGroupListName(), FacetType.GroupFacet.getURI()));
bus.subscribe(NeedCreatedEvent.class, this.groupCreator);
// wait for N+1 needCreatedEvents, then connect the members with the group facet of the third need
this.needConnector = new ActionOnceAfterNEventsListener(ctx, "needConnector", NO_OF_GROUPMEMBERS + 1, new ConnectFromListToListAction(ctx, botContextWrapper.getGroupListName(), botContextWrapper.getGroupMembersListName(), FacetType.GroupFacet.getURI(), FacetType.OwnerFacet.getURI(), MILLIS_BETWEEN_MESSAGES, "Hi from the " + "GroupingBot!"));
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 from the GroupingBot!"));
bus.subscribe(ConnectFromOtherNeedEvent.class, this.autoOpener);
// now, once all connections have been opened, make 1 bot send a message to the group, the subsequent listener will cause let wild chatting to begin
this.conversationStarter = new ActionOnceAfterNEventsListener(ctx, "conversationStarter", NO_OF_GROUPMEMBERS, new RespondToMessageAction(ctx, MILLIS_BETWEEN_MESSAGES));
bus.subscribe(OpenFromOtherNeedEvent.class, this.conversationStarter);
// for each group member, there are 2 listeners waiting for messages. when they are all finished, we're done.
this.messagesDoneListener = new ActionOnceAfterNEventsListener(ctx, "messagesDoneListener", mainAutoResponderFilter, NO_OF_GROUPMEMBERS, new DeactivateAllNeedsOfListAction(ctx, botContextWrapper.getGroupMembersListName()));
bus.subscribe(FinishedEvent.class, this.messagesDoneListener);
// When the group facet need is deactivated, all connections are closed. wait for the close events and signal work done.
this.workDoneSignaller = new ActionOnceAfterNEventsListener(ctx, "workDoneSignaller", NO_OF_GROUPMEMBERS, new SignalWorkDoneAction(ctx));
bus.subscribe(CloseFromOtherNeedEvent.class, this.workDoneSignaller);
// start the whole thing:
// create needs every trigger execution until N needs are created
this.groupMemberCreator = new ActionOnEventListener(ctx, "groupMemberCreator", new CreateNeedWithFacetsAction(ctx, botContextWrapper.getGroupMembersListName(), FacetType.OwnerFacet.getURI()), NO_OF_GROUPMEMBERS);
bus.subscribe(ActEvent.class, this.groupMemberCreator);
}
use of won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener in project webofneeds by researchstudio-sat.
the class RandomSimulatorBot method initializeEventListeners.
@Override
protected void initializeEventListeners() {
final EventListenerContext ctx = getEventListenerContext();
EventBus bus = getEventBus();
final Counter needCreationSuccessfulCounter = new CounterImpl("needsCreated");
final Counter needCreationFailedCounter = new CounterImpl("needCreationFailed");
final Counter needCreationStartedCounter = new CounterImpl("creationStarted");
final Counter creationUnfinishedCounter = new CounterImpl("creationUnfinished");
// create the first need when the first actEvent happens
this.groupMemberCreator = new ActionOnceAfterNEventsListener(ctx, "groupMemberCreator", 1, new MultipleActions(ctx, new IncrementCounterAction(ctx, needCreationStartedCounter), new IncrementCounterAction(ctx, creationUnfinishedCounter), new CreateNeedWithFacetsAction(ctx, getBotContextWrapper().getNeedCreateListName())));
bus.subscribe(ActEvent.class, this.groupMemberCreator);
// when a need is created (or it failed), decrement the creationUnfinishedCounter
EventListener downCounter = new ActionOnEventListener(ctx, "downCounter", new DecrementCounterAction(ctx, creationUnfinishedCounter));
// count a successful need creation
bus.subscribe(NeedCreatedEvent.class, downCounter);
// if a creation failed, we don't want to keep us from keeping the correct count
bus.subscribe(NeedCreationFailedEvent.class, downCounter);
// we count the one execution when the creator realizes that the producer is exhausted, we have to count down
// once for that, too.
bus.subscribe(NeedProducerExhaustedEvent.class, downCounter);
// also, keep track of what worked and what didn't
bus.subscribe(NeedCreationFailedEvent.class, new ActionOnEventListener(ctx, new IncrementCounterAction(ctx, needCreationFailedCounter)));
bus.subscribe(NeedCreatedEvent.class, new ActionOnEventListener(ctx, new IncrementCounterAction(ctx, needCreationSuccessfulCounter)));
// print a logging message every N needs
bus.subscribe(NeedCreatedEvent.class, new ActionOnEventListener(ctx, "logger", new BaseEventBotAction(ctx) {
int lastOutput = 0;
@Override
protected void doRun(final Event event, EventListener executingListener) throws Exception {
int cnt = needCreationStartedCounter.getCount();
int unfinishedCount = creationUnfinishedCounter.getCount();
int successCnt = needCreationSuccessfulCounter.getCount();
int failedCnt = needCreationFailedCounter.getCount();
if (cnt - lastOutput >= 200) {
logger.info("started creation of {} needs, creation not yet finished for {}. Successful: {}, failed: {}", new Object[] { cnt, unfinishedCount, successCnt, failedCnt });
lastOutput = cnt;
}
}
}));
// each time a need was created, wait for a random interval, then create another one
bus.subscribe(NeedCreatedEvent.class, new ActionOnEventListener(ctx, new RandomDelayedAction(ctx, MIN_NEXT_CREATION_TIMEOUT_MILLIS, MAX_NEXT_CREATION_TIMEOUT_MILLIS, this.hashCode(), new CreateNeedWithFacetsAction(ctx, getBotContextWrapper().getNeedCreateListName()))));
// when a hint is received, connect fraction of the cases after a random timeout
bus.subscribe(HintFromMatcherEvent.class, new ActionOnEventListener(ctx, "hint-reactor", new RandomDelayedAction(ctx, MIN_RECATION_TIMEOUT_MILLIS, MAX_REACTION_TIMEOUT_MILLIS, (long) this.hashCode(), new MultipleActions(ctx, new SendFeedbackForHintAction(ctx), new ProbabilisticSelectionAction(ctx, PROB_OPEN_ON_HINT, (long) this.hashCode(), new OpenConnectionAction(ctx, "Hi!"), new CloseConnectionAction(ctx, "Bye!"))))));
// when an open or connect is received, send message or close randomly after a random timeout
EventListener opener = new ActionOnEventListener(ctx, "open-reactor", new RandomDelayedAction(ctx, MIN_RECATION_TIMEOUT_MILLIS, MAX_REACTION_TIMEOUT_MILLIS, (long) this.hashCode(), new ProbabilisticSelectionAction(ctx, PROB_MESSAGE_ON_OPEN, (long) this.hashCode(), new OpenConnectionAction(ctx, "Hi!"), new CloseConnectionAction(ctx, "Bye!"))));
bus.subscribe(OpenFromOtherNeedEvent.class, opener);
bus.subscribe(ConnectFromOtherNeedEvent.class, opener);
// when an open is received, send message or close randomly after a random timeout
EventListener replyer = new ActionOnEventListener(ctx, "message-reactor", new RandomDelayedAction(ctx, MIN_RECATION_TIMEOUT_MILLIS, MAX_REACTION_TIMEOUT_MILLIS, (long) this.hashCode(), new ProbabilisticSelectionAction(ctx, PROB_MESSAGE_ON_MESSAGE, (long) this.hashCode(), new SendMessageAction(ctx), new CloseConnectionAction(ctx, "Bye!"))));
bus.subscribe(MessageFromOtherNeedEvent.class, replyer);
bus.subscribe(OpenFromOtherNeedEvent.class, replyer);
// When the needproducer is exhausted, stop.
this.workDoneSignaller = new ActionOnEventListener(ctx, "workDoneSignaller", new SignalWorkDoneAction(ctx), 1);
bus.subscribe(NeedProducerExhaustedEvent.class, this.workDoneSignaller);
}
use of won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener in project webofneeds by researchstudio-sat.
the class StandardTwoPhaseCommitBot 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);
// wait for N NeedCreatedEvents
creationWaiter = new WaitForNEventsListener(ctx, noOfNeeds);
bus.subscribe(NeedCreatedEvent.class, creationWaiter);
// when done, connect the participants to the coordinator
this.needConnector = new ActionOnEventListener(ctx, "needConnector", new FinishedEventFilter(creationWaiter), new ConnectFromListToListAction(ctx, botContextWrapper.getCoordinatorListName(), botContextWrapper.getParticipantListName(), FacetType.CoordinatorFacet.getURI(), FacetType.ParticipantFacet.getURI(), MILLIS_BETWEEN_MESSAGES, "Hi!"), 1);
bus.subscribe(FinishedEvent.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 NeedUriInNamedListFilter(ctx, botContextWrapper.getParticipantListName()), new OpenConnectionAction(ctx, "Hi!"));
bus.subscribe(ConnectFromOtherNeedEvent.class, this.autoOpener);
// after the last connect event, all connections are closed!
this.participantDeactivator = new ActionOnEventListener(ctx, "participantDeactivator", new NeedUriInNamedListFilter(ctx, botContextWrapper.getParticipantListName()), new TwoPhaseCommitDeactivateOnCloseAction(ctx), noOfNeeds - 1);
bus.subscribe(CloseFromOtherNeedEvent.class, this.participantDeactivator);
coordinatorDeactivator = new ActionOnEventListener(ctx, "coordinatorDeactivator", new FinishedEventFilter(participantDeactivator), new DeactivateAllNeedsOfListAction(ctx, botContextWrapper.getCoordinatorListName()), 1);
bus.subscribe(FinishedEvent.class, coordinatorDeactivator);
// 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);
}
Aggregations