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.Arrays;
import java.util.HashMap;
import java.util.Map;

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

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

    public ReportPacket() {
    }

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

    
    
    /**
     * Returns the number of hops between the source node and the sink. This is
     * a beacon/report message field only.
     *
     * @return the number of hops between the source node and the sink
     */
    public int getDist() {
        return data[10] & 0xFF;
    }

    public NetworkPacket setDist(byte value) {
        data[10] = value;
        return this;
    }

    /**
     * Returns an extimation of the residual charge of the batteries of the
     * node. This is a beacon/report message field only.
     *
     * @return an extimation of the residual charge of the batteries of the node
     */
    public int getBatt() {
        return data[11] & 0xFF;
    }

    public NetworkPacket setBatt(byte value) {
        data[11] = value;
        return this;
    }

    /**
     * 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 data[12] & 0xFF;
    }

    public NetworkPacket setNeigh(byte value) {
        data[12] = value;
        return this;
    }

    /**
     * Returns the payload of the packet as a byte array.
     *
     * @return the payload of the packet
     */
    @Override
    public byte[] getPayload() {
        return Arrays.copyOfRange(data, 13, this.getLen());
    }

    @Override
    public NetworkPacket setPayload(byte[] payload) {
        System.arraycopy(payload, 0, data, 13, payload.length);
        this.setLen((byte) (payload.length + 13));
        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 (getType() == 2) {
            int j = (i * 3) + 14;
            if (j < this.getLen()) {
                return new NodeAddress(data[j - 1], data[j]);
            }
        }
        return null;
    }

    /**
     * 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 (getType() == 2) {
            int j = (i * 3) + 15;
            if (j < this.getLen()) {
                return data[j] & 0xFF;
            }
        }
        return -1;
    }

       /**
     * 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) {
        byte[] newData = new byte[map.size() * 3];
        int i = 0;
        for (Map.Entry entry : map.entrySet()) {
            newData[i] = entry.getKey().getHigh();
            i++;
            newData[i] = entry.getKey().getLow();
            i++;
            newData[i] = entry.getValue();
            i++;
        }
        this.setLen((byte) (13 + map.size() * 3));
        this.setNeigh((byte) map.size());
        System.arraycopy(newData, 0, data, 13, newData.length);
        return this;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy