Skip to content

Commit d65eec0

Browse files
committed
hop list is now accessible when iterating message from a channel.
1 parent 50285cf commit d65eec0

File tree

4 files changed

+53
-66
lines changed

4 files changed

+53
-66
lines changed

src/net/sharksystem/asap/ASAPMessages.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.sharksystem.asap;
22

33
import net.sharksystem.asap.ASAPException;
4+
import net.sharksystem.asap.engine.ASAPInternalChunk;
45

56
import java.io.IOException;
67
import java.util.Iterator;
@@ -69,4 +70,12 @@ CharSequence getMessageAsCharSequence(int position, boolean chronologically)
6970

7071
byte[] getMessage(int position, boolean chronologically)
7172
throws ASAPException, IOException;
73+
74+
/**
75+
* Return chunk in which message at position is to be found
76+
* @param position
77+
* @param chronologically
78+
* @return
79+
*/
80+
ASAPInternalChunk getChunk(int position, boolean chronologically) throws IOException, ASAPException;
7281
}

src/net/sharksystem/asap/apps/testsupport/ASAPMessagesMock.java

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/net/sharksystem/asap/engine/ASAPInMemoMessages.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,32 @@ public CharSequence getMessageAsCharSequence(int position, boolean chronological
147147
return new String(this.getMessage(position, chronologically));
148148
}
149149

150+
public ASAPInternalChunk getChunk(int position, boolean chronologically) throws IOException, ASAPException {
151+
this.initialize();
152+
153+
if(position >= this.numberOfMessages)
154+
throw new ASAPException("Position reaches beyond total number of messages in this chunk (is it even empty?)");
155+
156+
if(!chronologically) {
157+
// invert position - first becomes last etc.
158+
position = this.numberOfMessages - 1 - position;
159+
}
160+
161+
ASAPInternalChunk foundChunk = null;
162+
// find chunk
163+
for(ASAPInternalChunk chunk : this.chunkList) {
164+
if(position < chunk.getNumberMessage()) {
165+
foundChunk = chunk;
166+
break;
167+
}
168+
169+
position -= chunk.getNumberMessage();
170+
}
171+
172+
if(foundChunk == null) throw new ASAPException("cannot find chunk for position - looks like a bug");
173+
return foundChunk;
174+
}
175+
150176
@Override
151177
public byte[] getMessage(int position, boolean chronologically)
152178
throws ASAPException, IOException {
@@ -162,7 +188,7 @@ public byte[] getMessage(int position, boolean chronologically)
162188
}
163189

164190
if(this.messageCache != null && position >= this.firstIndexMessageCache && position <= this.lastIndexMessageCache) {
165-
return this.messageCache.get(position - this.firstIndexMessageCache);
191+
return this.messageCache.get(position - this.firstIndexMessageCache);
166192
}
167193

168194
// not yet in cache - find chunk with required message

src/net/sharksystem/asap/engine/ASAPMessagesMerger.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package net.sharksystem.asap.engine;
22

3+
import net.sharksystem.SharkNotSupportedException;
34
import net.sharksystem.asap.ASAPException;
45
import net.sharksystem.asap.ASAPMessageCompare;
56
import net.sharksystem.asap.ASAPMessages;
67
import net.sharksystem.asap.utils.PeerIDHelper;
78

9+
import javax.xml.transform.Source;
810
import java.io.IOException;
911
import java.util.ArrayList;
1012
import java.util.Iterator;
@@ -360,13 +362,12 @@ private List<SourceIndex> getIndexList(boolean chronologically) {
360362
return chronologically ? oldFirstPositionList : newFirstPositionList;
361363
}
362364

363-
@Override
364-
public byte[] getMessage(int position, boolean chronologically) throws ASAPException, IOException {
365+
private SourceIndex getSourceIndex(int position, boolean chronologically) throws ASAPException, IOException {
365366
if(position >= this.size)
366367
throw new ASAPException("position index must not exceed total number of messages: "
367-
+ position + " >= " + this.size);
368+
+ position + " >= " + this.size);
368369

369-
SourceIndex sourceIndex = null;
370+
SourceIndex sourceIndex;
370371
List<SourceIndex> usedList = this.getIndexList(chronologically);
371372

372373
sourceIndex = this.getSourceIndex(position, usedList);
@@ -380,7 +381,19 @@ public byte[] getMessage(int position, boolean chronologically) throws ASAPExcep
380381
throw new ASAPException("no message at position: " + position);
381382
}
382383

384+
return sourceIndex;
385+
}
386+
387+
@Override
388+
public byte[] getMessage(int position, boolean chronologically) throws ASAPException, IOException {
389+
SourceIndex sourceIndex = this.getSourceIndex(position, chronologically);
383390
return this.messageSources[sourceIndex.sourceIndex].getMessage(
384391
sourceIndex.positionInSource, chronologically);
385392
}
393+
394+
@Override
395+
public ASAPInternalChunk getChunk(int position, boolean chronologically) throws IOException, ASAPException {
396+
SourceIndex sourceIndex = this.getSourceIndex(position, chronologically);
397+
return this.messageSources[sourceIndex.positionInSource].getChunk(sourceIndex.positionInSource, chronologically);
398+
}
386399
}

0 commit comments

Comments
 (0)