Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache variables #54

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/core/DTNHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import movement.MovementModel;
Expand Down Expand Up @@ -34,6 +35,7 @@ public class DTNHost implements Comparable<DTNHost> {
private List<MessageListener> msgListeners;
private List<MovementListener> movListeners;
private List<NetworkInterface> net;
private List<Connection> allConnections;
private ModuleCommunicationBus comBus;

static {
Expand All @@ -60,6 +62,7 @@ public DTNHost(List<MessageListener> msgLs,
this.address = getNextAddress();
this.name = groupId+address;
this.net = new ArrayList<NetworkInterface>();
this.allConnections = new ArrayList<Connection>();

for (NetworkInterface i : interf) {
NetworkInterface ni = i.replicate();
Expand Down Expand Up @@ -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<Connection> getConnections() {
List<Connection> lc = new ArrayList<Connection>();

for (NetworkInterface i : net) {
lc.addAll(i.getConnections());
}

return lc;
return Collections.unmodifiableList(allConnections);
}

/**
Expand Down
13 changes: 6 additions & 7 deletions src/routing/MessageRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -157,6 +159,7 @@ public void init(DTNHost host, List<MessageListener> mListeners) {
this.blacklistedMessages = new HashMap<String, Object>();
this.mListeners = mListeners;
this.host = host;
this.bufferOccupancy = 0;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}

Expand Down