Skip to content

Commit

Permalink
If applied, this commit will update the target real mqtt port.
Browse files Browse the repository at this point in the history
  • Loading branch information
UellingtonDamasceno committed Feb 12, 2024
1 parent cbeda2e commit e97555f
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/main/java/dlt/load/balancer/model/Balancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,9 @@ private void processTransactions(Transaction transaction) {
timerTaskGateWay.cancel();

try {
this.removeFirstDevice(transaction.getSource().split("/")[2]);
String ip = transaction.getSource().split("/")[2]; //Só funcionará se o group tiver um '/' EX: cloud/c1 - Alterar
String port = transaction.getSource().split("/")[3]; //Idem para acima
this.removeFirstDevice(ip, port);

Transaction transactionDevice = new LBDevice(
source,
Expand Down Expand Up @@ -385,7 +387,9 @@ private void processTransactions(Transaction transaction) {
timerTaskGateWay.cancel();

try {
this.removeFirstDevice(transaction.getSource().split("/")[2]);
String ip = transaction.getSource().split("/")[2];
String port = transaction.getSource().split("/")[3];
this.removeFirstDevice(ip, port);
this.lastTransaction = null;
} catch (MqttException me) {
logger.info("Load Balancer - Error! Unable to remove the first device.");
Expand Down Expand Up @@ -436,14 +440,16 @@ public void run() {
timerPass = timerPass + 1;
log.info(String.valueOf(timerPass));

if ((timerPass * 1000) < TIMEOUT_LB_REPLY) {
if ((timerPass * 1000) >= TIMEOUT_LB_REPLY) {
log.info("TIME'S UP executeTimeOutLB");
if(multiLayerBalancing){
log.info("Send multi-layer balancing request.");
Transaction trans = new LBMultiRequest(buildSource(), groupManager.getGroup());
sendTransaction(trans);
}else{
lastTransaction = null;
}
lastTransaction = null;
timerTaskLb.cancel();
log.info("TIME'S UP executeTimeOutLB");
}
}
};
Expand Down Expand Up @@ -486,10 +492,12 @@ private String buildSource() {
return new StringBuilder(this.groupManager.getGroup())
.append("/")
.append(this.idManager.getIP())
.append("/")
.append(this.currentMqttPort())
.toString();
}

public void removeFirstDevice(String targetIp) throws MqttException {
public void removeFirstDevice(String targetIp, String targetMqttPort) throws MqttException {
try {
List<Device> allDevices = deviceManager.getAllDevices();

Expand All @@ -498,7 +506,7 @@ public void removeFirstDevice(String targetIp) throws MqttException {
JsonObject jsonPublish = new JsonObject();
jsonPublish.addProperty("id", deviceARemover.getId());
jsonPublish.addProperty("url", "tcp://" + targetIp);
jsonPublish.addProperty("port", deviceARemover.getId());
jsonPublish.addProperty("port", targetMqttPort);
jsonPublish.addProperty("user", "karaf");
jsonPublish.addProperty("password", "karaf");

Expand Down Expand Up @@ -610,4 +618,10 @@ private boolean isMultiLayerBalancing() {
}
return isMultiLayer != null && isMultiLayer.equals("1");
}

private String currentMqttPort(){
String port = System.getenv("GATEWAY_PORT");
return port == null ? "1883" : port;
}

}

0 comments on commit e97555f

Please sign in to comment.