Skip to content

Commit

Permalink
clean up mailer messages
Browse files Browse the repository at this point in the history
  • Loading branch information
danny-prodbase committed Sep 3, 2023
1 parent 7a68edb commit 4f3627d
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 23 deletions.
6 changes: 3 additions & 3 deletions src/Agent/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/Agent/BoSAgent/BoSAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/Agent/PDAgent/PDAgent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}
Expand Down
2 changes: 1 addition & 1 deletion src/Audit/Audit.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class Audit {
private final List<RecordedMessage> 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));
}

Expand Down
4 changes: 2 additions & 2 deletions src/Audit/RecordedMessage.java
Original file line number Diff line number Diff line change
@@ -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) {
}
18 changes: 9 additions & 9 deletions src/Mailer/Mailer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer, List<Message>> map = new HashMap<>();
private final HashMap<Integer, List<MailerMessage>> map = new HashMap<>();

public void send(int receiver, Message m) {
List<Message> l = map.get(receiver);
public void send(int receiver, MailerMessage m) {
List<MailerMessage> l = map.get(receiver);

synchronized (l) {
l.add(m);
// audit.recordMessage(m.getSenderId(), receiver, m);
}
}

public Message readOne(int receiver) {
List<Message> l = map.get(receiver);
public MailerMessage readOne(int receiver) {
List<MailerMessage> 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<Message> l= new ArrayList<>();
List<MailerMessage> l= new ArrayList<>();
this.map.put(agentId, l);
}
}
2 changes: 1 addition & 1 deletion src/Mailer/Messages/MailerMessage.java
Original file line number Diff line number Diff line change
@@ -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){
Expand Down
4 changes: 0 additions & 4 deletions src/Mailer/Messages/Message.java

This file was deleted.

0 comments on commit 4f3627d

Please sign in to comment.