csdn的博主ymaym写了许多代码分析,但不知为何没有了更新,我也试着写写,不足之处请批评!
ProphetRouter类
该类是个具体类,是ActiveRouter的一个子类,实现prophet路由算法
数据域:
publicstatic final double P_INIT = 0.75; 预先设置的概率参数
publicstatic final double DEFAULT_BETA = 0.25;默认传递因子
publicstatic final double GAMMA = 0.98; 衰减因子
public static final String PROPHET_NS ="ProphetRouter";
public static final StringSECONDS_IN_UNIT_S ="secondsInTimeUnit";
public static final String BETA_S ="beta";
private int secondsInTimeUnit;
private double beta;
private Map
privatedouble lastAgeUpdate; 最后一次投递预测概率衰减更新时间
publicProphetRouter(Settings s) 构造函数ProphetRouter(Settings s)由配置文件创建ProphetRouter对象。
复制ProphetRouter(Settingss) 构造函数,参数为r
protectedProphetRouter(ProphetRouter r) {
super(r);
this.secondsInTimeUnit= r.secondsInTimeUnit;
this.beta= r.beta;
initPreds();
}
/**
* Initializes predictability hash
*/
初始化预测概率
privatevoid initPreds()
@Override
publicvoid changedConnection(Connection con)
privatevoid updateDeliveryPredFor(DTNHost host) 当节点相遇时,更新该节点相遇概率P(a,b) = P(a,b)_old + (1 - P(a,b)_old) * P_INIT
publicdouble getPredFor(DTNHost host) 得到主机的投递预测概率,主机不存在返回0
/**
* Updates transitive (A->B->C) deliverypredictions.
* P(a,c) = P(a,c)_old + (1 -P(a,c)_old) * P(a,b) * P(b,c) * BETA
*
* @param host The B host who we just met
*/
privatevoid updateTransitivePreds(DTNHost host) 通过中间节点转发来完成消息投递的概率
privatevoid ageDeliveryPreds()投递预测概率衰减后的值
privateMap
ageDeliveryPreds();// make sure the aging is done
returnthis.preds;
}
@Override
publicvoid update()
/**
* Tries to send all other messages to allconnected hosts ordered by
* their delivery probability
* @return The return value of {@link #tryMessagesForConnected(List)}
*/
尝试向元组列表中对应的connections将其他消息按照投递概率发送messages,
privateTuple
List
newArrayList
Collection
/*for all connected hosts collect all messages that have a higher
probability of delivery by the other host */
for(Connection con : getConnections()) {
DTNHostother = con.getOtherNode(getHost());
ProphetRouterothRouter = (ProphetRouter)other.getRouter();
if(othRouter.isTransferring()) {
continue;// skip hosts that are transferring
}
for(Message m : msgCollection) {
if(othRouter.hasMessage(m.getId())) {
continue;// skip messages that the other one has
}
if(othRouter.getPredFor(m.getTo()) > getPredFor(m.getTo())) {
//the other node has higher probability of delivery
messages.add(newTuple
}
}
}
if(messages.size() == 0) {
returnnull;
}
//sort the message-connection tuples
Collections.sort(messages,new TupleComparator());
returntryMessagesForConnected(messages); //try to send messages
}
/**
* Comparator for Message-Connection-Tuplesthat orders the tuples by
* their delivery probability by the host onthe other side of the
* connection (GRTRMax)
*/
这个类是个比较器,用来比较预测概率的,通过比较决定是消息投递的优先权
privateclass TupleComparator implements Comparator
@Override
publicRoutingInfo getRoutingInfo()
@Override
publicMessageRouter replicate() {
ProphetRouterr = new ProphetRouter(this);
returnr;
}