Skip to content

Commit 29ae25e

Browse files
authored
Merge pull request #171 from fo-ifad/DIS6-VardatumLenghtFix
handle variable datum length also contains the id+variable datum lenght field
2 parents d2c6f96 + 714f809 commit 29ae25e

4 files changed

Lines changed: 166 additions & 9 deletions

File tree

src/main/java/edu/nps/moves/dis/AggregateStatePdu.java

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.*;
44
import java.io.*;
5+
import java.nio.ByteBuffer;
56

67
/**
78
* Section 5.3.9.1 informationa bout aggregating entities anc communicating
@@ -337,9 +338,9 @@ public short getPad2() {
337338
}
338339

339340
/**
340-
* This function returns the number of padding bits dependendant on how many Aggregate and Entity ID's
341-
* are in ID lists
342-
*
341+
* This function returns the number of padding bits dependendant on how many
342+
* Aggregate and Entity ID's are in ID lists
343+
*
343344
*/
344345
public short getPad2Bits() {
345346
int val = 16 * ((aggregateIDList.size() + entityIDList.size()) % 2);
@@ -453,6 +454,7 @@ public void marshal(java.nio.ByteBuffer buff) {
453454
* @since ??
454455
*/
455456
public void unmarshal(java.nio.ByteBuffer buff) {
457+
java.nio.ByteBuffer clone = clone(buff);
456458
super.unmarshal(buff);
457459

458460
aggregateID.unmarshal(buff);
@@ -498,15 +500,37 @@ public void unmarshal(java.nio.ByteBuffer buff) {
498500
}
499501

500502
numberOfVariableDatumRecords = buff.getInt();
503+
clone.position(buff.position());
504+
try {
505+
unmarshalVardatums(buff, true);
506+
} catch (Exception e) {
507+
try {
508+
unmarshalVardatums(clone, false);
509+
} catch (Exception ex) {
510+
}
511+
}
512+
} // end of unmarshal method
513+
514+
private void unmarshalVardatums(ByteBuffer buff, boolean varDatumLengthIsPayloadLenght) {
501515
for (int idx = 0; idx < numberOfVariableDatumRecords; idx++) {
502516
VariableDatum anX = new VariableDatum();
503-
anX.unmarshal(buff);
517+
if (varDatumLengthIsPayloadLenght) {
518+
anX.unmarshal(buff);
519+
} else {
520+
anX.unmarshalPayloadLenght(buff, true);
521+
}
504522
variableDatumList.add(anX);
505523
}
524+
}
506525

507-
} // end of unmarshal method
508-
509-
526+
public static java.nio.ByteBuffer clone(java.nio.ByteBuffer original) {
527+
java.nio.ByteBuffer clone = java.nio.ByteBuffer.allocate(original.capacity());
528+
original.rewind();//copy from the beginning
529+
clone.put(original);
530+
original.rewind();
531+
clone.flip();
532+
return clone;
533+
}
510534
/*
511535
* The equals method doesn't always work--mostly it works only on classes that consist only of primitives. Be careful.
512536
*/

src/main/java/edu/nps/moves/dis/VariableDatum.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.*;
44
import java.io.*;
5+
import java.nio.ByteBuffer;
56
import java.nio.charset.Charset;
67

78
/**
@@ -258,10 +259,16 @@ public void marshal(java.nio.ByteBuffer buff) {
258259
* @since ??
259260
*/
260261
public void unmarshal(java.nio.ByteBuffer buff) {
262+
unmarshalPayloadLenght(buff, false);
263+
} // end of unmarshal method
264+
265+
public void unmarshalPayloadLenght(ByteBuffer buff, boolean removeIdAndLengthFieldsFromVaribleDatumLength) {
261266
variableDatumID = buff.getInt();
262267
variableDatumLength = buff.getInt();
263268
int payloadBytes = (int) (variableDatumLength / 8);
264-
269+
if (removeIdAndLengthFieldsFromVaribleDatumLength) {
270+
payloadBytes -= 8;
271+
}
265272
if (variableDatumLength % 8 > 0) {
266273
payloadBytes++;
267274
}
@@ -271,7 +278,7 @@ public void unmarshal(java.nio.ByteBuffer buff) {
271278

272279
padding = createPadding(payloadBytes);
273280
buff.get(padding);
274-
} // end of unmarshal method
281+
}
275282

276283

277284
/*

src/test/java/edu/nps/moves/dis/AggregateStatePduTest.java

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,132 @@ public void unmarshal() throws IOException {
164164
assertTrue(aspdu.getVariableDatumList().containsAll(pduListVardatum));
165165
}
166166

167+
@Test
168+
public void unmarshalVardatumLength() throws IOException {
169+
PduFactory factory = new PduFactory();
170+
Pdu pdu = factory.createPdu(PduFileLoader.load("AggregateStatePduJCATS.raw"));
171+
172+
// Expected field values were determined from Wireshark: Decode As -> DIS.
173+
// Header
174+
assertEquals(6, pdu.getProtocolVersion());
175+
assertEquals(1, pdu.getExerciseID());
176+
assertEquals(33, pdu.getPduType());
177+
assertEquals(7, pdu.getProtocolFamily());
178+
assertEquals(224, pdu.getLength());
179+
assertEquals(0, pdu.getPadding());
180+
181+
AggregateStatePdu aspdu = (AggregateStatePdu) pdu;
182+
183+
//Aggregate ID
184+
assertEquals(201, aspdu.getAggregateID().getSite());
185+
assertEquals(2, aspdu.getAggregateID().getApplication());
186+
assertEquals(535, aspdu.getAggregateID().getEntity());
187+
188+
// Force ID
189+
assertEquals(1, aspdu.getForceID());
190+
191+
//Aggregate State
192+
assertEquals(2, aspdu.getAggregateState());
193+
194+
//Aggregate Type
195+
EntityType type = new EntityType();
196+
type.setEntityKind((short) 1);
197+
type.setDomain((short) 1);
198+
type.setCountry(225);
199+
type.setCategory((short) 5);
200+
type.setSubcategory((short) 3);
201+
type.setSpec((short) 0);
202+
type.setExtra((short) 0);
203+
assertEquals(type, aspdu.getAggregateType());
204+
205+
//Formation
206+
assertEquals(3, aspdu.getFormation());
207+
208+
//Aggregate Marking
209+
assertEquals("535", new String(aspdu.getAggregateMarking().getCharacters()).trim());
210+
211+
//Dimensions
212+
Vector3Float dim = new Vector3Float();
213+
dim.setX((float) 636.396);
214+
dim.setY((float) 989.95);
215+
dim.setZ((float) 179.332);
216+
assertEquals(dim.getX(), aspdu.getDimensions().getX(), 0.1);
217+
assertEquals(dim.getY(), aspdu.getDimensions().getY(), 0.1);
218+
assertEquals(dim.getZ(), aspdu.getDimensions().getZ(), 0.1);
219+
220+
//Orientation
221+
Orientation ori = new Orientation();
222+
ori.setPsi((float) -2.52703);
223+
ori.setTheta((float) -1.0777);
224+
ori.setPhi((float) -2.8237);
225+
assertEquals(ori.getPsi(), aspdu.getOrientation().getPsi(), 0.1);
226+
assertEquals(ori.getTheta(), aspdu.getOrientation().getTheta(), 0.1);
227+
assertEquals(ori.getPhi(), aspdu.getOrientation().getPhi(), 0.1);
228+
229+
//Center of Mass
230+
Vector3Double cen = new Vector3Double();
231+
cen.setX(3213067.76697673);
232+
cen.setY(4709330.86088639);
233+
cen.setZ(2850658.3344315);
234+
assertEquals(cen.getX(), aspdu.getCenterOfMass().getX(), 0.1);
235+
assertEquals(cen.getY(), aspdu.getCenterOfMass().getY(), 0.1);
236+
assertEquals(cen.getZ(), aspdu.getCenterOfMass().getZ(), 0.1);
237+
238+
//Velocity
239+
Vector3Float vel = new Vector3Float();
240+
vel.setX((float) 0);
241+
vel.setY((float) 0);
242+
vel.setZ((float) 0);
243+
assertEquals(vel.getX(), aspdu.getVelocity().getX(), 0.1);
244+
assertEquals(vel.getY(), aspdu.getVelocity().getY(), 0.1);
245+
assertEquals(vel.getZ(), aspdu.getVelocity().getZ(), 0.1);
246+
//number of Aggregates/Entities in Aggregate
247+
assertEquals(4, aspdu.getNumberOfDisAggregates());
248+
assertEquals(0, aspdu.getNumberOfDisEntities());
249+
assertEquals(0, aspdu.getNumberOfSilentAggregateTypes());
250+
assertEquals(0, aspdu.getNumberOfSilentEntityTypes());
251+
252+
//Entity ID List
253+
List<AggregateID> aggIdList = new ArrayList<>();
254+
AggregateID id1 = new AggregateID();
255+
id1.setSite(201);
256+
id1.setApplication(2);
257+
id1.setAggregateID(536);
258+
aggIdList.add(id1);
259+
AggregateID id2 = new AggregateID();
260+
id2.setSite(201);
261+
id2.setApplication(2);
262+
id2.setAggregateID(552);
263+
aggIdList.add(id2);
264+
AggregateID id3 = new AggregateID();
265+
id3.setSite(201);
266+
id3.setApplication(2);
267+
id3.setAggregateID(606);
268+
aggIdList.add(id3);
269+
AggregateID id4 = new AggregateID();
270+
id4.setSite(201);
271+
id4.setApplication(2);
272+
id4.setAggregateID(660);
273+
aggIdList.add(id4);
274+
for (int i = 0; i < aspdu.getAggregateIDList().size(); i++) {
275+
assertTrue(aggIdList.get(i).equals(aspdu.getAggregateIDList().get(i)));
276+
}
277+
//Variable Datums
278+
assertEquals(1, aspdu.getNumberOfVariableDatumRecords());
279+
List<VariableDatum> listVariableDatum = new ArrayList<>();
280+
VariableDatum datum0 = new VariableDatum(512, 32);
281+
282+
byte[] byteDatum0 = {(byte) 0, (byte) 4, (byte) 0, (byte) 0, (byte) 65, (byte) 187, (byte) 103, (byte) 127, (byte) 67, (byte) 11, (byte) 119, (byte) 188, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 67, (byte) 11, (byte) 119, (byte) 188, (byte) 193, (byte) 187, (byte) 103, (byte) 127, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 195, (byte) 11, (byte) 119, (byte) 188, (byte) 65, (byte) 187, (byte) 103, (byte) 127, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 67, (byte) 127, (byte) 130, (byte) 137, (byte) 195, (byte) 58, (byte) 81, (byte) 156, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0};
283+
284+
datum0.setVariableDatumID(999315);
285+
datum0.setPayload(byteDatum0);
286+
datum0.setVariableDatumLength(512);
287+
listVariableDatum.add(datum0);
288+
List<VariableDatum> pduListVardatum = aspdu.getVariableDatumList();
289+
290+
assertTrue(aspdu.getVariableDatumList().containsAll(listVariableDatum));
291+
}
292+
167293
@Test
168294
public void marshal() {
169295
AggregateStatePdu aspdu = new AggregateStatePdu();
224 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)