Skip to content

Commit

Permalink
fix: infinite wait when gateway doesn't get device
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanCapistrano committed Jul 18, 2023
1 parent 6ada2f1 commit b789621
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions src/main/java/dlt/load/balancer/model/Balancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.json.JSONArray;
import java.util.logging.Logger;

/**
*
* @author Allan Capistrano, Antonio Crispim, Uellington Damasceno
* @version 0.0.2
* @version 0.0.3
*/
public class Balancer implements ILedgerSubscriber, Runnable {

Expand Down Expand Up @@ -82,7 +82,7 @@ public void buildTimerTaskLB() {
@Override
public void run() {
Logger log = Logger.getLogger(Balancer.class.getName());

timerPass = timerPass + 1;
log.info(String.valueOf(timerPass));

Expand Down Expand Up @@ -153,8 +153,8 @@ public void start() {
public void stop() {
this.flagSubscribe = true;

this.subscribedTopics.forEach(
topic -> this.connector.unsubscribe(topic, this)
this.subscribedTopics.forEach(topic ->
this.connector.unsubscribe(topic, this)
);

this.executor.shutdown();
Expand Down Expand Up @@ -218,6 +218,7 @@ private void processTransactions(Transaction transaction) {
.equals(this.buildSource())
) {
timerTaskLb.cancel();
timerTaskGateWay.cancel();

String source = this.buildSource();
String group = this.groupManager.getGroup();
Expand All @@ -240,10 +241,14 @@ private void processTransactions(Transaction transaction) {
executeTimeOutGateWay();
} catch (IOException ioe) {
this.log.info(
"Load Balancer - Error! Unable to retrieve device list."
);
"Load Balancer - Error! Unable to retrieve device list."
);
ioe.printStackTrace();
}
} else if (transaction.getType().equals(TransactionType.LB_ENTRY_REPLY) &&
!((TargetedTransaction) transaction).getTarget()
.equals(this.buildSource())) {
executeTimeOutGateWay();
}

break;
Expand All @@ -269,6 +274,10 @@ private void processTransactions(Transaction transaction) {

// Colocar para a última transação ser nula.
this.lastTransaction = null;
} else if (transaction.getType().equals(TransactionType.LB_REQUEST) &&
!((TargetedTransaction) transaction).getTarget()
.equals(this.buildSource())) {
executeTimeOutGateWay();
}

break;
Expand Down Expand Up @@ -301,10 +310,14 @@ private void processTransactions(Transaction transaction) {
this.lastTransaction = null;
} catch (MqttException me) {
this.log.info(
"Load Balancer - Error! Unable to remove the first device."
);
"Load Balancer - Error! Unable to remove the first device."
);
me.printStackTrace();
}
} else if (transaction.getType().equals(TransactionType.LB_REPLY) &&
!((TargetedTransaction) transaction).getTarget()
.equals(this.buildSource())) {
executeTimeOutGateWay();
}

break;
Expand Down Expand Up @@ -421,8 +434,8 @@ public void removeFirstDevice(String targetIp) throws MqttException {
}
} catch (IOException ioe) {
this.log.info(
"Error! To retrieve device list or to remove the first device."
);
"Error! To retrieve device list or to remove the first device."
);
ioe.printStackTrace();
}
}
Expand All @@ -432,9 +445,7 @@ public void loadSwapReceberDispositivo(String deviceJSON) {
this.log.info("DeviceJSON: " + deviceJSON);

Device device = DeviceWrapper.toDevice(deviceJSON);
this.log.info(
"Device after convert: " + DeviceWrapper.toJSON(device)
);
this.log.info("Device after convert: " + DeviceWrapper.toJSON(device));

deviceManager.addDevice(device);
} catch (IOException ioe) {
Expand Down Expand Up @@ -485,8 +496,8 @@ public void run() {

if (inet.isReachable(3000)) {
if (this.flagSubscribe) {
this.subscribedTopics.forEach(
topic -> this.connector.subscribe(topic, this)
this.subscribedTopics.forEach(topic ->
this.connector.subscribe(topic, this)
);

this.flagSubscribe = false;
Expand Down

0 comments on commit b789621

Please sign in to comment.