Skip to content

Commit

Permalink
Refactoring part 6: Commlink
Browse files Browse the repository at this point in the history
*Removed access to CommLink.java from API.java, now access is directly provided from inside the CommLink class.
*Also removed the array of all the CommLink objects because they were all revering to the same static object so there shouldn't be any difference
  • Loading branch information
Jamie-Wubben committed Feb 16, 2021
1 parent 536a9c3 commit c4b549c
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 123 deletions.
22 changes: 1 addition & 21 deletions src/main/java/com/api/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.api.communications.CommLink;
import com.setup.Param;
import com.uavController.UAVParam;

import java.util.concurrent.atomic.AtomicReferenceArray;

Expand Down Expand Up @@ -38,26 +37,7 @@ public static ArduSim getArduSim() {
return API.ardusim;

}

/**
* Get the communication link needed for UAV-to-UAV communications.
* @param numUAV This specific UAV position in the data arrays (see documentation).
* @return Communication link used by the UAV to communicate with the other UAVs.
*/
public static CommLink getCommLink(int numUAV) {

synchronized (lockComm) {
if (API.publicCommLink == null) {
API.publicCommLink = new AtomicReferenceArray<>(Param.numUAVs);
for (int i = 0; i < Param.numUAVs; i++) {
API.publicCommLink.set(i, new CommLink(i, Param.numUAVs, UAVParam.broadcastPort));
}
}
}

return API.publicCommLink.get(numUAV);
}


/**
* Get the Copter instance to control the multicopter.
* @param numUAV This specific UAV position in the data arrays (see documentation).
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/api/ArduSimTools.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.api;

import com.api.communications.CommLink;
import com.api.cpuHelper.CPUData;
import com.api.formations.Formation;
import com.api.formations.FormationFactory;
Expand Down Expand Up @@ -2769,7 +2770,7 @@ public static String getTestGlobalConfiguration() {
}
sb.append("\n").append(Text.UAV_PROTOCOL_USED).append(" ").append(ArduSimTools.selectedProtocol);
sb.append("\n").append(Text.COMMUNICATIONS);
sb.append(API.getCommLink(0).toString());
sb.append(CommLink.getCommLink(0).toString());

if (Param.role == ArduSim.SIMULATOR_GUI || Param.role == ArduSim.SIMULATOR_CLI) {
sb.append("\n").append(Text.COLLISION_PARAMETERS);
Expand Down
47 changes: 35 additions & 12 deletions src/main/java/com/api/communications/CommLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,43 @@ public class CommLink {
public static final int DATAGRAM_MAX_LENGTH = 1472; // (bytes) 1500-20-8 (MTU - IP - UDP)

private int numUAV;
private static volatile CommLinkObject publicCommLink = null;

private static volatile CommLinkObject commLinkObject = null;
private static final Object PUBLIC_LOCK = new Object();

private volatile boolean advertised = false;

@SuppressWarnings("unused")
private CommLink() {}

public CommLink(int numUAV, int numUAVs, int port) {

/**
* Get the communication link needed for UAV-to-UAV communications.
* @param numUAV This specific UAV position in the data arrays (see documentation).
* @return Communication link used by the UAV to communicate with the other UAVs.
*/
public static CommLink getCommLink(int numUAV) {
return new CommLink(numUAV,Param.numUAVs,UAVParam.broadcastPort);
}

/**
* Private constructor of CommLink in order to facilitate the singletonPattern
* @param numUAV: the number of the current UAV (identifier)
* @param numUAVs: total number of UAVs
* @param port: port used while sending messages
*/
private CommLink(int numUAV, int numUAVs, int port) {
this.numUAV = numUAV;
synchronized(PUBLIC_LOCK) {
if (CommLink.publicCommLink == null) {
CommLink.publicCommLink = new CommLinkObject(numUAVs, port);
if (commLinkObject == null) {
commLinkObject = new CommLinkObject(numUAVs, port);
}
}
}

/**
* Method to initialize various parameters
* @param numUAVs: number of UAVs
* @param carrierSensing: boolean to turn on carrier sensing
* @param packetCollisionDetection: boolean to turn on packet Collision Detection
* @param bufferSize: size of the buffer
*/
public static void init(int numUAVs,boolean carrierSensing, boolean packetCollisionDetection, int bufferSize){
CommLinkObject.carrierSensingEnabled = carrierSensing;
CommLinkObject.pCollisionEnabled = packetCollisionDetection;
Expand All @@ -59,6 +79,9 @@ public static void init(int numUAVs,boolean carrierSensing, boolean packetCollis
}
}

/**
* Method to close all the communicationLinks
*/
public static void close(){
int numThreads = 2 * Param.numUAVs;
long now = System.currentTimeMillis();
Expand All @@ -82,7 +105,7 @@ public void sendBroadcastMessage(byte[] message) {
advertised = true;
}
} else {
CommLink.publicCommLink.sendBroadcastMessage(numUAV, message);
commLinkObject.sendBroadcastMessage(numUAV, message);
}
}

Expand All @@ -92,7 +115,7 @@ public void sendBroadcastMessage(byte[] message) {
* @return The message received, or null if a fatal error with the socket happens.
*/
public byte[] receiveMessage() {
return CommLink.publicCommLink.receiveMessage(numUAV, 0);
return commLinkObject.receiveMessage(numUAV, 0);
}

/**
Expand All @@ -102,14 +125,14 @@ public byte[] receiveMessage() {
* @return The message received, or null if a fatal error with the socket happens or the timeout is reached.
*/
public byte[] receiveMessage(int socketTimeout) {
return CommLink.publicCommLink.receiveMessage(numUAV, socketTimeout);
return commLinkObject.receiveMessage(numUAV, socketTimeout);
}

/**
* Generates a String representation of the communication statistics, once the experiment has finished.
*/
@Override
public String toString() {
return CommLink.publicCommLink.toString();
return commLinkObject.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.api.masterslavepattern.discovery;

import com.api.communications.CommLink;
import com.esotericsoftware.kryo.io.Input;
import es.upv.grc.mapper.Location2DUTM;
import com.setup.Text;
import com.api.communications.InternalCommLink;
import com.api.masterslavepattern.MSMessageID;
import com.api.masterslavepattern.MSParam;
import com.esotericsoftware.kryo.io.Input;
import com.setup.Text;
import es.upv.grc.mapper.Location2DUTM;

import java.util.concurrent.ConcurrentHashMap;

Expand All @@ -18,7 +18,7 @@ public class DiscoverMasterListener extends Thread {

private ConcurrentHashMap<Long, Location2DUTM> UAVsDetected;
private DiscoveryProgressListener listener;

private InternalCommLink commLink;

@SuppressWarnings("unused")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.protocols.followme.logic;

import com.api.API;
import com.esotericsoftware.kryo.io.Input;
import es.upv.grc.mapper.*;
import com.uavController.UAVParam;
import com.protocols.followme.pojo.Message;
import com.setup.Param;
import com.api.*;
import com.api.communications.CommLink;
import com.api.masterslavepattern.MasterSlaveHelper;
import com.api.masterslavepattern.discovery.DiscoveryProgressListener;
import com.api.masterslavepattern.safeTakeOff.SafeTakeOffContext;
import com.api.masterslavepattern.safeTakeOff.SafeTakeOffListener;
import com.esotericsoftware.kryo.io.Input;
import com.protocols.followme.pojo.Message;
import com.setup.Param;
import com.setup.sim.logic.SimParam;
import com.uavController.UAVParam;
import es.upv.grc.mapper.Location2DGeo;
import es.upv.grc.mapper.Location2DUTM;
import es.upv.grc.mapper.Location3D;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -51,7 +50,7 @@ public FollowMeListenerThread(int numUAV) {
this.isMaster = this.msHelper.isMaster();
this.takeOffHelper = this.copter.getSafeTakeOffHelper();
this.gui = API.getGUI(numUAV);
this.link = API.getCommLink(numUAV);
this.link = CommLink.getCommLink(numUAV);
this.inBuffer = new byte[CommLink.DATAGRAM_MAX_LENGTH];
this.input = new Input(inBuffer);
this.ardusim = API.getArduSim();
Expand All @@ -63,25 +62,21 @@ public void run() {
ardusim.sleep(FollowMeParam.STATE_CHANGE_TIMEOUT);
}

/** START PHASE */
// START PHASE
gui.logUAV(FollowMeText.START);
// Let the master detect slaves until the setup button is pressed
Map<Long, Location2DUTM> UAVsDetected = null;
if (this.isMaster) {
gui.logVerboseUAV(FollowMeText.SLAVE_START_LISTENER);
final AtomicInteger totalDetected = new AtomicInteger();
UAVsDetected = msHelper.DiscoverSlaves(new DiscoveryProgressListener() {

@Override
public boolean onProgressCheckActionPerformed(int numUAVs) {
// Just for logging purposes
if (numUAVs > totalDetected.get()) {
totalDetected.set(numUAVs);
gui.log(FollowMeText.MASTER_DETECTED_UAVS + numUAVs);
}
// We decide to continue when the setup button is pressed
return numUAVs == API.getArduSim().getNumUAVs() - 1;
UAVsDetected = msHelper.DiscoverSlaves(numUAVs -> {
// Just for logging purposes
if (numUAVs > totalDetected.get()) {
totalDetected.set(numUAVs);
gui.log(FollowMeText.MASTER_DETECTED_UAVS + numUAVs);
}
// We decide to continue when the setup button is pressed
return numUAVs == API.getArduSim().getNumUAVs() - 1;
});
} else {
gui.logVerboseUAV(FollowMeText.LISTENER_WAITING);
Expand Down Expand Up @@ -113,13 +108,7 @@ public boolean onProgressCheckActionPerformed(int numUAVs) {
}
gui.logUAV("ready to takeOff");
// 2. Take off all the UAVs
takeOffHelper.start(takeOff, new SafeTakeOffListener() {

@Override
public void onCompleteActionPerformed() {
currentState.set(SETUP_FINISHED);
}
});
takeOffHelper.start(takeOff, () -> currentState.set(SETUP_FINISHED));
while (currentState.get() < SETUP_FINISHED) {
ardusim.sleep(FollowMeParam.STATE_CHANGE_TIMEOUT);
}
Expand Down Expand Up @@ -193,7 +182,7 @@ public void onCompleteActionPerformed() {
}
}

/** MOVE TO LAND PHASE */
// MOVE TO LAND PHASE
if (currentState.get() == MOVE_TO_LAND) {
gui.logUAV(FollowMeText.MOVE_TO_LAND);
gui.updateProtocolState(FollowMeText.MOVE_TO_LAND);
Expand All @@ -213,12 +202,12 @@ public void onCompleteActionPerformed() {
moveTo.start();
try {
moveTo.join();
} catch (InterruptedException e) {}
} catch (InterruptedException ignored) {}
currentState.set(LANDING);
}
}

/** LANDING PHASE */
// LANDING PHASE
if (!copter.land()) {
gui.exit(FollowMeText.LAND_ERROR + " " + selfId);
}
Expand All @@ -238,7 +227,7 @@ public void onCompleteActionPerformed() {
}
}

/** FINISH PHASE */
// FINISH PHASE
gui.logUAV(FollowMeText.FINISH);
gui.updateProtocolState(FollowMeText.FINISH);
gui.logVerboseUAV(FollowMeText.LISTENER_FINISHED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public FollowMeTalkerThread(int numUAV) {
this.ardusim = API.getArduSim();
this.copter = API.getCopter(numUAV);
this.gui = API.getGUI(numUAV);
this.link = API.getCommLink(numUAV);
this.link = CommLink.getCommLink(numUAV);
this.outBuffer = new byte[CommLink.DATAGRAM_MAX_LENGTH];
this.output = new Output(outBuffer);

Expand Down Expand Up @@ -86,7 +86,7 @@ public void run() {
ardusim.sleep(FollowMeParam.STATE_CHANGE_TIMEOUT);
}

/** LANDING PHASE */
// LANDING PHASE
gui.logVerboseUAV(FollowMeText.MASTER_SEND_LAND);
output.reset();
output.writeShort(Message.LAND);
Expand All @@ -112,7 +112,7 @@ public void run() {
ardusim.sleep(FollowMeParam.STATE_CHANGE_TIMEOUT);
}

/** FINISH PHASE */
// FINISH PHASE
gui.logVerboseUAV(FollowMeText.TALKER_FINISHED);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public BeaconingThread(int numUAV) {
this.impactLocationUTM = MBCAPParam.impactLocationUTM[numUAV];

this.numUAV = numUAV;
this.link = API.getCommLink(numUAV);
this.link = CommLink.getCommLink(numUAV);
this.outBuffer = new byte[CommLink.DATAGRAM_MAX_LENGTH];
this.output = new Output(outBuffer);
this.copter = API.getCopter(numUAV);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.protocols.mbcap.logic;

import com.esotericsoftware.kryo.io.Input;

import com.api.API;
import com.api.communications.CommLink;
import com.esotericsoftware.kryo.io.Input;
import com.protocols.mbcap.gui.MBCAPPCCompanionDialog;
import com.protocols.mbcap.pojo.Beacon;

Expand All @@ -24,7 +22,7 @@ private MBCAPPCCompanionListener() {}

public MBCAPPCCompanionListener(MBCAPPCCompanionDialog dialog) {
this.dialog = dialog;
this.link = API.getCommLink(0);
this.link = CommLink.getCommLink(0);
this.inBuffer = new byte[CommLink.DATAGRAM_MAX_LENGTH];
this.input = new Input(inBuffer);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private ReceiverThread() {}
public ReceiverThread(int numUAV) {
this.beacons = MBCAPParam.beacons[numUAV];
this.numUAV = numUAV;
this.link = API.getCommLink(numUAV);
this.link = CommLink.getCommLink(numUAV);
this.inBuffer = new byte[CommLink.DATAGRAM_MAX_LENGTH];
this.input = new Input(inBuffer);
this.copter = API.getCopter(numUAV);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public MUSCOPListenerThread(int numUAV) {
this.gui = API.getGUI(numUAV);
this.inBuffer = new byte[CommLink.DATAGRAM_MAX_LENGTH];
this.input = new Input(inBuffer);
this.link = API.getCommLink(numUAV);
this.link = CommLink.getCommLink(numUAV);
this.ardusim = API.getArduSim();
this.msHelper = this.copter.getMasterSlaveHelper();
this.isMaster = this.msHelper.isMaster();
Expand Down
Loading

0 comments on commit c4b549c

Please sign in to comment.