Permalink
Browse files

Provide a getRemoteAddress utility method on ThriftClientManager

  • Loading branch information...
1 parent 094f1b8 commit 278791383c9c75feca465c67d7b979883193ea2a @andrewcox andrewcox committed Feb 26, 2013
Showing with 31 additions and 3 deletions.
  1. +31 −3 swift-service/src/main/java/com/facebook/swift/service/ThriftClientManager.java
@@ -39,12 +39,14 @@
import org.apache.thrift.protocol.TProtocolException;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.transport.TTransportException;
+import org.jboss.netty.channel.Channel;
import java.io.Closeable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.net.InetSocketAddress;
+import java.net.SocketAddress;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
@@ -196,15 +198,41 @@ public void close()
niftyClient.close();
}
+ /**
+ * Returns the {@link NiftyClientChannel} backing a Swift client
+ *
+ * @throws IllegalArgumentException if the client is not a Swift client
+ */
public NiftyClientChannel getNiftyChannel(Object client)
{
try {
InvocationHandler genericHandler = Proxy.getInvocationHandler(client);
- ThriftInvocationHandler thriftHandler = ThriftInvocationHandler.class.cast(genericHandler);
+ ThriftInvocationHandler thriftHandler = (ThriftInvocationHandler) genericHandler;
return thriftHandler.getChannel();
}
- catch (ClassCastException e) {
- throw new IllegalArgumentException("Not a swift client object", e);
+ catch (IllegalArgumentException | ClassCastException e) {
+ throw new IllegalArgumentException("Invalid swift client object", e);
+ }
+ }
+
+ /**
+ * Returns the remote address that a Swift client is connected to
+ *
+ * @throws IllegalArgumentException if the client is not a Swift client or is not connected
+ * through an internet socket
+ */
+ public HostAndPort getRemoteAddress(Object client)
+ {
+ NiftyClientChannel niftyChannel = getNiftyChannel(client);
+
+ try {
+ Channel nettyChannel = niftyChannel.getNettyChannel();
+ SocketAddress address = nettyChannel.getRemoteAddress();
+ InetSocketAddress inetAddress = (InetSocketAddress) address;
+ return HostAndPort.fromParts(inetAddress.getHostName(), inetAddress.getPort());
+ }
+ catch (NullPointerException | ClassCastException e) {
+ throw new IllegalArgumentException("Invalid swift client object", e);
}
}

0 comments on commit 2787913

Please sign in to comment.