Skip to content

Commit

Permalink
Merge pull request #13 from AllanCapistrano/feat/disturbing-behavior
Browse files Browse the repository at this point in the history
Feat/disturbing behavior
  • Loading branch information
AllanCapistrano authored Feb 25, 2024
2 parents 7e82d09 + 1056d31 commit 5870b5a
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 19 deletions.
48 changes: 40 additions & 8 deletions src/main/java/reputation/node/models/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import reputation.node.reputation.credibility.NodeCredibility;
import reputation.node.services.NodeTypeService;
import reputation.node.tangle.LedgerConnector;
import reputation.node.tasks.ChangeDisturbingNodeBehaviorTask;
import reputation.node.tasks.CheckDevicesTask;
import reputation.node.tasks.CheckNodesServicesTask;
import reputation.node.tasks.RequestDataTask;
Expand All @@ -63,6 +64,7 @@ public class Node implements NodeTypeService, ILedgerSubscriber {
private int waitDeviceResponseTaskTime;
private int checkNodesServicesTaskTime;
private int waitNodesResponsesTaskTime;
private int changeDisturbingNodeBehaviorTaskTime;
private List<Device> devices;
private List<Transaction> nodesWithServices;
private LedgerConnector ledgerConnector;
Expand All @@ -78,6 +80,7 @@ public class Node implements NodeTypeService, ILedgerSubscriber {
private boolean isAverageEvaluationZero = false;
private boolean useCredibility;
private boolean useLatestCredibility;
private double reputationValue;
private NodeCredibility nodeCredibility;
private CsvWriterService csvWriter;
private String[] csvData = new String[6];
Expand Down Expand Up @@ -690,6 +693,22 @@ private void createTasks() {
0,
this.checkNodesServicesTaskTime * 1000
);
/* Somente se um nó do tipo perturbador. */
if (this.getNodeType().getType().toString().equals("DISTURBING")) {
new Timer()
.scheduleAtFixedRate(
new ChangeDisturbingNodeBehaviorTask(
this,
new ReputationUsingKMeans(
this.kMeans,
this.nodeCredibility,
this.getNodeType().getNodeId()
)
),
0,
this.changeDisturbingNodeBehaviorTaskTime * 1000
);
}
}

/**
Expand Down Expand Up @@ -975,18 +994,12 @@ private int getLastEvaluation(
* Altera o comportamento do nó, caso seja um nó malicioso.
*/
private void changeMaliciousNodeBehavior() {
if (
this.getNodeType()
.getNode()
.getConductType()
.toString()
.equals("MALICIOUS")
) {
if (this.getNodeType().getType().toString().equals("MALICIOUS")) {
this.getNodeType().getNode().defineConduct();

logger.info(
String.format(
"Changing Malicious behavior to '%s'.",
"Changing Malicious node behavior to '%s'.",
this.getNodeType().getNode().getConductType().toString()
)
);
Expand Down Expand Up @@ -1242,4 +1255,23 @@ public boolean isUseLatestCredibility() {
public void setUseLatestCredibility(boolean useLatestCredibility) {
this.useLatestCredibility = useLatestCredibility;
}

public double getReputationValue() {
return reputationValue;
}

public void setReputationValue(double reputationValue) {
this.reputationValue = reputationValue;
}

public int getChangeDisturbingNodeBehaviorTaskTime() {
return changeDisturbingNodeBehaviorTaskTime;
}

public void setChangeDisturbingNodeBehaviorTaskTime(
int changeDisturbingNodeBehaviorTaskTime
) {
this.changeDisturbingNodeBehaviorTaskTime =
changeDisturbingNodeBehaviorTaskTime;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package reputation.node.tasks;

import dlt.client.tangle.hornet.model.transactions.Transaction;
import java.util.List;
import java.util.TimerTask;
import java.util.logging.Logger;
import reputation.node.models.Node;
import reputation.node.reputation.IReputation;

/**
* Task para alterar o comportamento de um nó do tipo Perturbador.
*
* @author Allan Capistrano
* @version 1.0.0
*/
public class ChangeDisturbingNodeBehaviorTask extends TimerTask {

private static final double REPUTATION_THRESHOLD = 0.9;

private final Node node;
private final IReputation reputation;
private boolean changeBehaviorFlag = true;

private static final Logger logger = Logger.getLogger(
ChangeDisturbingNodeBehaviorTask.class.getName()
);

/**
* Método construtor.
* @param node Node - O nó que verificará a própria reputação.
* @param reputation IReputation - Objeto para calcular a reputação.
*/
public ChangeDisturbingNodeBehaviorTask(Node node, IReputation reputation) {
this.node = node;
this.reputation = reputation;
}

@Override
public void run() {
List<Transaction> evaluationTransactions =
this.node.getLedgerConnector()
.getLedgerReader()
.getTransactionsByIndex(this.node.getNodeType().getNodeId(), false);

double reputationValue =
this.reputation.calculate(
evaluationTransactions,
this.node.isUseLatestCredibility(),
this.node.isUseCredibility()
);

logger.info(
String.format("Disturbing node reputation: %f", reputationValue)
);

if (this.changeBehaviorFlag && reputationValue > REPUTATION_THRESHOLD) {
this.node.getNodeType().getNode().defineConduct();
this.changeBehaviorFlag = false;
}

/**
* Quando o nó alcançar uma reputação negativa, ele está habilitado a
* poder alterar novamente seu comportamento.
*/
if (reputationValue <= 0) {
this.changeBehaviorFlag = true;
}
}
}
11 changes: 0 additions & 11 deletions src/main/java/reputation/node/tasks/CheckNodeReputationTask.java

This file was deleted.

2 changes: 2 additions & 0 deletions src/main/resources/OSGI-INF/blueprint/blueprint.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<property name="waitDeviceResponseTaskTime" value="${waitDeviceResponseTaskTime}" />
<property name="checkNodesServicesTaskTime" value="${checkNodesServicesTaskTime}" />
<property name="waitNodesResponsesTaskTime" value="${waitNodesResponsesTaskTime}" />
<property name="changeDisturbingNodeBehaviorTaskTime" value="${changeDisturbingNodeBehaviorTaskTime}" />
<property name="deviceManager" ref="deviceManagerService" />
<property name="ledgerConnector" ref="ledgerConnector" />
<property name="useCredibility" value="${useCredibility}" />
Expand Down Expand Up @@ -68,6 +69,7 @@
<cm:property name="waitDeviceResponseTaskTime" value="10" />
<cm:property name="checkNodesServicesTaskTime" value="45" />
<cm:property name="waitNodesResponsesTaskTime" value="30" />
<cm:property name="changeDisturbingNodeBehaviorTaskTime" value="30" />
<cm:property name="useCredibility" value="true" />
<cm:property name="useLatestCredibility" value="true" />
<cm:property name="debugModeValue" value="true" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ checkNodesServicesTaskTime=45
# Tempo máximo (segundos) de espera da resposta do nós para a requisição de pretação de serviço.
# Obs: Tem que ser menor do que o 'checkNodesServicesTaskTime'.
waitNodesResponsesTaskTime=30
# Tempo (segundos) para verificar a reputação e alterar o comportamento do nó do tipo Perbubador.
changeDisturbingNodeBehaviorTaskTime=30
# Determina se deseja usar (true) ou não (false) a credibilidade no sistema.
useCredibility=true
# Determina se é para usar (true) ou não (false) a credibilidade mais recente para o cálculo da reputação.
Expand Down

0 comments on commit 5870b5a

Please sign in to comment.