Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 31751a8

Browse files
committed
Merge branch 'master' of https://github.com/real-logic/Aeron.git
2 parents 8b85c5a + 1575daa commit 31751a8

File tree

5 files changed

+52
-58
lines changed

5 files changed

+52
-58
lines changed

aeron-client/src/main/java/uk/co/real_logic/aeron/Image.java

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
import uk.co.real_logic.agrona.concurrent.UnsafeBuffer;
2222
import uk.co.real_logic.agrona.concurrent.status.Position;
2323

24-
import java.nio.ByteOrder;
2524
import java.nio.channels.FileChannel;
2625
import java.util.Arrays;
2726

27+
import static java.nio.ByteOrder.LITTLE_ENDIAN;
2828
import static uk.co.real_logic.aeron.logbuffer.LogBufferDescriptor.*;
2929
import static uk.co.real_logic.aeron.logbuffer.TermReader.*;
3030
import static uk.co.real_logic.aeron.protocol.DataHeaderFlyweight.TERM_ID_FIELD_OFFSET;
@@ -165,11 +165,13 @@ public boolean isClosed()
165165
* The position this {@link Image} has been consumed to by the subscriber.
166166
*
167167
* @return the position this {@link Image} has been consumed to by the subscriber.
168-
* @throws IllegalStateException is the {@link Image} is closed.
169168
*/
170169
public long position()
171170
{
172-
ensureOpen();
171+
if (isClosed)
172+
{
173+
return 0;
174+
}
173175

174176
return subscriberPosition.get();
175177
}
@@ -191,11 +193,13 @@ public FileChannel fileChannel()
191193
* @param fragmentHandler to which messages are delivered.
192194
* @param fragmentLimit for the number of fragments to be consumed during one polling operation.
193195
* @return the number of fragments that have been consumed.
194-
* @throws IllegalStateException is the {@link Image} is closed.
195196
*/
196197
public int poll(final FragmentHandler fragmentHandler, final int fragmentLimit)
197198
{
198-
ensureOpen();
199+
if (isClosed)
200+
{
201+
return 0;
202+
}
199203

200204
final long position = subscriberPosition.get();
201205
final int termOffset = (int)position & termLengthMask;
@@ -219,11 +223,13 @@ public int poll(final FragmentHandler fragmentHandler, final int fragmentLimit)
219223
* @param blockHandler to which block is delivered.
220224
* @param blockLengthLimit up to which a block may be in length.
221225
* @return the number of bytes that have been consumed.
222-
* @throws IllegalStateException is the {@link Image} is closed.
223-
* */
226+
*/
224227
public int blockPoll(final BlockHandler blockHandler, final int blockLengthLimit)
225228
{
226-
ensureOpen();
229+
if (isClosed)
230+
{
231+
return 0;
232+
}
227233

228234
final long position = subscriberPosition.get();
229235
final int termOffset = (int)position & termLengthMask;
@@ -237,7 +243,7 @@ public int blockPoll(final BlockHandler blockHandler, final int blockLengthLimit
237243
{
238244
try
239245
{
240-
final int termId = termId(termBuffer, termOffset);
246+
final int termId = termBuffer.getInt(termOffset + TERM_ID_FIELD_OFFSET, LITTLE_ENDIAN);
241247

242248
blockHandler.onBlock(termBuffer, termOffset, bytesConsumed, sessionId, termId);
243249
}
@@ -259,11 +265,13 @@ public int blockPoll(final BlockHandler blockHandler, final int blockLengthLimit
259265
* @param fileBlockHandler to which block is delivered.
260266
* @param blockLengthLimit up to which a block may be in length.
261267
* @return the number of bytes that have been consumed.
262-
* @throws IllegalStateException is the {@link Image} is closed.
263268
*/
264269
public int filePoll(final FileBlockHandler fileBlockHandler, final int blockLengthLimit)
265270
{
266-
ensureOpen();
271+
if (isClosed)
272+
{
273+
return 0;
274+
}
267275

268276
final long position = subscriberPosition.get();
269277
final int termOffset = (int)position & termLengthMask;
@@ -280,7 +288,7 @@ public int filePoll(final FileBlockHandler fileBlockHandler, final int blockLeng
280288
try
281289
{
282290
final long offset = ((long)capacity * activeIndex) + termOffset;
283-
final int termId = termId(termBuffer, termOffset);
291+
final int termId = termBuffer.getInt(termOffset + TERM_ID_FIELD_OFFSET, LITTLE_ENDIAN);
284292

285293
fileBlockHandler.onBlock(logBuffers.fileChannel(), offset, bytesConsumed, sessionId, termId);
286294
}
@@ -301,23 +309,6 @@ ManagedResource managedResource()
301309
return new ImageManagedResource();
302310
}
303311

304-
private void ensureOpen()
305-
{
306-
if (isClosed)
307-
{
308-
throw new IllegalStateException(String.format(
309-
"Image is closed: channel=%s streamId=%d sessionId=%d",
310-
subscription.channel(),
311-
subscription.streamId(),
312-
sessionId));
313-
}
314-
}
315-
316-
private static int termId(final UnsafeBuffer buffer, final int frameOffset)
317-
{
318-
return buffer.getInt(frameOffset + TERM_ID_FIELD_OFFSET, ByteOrder.LITTLE_ENDIAN);
319-
}
320-
321312
private class ImageManagedResource implements ManagedResource
322313
{
323314
private long timeOfLastStateChange = 0;

aeron-client/src/main/java/uk/co/real_logic/aeron/Publication.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ public class Publication implements AutoCloseable
5151
*/
5252
public static final long ADMIN_ACTION = -3;
5353

54+
/**
55+
* The {@link Publication} has been closed and should no longer be used.
56+
*/
57+
public static final long CLOSED = -4;
58+
5459
private final long registrationId;
5560
private final int streamId;
5661
private final int sessionId;
@@ -154,9 +159,7 @@ public int maxMessageLength()
154159
*/
155160
public boolean hasBeenConnected()
156161
{
157-
ensureOpen();
158-
159-
return positionLimit.getVolatile() > 0;
162+
return !isClosed && positionLimit.getVolatile() > 0;
160163
}
161164

162165
/**
@@ -206,7 +209,10 @@ void release()
206209
*/
207210
public long position()
208211
{
209-
ensureOpen();
212+
if (isClosed)
213+
{
214+
return CLOSED;
215+
}
210216

211217
final int activeTermId = activeTermId(logMetaDataBuffer);
212218
final int currentTail = termAppenders[indexByTerm(initialTermId, activeTermId)].tailVolatile();
@@ -223,7 +229,10 @@ public long position()
223229
*/
224230
public long positionLimit()
225231
{
226-
ensureOpen();
232+
if (isClosed)
233+
{
234+
return CLOSED;
235+
}
227236

228237
return positionLimit.getVolatile();
229238
}
@@ -246,11 +255,13 @@ public long offer(final DirectBuffer buffer)
246255
* @param offset offset in the buffer at which the encoded message begins.
247256
* @param length in bytes of the encoded message.
248257
* @return The new stream position, otherwise {@link #NOT_CONNECTED}, {@link #BACK_PRESSURED} or {@link #ADMIN_ACTION}.
249-
* @throws IllegalStateException if the publication is closed.
250258
*/
251259
public long offer(final DirectBuffer buffer, final int offset, final int length)
252260
{
253-
ensureOpen();
261+
if (isClosed)
262+
{
263+
return CLOSED;
264+
}
254265

255266
final long limit = positionLimit.getVolatile();
256267
final int initialTermId = this.initialTermId;
@@ -303,13 +314,15 @@ else if (0 == limit)
303314
* @param bufferClaim to be populated if the claim succeeds.
304315
* @return The new stream position, otherwise {@link #NOT_CONNECTED}, {@link #BACK_PRESSURED} or {@link #ADMIN_ACTION}.
305316
* @throws IllegalArgumentException if the length is greater than max payload length within an MTU.
306-
* @throws IllegalStateException if the publication is closed.
307317
* @see BufferClaim#commit()
308318
* @see BufferClaim#abort()
309319
*/
310320
public long tryClaim(final int length, final BufferClaim bufferClaim)
311321
{
312-
ensureOpen();
322+
if (isClosed)
323+
{
324+
return CLOSED;
325+
}
313326

314327
final long limit = positionLimit.getVolatile();
315328
final int initialTermId = this.initialTermId;
@@ -379,14 +392,4 @@ else if (nextOffset == TermAppender.TRIPPED)
379392

380393
return newPosition;
381394
}
382-
383-
private void ensureOpen()
384-
{
385-
if (isClosed)
386-
{
387-
throw new IllegalStateException(String.format(
388-
"Publication is closed: channel=%s streamId=%d sessionId=%d registrationId=%d",
389-
channel, streamId, sessionId, registrationId));
390-
}
391-
}
392395
}

aeron-client/src/test/java/uk/co/real_logic/aeron/ImageTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,16 @@ public void setUp()
9292
when(logBuffers.atomicBuffers()).thenReturn(atomicBuffers);
9393
}
9494

95-
@Test(expected = IllegalStateException.class)
96-
public void shouldThrowExceptionWhenPollingOnClosed()
95+
@Test
96+
public void shouldHandleClosedImage()
9797
{
9898
final long initialPosition = computePosition(INITIAL_TERM_ID, 0, POSITION_BITS_TO_SHIFT, INITIAL_TERM_ID);
9999
final Image image = createImage(initialPosition);
100100

101101
image.managedResource();
102102

103103
assertTrue(image.isClosed());
104-
image.poll(mockFragmentHandler, Integer.MAX_VALUE);
104+
assertThat(image.poll(mockFragmentHandler, Integer.MAX_VALUE), is(0));
105105
}
106106

107107
@Test

aeron-client/src/test/java/uk/co/real_logic/aeron/PublicationTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,27 +84,27 @@ public void setUp()
8484
publication.incRef();
8585
}
8686

87-
@Test(expected = IllegalStateException.class)
87+
@Test
8888
public void shouldEnsureThePublicationIsOpenBeforeReadingPosition()
8989
{
9090
publication.close();
91-
publication.position();
91+
assertThat(publication.position(), is(Publication.CLOSED));
9292
}
9393

94-
@Test(expected = IllegalStateException.class)
94+
@Test
9595
public void shouldEnsureThePublicationIsOpenBeforeOffer()
9696
{
9797
publication.close();
9898
assertTrue(publication.isClosed());
99-
publication.offer(atomicSendBuffer);
99+
assertThat(publication.offer(atomicSendBuffer), is(Publication.CLOSED));
100100
}
101101

102-
@Test(expected = IllegalStateException.class)
102+
@Test
103103
public void shouldEnsureThePublicationIsOpenBeforeClaim()
104104
{
105105
publication.close();
106106
final BufferClaim bufferClaim = new BufferClaim();
107-
publication.tryClaim(SEND_BUFFER_CAPACITY, bufferClaim);
107+
assertThat(publication.tryClaim(SEND_BUFFER_CAPACITY, bufferClaim), is(Publication.CLOSED));
108108
}
109109

110110
@Test

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ subprojects {
184184

185185
project(':aeron-client') {
186186
dependencies {
187-
compile 'uk.co.real-logic:Agrona:0.4.8'
187+
compile 'uk.co.real-logic:Agrona:0.4.9-SNAPSHOT'
188188
}
189189

190190
task sourcesJar(type: Jar) {

0 commit comments

Comments
 (0)