/** * @return The {@link MessageContents} that this message contains. */ public MessageContents getContents() { return contents(); }
CommDeviceBuilder(CommModel m, CommUser u) { model = m; user = u; used = false; deviceReliability = model.getDefaultReliability(); deviceMaxRange = model.getDefaultMaxRange(); }
/** * @return The {@link CommUser} that send this message. */ public CommUser getSender() { return from(); }
@Override public void setCommDevice(CommDeviceBuilder builder) { if (range >= 0) { builder.setMaxRange(range); } device = Optional.of(builder .setReliability(reliability) .build()); }
void sendMessages() { if (!getMaxRange().isPresent() || user.getPosition().isPresent()) { for (final Message msg : outbox) { model.send(msg, reliability); } clearOutbox(); } }
@Before public void setUp() { builders = new CommDeviceBuilder[COMMUSERS.length]; final CommModel commModel = CommModel .builder() .build(CommModelTest.fakeDependencies()); for (int i = 0; i < COMMUSERS.length; i++) { builders[i] = new CommDeviceBuilder(commModel, COMMUSERS[i]); } }
@Test(expected = IllegalArgumentException.class) public void testBroadcastRangeTooLarge() { builders[0].setMaxRange(10); final CommDevice device = builders[0].build(); device.broadcast(mock(MessageContents.class), 15); }
/** * Tests that comm users should create a device. */ @Test public void testIdleCommUser() { boolean fail = false; try { CommModel.builder().build(fakeDependencies()) .register(new IdleCommUser()); } catch (final IllegalStateException e) { fail = true; } assertTrue(fail); }
private void doSend(Message msg, CommUser to, CommDevice recipient, double sendReliability) { if (msg.predicate().apply(to) && hasSucces(sendReliability, recipient.getReliability())) { recipient.receive(msg); } }
@Override public void setCommDevice(CommDeviceBuilder builder) { commDevice = Optional.of( builder.setReliability(reliability) .build()); }
@Override public boolean unregister(CommUser commUser) { checkArgument(contains(commUser), "CommModel does not contain %s.", commUser); final CommDevice unregDevice = usersDevices.remove(commUser); unregDevice.unregister(); unregisteredUsersDevices.put(commUser, unregDevice); usersHasChanged = true; if (eventDispatcher.hasListenerFor(EventTypes.REMOVE_COMM_USER)) { eventDispatcher.dispatchEvent(new CommModelEvent( EventTypes.REMOVE_COMM_USER, this, unregDevice, commUser)); } return true; }
/** * Returns a copy of this builder with the reliability of the device to be * constructed set to the specified value. The reliability is applied for * both sending and receiving messages. Reliability must be * <code>0 ≤ r ≤ 1</code>. * @param reliability The reliability to set. * @return A new instance of {@link Builder} with reliability set to the * specified value. */ @CheckReturnValue public Builder withDefaultDeviceReliability(double reliability) { checkReliability(reliability); return new AutoValue_CommModel_Builder(reliability, defaultMaxRange()); }
/** * Returns a copy of this builder with the default maximum range for all * devices set to the specified value. By default devices will only be able * to send messages to other devices that are within this max range. * @param maxRange The maxRange to set. * @return A new instance of {@link Builder} with max range set to the * specified value. */ @CheckReturnValue public Builder withDefaultDeviceMaxRange(double maxRange) { checkRangeIsPositive(maxRange); return new AutoValue_CommModel_Builder(defaultReliability(), Optional.of(maxRange)); }
Builder() { setDependencies(RandomProvider.class); setProvidingTypes(CommModel.class); }
static CommDeviceBuilder builder(CommModel m, CommUser u) { return new CommDeviceBuilder(m, u); }
/** * Sets the reliability of the device to be constructed. The reliability is * applied for both sending and receiving messages. Reliability must be * <code>0 ≤ r ≤ 1</code>. * @param reliability The reliability to set. * @return This, as per the builder pattern. */ public CommDeviceBuilder setReliability(double reliability) { CommModel.checkReliability(reliability); deviceReliability = reliability; return this; }
/** * @return A new builder for creating a {@link CommModel}. */ public static Builder builder() { return Builder.create(); }
/** * Sets the maximum range. This means that the device to be created will only * be able to send messages to other devices that are within this range. * @param range The max range to set. * @return This, as per the builder pattern. */ public CommDeviceBuilder setMaxRange(double range) { checkRangeIsPositive(range); deviceMaxRange = Optional.of(range); return this; }
/** * Creates a new default builder. */ @Before public void setUp() { builder = new CommDeviceBuilder(CommModel.builder() .build(CommModelTest.fakeDependencies()), mock(CommUser.class)); }