⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nodetimer.java

📁 java B++树 一个很好的算法来实现这些问题 推荐!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        node.nodeState = ParentNode.OTHER_STATUS;
        System.out.println( "RMTimer已经结束" );
        node.m_RHELLOList.clear(); //---------------在这里清除所有的RHELLOList内容
    }

    //利用m_RHELLOList的NeighbourList对邻接表进行重构--用于节点不是孤点的情况--复杂处理
    private synchronized void rebuildGroupAdjComplex() {
        //由于是重构组邻接表,所以要先将其清空
        node.groupAdjacencyList.clear();
        NeighbourList nebrList;
        MddbPacket pack;
        for( int i = 0; i < node.m_RHELLOList.size(); i++ ) {
            pack = ( MddbPacket )node.m_RHELLOList.get( i );
            nebrList = nebrListInRHelloPack( pack );
            //如果RHELLO发送者是自己的成员,该成员就是LocalGateway
            if( node.mbrList.contains( new Long( pack.getSrcId() ) ) ) {
                for(int j = 0; j < nebrList.getSize(); j++)
                {
                    //获得邻居的邻接表中邻居的MasterId
                    long nebrNebrMasterId = nebrList.getNebrMasterId(i);
                    if( nebrNebrMasterId == node.ID )//nebr的nebr是自己的成员
                        continue;//对于自己的成员不予理睬

                    //如果到另一个组的邻接信息已存在,则跳过 ,下面2表示是本地nebrNebrMaster
                    if( node.groupAdjacencyList.containsGatewayId(pack.getSrcId(),2) != -1 )
                       continue;
                    //将以前没加入过的加入到新构建的邻接表中
                    node.groupAdjacencyList.add(pack.getSrcId(),
                                                nebrList.getNebrId(i),
                                                nebrNebrMasterId);
                }
            }else{//如果RHELLO发送者不是自己的成员
                //获得RHELLO包中的发送节点的Id,MasterId,与发送结点的距离
                long[] senderInfo = getInfo(pack);
                //若与该组的邻接信息不存在,则节点自己就是gateway
                if( node.groupAdjacencyList.containsGatewayId(senderInfo[1],2) == -1)
                    node.groupAdjacencyList.add(node.ID,senderInfo[0],senderInfo[1]);
            }
        }
    }

    //返回NeighbourList
    private NeighbourList nebrListInRHelloPack( MddbPacket packet ) {
        NeighbourList nebrTemp = new NeighbourList();
        StringTokenizer st = new StringTokenizer( packet.getData(), "@" );
        st.nextToken(); //跳过MasterId
        st.nextToken();
        st.nextToken(); //跳过成员数和与HELLO发送者的距离
        while( st.hasMoreTokens() ) {
            StringTokenizer cSt = new StringTokenizer( st.nextToken(), ":" );
            nebrTemp.add( Long.parseLong( cSt.nextToken() ),
                          Long.parseLong( cSt.nextToken() ),
                          new Point( Integer.parseInt( cSt.nextToken() ),
                                     Integer.parseInt( cSt.nextToken() ) ) );
        }
        return nebrTemp;
    }

    //利用m_RHELLOList的NeighbourList对邻接表进行重构--用于节点是孤点的情况--处理比较简单
    private synchronized void rebuildGroupAdjListSimple() {
        HashMap con = new HashMap(); //用于存组号和最短距离
        //HashMap neiList;
        MddbPacket mp;
        long[] info;
        for( int i = 0; i < node.m_RHELLOList.size(); i++ ) {
            mp = ( MddbPacket )node.m_RHELLOList.get( i );
            info = getInfo( mp ); //获得RHELLO包中的发送节点的Id,MasterId,与发送结点距离
            if( !con.containsKey( new Long( info[1] ) ) ) {
                con.put( new Long( info[1] ), info );
            }
            else {
                if( getDistance( mp.getSrcAddr() ) <
                    ( ( long[] )con.get( new Long( info[1] ) ) )[2] ) {
                    con.remove( new Long( info[1] ) );
                    con.put( new Long( info[1] ), info );
                }
            }
        }
        //由于是重构组邻接表,所以要先将其清空
        node.groupAdjacencyList.clear();
        //将得到的表添加到组邻接表中
        Set eSet = con.entrySet();
        Iterator iter = eSet.iterator();
        while( iter.hasNext() ) {
            Map.Entry entry = ( Map.Entry )iter.next();
            long[] temp = ( long[] )entry.getValue();
            node.getGroupAdjacencyList().add( node.ID, temp[0], temp[1] );
        }
    }

    //获得RHELLO包中的发送节点的Id,MasterId,与发送结点的距离
    private long[] getInfo( MddbPacket rHelloPack ) {
        long[] info = new long[3];
        StringTokenizer st = new StringTokenizer( rHelloPack.getData(), "@" );
        info[0] = rHelloPack.getSrcId(); //装入发送节点的Id,以便和邻接表格式对齐
        info[1] = Long.parseLong( st.nextToken() ); //获得masterId
        st.nextToken(); //成员数没用,跳过
        info[2] = Long.parseLong( st.nextToken() ); //获得与HELLO发送者间的距离
        return info;
    }

    //得到本节点和给定的邻居节点的距离
    private int getDistance( Point neighbourPos ) {
        int x1 = ( int )node.position.getX();
        int y1 = ( int )node.position.getY();
        int x2 = ( int )neighbourPos.getX();
        int y2 = ( int )neighbourPos.getY();
        return( int )Math.sqrt( ( x1 - x2 ) * ( x1 - x2 ) +
                                ( y1 - y2 ) * ( y1 - y2 ) );
    }

    /////////////////////////////////////////////////////////////////////

    //发送MI包给所有的邻居
    private void sendMIPackToAllNeighbour() {
        //获得MasterId和NeighbourList------------------------------------
        StringBuffer sb = new StringBuffer();
        sb.append( node.masterId + "@" );
        Object[] nebrTemp;
        synchronized( node.nebrList ){
            for( int i = 0; i < node.nebrList.getSize(); i++ ) {
                nebrTemp = node.nebrList.get( i );
                sb.append( nebrTemp[0] + ":" );
                sb.append( nebrTemp[1] + ":" );
                sb.append( ( int ) ( ( Point )nebrTemp[2] ).getX() + ":" );
                sb.append( ( int ) ( ( Point )nebrTemp[2] ).getY() + ":" + "@" );
            }
            String miData = sb.toString();
            //---------------------------------------------------------------
            ArrayList temp = new ArrayList();
            temp.add( node.position );
            long neighbourId;

            for( int i = 0; i < node.nebrList.getSize(); i++ ) {
                neighbourId = node.nebrList.getNebrId( i );
                temp.add( node.getLocation( neighbourId ) );
                MddbPacket miPacket = new MddbPacket(
                    new Point( node.position ),
                    node.ID,
                    new Point( node.getLocation( neighbourId ) ),
                    node.getDistance( node.ID, neighbourId ),
                    temp,
                    MddbPacket.NET_MI,
                    node.timeStamp );
                miPacket.setData( miData );
                try {
                    node.pkgSender.sendNetPacket( miPacket,
                                                  MddbPacket.FRAME_GROUP_PKG );
                }
                catch( IOException ioe ) {
                    System.err.println( "包发送失败--PacketReceiver中" );
                    ioe.printStackTrace();
                    return;
                }
                temp.remove( 1 ); //更新目的节点
            }
        }
    }

    //--------------------------------------------------------------------------
    public void OnOverDelayTime() {
        synchronized(node.m_DelayQueue){
            for( int i = 0; i < node.m_DelayQueue.size(); i++ ) {
                MddbPacket mpack = ( MddbPacket )node.m_DelayQueue.get( i );
                if( mpack.getTimeStamp() == node.timeStamp &&
                    node.nodeState == ParentNode.RM_STATUS ) {
                    sendRMPacket( mpack );
                }
            }
            node.m_DelayQueue.clear();
        }
        System.out.println( "DelayTimer已经结束" );
    }

    //--------------------------------------------------------------------------
    public synchronized void actionPerformed( ActionEvent e ) {
        System.out.println( "节点" + node.ID + "第" + times + "次时间处理函数开始" );
        if( ( updatePositionTimer != null ) &&
            e.getSource().toString().equals( updatePositionTimer.toString() ) ) {
            OnOverUpdatePositionTime();
        }
        else if( ( delayTimer != null ) &&
                 e.getSource().toString().equals( delayTimer.toString() ) ) {
            OnOverDelayTime();
        }
        else if( ( helloBroadcastTimer != null ) &&
                 e.getSource().toString().equals( helloBroadcastTimer.toString() ) ) {
            OnOverHelloBroadcastTime();
        }
        else if( ( rmTimer != null ) &&
                 e.getSource().toString().equals( rmTimer.toString() ) ) {
            OnOverRMTime();
        }
        System.out.println( "节点" + node.ID + "第" + ( times++ ) + "次时间处理函数结束" );
    }

    public void startUpdatePositionTimer() {
        updatePositionTimer = new javax.swing.Timer( updatePositionTime, this );
        updatePositionTimer.setRepeats( true );
        updatePositionTimer.start();
    }

    public void startDelayTimer() {
        delayTimer = new javax.swing.Timer( delayTime, this );
        delayTimer.setRepeats( true );
        delayTimer.start();
    }

    public void startRMTimer() {
        if( rmTimer != null ) {
            if( rmTimer.isRunning() ) {
                rmTimer.stop();
            }
            rmTimer = null;
        }
        rmTimer = new javax.swing.Timer( delayTime, this );
        rmTimer.setRepeats( false );
        rmTimer.start();
    }

    public void startHelloBroadcastTimer() {
        if( helloBroadcastTimer != null ) {
            if( helloBroadcastTimer.isRunning() ) {
                helloBroadcastTimer.stop();
            }
            helloBroadcastTimer = null;
        }
        helloBroadcastTimer = new javax.swing.Timer( helloBroadcastTime, this );
        helloBroadcastTimer.setRepeats( false );
        helloBroadcastTimer.start();
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -