-
Notifications
You must be signed in to change notification settings - Fork 195
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
Contact-Based Routing (CBR) #45
Open
SujataPal
wants to merge
2
commits into
akeranen:master
Choose a base branch
from
SujataPal:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
/* CBR router | ||
* CBR.java | ||
* | ||
* @Author Sujata Pal | ||
* | ||
* Created on August 02, 2014 | ||
* Paper name: "Contact-Based Routing in DTNs" published in | ||
* ACM IMCOM 2015 | ||
*/ | ||
|
||
package routing; | ||
|
||
import core.Connection; | ||
import core.DTNHost; | ||
import core.Message; | ||
import core.Settings; | ||
import core.SimClock; | ||
import core.SimScenario; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import util.Tuple; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Random; | ||
import java.util.*; | ||
import core.*; | ||
import core.ConnectionListener; | ||
|
||
|
||
public class CBR extends ActiveRouter { | ||
|
||
|
||
protected Map<Integer, ArrayList<Integer>> nodesInf; | ||
protected ArrayList<Integer> nodeConnections; | ||
|
||
|
||
/** | ||
* Constructor. Creates a new message router based on the settings in | ||
* the given Settings object. | ||
* @param s The settings object | ||
*/ | ||
public CBR(Settings s) { | ||
super(s); | ||
init(); | ||
|
||
} | ||
|
||
/** | ||
* Copy constructor. | ||
* @param r The router prototype where setting values are copied from | ||
*/ | ||
protected CBR(CBR r) { | ||
super(r); | ||
init(); | ||
|
||
} | ||
|
||
private void init() { | ||
|
||
this.nodesInf = new TreeMap<Integer, ArrayList<Integer>>(); | ||
this.nodeConnections = new ArrayList<Integer> (); | ||
} | ||
|
||
@Override | ||
public void changedConnection(Connection con) { | ||
if (con.isUp()) { | ||
|
||
|
||
DTNHost self = this.getHost(); | ||
DTNHost otherNode = con.getOtherNode(self); | ||
|
||
|
||
if (! this.nodesInf.containsKey(otherNode.getAddress())) { | ||
ArrayList<Integer> items = new ArrayList<Integer>(); | ||
items.add(1); | ||
|
||
this.nodesInf.put(otherNode.getAddress(), items); | ||
} | ||
|
||
if (this.nodesInf.containsKey(otherNode.getAddress())) { | ||
ArrayList<Integer> items = this.nodesInf.get(otherNode.getAddress()); | ||
|
||
items.set(0, items.get(0) + 1); | ||
|
||
this.nodesInf.put(otherNode.getAddress(), items); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
@Override | ||
protected int checkReceiving(Message m, DTNHost from) { | ||
int recvCheck = super.checkReceiving(m, from); | ||
|
||
if (recvCheck == RCV_OK) { | ||
/* don't accept a message that has already traversed this node */ | ||
if (m.getHops().contains(getHost())) { | ||
recvCheck = DENIED_OLD; | ||
} | ||
} | ||
|
||
return recvCheck; | ||
} | ||
|
||
@Override | ||
public void update() { | ||
super.update(); | ||
//if(SimClock.getIntTime() == 43200) | ||
//System.out.println("[" + this.getHost() + "] list: " + this.nodesInf + "\n"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
if (isTransferring() || !canStartTransfer()) { | ||
return; | ||
} | ||
|
||
if (exchangeDeliverableMessages() != null) { | ||
return; | ||
} | ||
|
||
tryAllMessagesToAllConnections(); | ||
} | ||
|
||
|
||
@Override | ||
protected Connection tryMessagesToConnections(List<Message> messages, | ||
List<Connection> connections) { | ||
|
||
for (Message m : messages) { | ||
|
||
DTNHost msgDst = m.getTo(); | ||
DTNHost self = this.getHost(); | ||
int maxEnc = 0; | ||
DTNHost maxEncHost; | ||
Connection conEnc = connections.get(0); | ||
int k=0; | ||
for (int i=0, n=connections.size(); i<n; i++) { | ||
Connection con = connections.get(i); | ||
DTNHost otherNode = con.getOtherNode(self); | ||
|
||
CBR otherRouter = (CBR) otherNode.getRouter(); | ||
|
||
|
||
//if other end of the connection contain (meet with the) destination node of the current msg | ||
if (otherRouter.nodesInf.containsKey(msgDst.getAddress())) { | ||
ArrayList<Integer> items = otherRouter.nodesInf.get(msgDst.getAddress()); | ||
|
||
int encounter = items.get(0); | ||
if (encounter > maxEnc) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style issue: opening curly brackets on the same line in ifs |
||
{ | ||
maxEnc = encounter; | ||
maxEncHost = otherNode; | ||
conEnc = con; | ||
} | ||
|
||
} | ||
|
||
Random r = new Random(); | ||
k = r.nextInt(n); | ||
|
||
} | ||
if(maxEnc == 0) | ||
{ | ||
conEnc = connections.get(k); | ||
} | ||
int retVal = startTransfer(m, conEnc); | ||
if (retVal == RCV_OK) { | ||
return conEnc; // accepted a message, don't try others | ||
} | ||
else if (retVal > 0) { | ||
return null; // should try later -> don't bother trying others | ||
} | ||
} | ||
|
||
|
||
return null; | ||
} | ||
|
||
|
||
|
||
@Override | ||
public CBR replicate() { | ||
return new CBR(this); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to clean up the code before merging. And there's actually the core.Debug.p method for debug prints.