root / src / com / rackspace / cloud / android / ConnectionThrottleActivity.java @ 7dbfc514
History | View | Annotate | Download (8.3 kB)
1 |
package com.rackspace.cloud.android; |
---|---|
2 |
|
3 |
import org.apache.http.HttpResponse; |
4 |
|
5 |
import android.app.Activity; |
6 |
import android.os.AsyncTask; |
7 |
import android.os.Bundle; |
8 |
import android.view.View; |
9 |
import android.view.View.OnClickListener; |
10 |
import android.widget.Button; |
11 |
import android.widget.EditText; |
12 |
|
13 |
import com.rackspace.cloud.android.R; |
14 |
import com.rackspace.cloud.loadbalancer.api.client.ConnectionThrottle; |
15 |
import com.rackspace.cloud.loadbalancer.api.client.ConnectionThrottleManager; |
16 |
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancer; |
17 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
18 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
19 |
|
20 |
public class ConnectionThrottleActivity extends CloudActivity{ |
21 |
|
22 |
private LoadBalancer loadBalancer;
|
23 |
private ConnectionThrottle connectionThrottle;
|
24 |
private EditText minCons;
|
25 |
private EditText maxCons;
|
26 |
private EditText maxConRate;
|
27 |
private EditText rateInterval;
|
28 |
|
29 |
private final String ENABLE = "Enable Throttle"; |
30 |
private final String DISABLE = "Disable Throttle"; |
31 |
|
32 |
@Override
|
33 |
public void onCreate(Bundle savedInstanceState) { |
34 |
super.onCreate(savedInstanceState);
|
35 |
loadBalancer = (LoadBalancer) this.getIntent().getExtras().get("loadBalancer"); |
36 |
setContentView(R.layout.connectionthrottle); |
37 |
restoreState(savedInstanceState); |
38 |
} |
39 |
|
40 |
@Override
|
41 |
protected void onSaveInstanceState(Bundle outState) { |
42 |
super.onSaveInstanceState(outState);
|
43 |
outState.putSerializable("loadBalancer", loadBalancer);
|
44 |
} |
45 |
|
46 |
|
47 |
protected void restoreState(Bundle state) { |
48 |
super.restoreState(state);
|
49 |
|
50 |
if(state != null && state.containsKey("loadBalancer")){ |
51 |
loadBalancer = (LoadBalancer)state.getSerializable("loadBalancer");
|
52 |
} |
53 |
connectionThrottle = loadBalancer.getConnectionThrottle(); |
54 |
minCons = (EditText)findViewById(R.id.min_connections_text); |
55 |
maxCons = (EditText)findViewById(R.id.max_connections_text); |
56 |
maxConRate = (EditText)findViewById(R.id.max_connection_rate); |
57 |
rateInterval = (EditText)findViewById(R.id.rate_interval); |
58 |
|
59 |
setupButtons(); |
60 |
setupText(); |
61 |
} |
62 |
|
63 |
private void setupButtons(){ |
64 |
Button enable = (Button)findViewById(R.id.enable_throttle_button); |
65 |
if(loadBalancer.getConnectionThrottle() == null){ |
66 |
enable.setText(ENABLE); |
67 |
} else {
|
68 |
enable.setText(DISABLE); |
69 |
} |
70 |
enable.setOnClickListener(new OnClickListener() {
|
71 |
Button enable = (Button)findViewById(R.id.enable_throttle_button); |
72 |
@Override
|
73 |
public void onClick(View v) { |
74 |
if(enable.getText().toString().equals(ENABLE)){
|
75 |
ConnectionThrottle connectionThrottle = new ConnectionThrottle();
|
76 |
connectionThrottle.setMinConnections("25");
|
77 |
connectionThrottle.setMaxConnections("100");
|
78 |
connectionThrottle.setMaxConnectionRate("25");
|
79 |
connectionThrottle.setRateInterval("5");
|
80 |
|
81 |
loadBalancer.setConnectionThrottle(connectionThrottle); |
82 |
//Turn on EditTexts
|
83 |
minCons.setEnabled(true);
|
84 |
maxCons.setEnabled(true);
|
85 |
maxConRate.setEnabled(true);
|
86 |
rateInterval.setEnabled(true);
|
87 |
enable.setText(DISABLE); |
88 |
} else {
|
89 |
loadBalancer.setConnectionThrottle(null);
|
90 |
//Turn off EditTexts
|
91 |
minCons.setEnabled(false);
|
92 |
maxCons.setEnabled(false);
|
93 |
maxConRate.setEnabled(false);
|
94 |
rateInterval.setEnabled(false);
|
95 |
enable.setText(ENABLE); |
96 |
} |
97 |
} |
98 |
}); |
99 |
|
100 |
Button submit = (Button)findViewById(R.id.save_throttle_button); |
101 |
submit.setOnClickListener(new OnClickListener() {
|
102 |
|
103 |
Button enable = (Button)findViewById(R.id.enable_throttle_button); |
104 |
|
105 |
@Override
|
106 |
public void onClick(View v) { |
107 |
|
108 |
connectionThrottle = new ConnectionThrottle();
|
109 |
connectionThrottle.setMaxConnectionRate(maxConRate.getText().toString()); |
110 |
connectionThrottle.setMinConnections(minCons.getText().toString()); |
111 |
connectionThrottle.setMaxConnections(maxCons.getText().toString()); |
112 |
connectionThrottle.setRateInterval(rateInterval.getText().toString()); |
113 |
|
114 |
if(enable.getText().toString().equals(DISABLE)){
|
115 |
if(validText()){
|
116 |
//trackEvent(GoogleAnalytics.CATEGORY_LOAD_BALANCER, GoogleAnalytics.EVENT_LB_SESSION_PERSISTENCE, "", -1);
|
117 |
new UpdateConnectionThrottleTask().execute();
|
118 |
} |
119 |
} else {
|
120 |
//trackEvent(GoogleAnalytics.CATEGORY_LOAD_BALANCER, GoogleAnalytics.EVENT_LB_SESSION_PERSISTENCE, "", -1);
|
121 |
new DeleteConnectionThrottleTask().execute();
|
122 |
} |
123 |
} |
124 |
}); |
125 |
} |
126 |
|
127 |
private void setupText(){ |
128 |
if(loadBalancer.getConnectionThrottle() == null){ |
129 |
minCons.setEnabled(false);
|
130 |
maxCons.setEnabled(false);
|
131 |
maxConRate.setEnabled(false);
|
132 |
rateInterval.setEnabled(false);
|
133 |
|
134 |
//Set boxes to default values
|
135 |
minCons.setText("25");
|
136 |
maxCons.setText("100");
|
137 |
maxConRate.setText("25");
|
138 |
rateInterval.setText("5");
|
139 |
} else {
|
140 |
ConnectionThrottle throttle = loadBalancer.getConnectionThrottle(); |
141 |
|
142 |
//restore the current values to the boxes
|
143 |
minCons.setText(throttle.getMinConnections()); |
144 |
maxCons.setText(throttle.getMaxConnections()); |
145 |
maxConRate.setText(throttle.getMaxConnectionRate()); |
146 |
rateInterval.setText(throttle.getRateInterval()); |
147 |
} |
148 |
} |
149 |
|
150 |
private Boolean validText(){ |
151 |
return validEditText(maxCons, 0, 100000, "Max Connections") |
152 |
&& validEditText(minCons, 0, 1000, "Min Connections") |
153 |
&& validEditText(maxConRate, 0, 100000, "Max Connection Rate") |
154 |
&& validEditText(rateInterval, 1, 3600, "Rate Interval"); |
155 |
} |
156 |
|
157 |
private Boolean validEditText(EditText box, int min, int max, String boxName){ |
158 |
String result = box.getText().toString();
|
159 |
if(result.equals("")){ |
160 |
showAlert("Error", "Please enter a value for " + boxName + "."); |
161 |
return false; |
162 |
} else {
|
163 |
try {
|
164 |
int value = Integer.parseInt(result); |
165 |
if(value >= min && value <= max){
|
166 |
return true; |
167 |
} else {
|
168 |
showAlert("Error", boxName + " must be an integer between " + min + " and " + max + " inclusive."); |
169 |
return false; |
170 |
} |
171 |
} catch (NumberFormatException e) { |
172 |
showAlert("Error", boxName + " must be an integer between " + min + " and " + max + " inclusive."); |
173 |
return false; |
174 |
} |
175 |
} |
176 |
} |
177 |
|
178 |
public class UpdateConnectionThrottleTask extends AsyncTask<Void, Void, HttpBundle> { |
179 |
|
180 |
private CloudServersException exception;
|
181 |
|
182 |
@Override
|
183 |
protected void onPreExecute(){ |
184 |
showDialog(); |
185 |
} |
186 |
|
187 |
@Override
|
188 |
protected HttpBundle doInBackground(Void... arg0) { |
189 |
HttpBundle bundle = null;
|
190 |
try {
|
191 |
bundle = (new ConnectionThrottleManager(getContext())).update(loadBalancer, connectionThrottle);
|
192 |
} catch (CloudServersException e) {
|
193 |
exception = e; |
194 |
} |
195 |
return bundle;
|
196 |
} |
197 |
|
198 |
@Override
|
199 |
protected void onPostExecute(HttpBundle bundle) { |
200 |
hideDialog(); |
201 |
HttpResponse response = bundle.getResponse(); |
202 |
if (response != null) { |
203 |
int statusCode = response.getStatusLine().getStatusCode();
|
204 |
if (statusCode == 202 || statusCode == 200) { |
205 |
setResult(Activity.RESULT_OK); |
206 |
finish(); |
207 |
} else {
|
208 |
CloudServersException cse = parseCloudServersException(response); |
209 |
if ("".equals(cse.getMessage())) { |
210 |
showError("There was a problem editing the connection throttle.", bundle);
|
211 |
} else {
|
212 |
showError("There was a problem editing the connection throttle: " + cse.getMessage(), bundle);
|
213 |
} |
214 |
} |
215 |
} else if (exception != null) { |
216 |
showError("There was a problem editing the connection throttle: " + exception.getMessage(), bundle);
|
217 |
} |
218 |
} |
219 |
} |
220 |
|
221 |
public class DeleteConnectionThrottleTask extends AsyncTask<Void, Void, HttpBundle> { |
222 |
|
223 |
private CloudServersException exception;
|
224 |
|
225 |
@Override
|
226 |
protected void onPreExecute(){ |
227 |
showDialog(); |
228 |
} |
229 |
|
230 |
@Override
|
231 |
protected HttpBundle doInBackground(Void... arg0) { |
232 |
HttpBundle bundle = null;
|
233 |
try {
|
234 |
bundle = (new ConnectionThrottleManager(getContext())).delete(loadBalancer);
|
235 |
} catch (CloudServersException e) {
|
236 |
exception = e; |
237 |
} |
238 |
return bundle;
|
239 |
} |
240 |
|
241 |
@Override
|
242 |
protected void onPostExecute(HttpBundle bundle) { |
243 |
hideDialog(); |
244 |
HttpResponse response = bundle.getResponse(); |
245 |
if (response != null) { |
246 |
int statusCode = response.getStatusLine().getStatusCode();
|
247 |
if (statusCode == 202 || statusCode == 200) { |
248 |
setResult(Activity.RESULT_OK); |
249 |
finish(); |
250 |
} else {
|
251 |
CloudServersException cse = parseCloudServersException(response); |
252 |
if ("".equals(cse.getMessage())) { |
253 |
showError("There was a problem editing the connection throttle.", bundle);
|
254 |
} else {
|
255 |
showError("There was a problem editing the connection throttle: " + cse.getMessage(), bundle);
|
256 |
} |
257 |
} |
258 |
} else if (exception != null) { |
259 |
showError("There was a problem editing the connection throttle: " + exception.getMessage(), bundle);
|
260 |
} |
261 |
} |
262 |
} |
263 |
|
264 |
} |