Skip to content
Snippets Groups Projects
Commit 3c5af51f authored by John Furr's avatar John Furr
Browse files

Merge origin/master

parents f73c7269 9d072e14
No related branches found
No related tags found
No related merge requests found
...@@ -46,7 +46,6 @@ public class C_T_EspduRequestingUnit { ...@@ -46,7 +46,6 @@ public class C_T_EspduRequestingUnit {
//Receiving //Receiving
DatagramPacket receivingPacket; DatagramPacket receivingPacket;
InetAddress receivingAddress;
PduFactory pduFactory = new PduFactory(); PduFactory pduFactory = new PduFactory();
//Use a try/catch to ensure the you can connect to the Destination IP //Use a try/catch to ensure the you can connect to the Destination IP
...@@ -125,25 +124,52 @@ public class C_T_EspduRequestingUnit { ...@@ -125,25 +124,52 @@ public class C_T_EspduRequestingUnit {
// Lat and Long Coordinates that will be used for the location of the Requesting Unit // Lat and Long Coordinates that will be used for the location of the Requesting Unit
double lat = 36.595517; double lat = 36.595517;
double lon = -121.877000; double lon = -121.877000;
//Boolean for the initial connection
boolean connectionMade = false;
//Convert the lat and long coordinates to cartisian coordinates required by DIS
double disCoordinates[] = CoordinateConversions.getXYZfromLatLonDegrees(lat, lon, 1.0);
requestingUnitEsPdu.getEntityLocation().setX(disCoordinates[0]);
requestingUnitEsPdu.getEntityLocation().setY(disCoordinates[1]);
requestingUnitEsPdu.getEntityLocation().setZ(disCoordinates[2]);
//Create a Resupply Service Request
ServiceRequestPdu serviceRequest = new ServiceRequestPdu();
serviceRequest.setRequestingEntityID(entityID);
serviceRequest.setServiceTypeRequested((short) 1); //Resupply
List<SupplyQuantity> pSupplies = new ArrayList<SupplyQuantity>();
pSupplies.add(new SupplyQuantity());
serviceRequest.setSupplies(pSupplies);
//Create a Resupply Offer
ResupplyOfferPdu resupplyOffer = new ResupplyOfferPdu();
//Create a Resupply Received
AcknowledgePdu acknowledge = new AcknowledgePdu();
//Booleans for dialogue
boolean connectionMade = false;
boolean serviceRequestAcknowledged = false;
boolean serviceRequestComplete = false;
Set<InetAddress> broadcastAddresses; Set<InetAddress> broadcastAddresses;
try { try {
while (true) { while (true)
{
System.out.println("\nREQUESTING UNIT:\n\tListening\n"); System.out.println("\nREQUESTING UNIT:\n\tListening\n");
int timestamp = disTime.getDisAbsoluteTimestamp(); int timestamp = disTime.getDisAbsoluteTimestamp();
// Marshal out the espdu object to a byte array, then send a datagram // Marshal out the espdu object to a byte array
// packet with that data in it.
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos); DataOutputStream dos = new DataOutputStream(baos);
requestingUnitEsPdu.marshal(dos); requestingUnitEsPdu.marshal(dos);
// Marshal out the Service Request PDU object to a byte array
ByteArrayOutputStream baosServiceRequest = new ByteArrayOutputStream();
DataOutputStream dosServiceRequest = new DataOutputStream(baosServiceRequest);
serviceRequest.marshal(dosServiceRequest);
//While we have not recieved an acknowledgement from the receiving unit keep sending the entity state information //While we have not recieved an acknowledgement from the receiving unit keep sending the entity state information
while(!connectionMade) { while(!connectionMade)
{
//DIS Time must be used for this because DIS requires it. //DIS Time must be used for this because DIS requires it.
timestamp = disTime.getDisAbsoluteTimestamp(); timestamp = disTime.getDisAbsoluteTimestamp();
requestingUnitEsPdu.setTimestamp(timestamp); requestingUnitEsPdu.setTimestamp(timestamp);
...@@ -157,7 +183,6 @@ public class C_T_EspduRequestingUnit { ...@@ -157,7 +183,6 @@ public class C_T_EspduRequestingUnit {
while (broadcastIterator.hasNext()) { while (broadcastIterator.hasNext()) {
InetAddress broadcast = (InetAddress) broadcastIterator.next(); InetAddress broadcast = (InetAddress) broadcastIterator.next();
System.out.println("Sending broadcast datagram packet to " + broadcast);
DatagramPacket packet = new DatagramPacket(data, data.length, broadcast, port); DatagramPacket packet = new DatagramPacket(data, data.length, broadcast, port);
socket.send(packet); socket.send(packet);
} }
...@@ -166,20 +191,20 @@ public class C_T_EspduRequestingUnit { ...@@ -166,20 +191,20 @@ public class C_T_EspduRequestingUnit {
System.out.println("SENDING ENTITY STATE INFORMATION"); System.out.println("SENDING ENTITY STATE INFORMATION");
System.out.println("\tEID=[" + entityID.getSite() + "," + entityID.getApplication() + "," + entityID.getEntity() + "]"); System.out.println("\tEID=[" + entityID.getSite() + "," + entityID.getApplication() + "," + entityID.getEntity() + "]");
System.out.println("\tEntity Location: [" + requestingUnitEsPdu.getEntityLocation().getX() + ", " + requestingUnitEsPdu.getEntityLocation().getY() + ", " + requestingUnitEsPdu.getEntityLocation().getZ() + "]");
System.out.println("\tEntity Type: " + requestingUnitEsPdu.getEntityType().getEntityKind() + "\n");
//Prepare to receive a package //Prepare to receive a package
byte buffer[] = new byte[MAX_PDU_SIZE]; byte buffer[] = new byte[MAX_PDU_SIZE];
receivingPacket = new DatagramPacket(buffer, buffer.length); receivingPacket = new DatagramPacket(buffer, buffer.length);
socket.receive(receivingPacket); socket.receive(receivingPacket);
List<Pdu> pduBundle = pduFactory.getPdusFromBundle(receivingPacket.getData()); List<Pdu> pduBundle = pduFactory.getPdusFromBundle(receivingPacket.getData());
System.out.println("Bundle size is " + pduBundle.size());
//Iterate through the bundle //Iterate through the bundle
Iterator iterator = pduBundle.iterator(); Iterator iterator = pduBundle.iterator();
while (iterator.hasNext()) { while (iterator.hasNext()) {
Pdu aPdu = (Pdu) iterator.next(); Pdu aPdu = (Pdu) iterator.next();
System.out.println("got PDU of type: " + aPdu.getClass().getName());
if (aPdu instanceof EntityStatePdu) { if (aPdu instanceof EntityStatePdu) {
EntityID eid = ((EntityStatePdu) aPdu).getEntityID(); EntityID eid = ((EntityStatePdu) aPdu).getEntityID();
...@@ -189,6 +214,7 @@ public class C_T_EspduRequestingUnit { ...@@ -189,6 +214,7 @@ public class C_T_EspduRequestingUnit {
if(eType.getEntityKind() != requestingUnitEsPdu.getEntityType().getEntityKind()) { if(eType.getEntityKind() != requestingUnitEsPdu.getEntityType().getEntityKind()) {
connectionMade = true; connectionMade = true;
System.out.println("RECEIVED PACKET"); System.out.println("RECEIVED PACKET");
System.out.println("\tPacket Type: " + aPdu.getClass().getName());
System.out.println("\tEID:[" + eid.getSite() + ", " + eid.getApplication() + ", " + eid.getEntity() + "] "); System.out.println("\tEID:[" + eid.getSite() + ", " + eid.getApplication() + ", " + eid.getEntity() + "] ");
System.out.println("\tEntity Location: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "]"); System.out.println("\tEntity Location: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "]");
System.out.println("\tEntity Type: " + eType.getEntityKind() + "\n"); System.out.println("\tEntity Type: " + eType.getEntityKind() + "\n");
...@@ -196,25 +222,129 @@ public class C_T_EspduRequestingUnit { ...@@ -196,25 +222,129 @@ public class C_T_EspduRequestingUnit {
} }
} }
} }
while(!serviceRequestAcknowledged && connectionMade) {
//DIS Time must be used for this because DIS requires it.
timestamp = disTime.getDisAbsoluteTimestamp();
serviceRequest.setTimestamp(timestamp);
//The byte array here is the packet in DIS format. We put that into a
// datagram and send it.
byte[] dataServiceRquest = baosServiceRequest.toByteArray();
broadcastAddresses = getBroadcastAddresses();
Iterator broadcastIterator = broadcastAddresses.iterator();
while (broadcastIterator.hasNext()) {
InetAddress broadcast = (InetAddress) broadcastIterator.next();
DatagramPacket packet = new DatagramPacket(dataServiceRquest, dataServiceRquest.length, broadcast, port);
socket.send(packet);
}
// Send every 1 sec. Otherwise all this will be all over in a fraction of a second.
Thread.sleep(1000); // msec
//Print out the details on the service reuqest being sent
System.out.println("SENDING SERVICE REQUEST");
EntityID eid = serviceRequest.getRequestingEntityID();
short serviceType = serviceRequest.getServiceTypeRequested();
String serviceReqestTypeString = "null";
if (serviceType == 0) {
serviceReqestTypeString = "Other";
}
if (serviceType == 1) {
serviceReqestTypeString = "Resupply";
}
if (serviceType == 2) {
serviceReqestTypeString = "Repair";
}
System.out.println("\tEntity Requesting:[" + eid.getSite() + ", " + eid.getApplication() + ", " + eid.getEntity() + "] ");
System.out.println("\tService Type Requested: [" + serviceType + ", " + serviceReqestTypeString + "]\n");
//Prepare to receive a package
byte buffer[] = new byte[MAX_PDU_SIZE];
receivingPacket = new DatagramPacket(buffer, buffer.length);
socket.receive(receivingPacket);
List<Pdu> pduBundle = pduFactory.getPdusFromBundle(receivingPacket.getData());
//Iterate through the bundle
Iterator iterator = pduBundle.iterator();
while (iterator.hasNext()) {
Pdu aPdu = (Pdu) iterator.next();
if (aPdu instanceof ResupplyOfferPdu) {
resupplyOffer = (ResupplyOfferPdu) aPdu;
EntityID supplyingUnitEntityId = ((ResupplyOfferPdu) aPdu).getSupplyingEntityID();
EntityID receivingUnitEntityId = ((ResupplyOfferPdu) aPdu).getReceivingEntityID();
System.out.println("RECEIVED PACKET");
System.out.println("\tPacket Type: " + aPdu.getClass().getName());
System.out.println("\tSupplying Unit:\t[" + supplyingUnitEntityId.getSite() + ", " + supplyingUnitEntityId.getApplication() + ", " + supplyingUnitEntityId.getEntity() + "]");
System.out.println("\tRequesting Unit: [" + receivingUnitEntityId.getSite() + ", " + receivingUnitEntityId.getApplication() + ", " + receivingUnitEntityId.getEntity() + "]\n");
serviceRequestAcknowledged = true;
}
}
}
while(!serviceRequestComplete && serviceRequestAcknowledged) {
//DIS Time must be used for this because DIS requires it.
timestamp = disTime.getDisAbsoluteTimestamp();
acknowledge.setTimestamp(timestamp);
// Marshal out the Service Request PDU object to a byte array
ByteArrayOutputStream baosAcknowledge = new ByteArrayOutputStream();
DataOutputStream dosAcknowledge = new DataOutputStream(baosAcknowledge);
acknowledge.marshal(dosAcknowledge);
//The byte array here is the packet in DIS format. We put that into a
// datagram and send it.
byte[] dataAcknowledge = baosAcknowledge.toByteArray();
broadcastAddresses = getBroadcastAddresses();
Iterator broadcastIterator = broadcastAddresses.iterator();
while (broadcastIterator.hasNext()) {
InetAddress broadcast = (InetAddress) broadcastIterator.next();
DatagramPacket packet = new DatagramPacket(dataAcknowledge, dataAcknowledge.length, broadcast, port);
socket.send(packet);
}
// Send every 1 sec. Otherwise all this will be all over in a fraction of a second.
Thread.sleep(1000); // msec
//Print out the details on the service reuqest being sent
System.out.println("SENDING ACKNOWLEDGE\n");
//Prepare to receive a package
byte buffer[] = new byte[MAX_PDU_SIZE];
receivingPacket = new DatagramPacket(buffer, buffer.length);
socket.receive(receivingPacket);
List<Pdu> pduBundle = pduFactory.getPdusFromBundle(receivingPacket.getData());
//Iterate through the bundle
Iterator iterator = pduBundle.iterator();
while (iterator.hasNext()) {
Pdu aPdu = (Pdu) iterator.next();
if (aPdu instanceof AcknowledgePdu) {
System.out.println("RECEIVED PACKET");
System.out.println("\tPacket Type: " + aPdu.getClass().getName());
serviceRequestComplete = true;
}
}
}
if (serviceRequestComplete)
{
socket.close();
System.out.println("\nREQUEST COMPLETED - SOCKET IS NOW CLOSED");
break;
}
} }
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
System.out.println("Problem with DisExamples.EspduSender, see exception trace:"); System.out.println("Problem with DisExamples.EspduSender, see exception trace:");
System.out.println(e); System.out.println(e);
} }
} }
/**
* A number of sites get all snippy about using 255.255.255.255 for a
* broadcast address; it trips their security software and they kick you off
* their network. (Comcast, NPS.) This determines the broadcast address for
* all connected interfaces, based on the IP and subnet mask. If you have a
* dual-homed host it will return a broadcast address for both. If you have
* some VMs running on your host this will pick up the addresses for those
* as well--e.g. running VMWare on your laptop with a local IP this will
* also pick up a 192.168 address assigned to the VM by the host OS.
*
* @return set of all broadcast addresses
*/
public static Set<InetAddress> getBroadcastAddresses() { public static Set<InetAddress> getBroadcastAddresses() {
Set<InetAddress> broadcastAddresses = new HashSet<>(); Set<InetAddress> broadcastAddresses = new HashSet<>();
Enumeration interfaces; Enumeration interfaces;
......
...@@ -32,7 +32,7 @@ public class C_T_EspduSupplyerUnit { ...@@ -32,7 +32,7 @@ public class C_T_EspduSupplyerUnit {
public static final int DEFAULT_MULTICAST_PORT = 3000; public static final int DEFAULT_MULTICAST_PORT = 3000;
public static void main(String args[]) { public static void main(String args[]) {
System.out.println("\nSUPPLYER UNIT:\n\tProgram started\n"); System.out.println("\nSUPPLYER UNIT:\n\tProgram started\n");
//Initialize the MulticastSocket to null //Initialize the MulticastSocket to null
...@@ -40,15 +40,14 @@ public class C_T_EspduSupplyerUnit { ...@@ -40,15 +40,14 @@ public class C_T_EspduSupplyerUnit {
//Initialize the DIS time (this will be used later in the code //Initialize the DIS time (this will be used later in the code
DisTime disTime = DisTime.getInstance(); DisTime disTime = DisTime.getInstance();
// Initialize the Port, Mode, and Destination IP // Initialize the Port, Mode, and Destination IP
int port = DEFAULT_MULTICAST_PORT; int port = DEFAULT_MULTICAST_PORT;
NetworkMode mode = NetworkMode.BROADCAST; NetworkMode mode = NetworkMode.BROADCAST;
InetAddress destinationIp = null; InetAddress destinationIp = null;
//Receiving //Receiving
DatagramPacket receivingPacket; DatagramPacket receivingPacket;
InetAddress receivingAddress;
PduFactory pduFactory = new PduFactory(); PduFactory pduFactory = new PduFactory();
//Use a try/catch to ensure the you can connect to the Destination IP //Use a try/catch to ensure the you can connect to the Destination IP
...@@ -58,7 +57,7 @@ public class C_T_EspduSupplyerUnit { ...@@ -58,7 +57,7 @@ public class C_T_EspduSupplyerUnit {
System.out.println(e + " Cannot create multicast address"); System.out.println(e + " Cannot create multicast address");
System.exit(0); System.exit(0);
} }
// All system properties, passed in on the command line via -Dattribute=value // All system properties, passed in on the command line via -Dattribute=value
Properties systemProperties = System.getProperties(); Properties systemProperties = System.getProperties();
// IP address we send to // IP address we send to
...@@ -68,7 +67,6 @@ public class C_T_EspduSupplyerUnit { ...@@ -68,7 +67,6 @@ public class C_T_EspduSupplyerUnit {
// Network mode: unicast, multicast, broadcast // Network mode: unicast, multicast, broadcast
String networkModeString = systemProperties.getProperty("networkMode"); // unicast or multicast or broadcast String networkModeString = systemProperties.getProperty("networkMode"); // unicast or multicast or broadcast
try { try {
// Port we send to // Port we send to
if (portString != null) { if (portString != null) {
...@@ -101,18 +99,18 @@ public class C_T_EspduSupplyerUnit { ...@@ -101,18 +99,18 @@ public class C_T_EspduSupplyerUnit {
System.out.println(e); System.out.println(e);
System.exit(-1); System.exit(-1);
} }
//Establish the Requesting Unit EnityState PDU //Establish the Requesting Unit EnityState PDU
EntityStatePdu supplyerUnitEsPdu = new EntityStatePdu(); EntityStatePdu supplyerUnitEsPdu = new EntityStatePdu();
// Initialize values in the Entity State PDU object. // Initialize values in the Entity State PDU object.
//The exercise ID is a way to differentiate between different virtual worlds on one network. //The exercise ID is a way to differentiate between different virtual worlds on one network.
supplyerUnitEsPdu.setExerciseID((short) 1); supplyerUnitEsPdu.setExerciseID((short) 1);
// The EID is the unique identifier for objects in the world. // The EID is the unique identifier for objects in the world.
EntityID entityID = supplyerUnitEsPdu.getEntityID(); EntityID entityID = supplyerUnitEsPdu.getEntityID();
entityID.setSite(1); // 0 is apparently not a valid site number, per the spec entityID.setSite(2); // 0 is apparently not a valid site number, per the spec
entityID.setApplication(1); entityID.setApplication(2);
entityID.setEntity(2); entityID.setEntity(2);
// Set the entity type. (Check SISO for a list of enumerations) // Set the entity type. (Check SISO for a list of enumerations)
...@@ -121,47 +119,71 @@ public class C_T_EspduSupplyerUnit { ...@@ -121,47 +119,71 @@ public class C_T_EspduSupplyerUnit {
entityType.setCountry(225); // USA entityType.setCountry(225); // USA
entityType.setDomain((short) 1); // Land (vs air, surface, subsurface, space) entityType.setDomain((short) 1); // Land (vs air, surface, subsurface, space)
entityType.setCategory((short) 34); // Command Facility entityType.setCategory((short) 34); // Command Facility
// Lat and Long Coordinates that will be used for the location of the Requesting Unit // Lat and Long Coordinates that will be used for the location of the Requesting Unit
double lat = 75.123456; double lat = 75.123456;
double lon = -200.877000; double lon = -200.877000;
//Convert the lat and long coordinates to cartisian coordinates required by DIS
double disCoordinates[] = CoordinateConversions.getXYZfromLatLonDegrees(lat, lon, 1.0);
supplyerUnitEsPdu.getEntityLocation().setX(disCoordinates[0]);
supplyerUnitEsPdu.getEntityLocation().setY(disCoordinates[1]);
supplyerUnitEsPdu.getEntityLocation().setZ(disCoordinates[2]);
//Resupply Offer
ResupplyOfferPdu resupplyOffer = new ResupplyOfferPdu();
//Acknowledge
AcknowledgePdu acknowledge = new AcknowledgePdu();
//Booleans for dialogue
boolean someoneWantsToTalk = false;
boolean serviceRequestReceived = false;
boolean serviceRequestComplete = false;
Set<InetAddress> broadcastAddresses; Set<InetAddress> broadcastAddresses;
Set<Pdu> receivedMessages = new HashSet<>(); try {
try{
while (true) // Loop infinitely, receiving datagrams while (true) // Loop infinitely, receiving datagrams
{ {
System.out.println("\nSUPPYER UNIT:\n\tListening\n"); System.out.println("\nSUPPYER UNIT:\n\tListening\n");
int timestamp = disTime.getDisAbsoluteTimestamp(); int timestamp = disTime.getDisAbsoluteTimestamp();
// Marshal out the espdu object to a byte array, then send a datagram // Marshal out the espdu object to a byte array
// packet with that data in it.
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos); DataOutputStream dos = new DataOutputStream(baos);
supplyerUnitEsPdu.marshal(dos); supplyerUnitEsPdu.marshal(dos);
byte[] data = baos.toByteArray();
broadcastAddresses = getBroadcastAddresses(); while (!someoneWantsToTalk) {
Iterator broadcastIterator = broadcastAddresses.iterator(); //DIS Time must be used for this because DIS requires it.
timestamp = disTime.getDisAbsoluteTimestamp();
//Receiving Set Up supplyerUnitEsPdu.setTimestamp(timestamp);
byte buffer[] = new byte[MAX_PDU_SIZE];
receivingPacket = new DatagramPacket(buffer, buffer.length); //The byte array here is the packet in DIS format. We put that into a
socket.receive(receivingPacket); // datagram and send it.
List<Pdu> pduBundle = pduFactory.getPdusFromBundle(receivingPacket.getData()); byte[] data = baos.toByteArray();
System.out.println("Bundle size is " + pduBundle.size());
broadcastAddresses = getBroadcastAddresses();
Iterator receivingIterator = pduBundle.iterator(); Iterator broadcastIterator = broadcastAddresses.iterator();
while (receivingIterator.hasNext()) { //Receiving Set Up
Pdu aPdu = (Pdu) receivingIterator.next(); byte buffer[] = new byte[MAX_PDU_SIZE];
if (!receivedMessages.contains(aPdu)) { receivingPacket = new DatagramPacket(buffer, buffer.length);
System.out.println("got PDU of type: " + aPdu.getClass().getName()); socket.receive(receivingPacket);
List<Pdu> pduBundle = pduFactory.getPdusFromBundle(receivingPacket.getData());
Iterator receivingIterator = pduBundle.iterator();
while (receivingIterator.hasNext()) {
Pdu aPdu = (Pdu) receivingIterator.next();
//System.out.println("got PDU of type: " + aPdu.getClass().getName());
if (aPdu instanceof EntityStatePdu) { if (aPdu instanceof EntityStatePdu) {
EntityID eid = ((EntityStatePdu) aPdu).getEntityID(); EntityID eid = ((EntityStatePdu) aPdu).getEntityID();
EntityType eType = ((EntityStatePdu) aPdu).getEntityType(); EntityType eType = ((EntityStatePdu) aPdu).getEntityType();
Vector3Double position = ((EntityStatePdu) aPdu).getEntityLocation(); Vector3Double position = ((EntityStatePdu) aPdu).getEntityLocation();
System.out.println("RECEIVED PACKET"); System.out.println("RECEIVED PACKET");
System.out.println("\tPacket Type: " + aPdu.getClass().getName());
System.out.println("\tEID:[" + eid.getSite() + ", " + eid.getApplication() + ", " + eid.getEntity() + "] "); System.out.println("\tEID:[" + eid.getSite() + ", " + eid.getApplication() + ", " + eid.getEntity() + "] ");
System.out.println("\tEntity Location: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "]"); System.out.println("\tEntity Location: [" + position.getX() + ", " + position.getY() + ", " + position.getZ() + "]");
System.out.println("\tEntity Type: " + eType.getEntityKind() + "\n"); System.out.println("\tEntity Type: " + eType.getEntityKind() + "\n");
...@@ -169,15 +191,129 @@ public class C_T_EspduSupplyerUnit { ...@@ -169,15 +191,129 @@ public class C_T_EspduSupplyerUnit {
if (eType.getEntityKind() != supplyerUnitEsPdu.getEntityType().getEntityKind()) { if (eType.getEntityKind() != supplyerUnitEsPdu.getEntityType().getEntityKind()) {
while (broadcastIterator.hasNext()) { while (broadcastIterator.hasNext()) {
InetAddress broadcast = (InetAddress) broadcastIterator.next(); InetAddress broadcast = (InetAddress) broadcastIterator.next();
System.out.println("Sending broadcast datagram packet to " + broadcast);
DatagramPacket packet = new DatagramPacket(data, data.length, broadcast, port); DatagramPacket packet = new DatagramPacket(data, data.length, broadcast, port);
socket.send(packet); socket.send(packet);
} }
System.out.println("SENDING ENTITY STATE PACKET");
System.out.println("\tEID=[" + entityID.getSite() + "," + entityID.getApplication() + "," + entityID.getEntity() + "]");
System.out.println("\tEntity Location: [" + supplyerUnitEsPdu.getEntityLocation().getX() + ", " + supplyerUnitEsPdu.getEntityLocation().getY() + ", " + supplyerUnitEsPdu.getEntityLocation().getZ() + "]");
System.out.println("\tEntity Type: " + supplyerUnitEsPdu.getEntityType().getEntityKind() + "\n");
someoneWantsToTalk = true;
} }
} }
receivedMessages.add(aPdu);
} }
} }
while (!serviceRequestReceived && someoneWantsToTalk) {
//DIS Time must be used for this because DIS requires it.
timestamp = disTime.getDisAbsoluteTimestamp();
resupplyOffer.setTimestamp(timestamp);
//The byte array here is the packet in DIS format. We put that into a
// datagram and send it.
byte[] data = baos.toByteArray();
broadcastAddresses = getBroadcastAddresses();
Iterator broadcastIterator = broadcastAddresses.iterator();
//Receiving Set Up
byte buffer[] = new byte[MAX_PDU_SIZE];
receivingPacket = new DatagramPacket(buffer, buffer.length);
socket.receive(receivingPacket);
List<Pdu> pduBundle = pduFactory.getPdusFromBundle(receivingPacket.getData());
Iterator receivingIterator = pduBundle.iterator();
while (receivingIterator.hasNext()) {
Pdu aPdu = (Pdu) receivingIterator.next();
//System.out.println("got PDU of type: " + aPdu.getClass().getName());
if (aPdu instanceof ServiceRequestPdu) {
EntityID serviceRequestId = ((ServiceRequestPdu) aPdu).getRequestingEntityID();
short serviceRequestType = ((ServiceRequestPdu) aPdu).getServiceTypeRequested();
if (serviceRequestType == 1) {
System.out.println("RECEIVED PACKET");
System.out.println("\tPacket Type: " + aPdu.getClass().getName());
System.out.println("\tRequesting Entity ID:[" + serviceRequestId.getSite() + ", " + serviceRequestId.getApplication() + ", " + serviceRequestId.getEntity() + "] ");
System.out.println("\tService Type Requested: [" + serviceRequestType + ", Resupply]\n");
List<SupplyQuantity> pSupplies = ((ServiceRequestPdu) aPdu).getSupplies();
resupplyOffer.setReceivingEntityID(serviceRequestId);
resupplyOffer.setSupplyingEntityID(supplyerUnitEsPdu.getEntityID());
resupplyOffer.setSupplies(pSupplies);
// Marshal out the Service Request PDU object to a byte array
ByteArrayOutputStream baosResupplyOffer = new ByteArrayOutputStream();
DataOutputStream dosResupplyOffer = new DataOutputStream(baosResupplyOffer);
resupplyOffer.marshal(dosResupplyOffer);
byte[] dataResupplyOffer = baosResupplyOffer.toByteArray();
while (broadcastIterator.hasNext()) {
InetAddress broadcast = (InetAddress) broadcastIterator.next();
DatagramPacket packet = new DatagramPacket(dataResupplyOffer, dataResupplyOffer.length, broadcast, port);
socket.send(packet);
}
System.out.println("SENDING PACKET");
System.out.println("\tPacket Type: Resupply Offer");
System.out.println("\tReceiving Entity ID: [" + resupplyOffer.getReceivingEntityID().getSite() + ", " + resupplyOffer.getReceivingEntityID().getApplication() + ", " + resupplyOffer.getReceivingEntityID().getEntity() + "]");
System.out.println("\tSupplying Entity ID: [" + resupplyOffer.getSupplyingEntityID().getSite() + ", " + resupplyOffer.getSupplyingEntityID().getApplication() + ", " + resupplyOffer.getSupplyingEntityID().getEntity() + "]\n");
serviceRequestReceived = true;
}
}
}
}
while (!serviceRequestComplete && serviceRequestReceived ) {
//DIS Time must be used for this because DIS requires it.
timestamp = disTime.getDisAbsoluteTimestamp();
acknowledge.setTimestamp(timestamp);
//The byte array here is the packet in DIS format. We put that into a
// datagram and send it.
//byte[] data = baos.toByteArray();
broadcastAddresses = getBroadcastAddresses();
Iterator broadcastIterator = broadcastAddresses.iterator();
//Receiving Set Up
byte buffer[] = new byte[MAX_PDU_SIZE];
receivingPacket = new DatagramPacket(buffer, buffer.length);
socket.receive(receivingPacket);
List<Pdu> pduBundle = pduFactory.getPdusFromBundle(receivingPacket.getData());
Iterator receivingIterator = pduBundle.iterator();
while (receivingIterator.hasNext()) {
Pdu aPdu = (Pdu) receivingIterator.next();
if (aPdu instanceof AcknowledgePdu) {
System.out.println("RECEIVED PACKET");
System.out.println("\tPacket Type: " + aPdu.getClass().getName());
// Marshal out the Service Request PDU object to a byte array
ByteArrayOutputStream baosAcknowledge = new ByteArrayOutputStream();
DataOutputStream dosAcknowledge = new DataOutputStream(baosAcknowledge);
acknowledge.marshal(dosAcknowledge);
byte[] dataAcknowledge = baosAcknowledge.toByteArray();
while (broadcastIterator.hasNext()) {
InetAddress broadcast = (InetAddress) broadcastIterator.next();
DatagramPacket packet = new DatagramPacket(dataAcknowledge, dataAcknowledge.length, broadcast, port);
socket.send(packet);
}
System.out.println("SENDING PACKET");
System.out.println("\tPacket Type: Acknowledge");
serviceRequestComplete = true;
}
}
}
} // end while } // end while
} // End try } // End try
catch (IOException e) { catch (IOException e) {
...@@ -185,7 +321,7 @@ public class C_T_EspduSupplyerUnit { ...@@ -185,7 +321,7 @@ public class C_T_EspduSupplyerUnit {
System.out.println(e); System.out.println(e);
} }
} // end main } // end main
public static Set<InetAddress> getBroadcastAddresses() { public static Set<InetAddress> getBroadcastAddresses() {
Set<InetAddress> broadcastAddresses = new HashSet<>(); Set<InetAddress> broadcastAddresses = new HashSet<>();
Enumeration interfaces; Enumeration interfaces;
......
## Final Course Projects 2018September
Create a dedicated subdirectory for each individual or team project.
Example: `SmithJones`
See the [course syllabus](../../../../MV3500NetworkedGraphicsSyllabus2018JulySeptember.pdf) for details on how to document your project.
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