-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkState.java
More file actions
69 lines (52 loc) · 1.83 KB
/
LinkState.java
File metadata and controls
69 lines (52 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
public class LinkState {
private int src; // Node from which this edge points
private int dest; // Node to which this edge points
private int cost; // Total cost of the edge
public static final int ENCODING_SIZE = 6; // a LinkState object is encoded into a 6-byte array
public LinkState(int src, int dest, int cost) {
this.src = src;
this.dest = dest;
this.cost = cost;
}
public LinkState(byte[] encoded) {
this.src = Byte.toUnsignedInt(encoded[0]);
this.src <<= 8;
this.src |= Byte.toUnsignedInt(encoded[1]);
this.dest = Byte.toUnsignedInt(encoded[2]);
this.dest <<= 8;
this.dest |= Byte.toUnsignedInt(encoded[3]);
this.cost = Byte.toUnsignedInt(encoded[4]);
this.cost <<= 8;
this.cost |= Byte.toUnsignedInt(encoded[5]);
}
public void setCost(int cost) {
this.cost = cost;
}
public int[] getPorts() {
return new int[]{this.src, this.dest};
}
public int getCost() {
return this.cost;
}
public byte[] encode() {
byte[] output = new byte[6];
output[0] = (byte) (this.src >> 8);
output[1] = (byte) (this.src & 0xFF);
output[2] = (byte) (this.dest >> 8);
output[3] = (byte) (this.dest & 0xFF);
output[4] = (byte) (this.cost >> 8);
output[5] = (byte) (this.cost & 0xFF);
return output;
}
@Override
public String toString() {
String output = "Node " + this.src + " (" + this.cost + ")" + " <-> Node " + this.dest + ";";
return output;
}
public static void main(String[] args) {
LinkState poop = new LinkState(1, 2, 3);
System.out.println(poop);
LinkState poopoo = new LinkState(poop.encode());
System.out.println(poopoo);
}
}