Subversion Repositories SmartDukaan

Rev

Rev 1568 | Rev 3123 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.thrift.clients;

import in.shop2020.config.ConfigException;
import in.shop2020.thrift.clients.config.ConfigClient;
import in.shop2020.utils.Logger;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

public abstract class GenericServiceClient {
        
        protected String hostname = "localhost";
        protected int port = 8080;
        
        protected TSocket socket = null;
        protected TTransport transport = null;
        protected TProtocol protocol = null;
                
        private final int SOCKET_TIMEOUT = 1000 * 60 * 5;
        
        public GenericServiceClient(String clientIdentifier, String hostConfigKey, String portConfigKey) throws Exception{
                if(clientIdentifier == null||clientIdentifier.equals("")){
                        Logger.log("ClientIdentifier is null or empty, cannot initiate client", null);
                }
                this.hostname = getHost(hostConfigKey);
                this.port = getPort(portConfigKey);
                if(hostname == null || hostname.equals("")){
                        Logger.log("Hostname null for client :"+ clientIdentifier+ "using localhost", null);
                }else{
                        Logger.log("Initializing client for: "+ clientIdentifier+" with host as "+ hostname, null);
                }
                
                Logger.log("Initializing socket infra ", this);
                
                socket = new TSocket(hostname, port);
                socket.setTimeout(SOCKET_TIMEOUT);
                transport = new TFramedTransport(socket);
                //protocol = new TCompactProtocol(transport);
                protocol = new TBinaryProtocol(transport);
        }
        
        private String getHost(String key){
                try {
                        String host = ConfigClient.getClient().get(key);
                        return host;
                } catch (ConfigException e) {
                        Logger.log("Error while fetching hostname for key "+ key+" using localhost:"+ e, this);
                        return "localhost";
                }
        }
        
        private int getPort(String key){
                try {
                        String port = ConfigClient.getClient().get(key);
                        return Integer.parseInt(port);
                }catch (NumberFormatException ne){
                        Logger.log("Could not convert string to int for key "+ key+" using 8080 as port"+ ne, this);
                        return 9009;
                }
                catch (ConfigException e) {
                        Logger.log("Error while fetching port for key "+ key+" using 8080:"+ e, this);
                        return 9009;
                }
        }
        
        public abstract void closeSession();
        
        public void closeConnection(){
                if(transport != null && transport.isOpen()){
                        //Removed to prevent socket connection closed exception
                        ///this.closeSession();
                        Logger.log("Closing transport :", this);
                        transport.close();
                }
        }
        
        protected void finalize() throws Throwable{
                super.finalize();
                this.closeConnection();
        }
        
}