-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeighbor.java
More file actions
146 lines (107 loc) · 4.11 KB
/
Neighbor.java
File metadata and controls
146 lines (107 loc) · 4.11 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import java.io.*;
import java.net.*;
public class Neighbor {
private DatagramSocket sendSocket = null;
private int destPort = 0;
private InetAddress destIP = null;
private boolean active = false; //indicates direct link
private double linkCost = 10000;
private double cost = 1000000;
private boolean directNeighbor = false;
private InetAddress firstHop = null;
private int firstHopPort = 0;
private long lastChecked;
//Adds direct neighbor from command line
public Neighbor (String ipAddr, String portNum, String destCost) throws UnknownHostException, SocketException{
this.destPort = Integer.parseInt(portNum);
this.linkCost = Double.parseDouble(destCost);
this.destIP = InetAddress.getByName(ipAddr);
this.directNeighbor = true;
this.firstHop = destIP;
this.firstHopPort = destPort;
this.active = true;
this.sendSocket = new DatagramSocket();
this.lastChecked = System.currentTimeMillis();
}
public Neighbor (InetAddress ipAddr, int portNum, double destCost) throws UnknownHostException, SocketException{
this.destPort = portNum;
this.linkCost = destCost;
this.destIP = ipAddr;
this.directNeighbor = true;
this.firstHop = destIP;
this.firstHopPort = destPort;
this.active = true;
this.sendSocket = new DatagramSocket();
this.lastChecked = System.currentTimeMillis();
}
public Neighbor (String ipAddr, String portNum, InetAddress firstLink, int linkPort) throws UnknownHostException {
this.destPort = Integer.parseInt(portNum);
//this.destCost = Double.parseDouble(destCost);
this.destIP = InetAddress.getByName(ipAddr);
this.firstHop = firstLink;
this.firstHopPort = linkPort;
//sendSocket = new DatagramSocket();
}
public Neighbor(InetAddress destAddress, InetAddress linkAddr, int portNum, double mycost){
this.destPort = portNum;
this.destIP = destAddress;
this.firstHop = linkAddr;
this.firstHopPort = portNum;
this.cost = mycost;
}
public void sendToNeighbor (byte[] distVector) throws IOException {
DatagramPacket sendPacket = new DatagramPacket(distVector, distVector.length, destIP, destPort);
sendSocket.send(sendPacket);
}
public boolean isActive() {
return active;
}
public boolean isNeighbor() {
return directNeighbor;
}
public void setLinkCost (double newCost){
if (active){
this.linkCost = newCost;
return;
}
this.cost = newCost;
}
public void printNeighbor() {
if(active){
System.out.format("Destination = %s:%d, Cost = %f, Link = (%s:%d)%n", destIP.getHostAddress(), destPort, linkCost, firstHop.getHostAddress(), firstHopPort);
return;
}
System.out.format("Destination = %s:%d, Cost = %f, Link = (%s:%d)%n", destIP.getHostAddress(), destPort, cost, firstHop.getHostAddress(), firstHopPort);
}
public byte[] getDestByteArray() {
return destIP.getAddress();
}
public int getDestPort(){
return destPort;
}
public double getLinkCost() {
if(active) {
return linkCost;
}
return cost;
}
public boolean equalsNeighbor(InetAddress addr, int port){
if(this.destPort == port && this.destIP.equals(addr)) {
return true;
}
return false;
}
public void setActive(boolean val) {
this.active = val;
}
public void handleTimeout(long timeout, long currTime) {
long duration = currTime - lastChecked;
if(duration > (3 * timeout)) {
this.setActive(false);
//System.out.format("The node %d timedout%n", destPort);
}
}
public void setTime() {
this.lastChecked = System.currentTimeMillis();
}
}