Search in sources :

Example 1 with BehaviourBarrier

use of won.bot.framework.eventbot.behaviour.BehaviourBarrier 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);
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) BotBehaviour(won.bot.framework.eventbot.behaviour.BotBehaviour) BehaviourBarrier(won.bot.framework.eventbot.behaviour.BehaviourBarrier) ExecuteWonMessageCommandBehaviour(won.bot.framework.eventbot.behaviour.ExecuteWonMessageCommandBehaviour) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) MessageFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.MessageFromOtherNeedEvent) CreateNeedCommandResultEvent(won.bot.framework.eventbot.event.impl.command.create.CreateNeedCommandResultEvent) InitializeEvent(won.bot.framework.eventbot.event.impl.lifecycle.InitializeEvent) BaseEvent(won.bot.framework.eventbot.event.BaseEvent) BaseNeedSpecificEvent(won.bot.framework.eventbot.event.BaseNeedSpecificEvent) OpenCommandEvent(won.bot.framework.eventbot.event.impl.command.open.OpenCommandEvent) 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) ConnectionMessageCommandEvent(won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent) CommandEvent(won.bot.framework.eventbot.event.impl.cmd.CommandEvent) CreateNeedCommandEvent(won.bot.framework.eventbot.event.impl.command.create.CreateNeedCommandEvent) ConnectCommandResultEvent(won.bot.framework.eventbot.event.impl.command.connect.ConnectCommandResultEvent) ConnectCommandEvent(won.bot.framework.eventbot.event.impl.command.connect.ConnectCommandEvent) ActionOnFirstEventListener(won.bot.framework.eventbot.listener.impl.ActionOnFirstEventListener) ActionOnEventListener(won.bot.framework.eventbot.listener.impl.ActionOnEventListener) EventListener(won.bot.framework.eventbot.listener.EventListener) ActionOnFirstEventListener(won.bot.framework.eventbot.listener.impl.ActionOnFirstEventListener)

Aggregations

EventListenerContext (won.bot.framework.eventbot.EventListenerContext)1 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)1 BehaviourBarrier (won.bot.framework.eventbot.behaviour.BehaviourBarrier)1 BotBehaviour (won.bot.framework.eventbot.behaviour.BotBehaviour)1 ExecuteWonMessageCommandBehaviour (won.bot.framework.eventbot.behaviour.ExecuteWonMessageCommandBehaviour)1 BaseEvent (won.bot.framework.eventbot.event.BaseEvent)1 BaseNeedSpecificEvent (won.bot.framework.eventbot.event.BaseNeedSpecificEvent)1 Event (won.bot.framework.eventbot.event.Event)1 CommandEvent (won.bot.framework.eventbot.event.impl.cmd.CommandEvent)1 ConnectCommandEvent (won.bot.framework.eventbot.event.impl.command.connect.ConnectCommandEvent)1 ConnectCommandResultEvent (won.bot.framework.eventbot.event.impl.command.connect.ConnectCommandResultEvent)1 ConnectionMessageCommandEvent (won.bot.framework.eventbot.event.impl.command.connectionmessage.ConnectionMessageCommandEvent)1 CreateNeedCommandEvent (won.bot.framework.eventbot.event.impl.command.create.CreateNeedCommandEvent)1 CreateNeedCommandResultEvent (won.bot.framework.eventbot.event.impl.command.create.CreateNeedCommandResultEvent)1 OpenCommandEvent (won.bot.framework.eventbot.event.impl.command.open.OpenCommandEvent)1 InitializeEvent (won.bot.framework.eventbot.event.impl.lifecycle.InitializeEvent)1 ConnectFromOtherNeedEvent (won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent)1 MessageFromOtherNeedEvent (won.bot.framework.eventbot.event.impl.wonmessage.MessageFromOtherNeedEvent)1 OpenFromOtherNeedEvent (won.bot.framework.eventbot.event.impl.wonmessage.OpenFromOtherNeedEvent)1 EventListener (won.bot.framework.eventbot.listener.EventListener)1