From 1d62db82a9fd857915e02f44b1c4c496a1590231 Mon Sep 17 00:00:00 2001 From: Sujata Pal Date: Sun, 26 Feb 2017 01:02:55 +0530 Subject: [PATCH 1/2] Add code for Contact-Based Routing (CBR) --- src/routing/CBR.java | 184 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 src/routing/CBR.java diff --git a/src/routing/CBR.java b/src/routing/CBR.java new file mode 100644 index 000000000..5f92907fc --- /dev/null +++ b/src/routing/CBR.java @@ -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> nodesInf; + protected ArrayList 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>(); + this.nodeConnections = new ArrayList (); + } + + @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 items = new ArrayList(); + items.add(1); + + this.nodesInf.put(otherNode.getAddress(), items); + } + + if (this.nodesInf.containsKey(otherNode.getAddress())) { + ArrayList 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"); + if (isTransferring() || !canStartTransfer()) { + return; + } + + if (exchangeDeliverableMessages() != null) { + return; + } + + tryAllMessagesToAllConnections(); + } + + + @Override + protected Connection tryMessagesToConnections(List messages, + List 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 items = otherRouter.nodesInf.get(msgDst.getAddress()); + + int encounter = items.get(0); + if (encounter > maxEnc) + { + 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); + } + +} From e2107358d269229c247ece997fe5426a31314023 Mon Sep 17 00:00:00 2001 From: Sujata Pal Date: Thu, 20 Apr 2017 10:49:53 +0530 Subject: [PATCH 2/2] Update CBR.java --- src/routing/CBR.java | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/routing/CBR.java b/src/routing/CBR.java index 5f92907fc..77d44a009 100644 --- a/src/routing/CBR.java +++ b/src/routing/CBR.java @@ -1,5 +1,5 @@ -/* CBR router - * CBR.java +/* CBRRouter router + * CBRRouter.java * * @Author Sujata Pal * @@ -27,7 +27,7 @@ import core.ConnectionListener; -public class CBR extends ActiveRouter { +public class CBRRouter extends ActiveRouter { protected Map> nodesInf; @@ -39,7 +39,7 @@ public class CBR extends ActiveRouter { * the given Settings object. * @param s The settings object */ - public CBR(Settings s) { + public CBRRouter(Settings s) { super(s); init(); @@ -49,7 +49,7 @@ public CBR(Settings s) { * Copy constructor. * @param r The router prototype where setting values are copied from */ - protected CBR(CBR r) { + protected CBRRouter(CBRRouter r) { super(r); init(); @@ -106,8 +106,7 @@ protected int checkReceiving(Message m, DTNHost from) { @Override public void update() { super.update(); - //if(SimClock.getIntTime() == 43200) - //System.out.println("[" + this.getHost() + "] list: " + this.nodesInf + "\n"); + if (isTransferring() || !canStartTransfer()) { return; } @@ -136,7 +135,7 @@ protected Connection tryMessagesToConnections(List messages, Connection con = connections.get(i); DTNHost otherNode = con.getOtherNode(self); - CBR otherRouter = (CBR) otherNode.getRouter(); + CBRRouter otherRouter = (CBRRouter) otherNode.getRouter(); //if other end of the connection contain (meet with the) destination node of the current msg @@ -144,21 +143,18 @@ protected Connection tryMessagesToConnections(List messages, ArrayList items = otherRouter.nodesInf.get(msgDst.getAddress()); int encounter = items.get(0); - if (encounter > maxEnc) - { + if (encounter > maxEnc) { maxEnc = encounter; maxEncHost = otherNode; conEnc = con; } - } Random r = new Random(); k = r.nextInt(n); } - if(maxEnc == 0) - { + if(maxEnc == 0) { conEnc = connections.get(k); } int retVal = startTransfer(m, conEnc); @@ -170,15 +166,14 @@ else if (retVal > 0) { } } - return null; } @Override - public CBR replicate() { - return new CBR(this); + public CBRRouter replicate() { + return new CBRRouter(this); } }