Skip to content

Commit

Permalink
protocol v3 that supports storing end sample
Browse files Browse the repository at this point in the history
  • Loading branch information
dernasherbrezon committed Jan 28, 2024
1 parent d0f2bf5 commit 0bc1d2f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/main/java/ru/r2cloud/jradio/BeaconInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public boolean hasNext() {
int protocolVersion = is.readInt();
if (protocolVersion == BeaconOutputStream.PROTOCOL_V2) {
current = readProtocolv2();
} else if (protocolVersion == BeaconOutputStream.PROTOCOL_V3) {
current = readProtocolv3();
} else {
return false;
}
Expand Down Expand Up @@ -66,6 +68,12 @@ private T readProtocolv2() throws Exception {
return result;
}

private T readProtocolv3() throws Exception {
T result = readProtocolv2();
result.setEndSample(is.readLong());
return result;
}

@Override
public T next() {
if (current == null) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/ru/r2cloud/jradio/BeaconOutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public class BeaconOutputStream implements Closeable {

public static final int PROTOCOL_V2 = 1;
public static final int PROTOCOL_V3 = 2;
private final DataOutputStream dos;

public BeaconOutputStream(OutputStream os) {
Expand All @@ -19,7 +20,7 @@ public void write(Beacon beacon) throws IOException {
return;
}
dos.writeInt(0);
dos.writeInt(PROTOCOL_V2);
dos.writeInt(PROTOCOL_V3);
dos.writeInt(beacon.getRawData().length);
dos.write(beacon.getRawData());
dos.writeLong(beacon.getBeginMillis());
Expand All @@ -46,6 +47,7 @@ public void write(Beacon beacon) throws IOException {
dos.writeFloat(0.0f);
dos.writeLong(0);
}
dos.writeLong(beacon.getEndSample());
}

@Override
Expand Down
23 changes: 22 additions & 1 deletion src/test/java/ru/r2cloud/jradio/BeaconInputStreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public void testReadWrite() throws Exception {
meta.setSnr(1.22f);
meta.setFrequencyError(1001l);
data.setRxMeta(meta);
data.setEndSample(2L);

bos.write(data);
bos.close();
Expand All @@ -42,13 +43,33 @@ public void testReadWrite() throws Exception {
assertEquals(data.getRxMeta().getRssi(), actual.getRxMeta().getRssi(), 0.0001);
assertEquals(data.getRxMeta().getSnr(), actual.getRxMeta().getSnr(), 0.0001);
assertEquals(data.getRxMeta().getFrequencyError(), actual.getRxMeta().getFrequencyError());
assertEquals(data.getEndSample(), actual.getEndSample());
assertArrayEquals(data.getRawData(), actual.getRawData());
assertFalse(bis.hasNext());
bis.close();
}

@Test
public void testOldFormat() throws Exception {
public void testv2Format() throws Exception {
byte[] data = new byte[] { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 1, 2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 63, -99, 112, -92, 63, -100, 40, -10, 0, 0, 0, 0, 0, 0, 3, -23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
ByteArrayInputStream bais = new ByteArrayInputStream(data);
BeaconInputStream<RawBeacon> bis = new BeaconInputStream<>(bais, RawBeacon.class);
assertTrue(bis.hasNext());
Beacon actual = bis.next();
assertNotNull(actual);
assertEquals(1L, actual.getBeginMillis());
assertEquals(1L, actual.getBeginSample());
assertArrayEquals(new byte[] { 0x01, 0x02 }, actual.getRawData());
assertNotNull(actual.getRxMeta());
assertEquals(1.23f, actual.getRxMeta().getRssi(), 0.0f);
assertEquals(1.22f, actual.getRxMeta().getSnr(), 0.0f);
assertEquals(1001L, actual.getRxMeta().getFrequencyError().longValue());
assertFalse(bis.hasNext());
bis.close();
}

@Test
public void testv1Format() throws Exception {
byte[] data = new byte[] { 0, 0, 0, 2, 1, 2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1 };
ByteArrayInputStream bais = new ByteArrayInputStream(data);
BeaconInputStream<RawBeacon> bis = new BeaconInputStream<>(bais, RawBeacon.class);
Expand Down

0 comments on commit 0bc1d2f

Please sign in to comment.