Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
376 ashish 1
package in.shop2020.thrift.clients;
2
 
3
import in.shop2020.config.ConfigException;
4
import in.shop2020.thrift.clients.config.ConfigClient;
5
import in.shop2020.utils.Logger;
6
 
7
import org.apache.thrift.protocol.TBinaryProtocol;
8
import org.apache.thrift.protocol.TProtocol;
9
import org.apache.thrift.transport.TSocket;
10
import org.apache.thrift.transport.TTransport;
11
 
12
public abstract class GenericServiceJavaClient {
13
	protected String hostname = "localhost";
14
	protected int port = 8080;
15
 
16
	protected TSocket socket = null;
17
	protected TTransport transport = null;
18
	protected TProtocol protocol = null;
19
 
20
	private final int SOCKET_TIMEOUT = 1000 * 60 * 5;
21
 
22
	public GenericServiceJavaClient(String clientIdentifier, String hostConfigKey, String portConfigKey) throws Exception{
23
		if(clientIdentifier == null||clientIdentifier.equals("")){
24
			Logger.log("ClientIdentifier is null or empty, cannot initiate client", null);
25
		}
26
		this.hostname = getHost(hostConfigKey);
27
		this.port = getPort(portConfigKey);
28
		if(hostname == null || hostname.equals("")){
29
			Logger.log("Hostname null for client :"+ clientIdentifier+ "using localhost", null);
30
		}else{
31
			Logger.log("Initializing client for: "+ clientIdentifier+" with host as "+ hostname, null);
32
		}
33
 
34
		Logger.log("Initializing socket infra ", this);
35
 
36
		//socket = new TSocket(hostname, port);
37
		///socket.setTimeout(SOCKET_TIMEOUT);
38
		//transport = new TFramedTransport(socket);
39
		transport = new TSocket(hostname, port);
40
		//protocol = new TCompactProtocol(transport);
41
		protocol = new TBinaryProtocol(transport);
42
	}
43
 
44
	private String getHost(String key){
45
		try {
46
			String host = ConfigClient.getClient().get(key);
47
			return host;
48
		} catch (ConfigException e) {
49
			Logger.log("Error while fetching hostname for key "+ key+" using localhost:"+ e, this);
50
			return "localhost";
51
		}
52
	}
53
 
54
	private int getPort(String key){
55
		try {
56
			String port = ConfigClient.getClient().get(key);
57
			return Integer.parseInt(port);
58
		}catch (NumberFormatException ne){
59
			Logger.log("Could not convert string to int for key "+ key+" using 8080 as port"+ ne, this);
60
			return 8080;
61
		}
62
		catch (ConfigException e) {
63
			Logger.log("Error while fetching port for key "+ key+" using 8080:"+ e, this);
64
			return 8080;
65
		}
66
	}
67
 
68
}