@Override public String toString() { return MoreObjects.toStringHelper("Message") .add("sender", getSender()) .add("contents", getContents()) .toString(); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof Message) { Message that = (Message) o; return (this.from.equals(that.from())) && (this.to.equals(that.to())) && (this.contents.equals(that.contents())) && (this.predicate.equals(that.predicate())); } return false; }
void send(Message msg, double senderReliability) { // direct if (msg.to().isPresent()) { if (usersDevices.containsKey(msg.to().get())) { final CommDevice recipient = usersDevices.get(msg.to().get()); doSend(msg, msg.to().get(), recipient, senderReliability); } } else { // broadcast for (final Entry<CommUser, CommDevice> entry : usersDevices.entrySet()) { if (msg.from() != entry.getKey()) { doSend(msg, entry.getKey(), entry.getValue(), senderReliability); } } } }
/** * Tests the correct sending and receiving of a direct message. */ @Test public void testSendDirectMsg() { agent1.device().send(Contents.YO, agent2); assertTrue(agent2.device().getUnreadMessages().isEmpty()); // send messages model.afterTick(TimeLapseFactory.create(0, 100)); assertTrue(agent1.device().getUnreadMessages().isEmpty()); final List<Message> msgs = agent2.device().getUnreadMessages(); assertEquals(1, msgs.size()); assertFalse(msgs.get(0).toString().isEmpty()); assertSame(Contents.YO, msgs.get(0).getContents()); assertSame(agent1, msgs.get(0).getSender()); assertTrue(agent2.device().getUnreadMessages().isEmpty()); }
/** * Tests that the messages appear in the device in the actual send ordering, * which is the order in which agents have been registered in the comm model * and not the order of broadcast invocation. */ @Test public void testBroadcastReceiveOrdering() { agent4.device().broadcast(Contents.YO); agent1.device().broadcast(Contents.HELLO_WORLD); agent2.device().broadcast(Contents.YO); agent1.device().broadcast(Contents.HELLO_WORLD); agent3.device().broadcast(Contents.YO); agent4.device().broadcast(Contents.YO); model.afterTick(TimeLapseFactory.create(0, 100)); final List<Message> msgs = agent5.device().getUnreadMessages(); assertSame(agent1, msgs.get(0).getSender()); assertSame(agent1, msgs.get(1).getSender()); assertSame(agent2, msgs.get(2).getSender()); assertSame(agent3, msgs.get(3).getSender()); assertSame(agent4, msgs.get(4).getSender()); assertSame(agent4, msgs.get(5).getSender()); }
outbox.add(Message.createBroadcast(user, contents, rangePredicate));
/** * @return The {@link MessageContents} that this message contains. */ public MessageContents getContents() { return contents(); }
/** * @return The {@link CommUser} that send this message. */ public CommUser getSender() { return from(); }
/** * @return <code>true</code> if this message is broadcast, or * <code>false</code> if this message is a direct message. */ public boolean isBroadcast() { return !to().isPresent(); }
private void doSend(Message msg, CommUser to, CommDevice recipient, double sendReliability) { if (msg.predicate().apply(to) && hasSucces(sendReliability, recipient.getReliability())) { recipient.receive(msg); } }
"%s can not send message to unknown recipient: %s.", user, recipient); outbox.add(Message.createDirect(user, recipient, contents, rangePredicate));
assertEquals(2, rangedNoPos.device().getOutbox().size()); final List<Message> msgs = rangedNoPos.device().getOutbox(); assertFalse(msgs.get(0).isBroadcast()); assertTrue(msgs.get(1).isBroadcast());
/** * Test for send message with limited range. */ @Test public void testSendDirectMsgWithRange() { final Agent ranged = new RangedAgent(new Point(0, 5), 5); model.register(ranged); // is possible, is within range ranged.device().send(Contents.YO, agent1); // not possible, too far ranged.device().send(Contents.YO, agent3); // receiving messages from outside range is possible agent3.device().send(Contents.YO, ranged); model.afterTick(TimeLapseFactory.create(0, 100)); final Message m = agent1.device().getUnreadMessages().get(0); assertSame(ranged, m.getSender()); assertTrue(agent3.device().getUnreadMessages().isEmpty()); assertSame(agent3, ranged.device().getUnreadMessages().get(0) .getSender()); }
final Predicate<CommUser> broadcastRangePred = new RangePredicate(user, range); outbox.add(Message.createBroadcast(user, contents, broadcastRangePred));
if (message.getContents() == Messages.WHO_ARE_YOU) { device.get().send(new MyNameIs(name), message.getSender()); } else if (message.getContents() instanceof MyNameIs) { ((MyNameIs) message.getContents()).getName(); System.out.println("I, " + name + ", just met " + nameOfOther); knownAgents.add(message.getSender()); if (!knownAgents.contains(message.getSender())) { device.get().send(Messages.WHO_ARE_YOU, message.getSender());
if (message.getContents() == Messages.WHO_ARE_YOU) { device.get().send(new MyNameIs(name), message.getSender()); } else if (message.getContents() instanceof MyNameIs) { ((MyNameIs) message.getContents()).getName(); System.out.println("I, " + name + ", just met " + nameOfOther); knownAgents.add(message.getSender()); if (!knownAgents.contains(message.getSender())) { device.get().send(Messages.WHO_ARE_YOU, message.getSender());
assertEquals(Contents.HELLO_WORLD, msgs2.get(0).getContents()); assertEquals(agent1, msgs2.get(0).getSender()); assertEquals(msgs2, msgs3); assertEquals(msgs2, msgs4);