Search in sources :

Example 1 with ConnectionSpecificEvent

use of won.bot.framework.eventbot.event.ConnectionSpecificEvent in project webofneeds by researchstudio-sat.

the class OpenConnectionDebugAction method doRun.

@Override
public void doRun(final Event event, EventListener executingListener) throws Exception {
    if (!(event instanceof ConnectFromOtherNeedEvent)) {
        return;
    }
    if (event instanceof WonMessageReceivedOnConnectionEvent) {
        WonMessage msg = ((WonMessageReceivedOnConnectionEvent) event).getWonMessage();
        String message = WonRdfUtils.MessageUtils.getTextMessage(msg);
        if (message == null) {
            message = "";
        }
        Matcher ignoreMatcher = PATTERN_IGNORE.matcher(message);
        if (ignoreMatcher.find()) {
            logger.debug("not reacting to incoming message of type {} as the welcome message contained 'ignore'", msg.getMessageType());
            return;
        }
        Matcher waitMatcher = PATTERN_WAIT.matcher(message);
        final boolean wait = waitMatcher.find();
        int waitSeconds = 15;
        if (wait && waitMatcher.groupCount() == 2) {
            waitSeconds = Integer.parseInt(waitMatcher.group(2));
        }
        Matcher denyMatcher = PATTERN_DENY.matcher(message);
        final boolean deny = denyMatcher.find();
        ConnectionSpecificEvent connectEvent = (ConnectionSpecificEvent) event;
        logger.debug("auto-replying to connect for connection {}", connectEvent.getConnectionURI());
        URI connectionUri = connectEvent.getConnectionURI();
        String finalWelcomeMessage = welcomeMessage;
        if (wait || deny) {
            finalWelcomeMessage = welcomeMessage + " " + (deny ? "Denying" : "Accepting") + " your request " + (wait ? " after a timeout of " + waitSeconds + " seconds" : "");
        } else {
            finalWelcomeMessage = welcomeMessage + " " + welcomeHelpMessage;
        }
        final WonMessage toSend = deny ? createCloseWonMessage(connectionUri, finalWelcomeMessage) : createOpenWonMessage(connectionUri, finalWelcomeMessage);
        Runnable task = () -> {
            getEventListenerContext().getWonMessageSender().sendWonMessage(toSend);
        };
        if (wait) {
            Date when = new Date(System.currentTimeMillis() + waitSeconds * 1000);
            getEventListenerContext().getTaskScheduler().schedule(task, when);
        } else {
            task.run();
        }
    }
}
Also used : ConnectionSpecificEvent(won.bot.framework.eventbot.event.ConnectionSpecificEvent) WonMessageReceivedOnConnectionEvent(won.bot.framework.eventbot.event.impl.wonmessage.WonMessageReceivedOnConnectionEvent) Matcher(java.util.regex.Matcher) WonMessage(won.protocol.message.WonMessage) URI(java.net.URI) ConnectFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent) Date(java.util.Date)

Example 2 with ConnectionSpecificEvent

use of won.bot.framework.eventbot.event.ConnectionSpecificEvent in project webofneeds by researchstudio-sat.

the class ExecuteSendMessageCommandAction method makeLogMessageString.

private String makeLogMessageString(Event event) {
    StringBuilder sb = new StringBuilder();
    MessageCommandEvent messageCommandEvent = (MessageCommandEvent) event;
    sb.append("sending message of type ").append(messageCommandEvent.getWonMessageType());
    if (event instanceof NeedSpecificEvent) {
        sb.append(" on behalf of need ").append(((NeedSpecificEvent) event).getNeedURI());
    }
    if (event instanceof RemoteNeedSpecificEvent) {
        sb.append(" to need ").append(((RemoteNeedSpecificEvent) event).getRemoteNeedURI());
    }
    if (event instanceof ConnectionSpecificEvent) {
        sb.append(" in connection ").append(((ConnectionSpecificEvent) event).getConnectionURI());
    }
    return sb.toString();
}
Also used : RemoteNeedSpecificEvent(won.bot.framework.eventbot.event.RemoteNeedSpecificEvent) NeedSpecificEvent(won.bot.framework.eventbot.event.NeedSpecificEvent) ConnectionSpecificEvent(won.bot.framework.eventbot.event.ConnectionSpecificEvent) RemoteNeedSpecificEvent(won.bot.framework.eventbot.event.RemoteNeedSpecificEvent) MessageCommandEvent(won.bot.framework.eventbot.event.impl.command.MessageCommandEvent)

Example 3 with ConnectionSpecificEvent

use of won.bot.framework.eventbot.event.ConnectionSpecificEvent in project webofneeds by researchstudio-sat.

the class OpenConnectionAction method doRun.

@Override
public void doRun(final Event event, EventListener executingListener) throws Exception {
    if (event instanceof ConnectFromOtherNeedEvent) {
        ConnectionSpecificEvent connectEvent = (ConnectionSpecificEvent) event;
        logger.debug("auto-replying to connect for connection {}", connectEvent.getConnectionURI());
        getEventListenerContext().getWonMessageSender().sendWonMessage(createOpenWonMessage(connectEvent.getConnectionURI()));
        return;
    } else if (event instanceof OpenFromOtherNeedEvent) {
        ConnectionSpecificEvent connectEvent = (ConnectionSpecificEvent) event;
        URI connectionState = WonLinkedDataUtils.getConnectionStateforConnectionURI(connectEvent.getConnectionURI(), getEventListenerContext().getLinkedDataSource());
        if (ConnectionState.REQUEST_RECEIVED.getURI().equals(connectionState)) {
            logger.debug("auto-replying to open(REQUEST_RECEIVED) with open for connection {}", connectEvent.getConnectionURI());
            getEventListenerContext().getWonMessageSender().sendWonMessage(createOpenWonMessage(connectEvent.getConnectionURI()));
        } else {
        // else do not respond - we assume the connection is now established.
        }
        return;
    } else if (event instanceof HintFromMatcherEvent) {
        // TODO: the hint with a match object is not really suitable here. Would be better to
        // use connection object instead
        HintFromMatcherEvent hintEvent = (HintFromMatcherEvent) event;
        logger.debug("opening connection based on hint {}", event);
        getEventListenerContext().getWonMessageSender().sendWonMessage(createConnectWonMessage(hintEvent.getMatch().getFromNeed(), hintEvent.getMatch().getToNeed(), FacetType.OwnerFacet.getURI(), FacetType.OwnerFacet.getURI()));
    }
}
Also used : ConnectionSpecificEvent(won.bot.framework.eventbot.event.ConnectionSpecificEvent) OpenFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.OpenFromOtherNeedEvent) HintFromMatcherEvent(won.bot.framework.eventbot.event.impl.wonmessage.HintFromMatcherEvent) URI(java.net.URI) ConnectFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent)

Example 4 with ConnectionSpecificEvent

use of won.bot.framework.eventbot.event.ConnectionSpecificEvent in project webofneeds by researchstudio-sat.

the class CloseConnectionAction method doRun.

@Override
protected void doRun(final Event event, EventListener executingListener) throws Exception {
    if (event instanceof ConnectionSpecificEvent) {
        ConnectionSpecificEvent connectionSpecificEvent = (ConnectionSpecificEvent) event;
        logger.debug("trying to close connection related to event {}", connectionSpecificEvent);
        try {
            URI connectionURI = null;
            connectionURI = connectionSpecificEvent.getConnectionURI();
            logger.debug("Extracted connection uri {}", connectionURI);
            if (connectionURI != null) {
                logger.debug("closing connection {}", connectionURI);
                getEventListenerContext().getWonMessageSender().sendWonMessage(createWonMessage(connectionURI));
            } else {
                logger.warn("could not determine which connection to close for event {}", event);
            }
        } catch (Exception e) {
            logger.warn("error trying to close connection", e);
        }
    } else {
        logger.warn("could not determine which connection to close for event {}", event);
    }
}
Also used : ConnectionSpecificEvent(won.bot.framework.eventbot.event.ConnectionSpecificEvent) URI(java.net.URI) WonMessageBuilderException(won.protocol.exception.WonMessageBuilderException)

Aggregations

ConnectionSpecificEvent (won.bot.framework.eventbot.event.ConnectionSpecificEvent)4 URI (java.net.URI)3 ConnectFromOtherNeedEvent (won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent)2 Date (java.util.Date)1 Matcher (java.util.regex.Matcher)1 NeedSpecificEvent (won.bot.framework.eventbot.event.NeedSpecificEvent)1 RemoteNeedSpecificEvent (won.bot.framework.eventbot.event.RemoteNeedSpecificEvent)1 MessageCommandEvent (won.bot.framework.eventbot.event.impl.command.MessageCommandEvent)1 HintFromMatcherEvent (won.bot.framework.eventbot.event.impl.wonmessage.HintFromMatcherEvent)1 OpenFromOtherNeedEvent (won.bot.framework.eventbot.event.impl.wonmessage.OpenFromOtherNeedEvent)1 WonMessageReceivedOnConnectionEvent (won.bot.framework.eventbot.event.impl.wonmessage.WonMessageReceivedOnConnectionEvent)1 WonMessageBuilderException (won.protocol.exception.WonMessageBuilderException)1 WonMessage (won.protocol.message.WonMessage)1