Subversion Repositories SmartDukaan

Rev

Rev 3123 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3123 Rev 3183
Line 35... Line 35...
35
	/**
35
	/**
36
	 * Current service instance being used
36
	 * Current service instance being used
37
	 */
37
	 */
38
	protected int serviceNumber = 0;
38
	protected int serviceNumber = 0;
39
	protected String hostname = "localhost";
39
	protected String hostname = "localhost";
40
	protected int port = 8080;
40
	protected int port = 0;
41
	
41
	
42
	protected String hostConfigKey;
42
	protected String hostConfigKey;
43
	protected String portConfigKey;
43
	protected String portConfigKey;
44
 
44
 
45
		
45
		
Line 51... Line 51...
51
	 * @throws TTransportException
51
	 * @throws TTransportException
52
	 */
52
	 */
53
	public GenericClient(String hostConfigKey, String portConfigKey) throws TTransportException{
53
	public GenericClient(String hostConfigKey, String portConfigKey) throws TTransportException{
54
		this.hostConfigKey = hostConfigKey; 
54
		this.hostConfigKey = hostConfigKey; 
55
		this.portConfigKey = portConfigKey;
55
		this.portConfigKey = portConfigKey;
-
 
56
		try {
56
		loadConfigParameters(hostConfigKey, portConfigKey);
57
			loadConfigParameters(hostConfigKey, portConfigKey);
57
		connectToService(FIRST_ATTEMPT);
58
			connectToService(FIRST_ATTEMPT);
-
 
59
		} catch (ConfigException e) {
-
 
60
			Logger.log("Config exception", this);
-
 
61
			throw new TTransportException("Unable to get parameters from config server.");
-
 
62
		}
58
	}
63
	}
59
	
64
	
60
	/**
65
	/**
61
	 * Load config parameters for the given key set
66
	 * Load config parameters for the given key set
62
	 * @param hostConfigKey
67
	 * @param hostConfigKey
63
	 * @param portConfigKey
68
	 * @param portConfigKey
64
	 */
69
	 */
65
	private void loadConfigParameters(String hostConfigKey, String  portConfigKey){
70
	private void loadConfigParameters(String hostConfigKey, String  portConfigKey) throws ConfigException{
66
		this.hostname = getHost(hostConfigKey);
71
		this.hostname = getHost(hostConfigKey);
67
		this.port = getPort(portConfigKey);
72
		this.port = getPort(portConfigKey);
68
	}
73
	}
69
	
74
	
70
 
75
 
71
	/**
76
	/**
72
	 * get the Host name from given key
77
	 * get the Host name from given key
73
	 * @param key
78
	 * @param key
74
	 * @return
79
	 * @return
-
 
80
	 * @throws ConfigException 
75
	 */
81
	 */
76
	private String getHost(String key){
82
	private String getHost(String key) throws ConfigException{
77
		try {
-
 
78
			String host = ConfigClient.getClient().get(key);
83
		String host = ConfigClient.getClient().get(key);
79
			return host;
84
		return host;
80
		} catch (ConfigException e) {
-
 
81
			Logger.log("Error while fetching hostname for key "+ key+" using localhost:"+ e, this);
-
 
82
			return "localhost";
-
 
83
		}
-
 
84
	}
85
	}
85
	
86
	
86
	/**
87
	/**
87
	 * get the Port from given key
88
	 * get the Port from given key
88
	 * @param key
89
	 * @param key
89
	 * @return
90
	 * @return
-
 
91
	 * @throws ConfigException 
90
	 */
92
	 */
91
	private int getPort(String key){
93
	private int getPort(String key) throws ConfigException{
92
		try {
-
 
93
			String port = ConfigClient.getClient().get(key);
94
			String port = ConfigClient.getClient().get(key);
-
 
95
			try{
94
			return Integer.parseInt(port);
96
				int portNumber = Integer.parseInt(port);
-
 
97
				return portNumber;
95
		}catch (NumberFormatException ne){
98
			}catch (NumberFormatException e) {
96
			Logger.log("Could not convert string to int for key "+ key+" using 8080 as port"+ ne, this);
99
				Logger.log("Unable to parse the port number to string", null);
97
			return 8080;
100
				throw new ConfigException(100, "Unable to parse the port number to string");
98
		}
101
			}
99
		catch (ConfigException e) {
-
 
100
			Logger.log("Error while fetching port for key "+ key+" using 8080:"+ e, this);
-
 
101
			return 8080;
-
 
102
		}
102
			
103
	}
103
	}
104
 
104
 
105
	/**
105
	/**
106
	 * Connect to the service. It also have mechanism to handle multiple service instances.
106
	 * Connect to the service. It also have mechanism to handle multiple service instances.
107
	 * Connect to default service:
107
	 * Connect to default service:
108
	 *     If fails: Connect to another services instances till have tried all services.
108
	 *     If fails: Connect to another services instances till have tried all services.
109
	 * Tries the default service at MAX_ATTEMPTS times  
109
	 * Tries the default service at MAX_ATTEMPTS times  
110
	 * @param attemptNumber
110
	 * @param attemptNumber
111
	 * @throws TTransportException
111
	 * @throws TTransportException
-
 
112
	 * @throws ConfigException 
112
	 */
113
	 */
113
	private void connectToService(int attemptNumber) throws TTransportException{
114
	private void connectToService(int attemptNumber) throws TTransportException, ConfigException{
114
		try{
115
		try{
115
			openTransport();
116
			openTransport();
116
		}catch (TTransportException e) {
117
		}catch (TTransportException e) {
117
			if(attemptNumber < MAX_ATTEMPTS){
118
			if(attemptNumber < MAX_ATTEMPTS){
118
				connectToService(attemptNumber+1);
119
				connectToService(attemptNumber+1);