if (device.get().getUnreadCount() > 0) { lastReceiveTime = timeLapse.getStartTime(); final List<Message> messages = device.get().getUnreadMessages(); device.get().send(new MyNameIs(name), message.getSender()); } else if (message.getContents() instanceof MyNameIs) { device.get().send(Messages.WHO_ARE_YOU, message.getSender()); device.get().broadcast(Messages.NICE_TO_MEET_YOU); } else if (device.get().getReceivedCount() == 0) { device.get().broadcast(Messages.HELLO_WORLD); } else if (timeLapse.getStartTime() - lastReceiveTime > LONELINESS_THRESHOLD) { device.get().broadcast(Messages.WHERE_IS_EVERYBODY);
&& viewOptions.contains(ViewOptions.RELIABILITY_COLOR)) { final RGB rgb = ColorUtil.interpolate(unreliableColor, reliableColor, device.getReliability()); color = Optional.of(new Color(gc.getDevice(), rgb)); if (device.getMaxRange().isPresent()) { helper.drawCircle(userPos, device.getMaxRange().get()); if (color.isPresent()) { gc.setBackground(color.get()); helper.fillCircle(userPos, device.getMaxRange().get()); sb.append(device.getReceivedCount()) .append('(') .append(device.getUnreadCount()) .append(')'); .append(String.format("%.2f", device.getReliability()));
void sendMessages() { if (!getMaxRange().isPresent() || user.getPosition().isPresent()) { for (final Message msg : outbox) { model.send(msg, reliability); } clearOutbox(); } }
private void doSend(Message msg, CommUser to, CommDevice recipient, double sendReliability) { if (msg.predicate().apply(to) && hasSucces(sendReliability, recipient.getReliability())) { recipient.receive(msg); } }
rangedNoPos.device().send(Contents.YO, agent1); model.afterTick(TimeLapseFactory.create(0, 100)); assertEquals(0, agent1.device().getReceivedCount()); assertEquals(0, agent1.device().getUnreadCount()); assertEquals(1, rangedNoPos.device().getOutbox().size()); rangedNoPos.device().broadcast(Contents.YO); model.afterTick(TimeLapseFactory.create(0, 100)); assertEquals(0, agent1.device().getReceivedCount()); assertEquals(0, agent2.device().getReceivedCount()); assertEquals(0, agent3.device().getReceivedCount()); assertEquals(0, agent4.device().getReceivedCount()); assertEquals(0, agent5.device().getReceivedCount()); assertEquals(0, ranged.device().getReceivedCount()); assertEquals(0, noPos.device().getReceivedCount()); assertEquals(2, rangedNoPos.device().getOutbox().size()); final List<Message> msgs = rangedNoPos.device().getOutbox(); assertFalse(msgs.get(0).isBroadcast()); assertTrue(msgs.get(1).isBroadcast()); assertEquals(0, rangedNoPos.device().getReceivedCount()); agent1.device().send(Contents.YO, rangedNoPos); model.afterTick(TimeLapseFactory.create(0, 100)); assertEquals(1, rangedNoPos.device().getReceivedCount()); ranged.device().send(Contents.YO, rangedNoPos); assertEquals(1, ranged.device().getOutbox().size()); model.afterTick(TimeLapseFactory.create(0, 100)); assertEquals(1, rangedNoPos.device().getReceivedCount());
agent1.device().send(Contents.YO, agent2); agent2.device().send(Contents.HELLO_WORLD, agent1); assertThat(agent1.device().getOutbox()).hasSize(1); assertThat(agent2.device().getOutbox()).hasSize(1); assertThat(agent1.device().getReceivedCount()).isEqualTo(0); assertThat(agent2.device().getReceivedCount()).isEqualTo(0); assertThat(agent1.device().getOutbox()).hasSize(0); assertThat(agent2.device().getOutbox()).hasSize(1); agent2.device().send(Contents.YO, agent1); } catch (final IllegalStateException e) { fail = true; agent2.device().broadcast(Contents.YO); } catch (final IllegalStateException e) { fail = true; agent2.device().clearOutbox(); } catch (final IllegalStateException e) { fail = true;
/** * Test that a broadcast only ends up at agents within the range. */ @Test public void testBroadcastWithRange() { final Agent ranged = new RangedAgent(new Point(0, 5), 5); model.register(ranged); ranged.device().broadcast(Contents.YO); model.afterTick(TimeLapseFactory.create(0, 100)); assertFalse(agent1.device().getUnreadMessages().isEmpty()); assertFalse(agent2.device().getUnreadMessages().isEmpty()); assertTrue(agent3.device().getUnreadMessages().isEmpty()); assertFalse(agent4.device().getUnreadMessages().isEmpty()); assertTrue(agent5.device().getUnreadMessages().isEmpty()); }
@Test public void testBroadcastRangeWithoutMaxRange() { when(COMMUSERS[0].getPosition()).thenReturn(Optional.of(new Point(0, 0))); when(COMMUSERS[1].getPosition()).thenReturn(Optional.of(new Point(5, 0))); when(COMMUSERS[2].getPosition()).thenReturn(Optional.of(new Point(10, 0))); final CommDevice device1 = builders[0].build(); final CommDevice device2 = builders[1].build(); final CommDevice device3 = builders[2].build(); assertEquals(0, device2.getReceivedCount()); assertEquals(0, device3.getReceivedCount()); device1.broadcast(mock(MessageContents.class), 7); device1.sendMessages(); assertEquals(1, device2.getReceivedCount()); assertEquals(0, device3.getReceivedCount()); }
/** * 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 what happens if an agent no longer has a position. */ @Test public void testSenderIsRemovedDuringSend() { final Agent a = new Agent(new Point(0, 0)); final Agent b = new RangedAgent(new Point(0, 0), 5); model.register(a); model.register(b); // a is removed during the same tick as that it sends a message, this // doesn't matter since it has no range, therefore it sends anyway. a.device().broadcast(Contents.YO); a.setPosition(null); model.afterTick(TimeLapseFactory.create(0, 100)); assertEquals(1, agent1.device().getReceivedCount()); // b is removed during the same tick as that is sends a message, since it is // a ranged device the message is not sent and will stay in the outbox. b.device().broadcast(Contents.YO); b.setPosition(null); model.afterTick(TimeLapseFactory.create(0, 100)); assertEquals(1, agent1.device().getReceivedCount()); assertEquals(1, b.device().getOutbox().size()); }
@Test(expected = IllegalArgumentException.class) public void testBroadcastRangeNegative() { final CommDevice device = builders[0].build(); device.broadcast(mock(MessageContents.class), -1); }
/** * Tests that the returned list is unmodifiable. */ @Test public void testUnmodifiableUnreadMessages() { final List<Message> msgs = agent1.device().getUnreadMessages(); boolean fail = false; try { msgs.clear(); } catch (final UnsupportedOperationException e) { fail = true; } assertTrue(fail); }
/** * Test for input validation of max range. */ @Test public void testSetMaxRange() { boolean fail = false; try { builder.setMaxRange(-.0000001); } catch (final IllegalArgumentException e) { fail = true; } assertTrue(fail); assertEquals(10d, builder.setMaxRange(10).build().getMaxRange().get(), 0); setUp(); assertEquals(Optional.absent(), builder.build().getMaxRange()); } }
/** * Test for input validation of reliability. */ @Test public void testSetReliability() { assertEquals(0d, builder.setReliability(0).build().getReliability(), 0d); setUp(); assertEquals(1d, builder.setReliability(1).build().getReliability(), 0d); boolean fail = false; try { builder.setReliability(1.0000001); } catch (final IllegalArgumentException e) { fail = true; } assertTrue(fail); fail = false; try { builder.setReliability(-0.0000001); } catch (final IllegalArgumentException e) { fail = true; } assertTrue(fail); }
@Override public void afterTick(TimeLapse timeLapse) { final Set<CommDevice> devices = usersDevices.values(); for (final CommDevice device : devices) { device.sendMessages(); } }
/** * Test input validation. */ @Test public void testSendDirectMsgInputValidation() { boolean fail = false; try { agent1.device().send(Contents.YO, agent1); } catch (final IllegalArgumentException e) { fail = true; } assertTrue(fail); fail = false; final Agent unknown = new Agent(new Point(0, 0)); try { agent1.device().send(Contents.YO, unknown); } catch (final IllegalArgumentException e) { fail = true; } assertTrue(fail); }
/** * Tests broadcasting from and to unreliable agents. */ @Test public void testBroadcastUnreliable() { final Agent unreliable = new Agent(new Point(5, 5), 0); model.register(unreliable); unreliable.device().broadcast(Contents.YO); model.afterTick(TimeLapseFactory.create(0, 100)); assertTrue(agent1.device().getUnreadMessages().isEmpty()); assertTrue(agent2.device().getUnreadMessages().isEmpty()); agent1.device().broadcast(Contents.YO); model.afterTick(TimeLapseFactory.create(0, 100)); assertTrue(unreliable.device().getUnreadMessages().isEmpty()); assertEquals(1, agent2.device().getUnreadMessages().size()); }
/** * Test for unreliable direct message. */ @Test public void testSendDirectMsgUnreliable() { final Agent unreliable = new Agent(new Point(5, 5), 0); model.register(unreliable); unreliable.device().send(Contents.YO, agent1); model.afterTick(TimeLapseFactory.create(0, 100)); assertTrue(agent1.device().getUnreadMessages().isEmpty()); agent1.device().send(Contents.YO, unreliable); model.afterTick(TimeLapseFactory.create(0, 100)); assertTrue(unreliable.device().getUnreadMessages().isEmpty()); }
@Test(expected = IllegalArgumentException.class) public void testBroadcastRangeTooLarge() { builders[0].setMaxRange(10); final CommDevice device = builders[0].build(); device.broadcast(mock(MessageContents.class), 15); }
if (device.get().getUnreadCount() > 0) { lastReceiveTime = timeLapse.getStartTime(); final List<Message> messages = device.get().getUnreadMessages(); device.get().send(new MyNameIs(name), message.getSender()); } else if (message.getContents() instanceof MyNameIs) { device.get().send(Messages.WHO_ARE_YOU, message.getSender()); device.get().broadcast(Messages.NICE_TO_MEET_YOU); } else if (device.get().getReceivedCount() == 0) { device.get().broadcast(Messages.HELLO_WORLD); } else if (timeLapse.getStartTime() - lastReceiveTime > LONELINESS_THRESHOLD) { device.get().broadcast(Messages.WHERE_IS_EVERYBODY);