use of won.bot.framework.eventbot.action.BaseEventBotAction in project webofneeds by researchstudio-sat.
the class DebugBot method initializeEventListeners.
@Override
protected void initializeEventListeners() {
String welcomeMessage = "Greetings! \nI am the DebugBot. I " + "can simulate multiple other users so you can test things. I understand a few commands. \nTo see which ones, " + "type 'usage'.";
String welcomeHelpMessage = "When connecting with me, you can say 'ignore', or 'deny' to make me ignore or deny requests, and 'wait N' to make me wait N seconds (max 99) before reacting.";
EventListenerContext ctx = getEventListenerContext();
EventBus bus = getEventBus();
// eagerly cache RDF data
BotBehaviour eagerlyCacheBehaviour = new EagerlyPopulateCacheBehaviour(ctx);
eagerlyCacheBehaviour.activate();
// react to a bot command activating/deactivating eager caching
bus.subscribe(SetCacheEagernessCommandEvent.class, new ActionOnEventListener(ctx, new BaseEventBotAction(ctx) {
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
if (event instanceof SetCacheEagernessCommandEvent) {
if (((SetCacheEagernessCommandEvent) event).isEager()) {
eagerlyCacheBehaviour.activate();
} else {
eagerlyCacheBehaviour.deactivate();
}
}
}
}));
// react to a message that was not identified as a debug command
BotBehaviour connectionMessageBehaviour = new ConnectionMessageBehaviour(ctx);
connectionMessageBehaviour.activate();
// react to the debug deactivate command (deactivate my need)
BotBehaviour deactivateNeedBehaviour = new DeactivateNeedBehaviour(ctx);
deactivateNeedBehaviour.activate();
// react to the close behaviour
BotBehaviour closeBehaviour = new CloseBevahiour(ctx);
closeBehaviour.activate();
// register with WoN nodes, be notified when new needs are created
RegisterMatcherAction registerMatcherAction = new RegisterMatcherAction(ctx);
this.matcherRegistrator = new ActionOnEventListener(ctx, registerMatcherAction, 1);
bus.subscribe(ActEvent.class, this.matcherRegistrator);
RandomDelayedAction delayedRegistration = new RandomDelayedAction(ctx, registrationMatcherRetryInterval, registrationMatcherRetryInterval, 0, registerMatcherAction);
ActionOnEventListener matcherRetryRegistrator = new ActionOnEventListener(ctx, delayedRegistration);
bus.subscribe(MatcherRegisterFailedEvent.class, matcherRetryRegistrator);
// create the echo need for debug initial connect - if we're not reacting to the creation of our own echo need.
CreateDebugNeedWithFacetsAction needForInitialConnectAction = new CreateDebugNeedWithFacetsAction(ctx, true, true);
needForInitialConnectAction.setIsInitialForConnect(true);
ActionOnEventListener initialConnector = new ActionOnEventListener(ctx, new NotFilter(new NeedUriInNamedListFilter(ctx, ctx.getBotContextWrapper().getNeedCreateListName())), needForInitialConnectAction);
bus.subscribe(NeedCreatedEventForMatcher.class, initialConnector);
// create the echo need for debug initial hint - if we're not reacting to the creation of our own echo need.
CreateDebugNeedWithFacetsAction initialHinter = new CreateDebugNeedWithFacetsAction(ctx, true, true);
initialHinter.setIsInitialForHint(true);
ActionOnEventListener needForInitialHintListener = new ActionOnEventListener(ctx, new NotFilter(new NeedUriInNamedListFilter(ctx, ctx.getBotContextWrapper().getNeedCreateListName())), initialHinter);
bus.subscribe(NeedCreatedEventForMatcher.class, needForInitialHintListener);
// as soon as the echo need triggered by debug connect created, connect to original
this.needConnector = new ActionOnEventListener(ctx, "needConnector", new RandomDelayedAction(ctx, CONNECT_DELAY_MILLIS, CONNECT_DELAY_MILLIS, 1, new ConnectWithAssociatedNeedAction(ctx, FacetType.OwnerFacet.getURI(), FacetType.OwnerFacet.getURI(), welcomeMessage + " " + welcomeHelpMessage)));
bus.subscribe(NeedCreatedEventForDebugConnect.class, this.needConnector);
// as soon as the echo need triggered by debug hint command created, hint to original
this.needHinter = new ActionOnEventListener(ctx, "needHinter", new RandomDelayedAction(ctx, CONNECT_DELAY_MILLIS, CONNECT_DELAY_MILLIS, 1, new HintAssociatedNeedAction(ctx, FacetType.OwnerFacet.getURI(), FacetType.OwnerFacet.getURI(), matcherUri)));
bus.subscribe(NeedCreatedEventForDebugHint.class, this.needHinter);
// if the original need wants to connect - always open
this.autoOpener = new ActionOnEventListener(ctx, new MultipleActions(ctx, new OpenConnectionDebugAction(ctx, welcomeMessage, welcomeHelpMessage), new PublishSetChattinessEventAction(ctx, true)));
bus.subscribe(ConnectFromOtherNeedEvent.class, this.autoOpener);
EventBotAction userCommandAction = new DebugBotIncomingMessageToEventMappingAction(ctx);
// if the remote side opens, send a greeting and set to chatty.
bus.subscribe(OpenFromOtherNeedEvent.class, new ActionOnEventListener(ctx, new MultipleActions(ctx, userCommandAction, new PublishSetChattinessEventAction(ctx, true))));
// if the bot receives a text message - try to map the command of the text message to a DebugEvent
messageFromOtherNeedListener = new ActionOnEventListener(ctx, userCommandAction);
bus.subscribe(MessageFromOtherNeedEvent.class, messageFromOtherNeedListener);
// react to usage command event
this.usageMessageSender = new ActionOnEventListener(ctx, new SendMultipleMessagesAction(ctx, DebugBotIncomingMessageToEventMappingAction.USAGE_MESSAGES));
bus.subscribe(UsageDebugCommandEvent.class, usageMessageSender);
bus.subscribe(CloseCommandSuccessEvent.class, new ActionOnEventListener(ctx, "chattiness off", new PublishSetChattinessEventAction(ctx, false)));
// react to close event: set connection to not chatty
bus.subscribe(CloseFromOtherNeedEvent.class, new ActionOnEventListener(ctx, new PublishSetChattinessEventAction(ctx, false)));
// react to the hint and connect commands by creating a need (it will fire correct need created for connect/hint
// events)
needCreator = new ActionOnEventListener(ctx, new CreateDebugNeedWithFacetsAction(ctx, true, true));
bus.subscribe(HintDebugCommandEvent.class, needCreator);
bus.subscribe(ConnectDebugCommandEvent.class, needCreator);
bus.subscribe(SendNDebugCommandEvent.class, new ActionOnEventListener(ctx, new SendNDebugMessagesAction(ctx, DELAY_BETWEEN_N_MESSAGES, DebugBotIncomingMessageToEventMappingAction.N_MESSAGES)));
MessageTimingManager timingManager = new MessageTimingManager(ctx, 20);
// on every actEvent there is a chance we send a chatty message
bus.subscribe(ActEvent.class, new ActionOnEventListener(ctx, new SendChattyMessageAction(ctx, CHATTY_MESSAGE_PROBABILITY, timingManager, DebugBotIncomingMessageToEventMappingAction.RANDOM_MESSAGES, DebugBotIncomingMessageToEventMappingAction.LAST_MESSAGES)));
// set the chattiness of the connection
bus.subscribe(SetChattinessDebugCommandEvent.class, new ActionOnEventListener(ctx, new SetChattinessAction(ctx)));
// process eliza messages with eliza
bus.subscribe(MessageToElizaEvent.class, new ActionOnEventListener(ctx, new AnswerWithElizaAction(ctx, 20)));
// remember when we sent the last message
bus.subscribe(WonMessageSentOnConnectionEvent.class, new ActionOnEventListener(ctx, new RecordMessageSentTimeAction(ctx, timingManager)));
// remember when we got the last message
bus.subscribe(WonMessageReceivedOnConnectionEvent.class, new ActionOnEventListener(ctx, new RecordMessageReceivedTimeAction(ctx, timingManager)));
// initialize the sent timestamp when the open message is received
bus.subscribe(OpenFromOtherNeedEvent.class, new ActionOnEventListener(ctx, new RecordMessageSentTimeAction(ctx, timingManager)));
// initialize the sent timestamp when the connect message is received
bus.subscribe(ConnectFromOtherNeedEvent.class, new ActionOnEventListener(ctx, new RecordMessageSentTimeAction(ctx, timingManager)));
}
use of won.bot.framework.eventbot.action.BaseEventBotAction in project webofneeds by researchstudio-sat.
the class GroupCycleBot method initializeEventListeners.
@Override
protected void initializeEventListeners() {
EventListenerContext ctx = getEventListenerContext();
// start with a friendly message
ctx.getEventBus().subscribe(InitializeEvent.class, new ActionOnFirstEventListener(ctx, new BaseEventBotAction(ctx) {
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
logger.info("");
logger.info("We will create {} groups with {} members each.", NUMBER_OF_GROUPS, NUMBER_OF_GROUPMEMBERS);
logger.info("The groups all be connected to each other, resulting in {} group-group connections", NUMBER_OF_GROUPS * (NUMBER_OF_GROUPS - 1) / 2);
logger.info("Then, one group member will send a message to its group, which should reach all other group members exactly once");
logger.info("This will result in {} messages being received.", NUMBER_OF_GROUPS * NUMBER_OF_GROUPMEMBERS - 1);
logger.info("The groups will forward {} messages and suppress {} duplicates", NUMBER_OF_GROUPS * (NUMBER_OF_GROUPS + NUMBER_OF_GROUPMEMBERS - 2), (int) Math.pow(NUMBER_OF_GROUPS, 2) - 3 * NUMBER_OF_GROUPS + 2);
logger.info("");
}
}));
// understand message commands
BotBehaviour messageCommandBehaviour = new ExecuteWonMessageCommandBehaviour(ctx);
messageCommandBehaviour.activate();
// if we receive a connection message, log it
BotBehaviour logConnectionMessageBehaviour = new LogConnectionMessageBehaviour(ctx);
logConnectionMessageBehaviour.activate();
// log other important events (group/member creation and conneciton)
BotBehaviour infoBehaviour = new OutputInfoMessagesBehaviour(ctx);
infoBehaviour.activate();
// wait for both groups to finish being set up, then connect the groups
BehaviourBarrier barrier = new BehaviourBarrier(ctx);
for (int i = 0; i < NUMBER_OF_GROUPS; i++) {
// create group 1, its members, and connect them
CreateGroupBehaviour groupCreate = new CreateGroupBehaviour(ctx);
OpenOnConnectBehaviour groupOpenOnConnect = new OpenOnConnectBehaviour(ctx);
CreateGroupMembersBehaviour groupMembers = new CreateGroupMembersBehaviour(ctx);
groupCreate.onDeactivateActivate(groupOpenOnConnect, groupMembers);
barrier.waitFor(groupMembers);
// wait for the initialize event and trigger group creation
ctx.getEventBus().subscribe(InitializeEvent.class, new ActionOnFirstEventListener(ctx, new BaseEventBotAction(ctx) {
@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
groupCreate.activate();
}
}));
}
BotBehaviour connectGroupsBehaviour = new ConnectGroupsBehaviour(ctx);
barrier.thenStart(connectGroupsBehaviour);
barrier.activate();
// after connecting the groups, send one message on behalf of one of the group members
// and count the messages that group members receive
// when all groups are connected, start the count behaviour
CountReceivedMessagesBehaviour countReceivedMessagesBehaviour = new CountReceivedMessagesBehaviour(ctx);
connectGroupsBehaviour.onDeactivateActivate(countReceivedMessagesBehaviour);
// wait for the count behaviour to have started, then send the group message
BotBehaviour sendInitialMessageBehaviour = new SendOneMessageBehaviour(ctx);
countReceivedMessagesBehaviour.onActivateActivate(sendInitialMessageBehaviour);
}
use of won.bot.framework.eventbot.action.BaseEventBotAction 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.action.BaseEventBotAction in project webofneeds by researchstudio-sat.
the class LastSeenNeedsMatcherBot method initializeEventListeners.
@Override
protected void initializeEventListeners() {
EventListenerContext ctx = getEventListenerContext();
EventBus bus = getEventBus();
// subscribe this bot with the WoN nodes' 'new need' topic
RegisterMatcherAction registerMatcherAction = new RegisterMatcherAction(ctx);
this.matcherRegistrator = new ActionOnEventListener(ctx, registerMatcherAction, 1);
bus.subscribe(ActEvent.class, this.matcherRegistrator);
RandomDelayedAction delayedRegistration = new RandomDelayedAction(ctx, registrationMatcherRetryInterval, registrationMatcherRetryInterval, 0, registerMatcherAction);
ActionOnEventListener matcherRetryRegistrator = new ActionOnEventListener(ctx, delayedRegistration);
bus.subscribe(MatcherRegisterFailedEvent.class, matcherRetryRegistrator);
bus.subscribe(NeedCreatedEventForMatcher.class, new ActionOnEventListener(ctx, "lastSeenNeedsMatcher", new BaseEventBotAction(ctx) {
@Override
protected void doRun(final Event event, EventListener executingListener) throws Exception {
NeedCreatedEventForMatcher needCreatedEvent = (NeedCreatedEventForMatcher) event;
URI currentNeedURI = needCreatedEvent.getNeedURI();
URI lastNeedURI = lastNeedUriReference.getAndSet(currentNeedURI);
URI originator = matcherUri;
if (lastNeedURI == null) {
logger.info("First invocation. Remembering {} for matching it later", currentNeedURI);
return;
} else {
logger.info("Sending hint for {} and {}", currentNeedURI, lastNeedURI);
}
ctx.getMatcherProtocolNeedServiceClient().hint(currentNeedURI, lastNeedURI, 0.5, originator, null, createWonMessage(currentNeedURI, lastNeedURI, 0.5, originator));
ctx.getMatcherProtocolNeedServiceClient().hint(lastNeedURI, currentNeedURI, 0.5, originator, null, createWonMessage(lastNeedURI, currentNeedURI, 0.5, originator));
}
}));
}
use of won.bot.framework.eventbot.action.BaseEventBotAction 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);
}
Aggregations