diff --git a/src/main/java/reputation/node/models/Node.java b/src/main/java/reputation/node/models/Node.java index ae6e450..bdd1d9a 100644 --- a/src/main/java/reputation/node/models/Node.java +++ b/src/main/java/reputation/node/models/Node.java @@ -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; @@ -63,6 +64,7 @@ public class Node implements NodeTypeService, ILedgerSubscriber { private int waitDeviceResponseTaskTime; private int checkNodesServicesTaskTime; private int waitNodesResponsesTaskTime; + private int changeDisturbingNodeBehaviorTaskTime; private List devices; private List nodesWithServices; private LedgerConnector ledgerConnector; @@ -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]; @@ -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 + ); + } } /** @@ -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() ) ); @@ -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; + } } diff --git a/src/main/java/reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java b/src/main/java/reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java new file mode 100644 index 0000000..ab137a0 --- /dev/null +++ b/src/main/java/reputation/node/tasks/ChangeDisturbingNodeBehaviorTask.java @@ -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 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; + } + } +} diff --git a/src/main/java/reputation/node/tasks/CheckNodeReputationTask.java b/src/main/java/reputation/node/tasks/CheckNodeReputationTask.java deleted file mode 100644 index ffba96c..0000000 --- a/src/main/java/reputation/node/tasks/CheckNodeReputationTask.java +++ /dev/null @@ -1,11 +0,0 @@ -package reputation.node.tasks; - -/** - * Task para verificar de tempos em tempos a reputação do próprio nó. - * - * @author Allan Capistrano - * @version 1.0.0 - */ -public class CheckNodeReputationTask { - // TODO: Ideia, no nó do tipo perturbador, quando ele alcançar uma boa reputação, mudar o seu comportamento aqui, através do método defineConduct(). -} diff --git a/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/src/main/resources/OSGI-INF/blueprint/blueprint.xml index 21fb60d..dc7774f 100644 --- a/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -37,6 +37,7 @@ + @@ -68,6 +69,7 @@ + diff --git a/src/main/resources/br.uefs.larsid.soft_iot.reputation_node.cfg b/src/main/resources/br.uefs.larsid.soft_iot.reputation_node.cfg index 770ba8f..d6050b7 100644 --- a/src/main/resources/br.uefs.larsid.soft_iot.reputation_node.cfg +++ b/src/main/resources/br.uefs.larsid.soft_iot.reputation_node.cfg @@ -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.