package backtype.storm.utils;

import backtype.storm.generated.DRPCExecutionException;
import backtype.storm.generated.DistributedRPC;
import org.apache.thrift7.TException;
import org.apache.thrift7.protocol.TBinaryProtocol;
import org.apache.thrift7.transport.TFramedTransport;
import org.apache.thrift7.transport.TSocket;
import org.apache.thrift7.transport.TTransport;

public class DRPCClient implements DistributedRPC.Iface {
    private TTransport conn;
    private DistributedRPC.Client client;
    private String host;
    private int port;

    public DRPCClient(String host, int port) {
        try {
            this.host = host;
            this.port = port;
            connect();
        } catch(TException e) {
            throw new RuntimeException(e);
        }
    }
    
    private void connect() throws TException {
        conn = new TFramedTransport(new TSocket(host, port));
        client = new DistributedRPC.Client(new TBinaryProtocol(conn));
        conn.open();
    }
    
    public String getHost() {
        return host;
    }
    
    public int getPort() {
        return port;
    }   
    
    public String execute(String func, String args) throws TException, DRPCExecutionException {
        try {
            if(client==null) connect();
            return client.execute(func, args);
        } catch(TException e) {
            client = null;
            throw e;
        } catch(DRPCExecutionException e) {
            client = null;
            throw e;
        }
    }

    public void close() {
        conn.close();
    }
}

@@ -541,16 +541,45 @@
 nt port;
+%0A    private Integer timeout;
 %0A%0A    pu
@@ -615,16 +615,33 @@
 int port
+, Integer timeout
 ) %7B%0A    
@@ -706,24 +706,60 @@
 ort = port;%0A
+            this.timeout = timeout;%0A
             
@@ -874,49 +874,267 @@
    p
-rivate void connect() throws TException %7B
+ublic DRPCClient(String host, int port) %7B%0A        this(host, port, null);%0A    %7D%0A    %0A    private void connect() throws TException %7B%0A        TSocket socket = new TSocket(host, port);%0A        if(timeout!=null) %7B%0A            socket.setTimeout(timeout);%0A        %7D
 %0A   
@@ -1166,39 +1166,22 @@
 ansport(
-new TSocket(host, port)
+socket
 );%0A     

add ability to override timeout in drpcclient