简单实现GC编码

代码结构如下:

其中sensorNode代码有(第一次实现没有经验,以后肯定会改进的):

package Super;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.sound.sampled.LineListener;

public class Nodes {
    
    int id; //唯一标示节点
    Point point; 
    NodeStat nodeStat;
    List<Packets> space; // 存储空间,每个包存储的是节点的id
    List<SensorNodes> neigh; // 邻居集合
    int state; // 状态,因为可能哟多种状态,所以设置为了int
    int capture_size=10;
    int radius=2;//定义一个节点的邻居半径
    public Nodes() {   //定义一个无参的构造方法

    }
    public Nodes(int id,List<Packets> space,List<SensorNodes> neigh,int state) {
        super();
        this.id=id;
        this.neigh = neigh;
        this.space = space;
        this.state = state;
    }
    public void init(){
        NodeStat nodeStat=new NodeStat();
        Point point=new Point();
        List<SensorNodes> neigh=new ArrayList<SensorNodes>();
        List<Packets> space=new ArrayList<Packets>();
        this.space=space;
        this.neigh = neigh;
        this.point = point;
        this.nodeStat=nodeStat;
        this.On();
    }
    public void On(){
        this.state=1;
    }
    public void Off(){
        this.state=0;    
    }
    public int CheckOn(){
        return this.state;    
    }

//    搜索所有节点并且将活的邻居节点加入到自己的邻居内
    public void DetectNeighbors(List<SensorNodes> sensorNodes2){
        for(SensorNodes sensorNodes:sensorNodes2) {
            if (sensorNodes.state==1) {
                double _x = Math.abs(this.point.x_loc - sensorNodes.point.x_loc);
                double _y = Math.abs(this.point.y_loc - sensorNodes.point.y_loc);
                double distance=Math.sqrt(_x*_x+_y*_y);
                if (distance<radius && sensorNodes!=this) {
                    this.neigh.add(sensorNodes);
                }
            }
        }
    }
    //更新自己的邻居节点
    public void RefreshNeighbors(){
         Iterator<SensorNodes> iterator = this.neigh.iterator();
         while (iterator.hasNext()) {
             SensorNodes temp = iterator.next();
             if (0==temp.state) {
                    iterator.remove();
             }
         }
    }
//    返回节点的位置
    public Point GetPosition(){
        return this.point;
    }

    
//    随机选择一个包,有度限制,并且返回这个包(还未源节点的异或),保证传过来都是能用的,要是符合条件就返回结合后的包,要是不符合就返回原包
    public Packets RandSelectPacket(int degree){
        Random random = new Random();
        int maxRound=10;
        int i=0;
        while (i<maxRound) {
            i++;
            int random_pac=random.nextInt(this.space.size()-1);
            random_pac=random_pac+1;
                if (degree==1)  {
                    return this.space.get(random_pac);
                }
                else {
                    if(this.space.get(random_pac).getDegree()<degree && this.space.get(random_pac).returnIntersection(this.space.get(0))) {
                        return this.space.get(random_pac).addSensorNodes(this.space.get(0).id_sets.get(0));
                    }
                    else{
                        return this.space.get(random_pac);
                    }
                }
        }
        return null;
    }

//    随机选择一个包,没有度限制,并且返回这个包
    public Packets RERandSelectPacket(){
        Random random = new Random();
        int random_pac=random.nextInt(this.space.size());
        return this.space.get(random_pac);    
    }
//    广播时对邻居进行选择
    public List<SensorNodes> RandSelectNeigh(List<SensorNodes> allNodes) {
        List<SensorNodes> neigh=new ArrayList<SensorNodes>();
        Random random=new Random();
        int neighNum=random.nextInt(15);
        for(int i=0;i<neighNum;i++) {
            int neighid=random.nextInt(allNodes.size());
            if (allNodes.get(neighid).state==1&&allNodes.get(neighid).id!=this.id) {
                neigh.add(allNodes.get(neighid));
            }
        }
        return neigh;
        
    }
//    随机选择一个邻居
    public SensorNodes RandSelectSensorNodes(){
        Random random = new Random();
        int maxRound=100;
        int i=0;
        while(true) {
            if (this.neigh.size()==0) {
                System.out.println("没有邻居了");
                return null;
            }
            else {
                int random_neigh=random.nextInt(this.neigh.size());
                SensorNodes sensorNodes=this.neigh.get(random_neigh);
                if (sensorNodes.state==1) {
                    return sensorNodes;
                }
                else {
                    i++;
                    if (i>maxRound) {
                        return null;
                    }
                    else {
                        continue;
                    }
                }
            }
        }
    }
//    单播方式,暂时考虑的是随机选出一个邻居节点,然后把自己的一个包与邻居的任一一个包进行结合,并且最后返回是这个邻居节点
    public void Unicast(int degree){
        Packets packetsSend=this.RandSelectPacket(degree);
        SensorNodes sensorRev=this.RandSelectSensorNodes();
        if (sensorRev==null) {
            return;
        }
        else {
            Packets packetsRev=sensorRev.RandSelectPacket(degree);
            int indexSend=this.space.indexOf(packetsSend);
            int indexRev=sensorRev.space.indexOf(packetsRev);
            if (packetsSend==null) {
                System.out.println("本次暂未交换数据");
                return;
            }
            else {
                if (degree==1) {
                    sensorRev.space.set(indexRev, packetsSend);
                    this.space.set(indexSend,packetsRev);
//                    System.out.println(degree+"度包成功交换一次");
                    }
                else {
                    sensorRev.space.set(indexRev, packetsSend);
                    this.space.set(indexSend,packetsRev);
//                    System.out.println(degree+"度包成功交换一次");
                    }
                }
        }
    }
    
    
//    广播方式,在选择的邻居列表内进行单播
    public SensorNodes Broadcast(int degree,List<SensorNodes> allNodes){
        Packets packetsSend=this.RandSelectPacket(degree);
        List<SensorNodes> neigh=this.RandSelectNeigh(allNodes);
        for(SensorNodes sensorNodes:neigh) {
            Packets packetsRev=sensorNodes.RERandSelectPacket();
            Packets newpacketRev=packetsRev.combin_pac(packetsSend);
            sensorNodes.space.remove(packetsRev);
            sensorNodes.space.add(newpacketRev);
            this.nodeStat.nsend_+=1;
            sensorNodes.nodeStat.nrecv_+=1;
            return sensorNodes;
        }
        return null;
        
//        
//        Random random = new Random();
//        int random_neigh=random.nextInt(this.neigh.size());    
//        SensorNodes sensorNodes=this.neigh.get(random_neigh);

    }
    
//    发送包
    public Packets Sendpkt(int degree){
        return RandSelectPacket(degree);
    }

//    接收包
    public void Recvpkt(){
        
    }

}
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yann.bai

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值