Skip to content
This repository has been archived by the owner on Jul 27, 2019. It is now read-only.

Commit

Permalink
Cache hostname lookups.
Browse files Browse the repository at this point in the history
  • Loading branch information
cnaude committed Oct 2, 2014
1 parent 1273fa3 commit 2da19cc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
Expand Up @@ -38,13 +38,14 @@ public void onPlayerJoinEvent(final PlayerJoinEvent event) {
plugin.getServer().getScheduler().runTaskLaterAsynchronously(plugin, new Runnable() {
@Override
public void run() {
plugin.clearHostCache(event.getPlayer());
for (PurpleBot ircBot : plugin.ircBots.values()) {
ircBot.gameJoin(event.getPlayer(), event.getJoinMessage());
if (plugin.netPackets != null) {
plugin.netPackets.updateTabList(event.getPlayer());
}
}
plugin.updateDisplayNameCache(event.getPlayer());
plugin.updateDisplayNameCache(event.getPlayer());
}
}, 20);
}
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/cnaude/purpleirc/PurpleIRC.java
Expand Up @@ -48,6 +48,8 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -86,6 +88,7 @@ public class PurpleIRC extends JavaPlugin {
private final CaseInsensitiveMap<CaseInsensitiveMap<String>> ircTownyChannelMessages;
private final CaseInsensitiveMap<CaseInsensitiveMap<String>> heroChannelMessages;
private final CaseInsensitiveMap<CaseInsensitiveMap<String>> heroActionChannelMessages;
private Map<String, String> hostCache;
public String defaultPlayerSuffix,
defaultPlayerPrefix,
defaultPlayerGroup,
Expand Down Expand Up @@ -161,6 +164,7 @@ public PurpleIRC() {
this.heroChannelMessages = new CaseInsensitiveMap<>();
this.heroActionChannelMessages = new CaseInsensitiveMap<>();
this.displayNameCache = new CaseInsensitiveMap<>();
this.hostCache = new HashMap<>();
this.cacheFile = new File("plugins/PurpleIRC/displayName.cache");
}

Expand Down Expand Up @@ -1176,4 +1180,30 @@ public void loadDisplayNameCache() {
logError(e.getMessage());
}
}

public String getPlayerHost(Player player) {
String playerIP = player.getAddress().getAddress().getHostAddress();
if (hostCache.containsKey(playerIP)) {
return hostCache.get(playerIP);
}
InetAddress addr = null;
try {
addr = InetAddress.getByName(playerIP);
} catch (UnknownHostException ex) {
logError(ex.getMessage());
}
String host = "UnknownHost";
if ( addr != null ) {
host = addr.getHostName();
hostCache.put(playerIP, host);
}
return host;
}

public void clearHostCache(Player player) {
String playerIP = player.getAddress().getAddress().getHostAddress();
if (hostCache.containsKey(playerIP)) {
hostCache.remove(playerIP);
}
}
}
11 changes: 1 addition & 10 deletions src/main/java/com/cnaude/purpleirc/Utilities/ChatTokenizer.java
Expand Up @@ -506,16 +506,7 @@ public String playerTokenizer(Player player, String message) {
String group = plugin.getPlayerGroup(player);
String displayName = player.getDisplayName();
String playerIP = player.getAddress().getAddress().getHostAddress();
InetAddress addr = null;
try {
addr = InetAddress.getByName(playerIP);
} catch (UnknownHostException ex) {
plugin.logError(ex.getMessage());
}
String host = "";
if ( addr != null ) {
host = addr.getHostName();
}
String host = plugin.getPlayerHost(player);
String worldName = "";
String worldAlias = "";
String worldColor = "";
Expand Down

0 comments on commit 2da19cc

Please sign in to comment.