-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDnsCacher.java
More file actions
50 lines (39 loc) · 1.21 KB
/
DnsCacher.java
File metadata and controls
50 lines (39 loc) · 1.21 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
import java.net.*;
import java.util.*;
/**
* This classed is used to control the caching of DNS within the proxy
* */
public class DnsCacher {
private Hashtable<String, ipAndTimeStamp> addresses; //hashtable containing each url (keys) and its correspondant IP and timestamp (values)
/**
* Class constructor, takes no parameters
* */
public DnsCacher() {
this.addresses = new Hashtable<String, ipAndTimeStamp>();
}
/**
* Getter for the hashtable
*
* @return The hashtable containing the pairs of urls with its associated IP and timestamp
* */
public Hashtable<String, ipAndTimeStamp> getAddresses() {
return this.addresses;
}
/**
* This method adds a new key/value to the hashtable
* */
public void addToAddress(String url) {
try {
//DNS lookup to find the ip
String fullAddress = InetAddress.getByName(url).toString();
String ip = fullAddress.split("/")[1];
//adds the element to the hash table
this.getAddresses().put(url, new ipAndTimeStamp(ip,System.currentTimeMillis()));
}
catch (Exception e) {
if (e instanceof UnknownHostException) {
System.out.println("Host: " + url + " not found");
}
}
}
}