diff --git a/src/core/DTNHost.java b/src/core/DTNHost.java index 258be0450..502c5b48b 100644 --- a/src/core/DTNHost.java +++ b/src/core/DTNHost.java @@ -6,6 +6,7 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.List; import movement.MovementModel; @@ -34,6 +35,7 @@ public class DTNHost implements Comparable { private List msgListeners; private List movListeners; private List net; + private List allConnections; private ModuleCommunicationBus comBus; static { @@ -60,6 +62,7 @@ public DTNHost(List msgLs, this.address = getNextAddress(); this.name = groupId+address; this.net = new ArrayList(); + this.allConnections = new ArrayList(); for (NetworkInterface i : interf) { NetworkInterface ni = i.replicate(); @@ -165,25 +168,21 @@ public ModuleCommunicationBus getComBus() { * @param con The connection object whose state changed */ public void connectionUp(Connection con) { + this.allConnections.add(con); this.router.changedConnection(con); } public void connectionDown(Connection con) { + this.allConnections.remove(con); this.router.changedConnection(con); } /** - * Returns a copy of the list of connections this host has with other hosts - * @return a copy of the list of connections this host has with other hosts + * Returns an immutable list of connections this host has with other hosts + * @return an immutable list of connections this host has with other hosts */ public List getConnections() { - List lc = new ArrayList(); - - for (NetworkInterface i : net) { - lc.addAll(i.getConnections()); - } - - return lc; + return Collections.unmodifiableList(allConnections); } /** diff --git a/src/routing/MessageRouter.java b/src/routing/MessageRouter.java index 4d21233a1..239848c1e 100644 --- a/src/routing/MessageRouter.java +++ b/src/routing/MessageRouter.java @@ -95,6 +95,8 @@ public abstract class MessageRouter { private DTNHost host; /** size of the buffer */ private long bufferSize; + /** current occupancy of the buffer */ + private long bufferOccupancy; /** TTL for all messages */ protected int msgTtl; /** Queue mode for sending messages */ @@ -157,6 +159,7 @@ public void init(DTNHost host, List mListeners) { this.blacklistedMessages = new HashMap(); this.mListeners = mListeners; this.host = host; + this.bufferOccupancy = 0; } /** @@ -274,17 +277,11 @@ public long getBufferSize() { * size isn't defined) */ public long getFreeBufferSize() { - long occupancy = 0; - if (this.getBufferSize() == Integer.MAX_VALUE) { return Integer.MAX_VALUE; } - for (Message m : getMessageCollection()) { - occupancy += m.getSize(); - } - - return this.getBufferSize() - occupancy; + return this.getBufferSize() - this.bufferOccupancy; } /** @@ -438,6 +435,7 @@ protected boolean isIncomingMessage(String id) { */ protected void addToMessages(Message m, boolean newMessage) { this.messages.put(m.getId(), m); + this.bufferOccupancy += m.getSize(); if (newMessage) { for (MessageListener ml : this.mListeners) { @@ -453,6 +451,7 @@ protected void addToMessages(Message m, boolean newMessage) { */ protected Message removeFromMessages(String id) { Message m = this.messages.remove(id); + this.bufferOccupancy -= m.getSize(); return m; }