@Test public void testGetNextMessageIdFailsIfAllMidsAreInUse() throws Exception { NetworkConfig config = network.createStandardTestConfig(); GroupedMessageIdTracker tracker = new GroupedMessageIdTracker(INITIAL_MID, 0, TOTAL_NO_OF_MIDS, config); for (int i = 0; i < TOTAL_NO_OF_MIDS / 2; i++) { int mid = tracker.getNextMessageId(); assertThat(mid, is(not(-1))); } for (int i = 0; i < TOTAL_NO_OF_MIDS / 2; i++) { int mid = tracker.getNextMessageId(); if (0 > mid) { return; } } fail("mids should run out."); }
public int getNextMessageId() { final long now = ClockUtil.nanoRealtime(); synchronized (this) { int mid = (currentMID & 0xffff) % range; int index = mid / sizeOfGroups; int nextIndex = (index + 1) % numberOfGroups; if (midLease[nextIndex] - now < 0) { midLease[index] = now + exchangeLifetimeNanos; currentMID = mid + 1; return mid + min; } } return Message.NONE; }
GroupedMessageIdTracker implements MessageIdTracker { public int getNextMessageId() { final long now = ClockUtil.nanoRealtime(); synchronized (this) { int mid = (currentMID & 0xffff) % range; int index = mid / sizeOfGroups; int nextIndex = (index + 1) % numberOfGroups; if (midLease[nextIndex] - now < 0) { midLease[index] = now + exchangeLifetimeNanos; currentMID = mid + 1; return mid + min; } } return Message.NONE; } }
GroupedMessageIdTracker implements MessageIdTracker { public int getNextMessageId() { final long now = ClockUtil.nanoRealtime(); synchronized (this) { int mid = (currentMID & 0xffff) % range; int index = mid / sizeOfGroups; int nextIndex = (index + 1) % numberOfGroups; if (midLease[nextIndex] - now < 0) { midLease[index] = now + exchangeLifetimeNanos; currentMID = mid + 1; return mid + min; } } return Message.NONE; } GroupedMessageIdTracker(int initialMid, int minMid, int maxMid, NetworkConfig config); }
GroupedMessageIdTracker implements MessageIdTracker { public int getNextMessageId() { final long now = ClockUtil.nanoRealtime(); synchronized (this) { int mid = (currentMID & 0xffff) % range; int index = mid / sizeOfGroups; int nextIndex = (index + 1) % numberOfGroups; if (midLease[nextIndex] - now < 0) { midLease[index] = now + exchangeLifetimeNanos; currentMID = mid + 1; return mid + min; } } return Message.NONE; } GroupedMessageIdTracker(int initialMid, int minMid, int maxMid, NetworkConfig config); int getNextMessageId(); int getGroupSize(); }
GroupedMessageIdTracker implements MessageIdTracker { public int getNextMessageId() { final long now = ClockUtil.nanoRealtime(); synchronized (this) { int mid = (currentMID & 0xffff) % range; int index = mid / sizeOfGroups; int nextIndex = (index + 1) % numberOfGroups; if (midLease[nextIndex] - now < 0) { midLease[index] = now + exchangeLifetimeNanos; currentMID = mid + 1; return mid + min; } } return Message.NONE; } GroupedMessageIdTracker(int initialMid, int minMid, int maxMid, NetworkConfig config); int getNextMessageId(); int getGroupSize();  }