use of won.bot.framework.eventbot.event.impl.wonmessage.MessageFromOtherAtomEvent in project webofneeds by researchstudio-sat.
the class MessageLifecycleMonitoringAction method doRun.
@Override
protected void doRun(final Event event, EventListener executingListener) throws Exception {
Stopwatch stopwatchB = SimonManager.getStopwatch("messageTripB");
Stopwatch stopwatchBC = SimonManager.getStopwatch("messageTripBC");
Stopwatch stopwatchBCD = SimonManager.getStopwatch("messageTripBCD");
Stopwatch stopwatchBCDE = SimonManager.getStopwatch("messageTripBCDE");
if (event instanceof MessageSpecificEvent) {
MessageSpecificEvent msgEvent = (MessageSpecificEvent) event;
URI msgURI = msgEvent.getMessageURI();
logger.debug("RECEIVED EVENT {} for uri {}", event, msgURI);
if (event instanceof MessageDispatchStartedEvent) {
Split splitB = stopwatchB.start();
Split splitBC = stopwatchBC.start();
Split splitBCD = stopwatchBCD.start();
Split splitBCDE = stopwatchBCDE.start();
msgSplitsB.put(msgURI.toString(), splitB);
msgSplitsBC.put(msgURI.toString(), splitBC);
msgSplitsBCD.put(msgURI.toString(), splitBCD);
msgSplitsBCDE.put(msgURI.toString(), splitBCDE);
connectionMsgUris.put(msgURI, msgEvent.getAtomURI());
} else if (event instanceof MessageDispatchedEvent) {
msgSplitsB.get(msgURI.toString()).stop();
}
} else if (event instanceof SuccessResponseEvent || event instanceof FailureResponseEvent) {
DeliveryResponseEvent responseEvent = (DeliveryResponseEvent) event;
if (connectionMsgUris.containsKey(responseEvent.getOriginalMessageURI())) {
responseMsgUris.put(responseEvent.getMessage().getMessageURI(), responseEvent.getAtomURI());
}
if (msgSplitsBC.get(responseEvent.getOriginalMessageURI().toString()) != null) {
logger.debug("RECEIVED RESPONSE EVENT {} for uri {}", event, responseEvent.getOriginalMessageURI());
msgSplitsBC.get(responseEvent.getOriginalMessageURI().toString()).stop();
}
} else if (event instanceof MessageFromOtherAtomEvent) {
WonMessage msg = ((MessageFromOtherAtomEvent) event).getWonMessage();
msgSplitsBCD.get(msg.getMessageURI()).stop();
connectionMsgUris.put(msg.getMessageURI(), msg.getRecipientAtomURI());
} else if (event instanceof CrawlReadyEvent) {
reportMessageSizes(connectionMsgUris, "Connection Messages");
reportMessageSizes(responseMsgUris, "Delivery Responses");
getEventListenerContext().getEventBus().publish(new CrawlDoneEvent());
}
}
use of won.bot.framework.eventbot.event.impl.wonmessage.MessageFromOtherAtomEvent in project webofneeds by researchstudio-sat.
the class AutomaticMessageResponderListener method handleMessageEvent.
private void handleMessageEvent(final ConnectionSpecificEvent messageEvent) {
getEventListenerContext().getTaskScheduler().schedule(() -> {
String incomingMessage = "cannot extract message";
if (messageEvent instanceof MessageFromOtherAtomEvent) {
WonMessage wonMessage = ((MessageFromOtherAtomEvent) messageEvent).getWonMessage();
incomingMessage = WonRdfUtils.MessageUtils.getTextMessage(wonMessage);
}
String message = createMessage(messageEvent);
URI connectionUri = messageEvent.getConnectionURI();
if (logger.isDebugEnabled()) {
logger.debug("connection {}: received message: {}", connectionUri, incomingMessage);
logger.debug("connection {}: sending message: {}", connectionUri, message);
}
try {
getEventListenerContext().getWonMessageSender().prepareAndSendMessage(createWonMessage(connectionUri, message));
} catch (Exception e) {
logger.warn("could not send message via connection {}", connectionUri, e);
}
}, new Date(System.currentTimeMillis() + this.millisTimeoutBeforeReply));
}
use of won.bot.framework.eventbot.event.impl.wonmessage.MessageFromOtherAtomEvent in project webofneeds by researchstudio-sat.
the class AclTests method testGroupChat.
@Test(timeout = 60 * 1000)
public void testGroupChat() throws Exception {
runTest(ctx -> {
EventBus bus = ctx.getEventBus();
final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
final URI atomUri1 = ctx.getWonNodeInformationService().generateAtomURI(wonNodeUri);
final URI atomUri2 = ctx.getWonNodeInformationService().generateAtomURI(wonNodeUri);
final URI atomUri3 = ctx.getWonNodeInformationService().generateAtomURI(wonNodeUri);
final URI atomUri4 = ctx.getWonNodeInformationService().generateAtomURI(wonNodeUri);
final URI atom1GroupSocket = URI.create(atomUri1.toString() + "#groupSocket");
final BotBehaviour bbCreateAtom1 = new BotBehaviour(ctx, "bbCreateAtom1") {
@Override
protected void onActivate(Optional<Object> message) {
final String atomUriString = atomUri1.toString();
AtomContent atomContent = AtomContent.builder(atomUri1).addTitle("Group Chat Atom (1/4)").addSocket(Socket.builder(atom1GroupSocket).setSocketDefinition(WXGROUP.GroupSocket.asURI()).build()).addType(URI.create(WON.Atom.getURI())).build();
Authorization autoConnectGroupSocket = Authorization.builder().setGranteeGranteeWildcard(GranteeWildcard.ANYONE).addGrant(b -> b.addSocket(s -> s.addSocketType(WXGROUP.GroupSocket.asURI()).addOperationsSimpleOperationExpression(OP_AUTO_CONNECT))).build();
WonMessage createMessage = WonMessageBuilder.createAtom().atom(atomUri1).content().graph(RdfOutput.toGraph(atomContent)).content().aclGraph(won.auth.model.RdfOutput.toGraph(getAnyoneMayConnectAuth())).content().aclGraph(won.auth.model.RdfOutput.toGraph(getAnyoneMayReadAnything())).content().aclGraph(won.auth.model.RdfOutput.toGraph(autoConnectGroupSocket)).build();
createMessage = ctx.getWonMessageSender().prepareMessage(createMessage);
ctx.getBotContextWrapper().rememberAtomUri(atomUri1);
EventListener successCallback = event -> {
logger.debug("group chat atom created");
deactivate();
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, "Create group chat atom");
EventBotActionUtils.makeAndSubscribeResponseListener(createMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(createMessage);
}
};
final BotBehaviour bbCreateAtom2 = new BotBehaviour(ctx, "bbCreateAtom2") {
@Override
protected void onActivate(Optional<Object> message) {
final String atomUriString = atomUri2.toString();
AtomContent atomContent = AtomContent.builder(atomUri2).addTitle("member atom A (2/4)").addSocket(Socket.builder(atomUriString + "#chatSocket").setSocketDefinition(WXCHAT.ChatSocket.asURI()).build()).addType(URI.create(WON.Atom.getURI())).build();
WonMessage createMessage = WonMessageBuilder.createAtom().atom(atomUri2).content().graph(RdfOutput.toGraph(atomContent)).content().aclGraph(won.auth.model.RdfOutput.toGraph(getAnyoneMayConnectAuth())).content().aclGraph(won.auth.model.RdfOutput.toGraph(getAnyoneMayReadAnything())).build();
createMessage = ctx.getWonMessageSender().prepareMessage(createMessage);
ctx.getBotContextWrapper().rememberAtomUri(atomUri2);
EventListener successCallback = event -> {
logger.debug("member atom A created");
deactivate();
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, "Create member atom A");
EventBotActionUtils.makeAndSubscribeResponseListener(createMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(createMessage);
}
};
final BotBehaviour bbCreateAtom3 = new BotBehaviour(ctx, "bbCreateAtom3") {
@Override
protected void onActivate(Optional<Object> message) {
final String atomUriString = atomUri3.toString();
AtomContent atomContent = AtomContent.builder(atomUri3).addTitle("member atom B (3/4)").addSocket(Socket.builder(atomUriString + "#chatSocket").setSocketDefinition(WXCHAT.ChatSocket.asURI()).build()).addType(URI.create(WON.Atom.getURI())).build();
WonMessage createMessage = WonMessageBuilder.createAtom().atom(atomUri3).content().graph(RdfOutput.toGraph(atomContent)).content().aclGraph(won.auth.model.RdfOutput.toGraph(getAnyoneMayConnectAuth())).content().aclGraph(won.auth.model.RdfOutput.toGraph(getAnyoneMayReadAnything())).build();
createMessage = ctx.getWonMessageSender().prepareMessage(createMessage);
ctx.getBotContextWrapper().rememberAtomUri(atomUri3);
EventListener successCallback = event -> {
logger.debug("member atom B created");
deactivate();
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, "Creating member atom B");
EventBotActionUtils.makeAndSubscribeResponseListener(createMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(createMessage);
}
};
final BotBehaviour bbCreateAtom4 = new BotBehaviour(ctx, "bbCreateAtom4") {
@Override
protected void onActivate(Optional<Object> message) {
final String atomUriString = atomUri4.toString();
AtomContent atomContent = AtomContent.builder(atomUri4).addTitle("member atom C (4/4)").addSocket(Socket.builder(atomUriString + "#chatSocket").setSocketDefinition(WXCHAT.ChatSocket.asURI()).build()).addType(URI.create(WON.Atom.getURI())).build();
WonMessage createMessage = WonMessageBuilder.createAtom().atom(atomUri4).content().graph(RdfOutput.toGraph(atomContent)).content().aclGraph(won.auth.model.RdfOutput.toGraph(getAnyoneMayConnectAuth())).content().aclGraph(won.auth.model.RdfOutput.toGraph(getAnyoneMayReadAnything())).build();
createMessage = ctx.getWonMessageSender().prepareMessage(createMessage);
ctx.getBotContextWrapper().rememberAtomUri(atomUri4);
EventListener successCallback = event -> {
logger.debug("member atom C created");
deactivate();
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, "Creating member atom C");
EventBotActionUtils.makeAndSubscribeResponseListener(createMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(createMessage);
}
};
BotBehaviour bbSendConnect21 = new BotBehaviour(ctx, "bbSendConnect21") {
@Override
protected void onActivate(Optional<Object> message) {
WonMessage connectMessage = WonMessageBuilder.connect().sockets().sender(URI.create(atomUri2.toString() + "#chatSocket")).recipient(atom1GroupSocket).direction().fromOwner().build();
connectMessage = ctx.getWonMessageSender().prepareMessage(connectMessage);
EventListener successCallback = event -> {
logger.debug("member A -> group connection requested");
deactivate();
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, "Requesting connection member A -> group");
EventBotActionUtils.makeAndSubscribeResponseListener(connectMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(connectMessage);
}
};
BotBehaviour bbSendConnect31 = new BotBehaviour(ctx, "bbSendConnect31") {
@Override
protected void onActivate(Optional<Object> message) {
WonMessage connectMessage = WonMessageBuilder.connect().sockets().sender(URI.create(atomUri3.toString() + "#chatSocket")).recipient(atom1GroupSocket).direction().fromOwner().build();
connectMessage = ctx.getWonMessageSender().prepareMessage(connectMessage);
EventListener successCallback = event -> {
logger.debug("member B -> group connection requested");
deactivate();
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, "Requesting connection member B -> group");
EventBotActionUtils.makeAndSubscribeResponseListener(connectMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(connectMessage);
}
};
BotBehaviour bbSendConnect41 = new BotBehaviour(ctx, "bbSendConnect41") {
@Override
protected void onActivate(Optional<Object> message) {
WonMessage connectMessage = WonMessageBuilder.connect().sockets().sender(URI.create(atomUri4.toString() + "#chatSocket")).recipient(atom1GroupSocket).direction().fromOwner().build();
connectMessage = ctx.getWonMessageSender().prepareMessage(connectMessage);
EventListener successCallback = event -> {
logger.debug("member C -> group connection requested");
deactivate();
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, "Requesting connection member C -> group");
EventBotActionUtils.makeAndSubscribeResponseListener(connectMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(connectMessage);
}
};
BotBehaviour bbWaitForAutoConnect = new BotBehaviour(ctx) {
@Override
protected void onActivate(Optional<Object> message) {
bus.subscribe(ConnectFromOtherAtomEvent.class, new ActionOnceAfterNEventsListener(ctx, new EventFilter() {
@Override
public boolean accept(Event event) {
boolean waitingForThis = ((ConnectFromOtherAtomEvent) event).getSenderSocket().equals(atom1GroupSocket);
return waitingForThis;
}
}, 3, new EventBotAction() {
@Override
public Runnable getActionTask(Event event, EventListener eventListener) {
return () -> deactivate();
}
}));
}
};
BotBehaviour bbSendMsg41 = new BotBehaviour(ctx, "bbSendMsg41") {
@Override
protected void onActivate(Optional<Object> message) {
WonMessage connectMessage = WonMessageBuilder.connectionMessage().sockets().sender(URI.create(atomUri4.toString() + "#chatSocket")).recipient(URI.create(atomUri1.toString() + "#groupSocket")).direction().fromOwner().content().text("Hello, world!").build();
connectMessage = ctx.getWonMessageSender().prepareMessage(connectMessage);
EventListener successCallback = event -> {
logger.debug("member C -> send message to group");
deactivate();
};
EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, "sending message member C -> group");
EventBotActionUtils.makeAndSubscribeResponseListener(connectMessage, successCallback, failureCallback, ctx);
ctx.getWonMessageSender().sendMessage(connectMessage);
}
};
BotBehaviour bbWaitForGroupMessage = new BotBehaviour(ctx) {
@Override
protected void onActivate(Optional<Object> message) {
bus.subscribe(MessageFromOtherAtomEvent.class, new ActionOnceAfterNEventsListener(ctx, new EventFilter() {
@Override
public boolean accept(Event event) {
boolean interestedInThis = ((MessageFromOtherAtomEvent) event).getWonMessage().getSenderSocketURIRequired().equals(atom1GroupSocket);
return interestedInThis;
}
}, 2, new EventBotAction() {
@Override
public Runnable getActionTask(Event event, EventListener eventListener) {
return () -> passTest(bus);
}
}));
}
};
BehaviourBarrier barrier = new BehaviourBarrier(ctx);
barrier.waitFor(bbCreateAtom1, bbCreateAtom2, bbCreateAtom3, bbCreateAtom4);
barrier.thenStart(bbSendConnect21, bbSendConnect31, bbSendConnect41);
barrier.activate();
bbWaitForAutoConnect.onDeactivateActivate(bbSendMsg41);
bbWaitForGroupMessage.activate();
bbWaitForAutoConnect.activate();
bbCreateAtom1.activate();
bbCreateAtom2.activate();
bbCreateAtom3.activate();
bbCreateAtom4.activate();
});
}
Aggregations