From 4f3627de13504c706fc7fa05059743d70eab1c6f Mon Sep 17 00:00:00 2001 From: dannyk Date: Sun, 3 Sep 2023 20:21:27 +0300 Subject: [PATCH] clean up mailer messages --- src/Agent/Agent.java | 6 +++--- src/Agent/BoSAgent/BoSAgent.java | 2 +- src/Agent/PDAgent/PDAgent.java | 3 +-- src/Audit/Audit.java | 2 +- src/Audit/RecordedMessage.java | 4 ++-- src/Mailer/Mailer.java | 18 +++++++++--------- src/Mailer/Messages/MailerMessage.java | 2 +- src/Mailer/Messages/Message.java | 4 ---- 8 files changed, 18 insertions(+), 23 deletions(-) delete mode 100644 src/Mailer/Messages/Message.java diff --git a/src/Agent/Agent.java b/src/Agent/Agent.java index a257a2c..13061ab 100644 --- a/src/Agent/Agent.java +++ b/src/Agent/Agent.java @@ -2,7 +2,7 @@ import Audit.Audit; import Mailer.*; -import Mailer.Messages.Message; +import Mailer.Messages.MailerMessage; import Mailer.Messages.PlayMessage; import java.util.List; @@ -52,7 +52,7 @@ public void run(){ protected void readMessages(){ while (true){ - Message message = mailer.readOne(agentId); + MailerMessage message = mailer.readOne(agentId); if(message instanceof PlayMessage){ return; } @@ -77,7 +77,7 @@ protected void triggerNextAgent(){ mailer.send(nextAgentId, playMessage); audit.recordMessage(agentId, nextAgentId, playMessage); } - protected abstract void handleMessage(Message message); + protected abstract void handleMessage(MailerMessage message); protected abstract void pickStrategy(); protected abstract void sendDecisionToNeighbors(); diff --git a/src/Agent/BoSAgent/BoSAgent.java b/src/Agent/BoSAgent/BoSAgent.java index 9372a63..9530e28 100644 --- a/src/Agent/BoSAgent/BoSAgent.java +++ b/src/Agent/BoSAgent/BoSAgent.java @@ -31,7 +31,7 @@ public BoSAgentSex getAgentSex() { return agentSex; } - protected void handleMessage(Message message) { + protected void handleMessage(MailerMessage message) { if(message instanceof BoSMessage bosMessage){ BoSNeighborData neighborData = new BoSNeighborData(bosMessage.getStrategy(), bosMessage.getAgentSex()); neighborsData.put(bosMessage.getSenderId(), neighborData); diff --git a/src/Agent/PDAgent/PDAgent.java b/src/Agent/PDAgent/PDAgent.java index 711ff0a..fd6d0b3 100644 --- a/src/Agent/PDAgent/PDAgent.java +++ b/src/Agent/PDAgent/PDAgent.java @@ -4,7 +4,6 @@ import Audit.Audit; import Mailer.*; import Mailer.Messages.MailerMessage; -import Mailer.Messages.Message; import Mailer.Messages.PDMessage; import java.util.HashMap; @@ -28,7 +27,7 @@ public int getPersonalGain() { } @Override - protected void handleMessage(Message message) { + protected void handleMessage(MailerMessage message) { if(message instanceof PDMessage pdMessage){ neighborsStrategies.put(pdMessage.getSenderId(), pdMessage.getStrategy()); } diff --git a/src/Audit/Audit.java b/src/Audit/Audit.java index d55f922..3a8f66c 100644 --- a/src/Audit/Audit.java +++ b/src/Audit/Audit.java @@ -7,7 +7,7 @@ public class Audit { private final List recordedMessages = new ArrayList<>(); - public void recordMessage(int sender, int receiver, Message message) { + public void recordMessage(int sender, int receiver, MailerMessage message) { recordedMessages.add(new RecordedMessage(sender, receiver, message)); } diff --git a/src/Audit/RecordedMessage.java b/src/Audit/RecordedMessage.java index c657265..bd922f0 100644 --- a/src/Audit/RecordedMessage.java +++ b/src/Audit/RecordedMessage.java @@ -1,6 +1,6 @@ package Audit; -import Mailer.Messages.Message; +import Mailer.Messages.MailerMessage; -public record RecordedMessage(int sender, int receiver, Message message) { +public record RecordedMessage(int sender, int receiver, MailerMessage message) { } diff --git a/src/Mailer/Mailer.java b/src/Mailer/Mailer.java index 3e3cdfe..893abe2 100644 --- a/src/Mailer/Mailer.java +++ b/src/Mailer/Mailer.java @@ -3,38 +3,38 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; -import Mailer.Messages.Message; + +import Mailer.Messages.MailerMessage; /* * used for communication among agents */ public class Mailer { - private final HashMap> map = new HashMap<>(); + private final HashMap> map = new HashMap<>(); - public void send(int receiver, Message m) { - List l = map.get(receiver); + public void send(int receiver, MailerMessage m) { + List l = map.get(receiver); synchronized (l) { l.add(m); -// audit.recordMessage(m.getSenderId(), receiver, m); } } - public Message readOne(int receiver) { - List l = map.get(receiver); + public MailerMessage readOne(int receiver) { + List l = map.get(receiver); if (l.isEmpty()) { return null; } synchronized (l) { - Message m = l.get(0); + MailerMessage m = l.get(0); l.remove(0); return m; } } public void register(int agentId) { - List l= new ArrayList<>(); + List l= new ArrayList<>(); this.map.put(agentId, l); } } diff --git a/src/Mailer/Messages/MailerMessage.java b/src/Mailer/Messages/MailerMessage.java index be84110..c303196 100644 --- a/src/Mailer/Messages/MailerMessage.java +++ b/src/Mailer/Messages/MailerMessage.java @@ -1,6 +1,6 @@ package Mailer.Messages; -public abstract class MailerMessage implements Message{ +public abstract class MailerMessage { protected final int from; protected MailerMessage(int agentId){ diff --git a/src/Mailer/Messages/Message.java b/src/Mailer/Messages/Message.java deleted file mode 100644 index 36c2df2..0000000 --- a/src/Mailer/Messages/Message.java +++ /dev/null @@ -1,4 +0,0 @@ -package Mailer.Messages; - -public interface Message { -}