Search in sources :

Example 1 with MessageCommandEvent

use of won.bot.framework.eventbot.event.impl.command.MessageCommandEvent in project webofneeds by researchstudio-sat.

the class ExecuteSendMessageCommandAction method doRun.

/**
 * Constructs the message via <code>createWonMessage</code> and registers a listener for the response from the
 * local WoN node and, if <code>messageIsSenttoRemoteNode</code> is true, a listener for the response from the
 * remote WoN node.
 * @param event
 * @param executingListener
 */
@Override
public final void doRun(Event event, EventListener executingListener) {
    T messageCommandEvent = (T) event;
    try {
        // create the message
        WonMessage message = createWonMessage(messageCommandEvent);
        if (message == null) {
            // assume that the implementation class handles logging and error event creation.
            return;
        }
        // register listeners for the ResponseMessage generated by the own WoN node
        EventBotActionUtils.makeAndSubscribeResponseListener(message, responseEvent -> {
            if (responseEvent instanceof SuccessResponseEvent) {
                SuccessResponseEvent successEvent = (SuccessResponseEvent) responseEvent;
                logger.debug(makeLogMessageString(event) + " succeeded on local WoN node");
                Event eventToPublish = createLocalNodeSuccessEvent(messageCommandEvent, message, successEvent);
                if (eventToPublish != null) {
                    getEventListenerContext().getEventBus().publish(eventToPublish);
                }
            }
        }, responseEvent -> {
            if (responseEvent instanceof FailureResponseEvent) {
                FailureResponseEvent failureEvent = (FailureResponseEvent) responseEvent;
                logger.info(makeLogMessageString(event) + " failed on local WoN node with message: {}", WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) responseEvent).getFailureMessage()));
                Event eventToPublish = createLocalNodeFailureEvent(messageCommandEvent, message, failureEvent);
                if (eventToPublish != null) {
                    getEventListenerContext().getEventBus().publish(eventToPublish);
                }
            }
        }, getEventListenerContext());
        // register listeners for the ResponseMessage generated by the remote WoN node
        if (messageIsSentToRemoteNode) {
            EventBotActionUtils.makeAndSubscribeRemoteResponseListener(message, responseEvent -> {
                if (responseEvent instanceof SuccessResponseEvent) {
                    SuccessResponseEvent successEvent = (SuccessResponseEvent) responseEvent;
                    logger.debug(makeLogMessageString(event) + " succeeded on remote WoN node");
                    Event eventToPublish = createRemoteNodeSuccessEvent(messageCommandEvent, message, successEvent);
                    if (eventToPublish != null) {
                        getEventListenerContext().getEventBus().publish(eventToPublish);
                    }
                }
            }, responseEvent -> {
                if (responseEvent instanceof FailureResponseEvent) {
                    FailureResponseEvent failureEvent = (FailureResponseEvent) responseEvent;
                    logger.info(makeLogMessageString(event) + " failed on remote WoN node with message: {}", WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) responseEvent).getFailureMessage()));
                    Event eventToPublish = createRemoteNodeFailureEvent(messageCommandEvent, message, failureEvent);
                    if (eventToPublish != null) {
                        getEventListenerContext().getEventBus().publish(eventToPublish);
                    }
                }
            }, getEventListenerContext());
        }
        // send the message
        getEventListenerContext().getWonMessageSender().sendWonMessage(message);
        if (logger.isDebugEnabled()) {
            logger.debug(makeLogMessageString(event));
        }
    } catch (Exception e) {
        logger.warn("error executing messageCommandEvent: ", e);
        getEventListenerContext().getEventBus().publish(createMessageNotSentEvent(messageCommandEvent, e.getMessage()));
    }
}
Also used : SuccessResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.SuccessResponseEvent) WonMessage(won.protocol.message.WonMessage) RemoteNeedSpecificEvent(won.bot.framework.eventbot.event.RemoteNeedSpecificEvent) MessageCommandEvent(won.bot.framework.eventbot.event.impl.command.MessageCommandEvent) SuccessResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.SuccessResponseEvent) ConnectionSpecificEvent(won.bot.framework.eventbot.event.ConnectionSpecificEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) MessageCommandSuccessEvent(won.bot.framework.eventbot.event.impl.command.MessageCommandSuccessEvent) MessageCommandFailureEvent(won.bot.framework.eventbot.event.impl.command.MessageCommandFailureEvent) Event(won.bot.framework.eventbot.event.Event) NeedSpecificEvent(won.bot.framework.eventbot.event.NeedSpecificEvent) MessageCommandNotSentEvent(won.bot.framework.eventbot.event.impl.command.MessageCommandNotSentEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) WonMessageBuilderException(won.protocol.exception.WonMessageBuilderException)

Example 2 with MessageCommandEvent

use of won.bot.framework.eventbot.event.impl.command.MessageCommandEvent 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 MessageCommandEvent

use of won.bot.framework.eventbot.event.impl.command.MessageCommandEvent in project webofneeds by researchstudio-sat.

the class ExecuteMessageCommandAction 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 AtomSpecificEvent) {
        sb.append(" on behalf of atom ").append(((AtomSpecificEvent) event).getAtomURI());
    }
    if (event instanceof TargetAtomSpecificEvent) {
        sb.append(" to atom ").append(((TargetAtomSpecificEvent) event).getTargetAtomURI());
    }
    if (event instanceof ConnectionSpecificEvent) {
        sb.append(" in connection ").append(((ConnectionSpecificEvent) event).getConnectionURI());
    }
    return sb.toString();
}
Also used : ConnectionSpecificEvent(won.bot.framework.eventbot.event.ConnectionSpecificEvent) AtomSpecificEvent(won.bot.framework.eventbot.event.AtomSpecificEvent) TargetAtomSpecificEvent(won.bot.framework.eventbot.event.TargetAtomSpecificEvent) MessageCommandEvent(won.bot.framework.eventbot.event.impl.command.MessageCommandEvent) TargetAtomSpecificEvent(won.bot.framework.eventbot.event.TargetAtomSpecificEvent)

Example 4 with MessageCommandEvent

use of won.bot.framework.eventbot.event.impl.command.MessageCommandEvent in project webofneeds by researchstudio-sat.

the class ExecuteMessageCommandAction method doRun.

/**
 * Constructs the message via <code>createWonMessage</code> and registers a
 * listener for the response from the local WoN node and, if
 * <code>messageIsSentToRemoteNode</code> is true, a listener for the response
 * from the remote WoN node.
 *
 * @param event
 * @param executingListener
 */
@Override
public final void doRun(Event event, EventListener executingListener) {
    T messageCommandEvent = (T) event;
    try {
        // create the message
        WonMessage msgToSend = createWonMessage(messageCommandEvent);
        if (msgToSend == null) {
            // creation.
            return;
        }
        // sign and set messageURI
        final WonMessage message = getEventListenerContext().getWonMessageSender().prepareMessage(msgToSend);
        // register listeners for the ResponseMessage generated by the own WoN node
        EventBotActionUtils.makeAndSubscribeResponseListener(message, responseEvent -> {
            if (responseEvent instanceof SuccessResponseEvent) {
                SuccessResponseEvent successEvent = (SuccessResponseEvent) responseEvent;
                logger.debug(makeLogMessageString(event) + " succeeded on local WoN node");
                Event eventToPublish = createLocalNodeSuccessEvent(messageCommandEvent, message, successEvent);
                if (eventToPublish != null) {
                    getEventListenerContext().getEventBus().publish(eventToPublish);
                }
            }
        }, responseEvent -> {
            if (responseEvent instanceof FailureResponseEvent) {
                FailureResponseEvent failureEvent = (FailureResponseEvent) responseEvent;
                logger.info(makeLogMessageString(event) + " failed on local WoN node with message: {}", WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) responseEvent).getFailureMessage()));
                Event eventToPublish = createLocalNodeFailureEvent(messageCommandEvent, message, failureEvent);
                if (eventToPublish != null) {
                    getEventListenerContext().getEventBus().publish(eventToPublish);
                }
            }
        }, getEventListenerContext());
        // register listeners for the ResponseMessage generated by the remote WoN node
        if (messageIsSentToRemoteNode) {
            EventBotActionUtils.makeAndSubscribeRemoteResponseListener(message, responseEvent -> {
                if (responseEvent instanceof SuccessResponseEvent) {
                    SuccessResponseEvent successEvent = (SuccessResponseEvent) responseEvent;
                    logger.debug(makeLogMessageString(event) + " succeeded on remote WoN node");
                    Event eventToPublish = createRemoteNodeSuccessEvent(messageCommandEvent, message, successEvent);
                    if (eventToPublish != null) {
                        getEventListenerContext().getEventBus().publish(eventToPublish);
                    }
                }
            }, responseEvent -> {
                if (responseEvent instanceof FailureResponseEvent) {
                    FailureResponseEvent failureEvent = (FailureResponseEvent) responseEvent;
                    logger.info(makeLogMessageString(event) + " failed on remote WoN node with message (more on loglevel 'debug'): {}", WonRdfUtils.MessageUtils.getTextMessage(((FailureResponseEvent) responseEvent).getFailureMessage()));
                    if (logger.isDebugEnabled()) {
                        logger.debug("failed message: \n {}", RdfUtils.toString(message.getCompleteDataset()));
                    }
                    Event eventToPublish = createRemoteNodeFailureEvent(messageCommandEvent, message, failureEvent);
                    if (eventToPublish != null) {
                        getEventListenerContext().getEventBus().publish(eventToPublish);
                    }
                }
            }, getEventListenerContext());
        }
        // send the message
        getEventListenerContext().getWonMessageSender().sendMessage(message);
        if (logger.isDebugEnabled()) {
            logger.debug(makeLogMessageString(event));
        }
    } catch (Exception e) {
        logger.warn("error executing messageCommandEvent: ", e);
        getEventListenerContext().getEventBus().publish(createMessageNotSentEvent(messageCommandEvent, e.getMessage()));
    }
}
Also used : SuccessResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.SuccessResponseEvent) WonMessage(won.protocol.message.WonMessage) MessageCommandEvent(won.bot.framework.eventbot.event.impl.command.MessageCommandEvent) SuccessResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.SuccessResponseEvent) ConnectionSpecificEvent(won.bot.framework.eventbot.event.ConnectionSpecificEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) AtomSpecificEvent(won.bot.framework.eventbot.event.AtomSpecificEvent) MessageCommandSuccessEvent(won.bot.framework.eventbot.event.impl.command.MessageCommandSuccessEvent) MessageCommandFailureEvent(won.bot.framework.eventbot.event.impl.command.MessageCommandFailureEvent) Event(won.bot.framework.eventbot.event.Event) TargetAtomSpecificEvent(won.bot.framework.eventbot.event.TargetAtomSpecificEvent) MessageCommandNotSentEvent(won.bot.framework.eventbot.event.impl.command.MessageCommandNotSentEvent) FailureResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent) WonMessageBuilderException(won.protocol.exception.WonMessageBuilderException)

Aggregations

ConnectionSpecificEvent (won.bot.framework.eventbot.event.ConnectionSpecificEvent)4 MessageCommandEvent (won.bot.framework.eventbot.event.impl.command.MessageCommandEvent)4 AtomSpecificEvent (won.bot.framework.eventbot.event.AtomSpecificEvent)2 Event (won.bot.framework.eventbot.event.Event)2 NeedSpecificEvent (won.bot.framework.eventbot.event.NeedSpecificEvent)2 RemoteNeedSpecificEvent (won.bot.framework.eventbot.event.RemoteNeedSpecificEvent)2 TargetAtomSpecificEvent (won.bot.framework.eventbot.event.TargetAtomSpecificEvent)2 MessageCommandFailureEvent (won.bot.framework.eventbot.event.impl.command.MessageCommandFailureEvent)2 MessageCommandNotSentEvent (won.bot.framework.eventbot.event.impl.command.MessageCommandNotSentEvent)2 MessageCommandSuccessEvent (won.bot.framework.eventbot.event.impl.command.MessageCommandSuccessEvent)2 FailureResponseEvent (won.bot.framework.eventbot.event.impl.wonmessage.FailureResponseEvent)2 SuccessResponseEvent (won.bot.framework.eventbot.event.impl.wonmessage.SuccessResponseEvent)2 WonMessageBuilderException (won.protocol.exception.WonMessageBuilderException)2 WonMessage (won.protocol.message.WonMessage)2