import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
+import java.net.InetAddress;
import java.net.Socket;
+import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.google.common.collect.Sets;
import com.google.common.eventbus.EventBus;
import com.google.common.io.Closeables;
+import com.google.common.primitives.Longs;
import com.google.common.util.concurrent.AbstractExecutionThreadService;
import com.google.common.util.concurrent.Service;
return Optional.absent();
}
+ /**
+ * Parses the given {@code ip} and returns an {@link InetAddress} from it.
+ *
+ * @param ip
+ * The IP to parse
+ * @return The parsed inet address, or {@link Optional#absent()} if no inet
+ * address could be parsed
+ */
+ private Optional<InetAddress> parseInetAddress(String ip) {
+ Long ipNumber = Longs.tryParse(ip);
+ if (ipNumber == null) {
+ return Optional.absent();
+ }
+
+ StringBuilder hostname = new StringBuilder(15);
+ hostname.append((ipNumber >>> 24) & 0xff).append('.');
+ hostname.append((ipNumber >>> 16) & 0xff).append('.');
+ hostname.append((ipNumber >>> 8) & 0xff).append('.');
+ hostname.append(ipNumber & 0xff);
+ try {
+ return Optional.of(InetAddress.getByName(hostname.toString()));
+ } catch (UnknownHostException uhe1) {
+ return Optional.absent();
+ }
+ }
+
/** Handles input and output for the connection. */
private class ConnectionHandler implements Closeable {