Skip to content

Commit

Permalink
Addressed Sonar scan report issues
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbourelly999 committed Jul 27, 2023
1 parent be88743 commit a5d58ec
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@

package org.eclipse.mosaic.fed.infrastructure.ambassador;

import org.eclipse.mosaic.interactions.sensor.Sensor;
import org.eclipse.mosaic.lib.geo.CartesianPoint;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.mosaic.interactions.sensor.Sensor;
import org.eclipse.mosaic.lib.geo.CartesianPoint;

/**
* InfrastructureInstance class represents a physical instance of an
* infrastructure node in the simulated environment.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;

import org.eclipse.mosaic.interactions.communication.V2xMessageTransmission;
import org.eclipse.mosaic.interactions.sensor.DetectedObject;
Expand Down Expand Up @@ -56,6 +53,7 @@ public class InfrastructureInstanceManager {
private Map<String, InfrastructureInstance> managedInstances = new HashMap<>();

private final Logger log = LoggerFactory.getLogger(this.getClass());

/**
* Register a new infrastructure instance with the MOSAIC system.
*
Expand All @@ -77,7 +75,7 @@ public void onNewRegistration(InfrastructureRegistrationMessage registration) {
registration.getSimulatedInteractionPort(),
registration.getLocation(),
registration.getSensors());

} catch (UnknownHostException e) {
log.error("Failed to create infrastructure instance with ID '{}' due to an unknown host exception: {}",
registration.getInfrastructureId(), e.getMessage());
Expand Down Expand Up @@ -108,22 +106,25 @@ private void newInfrastructureInstance(String infrastructureId, InetAddress rxMe
timeSyncPort, simulatedInteractionPort, location, sensors);
try {
tmp.bind();
log.info("New Infrastructure instance '{}' registered with Infrastructure Instance Manager.", infrastructureId);
log.info("New Infrastructure instance '{}' registered with Infrastructure Instance Manager.",
infrastructureId);
} catch (IOException e) {
log.error("Failed to bind infrastructure instance with ID '{}' to its RX message socket: {}",
infrastructureId, e.getMessage());
log.error("Stack trace:", e);
}

managedInstances.put(infrastructureId, tmp);
}


/**
* Callback to be invoked when CARMA Platform receives a V2X Message from the NS-3 simulation
* Callback to be invoked when CARMA Platform receives a V2X Message from the
* NS-3 simulation
*
* @param sourceAddr The V2X Message received
* @param txMsg The Host ID of the vehicle receiving the data
* @throws RuntimeException If the socket used to communicate with the platform experiences failure
* @param txMsg The Host ID of the vehicle receiving the data
* @throws RuntimeException If the socket used to communicate with the platform
* experiences failure
*/
public V2xMessageTransmission onV2XMessageTx(InetAddress sourceAddr, CarmaV2xMessage txMsg, long time) {
InfrastructureInstance sender = null;
Expand All @@ -135,7 +136,8 @@ public V2xMessageTransmission onV2XMessageTx(InetAddress sourceAddr, CarmaV2xMes

if (sender == null) {
// Unregistered instance attempting to send messages
throw new IllegalStateException("Unregistered CARMA Streets/V2XHub instance attempting to send messages via MOSAIC");
throw new IllegalStateException(
"Unregistered CARMA Streets/V2XHub instance attempting to send messages via MOSAIC");
}

AdHocMessageRoutingBuilder messageRoutingBuilder = new AdHocMessageRoutingBuilder(
Expand All @@ -149,13 +151,16 @@ public V2xMessageTransmission onV2XMessageTx(InetAddress sourceAddr, CarmaV2xMes
}

/**
* Callback to be invoked when an RSU receives a V2X Message from the NS-3 simulation
* @param rxMsg The V2X Message received
* Callback to be invoked when an RSU receives a V2X Message from the NS-3
* simulation
*
* @param rxMsg The V2X Message received
* @param rxRsuId The Host ID of the vehicle receiving the data
* @throws RuntimeException If the socket used to communicate with the platform experiences failure
* @throws RuntimeException If the socket used to communicate with the platform
* experiences failure
*/
public void onV2XMessageRx(byte[] rxMsg, String rxRsuId) {
if (!managedInstances.containsKey(rxRsuId)) {
if (!managedInstances.containsKey(rxRsuId)) {
return;
}

Expand All @@ -168,40 +173,41 @@ public void onV2XMessageRx(byte[] rxMsg, String rxRsuId) {
}

/**
* Callback to be invoked when a infrastructure instance receives a simulated object detection from
* Callback to be invoked when a infrastructure instance receives a simulated
* object detection from
* a registered simulated sensors.
*
* @param detection
* @param sensorId
*/
public void onObjectDetectionInteraction(DetectedObject detection) {
for (InfrastructureInstance instance : managedInstances.values()) {
if ( instance.containsSensor(detection.getSensorId()) ) {
try {
if (instance.containsSensor(detection.getSensorId())) {
try {
instance.sendInteraction(encodeObjectDetection(detection));
// Assuming each sensor would only ever be registered to a single infrastructure instance
// break out of loop.
// Assuming each sensor would only ever be registered to a single infrastructure
// instance
break;
}
catch( IOException e ) {
log.error("Error occured: {}", e);
} catch (IOException e) {
log.error("Error occured: {}", e.getMessage());
}
}
}
}

private byte[] encodeTimeMessage(InfrastructureTimeMessage message ) {
private byte[] encodeTimeMessage(InfrastructureTimeMessage message) {
return asJson(message).getBytes();
}

private String asJson( Object obj) {
private String asJson(Object obj) {
Gson gson = new Gson();
return gson.toJson(obj);
}

private byte[] encodeObjectDetection(DetectedObject detection) {
return asJson(detection).getBytes();
}


/**
* This function is used to send out encoded timestep update to all registered
* instances the manager has on the managed instances map
Expand Down Expand Up @@ -231,6 +237,7 @@ public void onTimeStepUpdate(InfrastructureTimeMessage message) throws IOExcepti
public boolean checkIfRegistered(String infrastructureId) {
return managedInstances.keySet().contains(infrastructureId);
}

public Map<String, InfrastructureInstance> getManagedInstances() {
return managedInstances;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,22 @@

package org.eclipse.mosaic.fed.infrastructure.ambassador;

import gov.dot.fhwa.saxton.CarmaV2xMessage;
import gov.dot.fhwa.saxton.CarmaV2xMessageReceiver;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.List;

import javax.xml.bind.DatatypeConverter;

import org.eclipse.mosaic.fed.application.ambassador.SimulationKernel;
import org.eclipse.mosaic.fed.infrastructure.configuration.InfrastructureConfiguration;
import org.eclipse.mosaic.interactions.application.InfrastructureV2xMessageReception;
import org.eclipse.mosaic.interactions.communication.AdHocCommunicationConfiguration;
import org.eclipse.mosaic.interactions.communication.V2xMessageReception;
import org.eclipse.mosaic.interactions.communication.V2xMessageTransmission;
import org.eclipse.mosaic.interactions.mapping.RsuRegistration;
import org.eclipse.mosaic.interactions.sensor.DetectedObject;
import org.eclipse.mosaic.interactions.sensor.DetectedObjectInteraction;
import org.eclipse.mosaic.interactions.sensor.Sensor;
import org.eclipse.mosaic.interactions.sensor.SensorRegistration;
Expand All @@ -45,15 +51,8 @@
import org.eclipse.mosaic.rti.api.InternalFederateException;
import org.eclipse.mosaic.rti.api.parameters.AmbassadorParameter;

import com.google.gson.Gson;

import javax.xml.bind.DatatypeConverter;
import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.List;
import gov.dot.fhwa.saxton.CarmaV2xMessage;
import gov.dot.fhwa.saxton.CarmaV2xMessageReceiver;

/**
* Implementation of a {@link AbstractFederateAmbassador} for Infrastructure
Expand Down Expand Up @@ -165,16 +164,14 @@ public void processInteraction(Interaction interaction) throws InternalFederateE


private synchronized void receiveDetectedObjectInteraction( DetectedObjectInteraction interaction) {
log.trace("Process Detected Object Interaction {}", interaction.toString());
log.trace("Process Detected Object Interaction {}", interaction);
infrastructureInstanceManager.onObjectDetectionInteraction(interaction.getDetectedObject());
}
/**
* Extract external message from received
* {@link InfrastructureV2xMessageReception} interaction.
*
* @param interaction Interaction indicates that the
* external message is received by a
* rsu.
* @param interaction Interaction indicates that the external message is received by a rsu.
*/
private synchronized void receiveV2xReceptionInteraction(V2xMessageReception interaction) {
String rsuId = interaction.getReceiverName();
Expand Down Expand Up @@ -300,7 +297,7 @@ public synchronized void processTimeAdvanceGrant(long time) throws InternalFeder
onRsuRegistrationRequest(reg.getInfrastructureId(), reg.getLocation().toGeo());
log.info("RSU Registration for "+ reg.getInfrastructureId() + " @ x, y, z: (" + reg.getLocation().getX() + ", " + reg.getLocation().getY() + ", " + reg.getLocation().getZ() + ")");
onDsrcRegistrationRequest(reg.getInfrastructureId());
log.debug("Sending SensorRegistration interactions for sensor : {}", reg.getSensors().toString());
log.debug("Sending SensorRegistration interactions for sensor : {}", reg.getSensors());
for (Sensor sensor : reg.getSensors()) {
// Trigger Sensor registrations for all listed sensors.
this.rti.triggerInteraction(new SensorRegistration(time,sensor));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.eclipse.mosaic.fed.infrastructure.ambassador;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.mosaic.interactions.sensor.Sensor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.eclipse.mosaic.lib.geo.CartesianPoint;
import org.eclipse.mosaic.lib.math.Vector3d;

public final class DetectedObject implements Serializable{
public final class DetectedObject implements Serializable {

private static final long serialVersionUID = 1L;

Expand Down Expand Up @@ -49,8 +49,6 @@ public final class DetectedObject implements Serializable{

private Size size;



public DetectedObject(DetectionType type, double confidence, String sensorId, String projString, String objectId,
CartesianPoint position, Vector3d velocity, Vector3d angularVelocity, Size size) {
this.type = type;
Expand Down Expand Up @@ -237,6 +235,5 @@ public boolean equals(Object obj) {
return false;
return true;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
import org.eclipse.mosaic.rti.api.Interaction;

public class DetectedObjectInteraction extends Interaction {
public final static String TYPE_ID = createTypeIdentifier(DetectedObjectInteraction.class);

public static final String TYPE_ID = createTypeIdentifier(DetectedObjectInteraction.class);

private DetectedObject detectedObject;

Expand All @@ -35,6 +36,32 @@ public void setDetectedObject(DetectedObject detectedObject) {
this.detectedObject = detectedObject;
}

@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((detectedObject == null) ? 0 : detectedObject.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
DetectedObjectInteraction other = (DetectedObjectInteraction) obj;
if (detectedObject == null) {
if (other.detectedObject != null)
return false;
} else if (!detectedObject.equals(other.detectedObject))
return false;
return true;
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ public enum DetectionType {
PEDESTRIAN("PEDESTRIAN");


public final String name;
public final String label;

/**
* Default constructor.
*
* @param name String
*/
DetectionType(String name) {
this.name = name;
this.label = name;
}

/**
Expand All @@ -45,14 +45,14 @@ public enum DetectionType {
*/
public static DetectionType fromName(String name) {
for (DetectionType type: DetectionType.values()) {
if (type.name == name) {
if (type.label == name) {
return type;
}
}
throw new IllegalArgumentException("Unknown DetectionType name " + name);
}

public String toString(){
return name;
public String getLabel(){
return label;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
*/
package org.eclipse.mosaic.interactions.sensor;

public class Orientation {
import java.io.Serializable;

public class Orientation implements Serializable{
private static final long serialVersionUID = 1L;

private double yaw;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.eclipse.mosaic.rti.api.Interaction;

public class SensorRegistration extends Interaction {
public final static String TYPE_ID = createTypeIdentifier(SensorRegistration.class);
public static final String TYPE_ID = createTypeIdentifier(SensorRegistration.class);

private Sensor sensor;
public SensorRegistration(long time, Sensor sensor) {
Expand Down
Loading

0 comments on commit a5d58ec

Please sign in to comment.