com.github.sdnwiselab.sdnwise.packet.NetworkPacket Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sdn-wise Show documentation
Show all versions of sdn-wise Show documentation
The stateful Software Defined Networking solution
for WIreless SEnsor Networks
/*
* 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.logging.Level;
import java.util.logging.Logger;
/**
* This class represents a generic SDN-WISE message.
*
* @author Sebastiano Milardo
* @version 0.1
*/
public class NetworkPacket implements Cloneable{
public final static byte SDN_WISE_LEN = 0,
SDN_WISE_NET_ID = 1,
SDN_WISE_SRC_H = 2,
SDN_WISE_SRC_L = 3,
SDN_WISE_DST_H = 4,
SDN_WISE_DST_L = 5,
SDN_WISE_TYPE = 6,
SDN_WISE_TTL = 7,
SDN_WISE_NXHOP_H = 8,
SDN_WISE_NXHOP_L = 9,
SDN_WISE_DIST = 10,
SDN_WISE_BATT = 11,
SDN_WISE_NEIGH = 12;
// packet types
public final static byte SDN_WISE_DATA = 0,
SDN_WISE_BEACON = 1,
SDN_WISE_REPORT = 2,
SDN_WISE_REQUEST = 3,
SDN_WISE_RESPONSE = 4,
SDN_WISE_OPEN_PATH = 5,
SDN_WISE_CONFIG = 6;
final byte[] data;
/**
* Returns a NetworkPacket given a byte array.
*
* @param data the data contained in the NetworkPacket
*/
public NetworkPacket(byte[] data) {
this.data = data;
}
public NetworkPacket() {
data = new byte[116];
this.setLen((byte) data.length);
}
public NetworkPacket(int[] data) {
this.data = new byte[data.length];
for (int i = 0; i < data.length; i++) {
this.data[i] = (byte) data[i];
}
}
/**
* Returns the length of the message.
*
* @return an integer representing the length of the message
*/
public final int getLen() {
return data[0] & 0xFF;
}
public final NetworkPacket setLen(byte value) {
data[0] = value;
return this;
}
/**
* Returns the NetworkId of the message.
*
* @return an integer representing the NetworkId of the message
*/
public final int getNetId() {
return data[1] & 0xFF;
}
public final NetworkPacket setNetId(byte value) {
data[1] = value;
return this;
}
/**
* Returns the address of the source node.
*
* @return the NodeAddress of the source node
*/
public final NodeAddress getSrc() {
return new NodeAddress(data[2], data[3]);
}
public final NetworkPacket setSrc(byte valueH, byte valueL) {
data[2] = valueH;
data[3] = valueL;
return this;
}
public NetworkPacket setSrc(NodeAddress node) {
setSrc(node.getHigh(), node.getLow());
return this;
}
public final NetworkPacket setSrc(String node) {
setSrc(new NodeAddress(node));
return this;
}
/**
* Returns the address of the destination node.
*
* @return the NodeAddress of the destination node
*/
public final NodeAddress getDst() {
return new NodeAddress(data[4], data[5]);
}
public final NetworkPacket setDst(byte valueH, byte valueL) {
data[4] = valueH;
data[5] = valueL;
return this;
}
public final NetworkPacket setDst(NodeAddress node) {
setDst(node.getHigh(), node.getLow());
return this;
}
public final NetworkPacket setDst(String node) {
setDst(new NodeAddress(node));
return this;
}
/**
* Returns the type of the message.
*
* @return an integer representing the type of the message
*/
public final int getType() {
return data[6] & 0xFF;
}
/**
* Sets the type of the message.
*
* @param type an integer representing the type of the message
* @return
*/
final NetworkPacket setType(byte type) {
data[6] = type;
return this;
}
/**
* Returns the Time To Live of the message.
*
* @return an integer representing the Time To Live of the message
*/
public final int getTtl() {
return data[7] & 0xFF;
}
public final NetworkPacket setTtl(byte value) {
data[7] = value;
return this;
}
/**
* Returns the NodeAddress of the next hop towards the destination.
*
* @return the NodeAddress of the the next hop towards the destination node
*/
public final NodeAddress getNxhop() {
return new NodeAddress(data[8], data[9]);
}
public final NetworkPacket setNxhop(byte valueH, byte valueL) {
data[8] = valueH;
data[9] = valueL;
return this;
}
public final NetworkPacket setNxhop(NodeAddress node) {
setNxhop(node.getHigh(), node.getLow());
return this;
}
public final NetworkPacket setNxhop(String node) {
setNxhop(new NodeAddress(node));
return this;
}
/**
* Returns the payload of the packet as a byte array.
*
* @return the payload of the packet
*/
public byte[] getPayload() {
return Arrays.copyOfRange(data, 10, this.getLen());
}
public NetworkPacket setPayload(byte[] payload) {
System.arraycopy(payload, 0, data, 10, payload.length);
this.setLen((byte) (payload.length + 10));
return this;
}
/**
* Returns a String representation of the NetworkPacket.
*
* @return a String representation of the NetworkPacket
*/
@Override
public String toString() {
StringBuilder str = new StringBuilder("[");
for (int i = 0; i < this.getLen(); i++) {
str.append(String.format("%3d", data[i] & 0xFF)).append(",");
}
str.append("]");
return str.toString();
}
/**
* Returns a byte array representation of the NetworkPacket.
*
* @return a byte array representation of the NetworkPacket
*/
public byte[] toByteArray() {
return Arrays.copyOf(data, this.getLen());
}
@Override
public NetworkPacket clone() {
try {
super.clone();
} catch (CloneNotSupportedException ex) {
Logger.getLogger(NetworkPacket.class.getName()).log(Level.SEVERE, null, ex);
}
return new NetworkPacket(data.clone());
}
public NetworkPacket setRequestFlag(){
if (getType() < 128){
setType((byte) (getType()+128));
}
return this;
}
public NetworkPacket unsetRequestFlag(){
if (getType() > 127){
setType((byte) (getType()-128));
}
return this;
}
}