Skip to content
Snippets Groups Projects
Commit eaf75a18 authored by Brutzman, Don's avatar Brutzman, Don
Browse files

debugged sender problem related to prior socket buffer not getting dropped

parent 91392728
No related branches found
No related tags found
No related merge requests found
......@@ -7,6 +7,7 @@ import edu.nps.moves.dis7.pdus.*;
import edu.nps.moves.dis7.enumerations.*;
import edu.nps.moves.dis7.utilities.PduFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/** Listen to all kinds of PDUs and report them */
......@@ -28,9 +29,8 @@ public class AllPduReceiver
*/
public static void main(String args[])
{
MulticastSocket socket;
DatagramPacket packet;
InetAddress address;
MulticastSocket multicastSocket;
InetAddress internetAddress;
PduFactory pduFactory = new PduFactory();
System.out.println(TRACE_PREFIX + "started...");
......@@ -38,33 +38,38 @@ public class AllPduReceiver
try {
if (args.length == 2)
{
address = InetAddress.getByName(args[0]);
socket = new MulticastSocket(Integer.parseInt(args[1]));
internetAddress = InetAddress.getByName(args[0]);
multicastSocket = new MulticastSocket(Integer.parseInt(args[1]));
}
else
{
System.out.println("Usage: AllPduReceiver <multicast group> <port>");
System.out.println("Default: AllPduReceiver " + DEFAULT_MULTICAST_ADDRESS + " " + DEFAULT_MULTICAST_PORT);
socket = new MulticastSocket(DEFAULT_MULTICAST_PORT);
address = InetAddress.getByName(DEFAULT_MULTICAST_ADDRESS);
multicastSocket = new MulticastSocket(DEFAULT_MULTICAST_PORT);
internetAddress = InetAddress.getByName(DEFAULT_MULTICAST_ADDRESS);
}
System.out.println("To quit: stop or kill this process");
socket.joinGroup(address);
multicastSocket.joinGroup(internetAddress);
byte buffer[] = new byte[1500]; // typical MTU size in bytes
while (true) // Loop infinitely, receiving datagrams
{
byte buffer[] = new byte[1500]; // typical MTU size in bytes
packet = new DatagramPacket(buffer, buffer.length); // reset packet each time
socket.receive(packet); // process blocks here until receipt of network packet with PDU
Arrays.fill(buffer, (byte)0);
DatagramPacket datagramPacket = new DatagramPacket(buffer, buffer.length); // reset packet each time
multicastSocket.receive(datagramPacket); // process blocks here until receipt of network packet with PDU
// datagramPacket.setLength(buffer.length);
// Pdu pdu = pduFactory.createPdu(packet.getData()); // packet.getData() returns byte[] array data buffer
List<Pdu> pduBundle = pduFactory.getPdusFromBundle(packet.getData(),packet.getLength());
if (pduBundle.size() > 1)
System.out.println("Received PDU bundle size is " + pduBundle.size());
else if (pduBundle.isEmpty())
System.out.println("Received PDU bundle is empty, packet.getData().length=" + packet.getData().length + ", error...");
List<Pdu> pduBundle = pduFactory.getPdusFromBundle(datagramPacket.getData(),datagramPacket.getLength());
if (pduBundle.isEmpty())
System.out.println("Received PDU bundle is empty, packet.getData().length=" + datagramPacket.getData().length + ", error...");
// else if (pduBundle.size() == 1)// common case, typically unremarkable
// System.out.println("Received PDU bundle size is " + pduBundle.size());
else if (pduBundle.size() > 1)
System.out.println("Received PDU bundle size is " + pduBundle.size());
for (Pdu nextPdu : pduBundle) // iterator loop through PDU bundle
{
......@@ -75,11 +80,9 @@ public class AllPduReceiver
StringBuilder message = new StringBuilder();
message.append(DisTime.timeStampToString(nextPdu.getTimestamp())).append(" ");
message.append("received DIS PDU ");
String currentPduTypePadded = String.format("%-34s", currentPduType); // - indicates right padding of whitespace
message.append(" " ).append(currentPduTypePadded);
if (currentPduType.getValue() < 10)
message.append(" "); // column spacing
message.append("received new DIS PDU ");
String currentPduTypePadded = String.format("%-48s", currentPduType); // - indicates right padding of whitespace
message.append(currentPduTypePadded);
// optional, verbose
// String currentPduTypeNamePadded = String.format("%-49s", currentPduTypeName); // - indicates right padding of whitespace
// message.append(" of type ").append(currentPduTypeNamePadded); // package.class name
......@@ -104,6 +107,7 @@ public class AllPduReceiver
System.out.println();
}
}
pduBundle.clear();
}
}
catch (IOException e) {
......
This diff is collapsed.
......@@ -447,7 +447,7 @@ public class AllPduSender
}
}
if (generatedPdusList.size() != 72) // TODO create an enumeration DISType.TOTAL_PDU_TYPES
System.out.println("Error: " + generatedPdusList.size() + " PDUs generated but 72 PDUs expected.");
System.out.println("Error: " + generatedPdusList.size() + " PDUs generated, but 72 PDUs expected.");
// Send the PDUs we created
System.out.println("Send the " + generatedPdusList.size() + " PDUs we created...");
......@@ -460,15 +460,16 @@ public class AllPduSender
}
multicastSocket.joinGroup(localMulticastAddress);
// keep object instantiations outside of loops for best performance
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
byte[] buffer;
Pdu aPdu;
DatagramPacket packet;
for (int idx = 0; idx < generatedPdusList.size(); idx++)
{
// careful here! keep object instantiations inside of loop to avoid endless array and packet growth
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
aPdu = generatedPdusList.get(idx);
try
{
......@@ -479,7 +480,9 @@ public class AllPduSender
multicastSocket.send(packet);
String currentPduTypeValuePadded = String.format("%2s", aPdu.getPduType().getValue());
String currentPduTypePadded = String.format("%-50s", aPdu.getPduType().toString()); // - indicates right padding of whitespace
String packetLengthPadded = String.format("%3s", packet.getLength());
System.out.print ("Sent DIS PDU " + currentPduTypeValuePadded + " " + currentPduTypePadded );
System.out.print ("(packet.getLength()=" + packetLengthPadded + ")"); // diagnostic, beware of ever-growing packet size!
System.out.println(" of type " + aPdu.getClass().getName());
Thread.sleep(THREAD_SLEEP_INTERVAL); // pause for debugging, if zero this process still yields
......@@ -517,15 +520,14 @@ public class AllPduSender
System.out.println("Usage: AllPduSender <multicast group> <port>");
System.out.println("Actual: AllPduSender " + multicastAddress.getHostAddress() + " " + port);
allPduSender = new AllPduSender(args[0], Integer.parseInt(args[1]));
totalSentPdus = allPduSender.run();
}
else
{
System.out.println("Usage: AllPduSender <multicast group> <port>");
System.out.println("Default: AllPduSender " + DEFAULT_MULTICAST_ADDRESS + " " + DEFAULT_MULTICAST_PORT);
allPduSender = new AllPduSender(DEFAULT_MULTICAST_ADDRESS, DEFAULT_MULTICAST_PORT);
totalSentPdus = allPduSender.run();
}
totalSentPdus = allPduSender.run(); // do it to it
System.out.println("OpenDis7Examples.AllPduSender complete, sent " + totalSentPdus + " PDUs total.");
}
}
This diff is collapsed.
......@@ -213,7 +213,7 @@ public class EspduSender
{
System.out.println(TRACE_PREFIX + "sending " + SEND_LOOPS_TO_PERFORM + " sets of packets:"); // + address.toString()
for (int index = 0; index < SEND_LOOPS_TO_PERFORM; index++)
for (int index = 1; index <= SEND_LOOPS_TO_PERFORM; index++)
{
// DIS time is a pain in the uh, neck. DIS time units are 2^31-1 units per
// hour, and time is set to DIS time units from the top of the hour.
......@@ -302,6 +302,8 @@ public class EspduSender
firePdu.setLocationInWorldCoordinates(espdu.getEntityLocation());
byte[] fireArray = firePdu.marshal();
System.out.println("FirePdu #" + index + " firePdu=[FireMissionIndex=" + firePdu.getFireMissionIndex() + ", descriptor=" + firePdu.getDescriptor()+ "]");
// CommentPdu newCommentPdu = new CommentPdu();
// ArrayList<VariableDatum> payloadList = new ArrayList<>();
// ArrayList<String> commentsList = new ArrayList<>();
......@@ -327,17 +329,19 @@ public class EspduSender
for (InetAddress networkAddress : localNetworkAddresses) {
if (espduArray.length > 0)
{
System.out.println(TRACE_PREFIX + "sending datagram packet [" + espdu.getPduType().toString() + "] to " +
String.format("%-15s", networkAddress.getHostAddress()) + " port " + port);
packet = new DatagramPacket(espduArray, espduArray.length, networkAddress, port);
System.out.println(TRACE_PREFIX + "sending datagram packet [" + espdu.getPduType().toString() + "] " +
"packet.getLength()=" + packet.getLength() + ", " + // diagnostic, beware of ever-growing packet size
String.format("to %-15s", networkAddress.getHostAddress()) + " port " + port);
socket.send(packet);
}
// TODO experiment with these! 8)
if (fireArray.length > 0)
{
System.out.println(TRACE_PREFIX + "sending datagram packet [" + firePdu.getPduType().toString() + " ] to " +
String.format("%-15s", networkAddress.getHostAddress()) + " port " + port);
packet = new DatagramPacket(fireArray, fireArray.length, networkAddress, port); // alternate
System.out.println(TRACE_PREFIX + "sending datagram packet [" + firePdu.getPduType().toString() + " ] " +
"packet.getLength()= " + packet.getLength() + ", " + // diagnostic, beware of ever-growing packet size
String.format("to %-15s", networkAddress.getHostAddress()) + " port " + port);
socket.send(packet);
}
// // TODO experiment with these! 8)
......
Invocation instructions:
1. run/debug EspduReceiver.java (since sender does not block, first be ready to listen)
2. run/debug EspduSender.java
Program responses, sender and receiver:
===================================================
run:
[OpenDis7Examples.EspduSender] started...
[OpenDis7Examples.EspduSender] sending multicast ESPDU packets to 239.1.2.3 port 3000
===============
espdu entityType information:
EntityKind =EntityKind 1 PLATFORM
Country =Country 225 UNITED_STATES_OF_AMERICA_USA
Domain =Land
Category =1
SubCategory=1
Specific =Country 225 UNITED_STATES_OF_AMERICA_USA
[OpenDis7Examples.EspduSender] sending 1 sets of packets:
===============
Create new PDUs
latitude, longitude: [36.595517, -121.87693999999999]
coordinate conversion: [-2707488.3677768684, -4353666.735244378, 3781450.3202754413]
Espdu #0 entityID=[1,2,3]
[OpenDis7Examples.EspduSender] sending datagram packet [DISPDUType 1 ENTITY_STATE] to 127.255.255.255 port 3000
[OpenDis7Examples.EspduSender] sending datagram packet [DISPDUType 2 FIRE ] to 127.255.255.255 port 3000
[OpenDis7Examples.EspduSender] sending datagram packet [DISPDUType 1 ENTITY_STATE] to 172.20.209.222 port 3000
[OpenDis7Examples.EspduSender] sending datagram packet [DISPDUType 2 FIRE ] to 172.20.209.222 port 3000
[OpenDis7Examples.EspduSender] sending datagram packet [DISPDUType 1 ENTITY_STATE] to 172.16.0.255 port 3000
[OpenDis7Examples.EspduSender] sending datagram packet [DISPDUType 2 FIRE ] to 172.16.0.255 port 3000
===============
[OpenDis7Examples.EspduSender] complete.
BUILD SUCCESSFUL (total time: 3 seconds)
===================================================
[OpenDis7Examples.EspduReceiver] started...
[OpenDis7Examples.EspduReceiver] listening for PDU packets on 239.1.2.3 port 3000
===============
1. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu
entityID triplet: [1, 2, 3]
Location in DIS coordinates: [-2707488.3677768684, -4353666.735244378, 3781450.3202754413]
2. received PDU type 2=FIRE edu.nps.moves.dis7.pdus.FirePdu
FirePdu locationInWorldCoordinates: [-2707488.3677768684, -4353666.735244378, 3781450.3202754413]
===============
3. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu
entityID triplet: [1, 2, 3]
Location in DIS coordinates: [-2707488.3677768684, -4353666.735244378, 3781450.3202754413]
4. received PDU type 2=FIRE edu.nps.moves.dis7.pdus.FirePdu
FirePdu locationInWorldCoordinates: [-2707488.3677768684, -4353666.735244378, 3781450.3202754413]
Invocation instructions:
1. run/debug EspduReceiver.java (since sender does not block, first be ready to listen)
2. run/debug EspduSender.java
Program responses, sender and receiver:
===================================================
ant -f C:\\x-nps-gitlab\\NetworkedGraphicsMV3500\\examples -Dnb.internal.action.name=run.single -Djavac.includes=OpenDis7Examples/EspduSender.java -Drun.class=OpenDis7Examples.EspduSender run-single
init:
Deleting: C:\x-nps-gitlab\NetworkedGraphicsMV3500\examples\build\built-jar.properties
deps-jar:
Updating property file: C:\x-nps-gitlab\NetworkedGraphicsMV3500\examples\build\built-jar.properties
Compiling 1 source file to C:\x-nps-gitlab\NetworkedGraphicsMV3500\examples\build\classes
warning: [options] bootstrap class path not set in conjunction with -source 8
Note: C:\x-nps-gitlab\NetworkedGraphicsMV3500\examples\src\OpenDis7Examples\EspduSender.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 warning
compile-single:
run-single:
[OpenDis7Examples.EspduSender] started...
[OpenDis7Examples.EspduSender] sending multicast ESPDU packets to 239.1.2.3 port 3000
===============
espdu entityType information:
EntityKind =EntityKind 1 PLATFORM
Country =Country 225 UNITED_STATES_OF_AMERICA_USA
Domain =Land
Category =1
SubCategory=1
Specific =Country 225 UNITED_STATES_OF_AMERICA_USA
[OpenDis7Examples.EspduSender] sending 1 sets of packets:
===============
Create new PDUs
latitude, longitude: [36.595517, -121.87706]
coordinate conversion: [-2707497.4860692197, -4353661.0646844525, 3781450.3202754413]
Espdu #1 entityID=[1,2,3]
FirePdu #1 firePdu=[FireMissionIndex=0, descriptor=MunitionDescriptor:
munitionType: EntityType:
entityKind: EntityKind 0 OTHER
domain: Other
country: Country 0 OTHER
category: 0
subCategory: 0
specific: 0
extra: 0
warhead: MunitionDescriptorWarhead 0 OTHER
fuse: MunitionDescriptorFuse 0 OTHER
quantity: 0
rate: 0
]
[OpenDis7Examples.EspduSender] sending datagram packet [DISPDUType 1 ENTITY_STATE] packet.getLength()=144, to 127.255.255.255 port 3000
[OpenDis7Examples.EspduSender] sending datagram packet [DISPDUType 2 FIRE ] packet.getLength()= 96, to 127.255.255.255 port 3000
[OpenDis7Examples.EspduSender] sending datagram packet [DISPDUType 1 ENTITY_STATE] packet.getLength()=144, to 172.19.143.255 port 3000
[OpenDis7Examples.EspduSender] sending datagram packet [DISPDUType 2 FIRE ] packet.getLength()= 96, to 172.19.143.255 port 3000
[OpenDis7Examples.EspduSender] sending datagram packet [DISPDUType 1 ENTITY_STATE] packet.getLength()=144, to 10.0.0.255 port 3000
[OpenDis7Examples.EspduSender] sending datagram packet [DISPDUType 2 FIRE ] packet.getLength()= 96, to 10.0.0.255 port 3000
[OpenDis7Examples.EspduSender] sending datagram packet [DISPDUType 1 ENTITY_STATE] packet.getLength()=144, to 172.20.209.219 port 3000
[OpenDis7Examples.EspduSender] sending datagram packet [DISPDUType 2 FIRE ] packet.getLength()= 96, to 172.20.209.219 port 3000
===============
[OpenDis7Examples.EspduSender] complete.
BUILD SUCCESSFUL (total time: 5 seconds)
===================================================
[OpenDis7Examples.EspduReceiver] started...
[OpenDis7Examples.EspduReceiver] listening for PDU packets on 239.1.2.3 port 3000
To quit: stop or kill this process
===============
1. received PDU type 1=ENTITY_STATE edu.nps.moves.dis7.pdus.EntityStatePdu
entityID triplet: [1, 2, 3]
Location in DIS coordinates: [-2707497.4860692197, -4353661.0646844525, 3781450.3202754413]
2. received PDU type 2=FIRE edu.nps.moves.dis7.pdus.FirePdu
FirePdu locationInWorldCoordinates: [-2707497.4860692197, -4353661.0646844525, 3781450.3202754413]
===============
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment