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();
}
}
}
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();
}
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()));
}
}
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);
}
}
Aggregations