All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.sdnwiselab.sdnwise.packet.ReportPacket Maven / Gradle / Ivy

/*
 * Copyright (C) 2015 SDN-WISE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package com.github.sdnwiselab.sdnwise.packet;

import com.github.sdnwiselab.sdnwise.util.NodeAddress;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author SDN-WISE
 */
public class ReportPacket extends BeaconPacket {

    public final static byte SDN_WISE_MAX_NEIG = 35;
     
    
    public ReportPacket(byte[] data) {
        super(data);
    }

    public ReportPacket() {
        super();
        this.setType(SDN_WISE_REPORT);
    }

    public ReportPacket(int[] data) {
        super(data);
    }

    /**
     * Returns the number of neigbors of the source node. This is a report
     * message field only.
     *
     * @return the number of neigbors of the source node.
     */
    public int getNeigh() {
        return this.getPayloadAt(2) & 0xFF;
    }

    public NetworkPacket setNeigh(int value) {
        if (value <= SDN_WISE_MAX_NEIG){
            this.setPayloadAt((byte)value, 2);
            this.setPayloadSize((byte)(3 + value * 3));
        }else{
            throw new IllegalArgumentException("Too many neighbors");
        }
        return this;
    }

        /**
     * Return the NodeAddress of the i-th node in the neigbor list. This is a
     * report message field only.
     *
     * @param i the i-th node in the neigbors list
     * @return the NodeAddress of the i-th node in the neigbors list
     */
    public NodeAddress getNeighbourAddress(int i) {
        if (i <= SDN_WISE_MAX_NEIG){
        return new NodeAddress(
                this.getPayloadAt(3 + i*3),
                this.getPayloadAt(4 + (i*3)));
        } else {
            throw new IllegalArgumentException("Index exceeds max number of neighbors");
        }
    }

    public ReportPacket setNeighbourAddressAt(NodeAddress addr, int i) {
        if (i <= SDN_WISE_MAX_NEIG){
            this.setPayloadAt(addr.getHigh(),(byte)(3+ i*3));
            this.setPayloadAt(addr.getLow(),(byte)(4 + (i*3)));
            if (this.getNeigh() < i){
                this.setNeigh(i);
            }
            return this;
        } else {
            throw new IllegalArgumentException("Index exceeds max number of neighbors");
        }
    }
    
    /**
     * Return the rssi value between the i-th node in the neigbor list and the
     * source node. This is a report message field only.
     *
     * @param i the i-th node in the neigbors list
     * @return the rssi value
     */
    public int getNeighbourWeight(int i) {
        if (i <= SDN_WISE_MAX_NEIG){
            return this.getPayloadAt(5 + (i*3));
        } else {
            throw new IllegalArgumentException("Index exceeds max number of neighbors");
        }
    }

    public ReportPacket setNeighbourWeightAt(byte value, int i) {
        if (i <= SDN_WISE_MAX_NEIG){
            this.setPayloadAt(value, (byte)(5 + i*3));
            if (this.getNeigh() < i){
                this.setNeigh(i);
            }
            return this;
        } else {
            throw new IllegalArgumentException("Index exceeds max number of neighbors");
        }
    }
    
       /**
     * Gets the list of Neighbors.
     *
     * @return an HashMap contening the neighbors and their weights
     */
    public HashMap getNeighborsHashMap() {
        HashMap map = new HashMap<>();
        int nNeig = this.getNeigh();
        for (int i = 0; i < nNeig; i++) {
            map.put(this.getNeighbourAddress(i),
                    (byte)this.getNeighbourWeight(i));
        }
        return map;
    }

    /**
     * Sets the list of Neighbors.
     *
     * @param map the map of neighbors to be set
     * @return
     */
    public NetworkPacket setNeighborsHashMap(HashMap map) {
        int i = 0;
        for (Map.Entry entry : map.entrySet()) {
            this.setNeighbourAddressAt(entry.getKey(),i);
            this.setNeighbourWeightAt(entry.getValue(), i);
            i++;
        }
        this.setNeigh((byte) map.size());
        return this;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy