Skip to content

Commit

Permalink
Rework InfrastructureInstance API to expose typed methods to encode a…
Browse files Browse the repository at this point in the history
…nd send appropriate messages
  • Loading branch information
paulbourelly999 committed Aug 7, 2023
1 parent a837403 commit d3276a9
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
import java.util.List;

import org.eclipse.mosaic.lib.geo.CartesianPoint;
import org.eclipse.mosaic.lib.objects.detector.DetectedObject;
import org.eclipse.mosaic.lib.objects.detector.Detector;
import org.eclipse.mosaic.rti.api.Interaction;

import com.google.gson.Gson;

/**
* InfrastructureInstance class represents a physical instance of an
Expand Down Expand Up @@ -216,13 +220,11 @@ public boolean containsSensor(String sensorId) {
}

/**
* Creates a DatagramSocket object and binds it to this infrastructure
* instance's receive message port
* Creates a DatagramSocket object
*
* @throws IOException if there is an issue with the underlying socket object or
* methods
* @throws IOException if there is an issue with the underlying socket object
*/
public void bind() throws IOException {
public void connect() throws IOException {
socket = new DatagramSocket();
}

Expand All @@ -244,17 +246,29 @@ public void sendV2xMsg(byte[] data) throws IOException {
* @param data The binary data to transmit
* @throws IOException If there is an issue with the underlying socket object or methods
*/
public void sendTimeSyncMsg(byte[] data) throws IOException {
sendPacket(data, timeSyncPort);
public void sendTimeSyncMsg(InfrastructureTimeMessage message) throws IOException {
sendPacket(toJsonBytes(message), timeSyncPort);
}

/**
* Helper method to serialize message into JSON and encode as bytes.
*
* @param message java object containing message information
* @return bytes encoded from JSON string representation of object.
*/
private byte[] toJsonBytes(Object message) {
Gson gson = new Gson();
return gson.toJson(message).getBytes();
}

/**
* Sends time sync data to the Infrastrucutre Instance Simulated Interaction port.
*
* @param data The binary data to transmit
* @throws IOException If there is an issue with the underlying socket object or methods
*/
public void sendInteraction(byte[] data) throws IOException {
sendPacket(data, simulatedInteractionPort);
public void sendDetection(DetectedObject detectedObject) throws IOException {
sendPacket(toJsonBytes(detectedObject), simulatedInteractionPort);
}
/**
* Method to send byte data to specified port using the infrastructure instance Datagramsocket.
Expand All @@ -263,7 +277,7 @@ public void sendInteraction(byte[] data) throws IOException {
* @param port in integer format to send Datagram to.
* @throws IOException
*/
public void sendPacket(byte[] data, int port) throws IOException {
private void sendPacket(byte[] data, int port) throws IOException {
if (socket == null) {
throw new IllegalStateException("Attempted to send data before opening socket");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private void newInfrastructureInstance(String infrastructureId, InetAddress rxMe
InfrastructureInstance tmp = new InfrastructureInstance(infrastructureId, rxMessageIpAddress, rxMessagePort,
timeSyncPort, simulatedInteractionPort, location, sensors);
try {
tmp.bind();
tmp.connect();
log.info("New Infrastructure instance '{}' registered with Infrastructure Instance Manager.",
infrastructureId);
} catch (IOException e) {
Expand Down Expand Up @@ -183,7 +183,7 @@ public void onDetectedObject(DetectedObject detection) {
for (InfrastructureInstance instance : managedInstances.values()) {
if (instance.containsSensor(detection.getSensorId())) {
try {
instance.sendInteraction(encodeMsg(detection));
instance.sendDetection(detection);
// Assuming each sensor would only ever be registered to a single infrastructure
// instance
break;
Expand All @@ -194,17 +194,7 @@ public void onDetectedObject(DetectedObject detection) {
}
}

/**
* Helper method to serialize message into JSON and encode as bytes.
*
* @param message java object containing message information
* @return bytes encoded from JSON string representation of object.
*/
private byte[] encodeMsg(Object message) {
Gson gson = new Gson();
return gson.toJson(message).getBytes();
}



/**
* This function is used to send out encoded timestep update to all registered
Expand All @@ -220,7 +210,7 @@ public void onTimeStepUpdate(InfrastructureTimeMessage message) throws IOExcepti
}

for (InfrastructureInstance currentInstance : managedInstances.values()) {
currentInstance.sendTimeSyncMsg(encodeMsg(message));
currentInstance.sendTimeSyncMsg(message);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import org.junit.Before;
import org.junit.Test;

import com.google.gson.Gson;

public class InfrastructureInstanceManagerTest {

Expand Down Expand Up @@ -114,12 +113,10 @@ public void testOnTimeStepUpdate() throws IOException {
message.setSeq(3);
message.setTimestep(300);
manager.onTimeStepUpdate(message);
Gson gson = new Gson();
byte[] datagram = gson.toJson(message).getBytes();
// Verify that all instances sendTimeSyncMsgs was called.
verify(instance1).sendTimeSyncMsg(datagram);
verify(instance2).sendTimeSyncMsg(datagram);
verify(instance2).sendTimeSyncMsg(datagram);
verify(instance1).sendTimeSyncMsg(message);
verify(instance2).sendTimeSyncMsg(message);
verify(instance2).sendTimeSyncMsg(message);

}

Expand All @@ -142,11 +139,9 @@ public void testOnDetectedObject() throws IOException{
detectedObject1.setAngularVelocityCovariance(covarianceMatrix);
// Attempt to send detected object to infrastructure instance
manager.onDetectedObject(detectedObject1);
Gson gson = new Gson();
byte[] datagram1 = gson.toJson(detectedObject1).getBytes();
// Verify Infrastructure Manager attempted to sent Detected Object
// to instance1
verify(instance1, times(1)).sendInteraction(datagram1);
verify(instance1, times(1)).sendDetection(detectedObject1);
// Create second detected object
DetectedObject detectedObject2 = new DetectedObject(
DetectionType.VAN,
Expand All @@ -162,10 +157,9 @@ public void testOnDetectedObject() throws IOException{
detectedObject2.setVelocityCovariance(covarianceMatrix);
detectedObject2.setAngularVelocityCovariance(covarianceMatrix);
manager.onDetectedObject(detectedObject2);
byte[] datagram2 = gson.toJson(detectedObject2).getBytes();
doThrow(new IOException("Something went wrong")).when(instance3).sendInteraction(datagram2);
verify(instance3, times(1)).sendInteraction(datagram2);
verify(instance2, never()).sendInteraction(any(byte[].class));
doThrow(new IOException("Something went wrong")).when(instance3).sendDetection(detectedObject2);
verify(instance3, times(1)).sendDetection(detectedObject2);
verify(instance2, never()).sendDetection(any(DetectedObject.class));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,20 @@
import java.util.ArrayList;

import org.eclipse.mosaic.lib.geo.CartesianPoint;
import org.eclipse.mosaic.lib.math.Vector3d;
import org.eclipse.mosaic.lib.objects.detector.DetectedObject;
import org.eclipse.mosaic.lib.objects.detector.DetectionType;
import org.eclipse.mosaic.lib.objects.detector.Detector;
import org.eclipse.mosaic.lib.objects.detector.DetectorType;
import org.eclipse.mosaic.lib.objects.detector.Orientation;
import org.eclipse.mosaic.lib.objects.detector.Size;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.internal.util.reflection.FieldSetter;

import com.google.gson.Gson;

public class InfrastructureInstanceTest {
/**
* Mock Datagram socket
Expand Down Expand Up @@ -148,31 +154,54 @@ public void testSendV2xMsg() throws IOException {
@Test
public void testSendTimeSyncMsg() throws IOException {
// Test SendTimeSyncMsg method
String test_msg = "test message";
instance.sendTimeSyncMsg(test_msg.getBytes());
InfrastructureTimeMessage test_msg = new InfrastructureTimeMessage();
test_msg.setSeq(1);
test_msg.setTimestep(100);
instance.sendTimeSyncMsg(test_msg);


// ArgumentCaptor to capture parameters passed to mock on method calls
ArgumentCaptor<DatagramPacket> packet = ArgumentCaptor.forClass(DatagramPacket.class);
// Verify socket.send(DatagramPacket packet) is called and capture packet
// parameter
verify(socket, times(1)).send(packet.capture());
// Convert message to bytes
Gson gson = new Gson();
byte[] message_bytes = gson.toJson(test_msg).getBytes();
// Verify parameter members
assertArrayEquals(test_msg.getBytes(), packet.getValue().getData());
assertArrayEquals(message_bytes, packet.getValue().getData());
assertEquals(instance.getTimeSyncPort(), packet.getValue().getPort());
assertEquals(address, packet.getValue().getAddress());
}

@Test
public void testSendInteraction() throws IOException {
// Test SendInteraction method
String test_msg = "test message";
instance.sendInteraction(test_msg.getBytes());
DetectedObject test_msg = new DetectedObject(
DetectionType.BUS,
0.5,
"sensor1",
"projection String",
"Object1",
CartesianPoint.xyz(1.1, 2, 3.2),
new Vector3d(0, 0, 0),
new Vector3d(),
new Size(0, 0, 0));
Double[] covarianceMatrix = new Double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
test_msg.setPositionCovariance(covarianceMatrix);
test_msg.setVelocityCovariance(covarianceMatrix);
test_msg.setAngularVelocityCovariance(covarianceMatrix);
instance.sendDetection(test_msg);
// ArgumentCaptor to capture parameters passed to mock on method calls
ArgumentCaptor<DatagramPacket> packet = ArgumentCaptor.forClass(DatagramPacket.class);
// Verify socket.send(DatagramPacket packet) is called and capture packet
// parameter
verify(socket, times(1)).send(packet.capture());
// Convert message to bytes
Gson gson = new Gson();
byte[] message_bytes = gson.toJson(test_msg).getBytes();
// Verify parameter members
assertArrayEquals(test_msg.getBytes(), packet.getValue().getData());
assertArrayEquals(message_bytes, packet.getValue().getData());
assertEquals(instance.getSimulatedInteractionPort(), packet.getValue().getPort());
assertEquals(address, packet.getValue().getAddress());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ else if (interaction.getClass().equals(AdHocCommunicationConfiguration.class)) {
// Assert that no other interactions were triggered
assertEquals(0, otherInteractions.size());


}

}

0 comments on commit d3276a9

Please sign in to comment.