diff --git a/src/main/java/reputation/node/models/Node.java b/src/main/java/reputation/node/models/Node.java index 2742e66..e0709b9 100644 --- a/src/main/java/reputation/node/models/Node.java +++ b/src/main/java/reputation/node/models/Node.java @@ -41,6 +41,7 @@ import reputation.node.reputation.credibility.NodeCredibility; import reputation.node.services.NodeTypeService; import reputation.node.tangle.LedgerConnector; +import reputation.node.tasks.CalculateNodeReputationTask; import reputation.node.tasks.ChangeDisturbingNodeBehaviorTask; import reputation.node.tasks.CheckDevicesTask; import reputation.node.tasks.CheckNodesServicesTask; @@ -66,6 +67,7 @@ public class Node implements NodeTypeService, ILedgerSubscriber { private int checkNodesServicesTaskTime; private int waitNodesResponsesTaskTime; private int changeDisturbingNodeBehaviorTaskTime; + private int calculateNodeReputationTaskTime; private List devices; private List nodesWithServices; private LedgerConnector ledgerConnector; @@ -86,10 +88,11 @@ public class Node implements NodeTypeService, ILedgerSubscriber { private NodeCredibility nodeCredibility; private CsvWriterService csvWriter; private String credibilityHeader; - private String[] csvData = new String[10]; + private String[] csvData = new String[11]; private long startedExperiment; private boolean flagStartedExperiment = true; private boolean changeDisturbingNodeBehaviorFlag = false; + private double currentReputation; private static final Logger logger = Logger.getLogger(Node.class.getName()); public Node() {} @@ -764,6 +767,20 @@ private void createTasks() { 0, this.checkNodesServicesTaskTime * 1000 ); + new Timer() + .scheduleAtFixedRate( + new CalculateNodeReputationTask( + this, + new ReputationUsingKMeans( + this.kMeans, + this.nodeCredibility, + this.getNodeType().getNodeId() + ) + ), + 0, + this.calculateNodeReputationTaskTime * 1000 + ); + /* Somente se um nó do tipo perturbador. */ if (this.getNodeType().getType().toString().equals("DISTURBING")) { new Timer() @@ -913,6 +930,8 @@ private float calculateCredibility( this.csvData[7] = String.valueOf(startedExperiment); /* Salvando o tempo em que calculou a nova credibilidade. */ this.csvData[8] = String.valueOf(System.currentTimeMillis()); + /* Salvando a reputação do nó */ + this.csvData[10] = String.valueOf(this.reputationValue); /* Escrevendo na blockchain a credibilidade calculado do nó avaliador */ try { @@ -1395,4 +1414,22 @@ public boolean isUseReputation() { public void setUseReputation(boolean useReputation) { this.useReputation = useReputation; } + + public double getCurrentReputation() { + return currentReputation; + } + + public void setCurrentReputation(double currentReputation) { + this.currentReputation = currentReputation; + } + + public int getCalculateNodeReputationTaskTime() { + return calculateNodeReputationTaskTime; + } + + public void setCalculateNodeReputationTaskTime( + int calculateNodeReputationTaskTime + ) { + this.calculateNodeReputationTaskTime = calculateNodeReputationTaskTime; + } } diff --git a/src/main/java/reputation/node/tasks/CalculateNodeReputationTask.java b/src/main/java/reputation/node/tasks/CalculateNodeReputationTask.java new file mode 100644 index 0000000..1a2b6e8 --- /dev/null +++ b/src/main/java/reputation/node/tasks/CalculateNodeReputationTask.java @@ -0,0 +1,46 @@ +package reputation.node.tasks; + +import dlt.client.tangle.hornet.model.transactions.Transaction; +import java.util.List; +import java.util.TimerTask; +import reputation.node.models.Node; +import reputation.node.reputation.IReputation; + +/** + * Task para calcular a reputação atual do nó. + * + * @author Allan Capistrano + * @version 1.0.0 + */ +public class CalculateNodeReputationTask extends TimerTask { + + private final Node node; + private final IReputation reputation; + + /** + * 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 CalculateNodeReputationTask(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() + ); + + this.node.setReputationValue(reputationValue); + } +} diff --git a/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/src/main/resources/OSGI-INF/blueprint/blueprint.xml index 06cf481..5b08a8b 100644 --- a/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -38,6 +38,7 @@ + @@ -72,11 +73,12 @@ + - + \ No newline at end of file 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 a7aa5bd..5507482 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 @@ -18,6 +18,8 @@ checkNodesServicesTaskTime=45 waitNodesResponsesTaskTime=30 # Tempo (segundos) para verificar a reputação e alterar o comportamento do nó do tipo Perbubador. changeDisturbingNodeBehaviorTaskTime=30 +# Tempo (segundos) para verificar o valor da reputação. +calculateNodeReputationTaskTime=20 # 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. @@ -26,6 +28,6 @@ useLatestCredibility=true useReputation=true # Cabeçalho do experimento de credibilidades. # Obs: Separe somente utilizando vírgula -credibilityHeader=Node_ID,Type,C(n),R,Tr(n),Cr_old(n),Cr_new(n),Started_experiment_time,wrote_file_time,Node_provider_ID +credibilityHeader=Node_ID,Type,C(n),R,Tr(n),Cr_old(n),Cr_new(n),Started_experiment_time,wrote_file_time,Node_provider_ID,Reputation debugModeValue=true \ No newline at end of file