From 9034ff3e2be51fa9d2b11214a114c4ab9363ec87 Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Sun, 24 Mar 2024 17:24:48 -0300 Subject: [PATCH 1/6] feat: adding currentReputation attribute --- src/main/java/reputation/node/models/Node.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/reputation/node/models/Node.java b/src/main/java/reputation/node/models/Node.java index 2742e66..b67d7bc 100644 --- a/src/main/java/reputation/node/models/Node.java +++ b/src/main/java/reputation/node/models/Node.java @@ -90,6 +90,7 @@ public class Node implements NodeTypeService, ILedgerSubscriber { 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() {} @@ -1395,4 +1396,12 @@ public boolean isUseReputation() { public void setUseReputation(boolean useReputation) { this.useReputation = useReputation; } + + public double getCurrentReputation() { + return currentReputation; + } + + public void setCurrentReputation(double currentReputation) { + this.currentReputation = currentReputation; + } } From aea653e349f64a9e97aa5f614b8701fa2b89788b Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Sun, 24 Mar 2024 17:27:12 -0300 Subject: [PATCH 2/6] feat: creating CalculateNodeReputationTask --- .../tasks/CalculateNodeReputationTask.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/main/java/reputation/node/tasks/CalculateNodeReputationTask.java 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); + } +} From 7b757b757a27d2b7a354c24b7444cec3087268dd Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Sun, 24 Mar 2024 17:28:59 -0300 Subject: [PATCH 3/6] feat: adding calculateNodeReputationTaskTime --- src/main/java/reputation/node/models/Node.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main/java/reputation/node/models/Node.java b/src/main/java/reputation/node/models/Node.java index b67d7bc..07b3e1c 100644 --- a/src/main/java/reputation/node/models/Node.java +++ b/src/main/java/reputation/node/models/Node.java @@ -66,6 +66,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; @@ -1404,4 +1405,14 @@ public double getCurrentReputation() { public void setCurrentReputation(double currentReputation) { this.currentReputation = currentReputation; } + + public int getCalculateNodeReputationTaskTime() { + return calculateNodeReputationTaskTime; + } + + public void setCalculateNodeReputationTaskTime( + int calculateNodeReputationTaskTime + ) { + this.calculateNodeReputationTaskTime = calculateNodeReputationTaskTime; + } } From bb64371a57a30d723cb425380ff6846d5d1ae931 Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Sun, 24 Mar 2024 17:30:04 -0300 Subject: [PATCH 4/6] feat: using CalculateNodeReputationTask --- src/main/java/reputation/node/models/Node.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/reputation/node/models/Node.java b/src/main/java/reputation/node/models/Node.java index 07b3e1c..c42d68f 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; @@ -766,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() From 4468315bd88872425bc91679405f49df2dc3c900 Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Sun, 24 Mar 2024 17:31:57 -0300 Subject: [PATCH 5/6] feat: adding calculateNodeReputationTaskTime property --- src/main/resources/OSGI-INF/blueprint/blueprint.xml | 2 ++ src/main/resources/br.uefs.larsid.soft_iot.reputation_node.cfg | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/src/main/resources/OSGI-INF/blueprint/blueprint.xml index 06cf481..b3d7a0c 100644 --- a/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -38,6 +38,7 @@ + @@ -72,6 +73,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 a7aa5bd..807fb2c 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. From 219cbca6b42529bdefaf04b6475186c6dc2720c6 Mon Sep 17 00:00:00 2001 From: AllanCapistrano Date: Sun, 24 Mar 2024 17:35:14 -0300 Subject: [PATCH 6/6] feat: adding reputation to CSV file --- src/main/java/reputation/node/models/Node.java | 6 ++++-- src/main/resources/OSGI-INF/blueprint/blueprint.xml | 2 +- .../resources/br.uefs.larsid.soft_iot.reputation_node.cfg | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/reputation/node/models/Node.java b/src/main/java/reputation/node/models/Node.java index c42d68f..e0709b9 100644 --- a/src/main/java/reputation/node/models/Node.java +++ b/src/main/java/reputation/node/models/Node.java @@ -88,7 +88,7 @@ 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; @@ -780,7 +780,7 @@ private void createTasks() { 0, this.calculateNodeReputationTaskTime * 1000 ); - + /* Somente se um nó do tipo perturbador. */ if (this.getNodeType().getType().toString().equals("DISTURBING")) { new Timer() @@ -930,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 { diff --git a/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/src/main/resources/OSGI-INF/blueprint/blueprint.xml index b3d7a0c..5b08a8b 100644 --- a/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -78,7 +78,7 @@ - + \ 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 807fb2c..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 @@ -28,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