Skip to content

Commit

Permalink
Update CARLA Ambassador to use XMLRPC Client
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbourelly999 committed Oct 18, 2023
1 parent 3967e5e commit cf3d508
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 32 deletions.
34 changes: 34 additions & 0 deletions co-simulation/NOTICE-THIRD-PARTY.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,30 @@ Apache Commons Text (1.6)
* Source: https://git-wip-us.apache.org/repos/asf?p=commons-text


Apache WebServices Common Utilities (1.0.2)

* License: The Apache Software License, Version 2.0
* Maven artifact: `org.apache.ws.commons.util:ws-commons-util:1.0.2`
* Project: http://ws.apache.org/commons/util
* Source: https://svn.apache.org/viewcvs.cgi/webservices/commons/trunk/util


Apache XML-RPC Client Library (3.1.3)

* License: The Apache Software License, Version 2.0
* Maven artifact: `org.apache.xmlrpc:xmlrpc-client:3.1.3`
* Project: http://ws.apache.org/xmlrpc/xmlrpc-client/
* Source: https://svn.apache.org/viewvc/webservices/xmlrpc/tags/xmlrpc-3.1.3/xmlrpc-client


Apache XML-RPC Common Library (3.1.3)

* License: The Apache Software License, Version 2.0
* Maven artifact: `org.apache.xmlrpc:xmlrpc-common:3.1.3`
* Project: http://ws.apache.org/xmlrpc/xmlrpc-common/
* Source: https://svn.apache.org/viewvc/webservices/xmlrpc/tags/xmlrpc-3.1.3/xmlrpc-common


Commons Compiler (2.7.5)

* License: New BSD License
Expand Down Expand Up @@ -355,3 +379,13 @@ Woodstox (5.1.0)
* Maven artifact: `com.fasterxml.woodstox:woodstox-core:5.1.0`
* Project: https://github.com/FasterXML/woodstox
* Source: https://github.com/FasterXML/woodstox


XML Commons External Components XML APIs (1.0.b2)

* License: The Apache Software License, Version 2.0
* Maven artifact: `xml-apis:xml-apis:1.0.b2`
* Project: http://xml.apache.org/commons/#external
* Source: https://svn.apache.org/viewvc/xml/commons/tags/xml-commons-1_0_b2


Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@
import com.google.common.collect.Lists;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringUtils;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClientException;
import org.eclipse.mosaic.fed.carla.carlaconnect.CarlaConnection;
import org.eclipse.mosaic.fed.carla.carlaconnect.CarlaXmlRpcClient;
import org.eclipse.mosaic.fed.carla.config.CarlaConfiguration;
import org.eclipse.mosaic.fed.sumo.traci.constants.CommandSimulationControl;
import org.eclipse.mosaic.fed.sumo.traci.writer.ListTraciWriter;
import org.eclipse.mosaic.fed.sumo.traci.writer.StringTraciWriter;
import org.eclipse.mosaic.interactions.application.*;
import org.eclipse.mosaic.interactions.detector.DetectorRegistration;
import org.eclipse.mosaic.lib.objects.detector.Detector;
import org.eclipse.mosaic.lib.util.ProcessLoggingThread;
import org.eclipse.mosaic.lib.util.objects.ObjectInstantiation;
import org.eclipse.mosaic.rti.TIME;
Expand All @@ -37,6 +41,7 @@
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -110,6 +115,8 @@ public class CarlaAmbassador extends AbstractFederateAmbassador {
*/
private final PriorityBlockingQueue<CarlaV2xMessageReception> carlaV2xInteractionQueue = new PriorityBlockingQueue<>();

private List<Detector> registeredDetectors = new ArrayList<>();

/**
* Creates a new {@link CarlaAmbassador} object.
*
Expand Down Expand Up @@ -355,12 +362,16 @@ public synchronized void processTimeAdvanceGrant(long time) throws InternalFeder
this.executedTimes++;

//call CarlaXmlRpcClient to ask for data whenever time advances
carlaXmlRpcClient.requestCarlaList();

for (Detector detector: registeredDetectors ) {
carlaXmlRpcClient.get_detected_objects(detector.getSensorId());
}
} catch (IllegalValueException e) {
log.error("Error during advanceTime(" + time + ")", e);
throw new InternalFederateException(e);
}
catch (XmlRpcException e) {
log.error("Failed to connect to CARLA Adapter : ", e);
}
}

/**
Expand Down Expand Up @@ -494,6 +505,21 @@ public void processInteraction(Interaction interaction) {
} else if (interaction.getTypeId().equals(CarlaV2xMessageReception.TYPE_ID)) {
this.receiveInteraction((CarlaV2xMessageReception) interaction);
}
else if (interaction.getTypeId().equals(DetectorRegistration.TYPE_ID)) {
this.receiveInteraction((DetectorRegistration) interaction);
}
}


private void receiveInteraction(DetectorRegistration interaction) {
try {
carlaXmlRpcClient.create_sensor(interaction);
registeredDetectors.add(interaction.getDetector());
}
catch(XmlRpcException e) {
log.error("Error occurred attempting to create sensor : {}", interaction.getDetector(), e);
}

}

/**
Expand All @@ -508,7 +534,7 @@ private void receiveInteraction(CarlaTraciResponse interaction) {
carlaConnection.getDataOutputStream().write(interaction.getResult());
}
} catch (Exception e) {
log.error("error occurs during process carla traci response interaction: " + e.getMessage());
log.error("error occurs during process carla traci response interaction: {} ", e.getMessage());
}
}

Expand All @@ -526,7 +552,7 @@ private void receiveInteraction(SimulationStepResponse interaction) {
}

} catch (Exception e) {
log.error("error occurs during process simulation step response interaction: " + e.getMessage());
log.error("error occurs during process simulation step response interaction: {}", e.getMessage());
}
}

Expand All @@ -536,7 +562,7 @@ private void receiveInteraction(SimulationStepResponse interaction) {
* @param interaction CarlaV2xMessageReception interaction
*/
private void receiveInteraction(CarlaV2xMessageReception interaction) {
log.info("{} received V2x message: {}.", interaction.getReceiverID(), interaction.getMessage().toString());
log.info("{} received V2x message: {}.", interaction.getReceiverID(), interaction.getMessage());

interactionQueue.add(interaction);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@
package org.eclipse.mosaic.fed.carla.carlaconnect;

import java.net.URL;
import java.util.Arrays;
import java.util.List;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.apache.xmlrpc.client.XmlRpcClientException;
import org.eclipse.mosaic.interactions.detector.DetectorRegistration;
import org.eclipse.mosaic.lib.objects.detector.DetectedObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -29,7 +34,10 @@
public class CarlaXmlRpcClient{

boolean isConnected;
private String registeredFunction = "test.echo";
private static final String CREATE_SENSOR = "create_simulated_semantic_lidar_sensor";
private static final String GET_SENSOR = "get_simulated_sensor";
private static final String GET_DETECTED_OBJECTS = "get_detected_objects";

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

Expand Down Expand Up @@ -63,33 +71,29 @@ public void initialize(URL xmlRpcServerUrl)
}


/**
* This method uses xmlrpc to connect with CARLA CDASim adapter service
* @throws XmlRpcException
*/
public void requestCarlaList()
{
if(!isConnected)
{

try {
throw new XmlRpcException("Server is not connected!");
}
catch (XmlRpcException e)
{
log.error("Server is not connected! {}", e.getMessage());
}
public void create_sensor(DetectorRegistration registration) throws XmlRpcException{
List<Double> location = Arrays.asList(registration.getDetector().getLocation().getX(), registration.getDetector().getLocation().getY(), registration.getDetector().getLocation().getZ());
List<Double> orientation = Arrays.asList(registration.getDetector().getOrientation().getPitch(), registration.getDetector().getOrientation().getRoll(), registration.getDetector().getOrientation().getYaw());

if (isConnected) {
Object[] params = new Object[]{1/** TODO Infrastructure ID */, registration.getDetector().getSensorId(), location, orientation};
Object result = client.execute(CREATE_SENSOR, params);
log.info((String)result);
}
try{
Object[] params = new Object[]{"Test " + java.time.LocalDateTime.now()};
Object result = client.execute(registeredFunction, params);
else {
log.warn("XMLRpcClient is not connected to CARLA Adapter!");
}
}

public void get_detected_objects(String sensorId) throws XmlRpcException{
if (isConnected) {
Object[] params = new Object[]{1/** TODO Infrastructure ID */, sensorId};
Object result = client.execute(GET_DETECTED_OBJECTS, params);
log.info((String)result);
}
catch (XmlRpcException x)
{
log.error("Errors occurred with xmlrpc connection {}", x.getMessage());
closeConnection();
}

else {
log.warn("XMLRpcClient is not connected to CARLA Adapter!");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.eclipse.mosaic.lib.objects.addressing.IpResolver;
import org.eclipse.mosaic.lib.objects.communication.AdHocConfiguration;
import org.eclipse.mosaic.lib.objects.communication.InterfaceConfiguration;
import org.eclipse.mosaic.lib.objects.detector.DetectedObject;
import org.eclipse.mosaic.lib.objects.detector.Detector;
import org.eclipse.mosaic.lib.objects.v2x.ExternalV2xMessage;
import org.eclipse.mosaic.lib.objects.v2x.V2xMessage;
Expand Down Expand Up @@ -290,7 +291,6 @@ public synchronized void processTimeAdvanceGrant(long time) throws InternalFeder
}
log.info("Infrastructure message ambassador processing timestep to {}.", time);
try {

// Handle any new infrastructure registration requests
List<InfrastructureRegistrationMessage> newRegistrations = infrastructureRegistrationReceiver
.getReceivedMessages();
Expand Down

0 comments on commit cf3d508

Please sign in to comment.