ONE源代码分析——router之Prophetrouter

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<DTNHost, Double> preds; 主机对应的预测概率值

         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.

          * <CODE>P(a,c) = P(a,c)_old + (1 -P(a,c)_old) * P(a,b) * P(b,c) * BETA

          * </CODE>

          * @param host The B host who we just met

          */

         privatevoid updateTransitivePreds(DTNHost host) 通过中间节点转发来完成消息投递的概率

   

         privatevoid ageDeliveryPreds()投递预测概率衰减后的值

        

         privateMap<DTNHost, Double> getDeliveryPreds() {

                   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<Message, Connection> tryOtherMessages() {

                   List<Tuple<Message,Connection>> messages =

                            newArrayList<Tuple<Message, Connection>>();

        

                   Collection<Message>msgCollection = getMessageCollection();

                  

                   /*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<Message, Connection>(m,con));

                                     }

                            }                         

                   }

                  

                   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  <Tuple<Message, Connection>>

        

         @Override

         publicRoutingInfo getRoutingInfo()

         

         @Override

         publicMessageRouter replicate() {

                   ProphetRouterr = new ProphetRouter(this);

                   returnr;

         }

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值