public Instant now() { return Instant.now(); } }
Instant instant() { return Instant.now(); } }
Instant instant() { return Instant.now(); } }
private Instant timeInstantNow() { return Instant.now(); // Add clock here for non-real-time. }
/** Returns the nearest to now instant that will have given amount of TAI millis since 2004. */ public static Instant millisMod32ToInstant(int intMillisX) { long millisX = Long.parseLong(Integer.toBinaryString(intMillisX), 2); // unsigned int... Instant now = Instant.now(); long millisNow = instantToTaiMillisSince2004Mod32(now); long delta = millisNow - millisX; // Small positive delta is what we expect. // Small negative delta is fine too, it would mean that the packet came from the future, // which can be explained by our clock being a little behind. // Huge negative delta is from previous mod32, and should be changed to small positive. // Huge positive delta might come from a packet a little from the future and next mod32, // we want instead a small negative delta. if (delta < -(1L << 31)) { delta += (1L << 32); } if (delta > (1L << 31)) { delta -= (1L << 32); } Instant instantX = now.minusMillis(delta); return instantX; }
@Override public LongPositionVector getLatestPosition() { Optional<Address> emptyAddress = Optional.empty(); return new LongPositionVector(emptyAddress, Instant.now(), currentPosition, true, speedMetersPerSecond, headingDegreesFromNorth); } }
/** Returns GPSd position until the first move is called. after which it never asks gpsd. */ @Override public LongPositionVector getLatestPosition() { logger.trace("position provider request to gpsd client with current position {}", currentPosition); Optional<Address> emptyAddress = Optional.empty(); return (currentPosition == null) ? super.getLatestPosition() : new LongPositionVector(emptyAddress, Instant.now(), currentPosition, true, speedMetersPerSecond, headingDegreesFromNorth); } }
@Override public LongPositionVector getLatestPosition() { return new LongPositionVector(emptyAddress, Instant.now(), new Position(lat, lon), isPositionConfident, 0, 0); } };
@Override public LongPositionVector getLatestPosition() { return new LongPositionVector(emptyAddress, Instant.now(), new Position(lat, lon), isPositionConfident, 0, 0); } };
@Test public void testNow() throws Exception { System.out.println("Date: " + new Date().getTime()); System.out.println("Joda: " + DateTime.now().getMillis()); System.out.println("JSR310: " + Instant.now().toEpochMilli()); }
logger.info("Sending first CAM"); send(cam); lastSend = Instant.now(); lastSendLowFreq = Instant.now(); lastSendPos = lpv; } else { long deltaTime = Duration.between(lastSend, Instant.now()).toMillis(); long lowFreqDeltaTime = Duration.between(lastSendLowFreq, Instant.now()).toMillis(); LongPositionVector lpv = position.getLatestPosition(); if (farEnough(lastSendPos, lpv)) { logger.info("Sending CAM" + (withLowFreq ? " with Low Freq container" : "")); send(cam); lastSend = Instant.now(); lastSendPos = lpv; if (withLowFreq) { lastSendLowFreq = lastSend; }
public void updateFromForwardedMessage(Address address, LongPositionVector position) { final Entry oldEntry = gnMap.get(address); final Entry entry = (oldEntry == null ? new Entry.Builder() : new Entry.Builder(oldEntry)) .address(address) .position(position) .timestamp(Instant.now()) .create(); logger.debug("Adding non-(SHB/BEACON) entry {}", address.toString()); putAndSchedule(entry); }
public void updateFromDirectMessage(final Address address, final MacAddress macAddress, final LongPositionVector position) { final Entry oldEntry = gnMap.get(address); final Entry entry = (oldEntry == null ? new Entry.Builder() : new Entry.Builder(oldEntry)) .address(address) .macAddress(macAddress) .position(position) .isNeighbour(true) .timestamp(Instant.now()) .create(); logger.debug("Adding direct neighbour {}", address.toString()); putAndSchedule(entry); }
@Override public ApiFuture<ResponseT> futureCall(RequestT request, ApiCallContext inputContext) { Preconditions.checkNotNull(request); HttpJsonCallContext context = HttpJsonCallContext.createDefault().nullToSelf(inputContext); @Nullable Instant deadline = context.getDeadline(); // Try to convert the timeout into a deadline and use it if it occurs before the actual deadline if (context.getTimeout() != null) { @Nonnull Instant newDeadline = Instant.now().plus(context.getTimeout()); if (deadline == null || newDeadline.isBefore(deadline)) { deadline = newDeadline; } } HttpJsonCallOptions callOptions = HttpJsonCallOptions.newBuilder() .setDeadline(deadline) .setCredentials(context.getCredentials()) .build(); return context.getChannel().issueFutureUnaryCall(callOptions, request, descriptor); }
@Override public ApiFuture<ResponseT> futureCall(RequestT request, ApiCallContext inputContext) { Preconditions.checkNotNull(request); HttpJsonCallContext context = HttpJsonCallContext.createDefault().nullToSelf(inputContext); @Nullable Instant deadline = context.getDeadline(); // Try to convert the timeout into a deadline and use it if it occurs before the actual deadline if (context.getTimeout() != null) { @Nonnull Instant newDeadline = Instant.now().plus(context.getTimeout()); if (deadline == null || newDeadline.isBefore(deadline)) { deadline = newDeadline; } } HttpJsonCallOptions callOptions = HttpJsonCallOptions.newBuilder() .setDeadline(deadline) .setCredentials(context.getCredentials()) .build(); return context.getChannel().issueFutureUnaryCall(callOptions, request, descriptor); }
@Test public void testTimeout() { HttpJsonChannel mockChannel = Mockito.mock(HttpJsonChannel.class); String expectedRequest = "fake"; HttpJsonDirectCallable<String, String> callable = new HttpJsonDirectCallable<>(API_DESCRIPTOR); // Mock the channel that captures the call options ArgumentCaptor<HttpJsonCallOptions> capturedCallOptions = ArgumentCaptor.forClass(HttpJsonCallOptions.class); Mockito.when( mockChannel.issueFutureUnaryCall( capturedCallOptions.capture(), Mockito.anyString(), Mockito.any(ApiMethodDescriptor.class))) .thenReturn(SettableApiFuture.create()); // Compose the call context Duration timeout = Duration.ofSeconds(10); Instant minExpectedDeadline = Instant.now().plus(timeout); HttpJsonCallContext callContext = HttpJsonCallContext.createDefault().withChannel(mockChannel).withTimeout(timeout); callable.futureCall(expectedRequest, callContext); Instant maxExpectedDeadline = Instant.now().plus(timeout); // Verify that the timeout was converted into a deadline assertThat(capturedCallOptions.getValue().getDeadline()).isAtLeast(minExpectedDeadline); assertThat(capturedCallOptions.getValue().getDeadline()).isAtMost(maxExpectedDeadline); }
@Test public void testTimeoutBeforeDeadline() { HttpJsonChannel mockChannel = Mockito.mock(HttpJsonChannel.class); String expectedRequest = "fake"; HttpJsonDirectCallable<String, String> callable = new HttpJsonDirectCallable<>(API_DESCRIPTOR); // Mock the channel that captures the call options ArgumentCaptor<HttpJsonCallOptions> capturedCallOptions = ArgumentCaptor.forClass(HttpJsonCallOptions.class); Mockito.when( mockChannel.issueFutureUnaryCall( capturedCallOptions.capture(), Mockito.anyString(), Mockito.any(ApiMethodDescriptor.class))) .thenReturn(SettableApiFuture.create()); // Compose the call context Duration timeout = Duration.ofSeconds(10); Instant subsequentDeadline = Instant.now().plusSeconds(15); Instant minExpectedDeadline = Instant.now().plus(timeout); HttpJsonCallContext callContext = HttpJsonCallContext.createDefault() .withChannel(mockChannel) .withDeadline(subsequentDeadline) .withTimeout(timeout); callable.futureCall(expectedRequest, callContext); Instant maxExpectedDeadline = Instant.now().plus(timeout); // Verify that the timeout was converted into a deadline assertThat(capturedCallOptions.getValue().getDeadline()).isAtLeast(minExpectedDeadline); assertThat(capturedCallOptions.getValue().getDeadline()).isAtMost(maxExpectedDeadline); }
@Test public void testTimeoutAfterDeadline() { HttpJsonChannel mockChannel = Mockito.mock(HttpJsonChannel.class); String expectedRequest = "fake"; HttpJsonDirectCallable<String, String> callable = new HttpJsonDirectCallable<>(API_DESCRIPTOR); // Mock the channel that captures the call options ArgumentCaptor<HttpJsonCallOptions> capturedCallOptions = ArgumentCaptor.forClass(HttpJsonCallOptions.class); Mockito.when( mockChannel.issueFutureUnaryCall( capturedCallOptions.capture(), Mockito.anyString(), Mockito.any(ApiMethodDescriptor.class))) .thenReturn(SettableApiFuture.create()); // Compose the call context Instant priorDeadline = Instant.now().plusSeconds(5); Duration timeout = Duration.ofSeconds(10); HttpJsonCallContext callContext = HttpJsonCallContext.createDefault() .withChannel(mockChannel) .withDeadline(priorDeadline) .withTimeout(timeout); callable.futureCall(expectedRequest, callContext); // Verify that the timeout was ignored assertThat(capturedCallOptions.getValue().getDeadline()).isEqualTo(priorDeadline); }
@Override public LongPositionVector getLatestPosition() { Optional<Address> emptyAddress = Optional.empty(); if (lastSeenTPV == null) { Instant timestamp = Instant.now(); Position position = new Position(Double.NaN, Double.NaN); // NaN or 0? boolean isPositionConfident = false; double speedMetersPerSecond = 0; double headingDegreesFromNorth = 0; return new LongPositionVector(emptyAddress, timestamp, position, isPositionConfident, speedMetersPerSecond, headingDegreesFromNorth); } else { final TPV tpv = lastSeenTPV; // Is this enough to ensure that tpv will remain the same // through the rest of the method? Instant timestamp = OffsetDateTime.parse(tpv.time()).toInstant(); Position position = new Position(tpv.lat(), tpv.lon()); boolean isPositionConfident = false; // TODO: double-check conditions for PAI=true. double speedMetersPerSecond = tpv.speed(); double headingDegreesFromNorth = tpv.track(); return new LongPositionVector(emptyAddress, timestamp, position, isPositionConfident, speedMetersPerSecond, headingDegreesFromNorth); } }