root / src / com / rackspacecloud / android / ConnectionThrottleActivity.java @ 21c57799
History | View | Annotate | Download (8.1 kB)
1 |
package com.rackspacecloud.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.util.Log; |
9 |
import android.view.View; |
10 |
import android.view.View.OnClickListener; |
11 |
import android.widget.Button; |
12 |
import android.widget.EditText; |
13 |
|
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 |
new UpdateConnectionThrottleTask().execute();
|
117 |
} |
118 |
} else {
|
119 |
new DeleteConnectionThrottleTask().execute();
|
120 |
} |
121 |
} |
122 |
}); |
123 |
} |
124 |
|
125 |
private void setupText(){ |
126 |
if(loadBalancer.getConnectionThrottle() == null){ |
127 |
minCons.setEnabled(false);
|
128 |
maxCons.setEnabled(false);
|
129 |
maxConRate.setEnabled(false);
|
130 |
rateInterval.setEnabled(false);
|
131 |
|
132 |
//Set boxes to default values
|
133 |
minCons.setText("25");
|
134 |
maxCons.setText("100");
|
135 |
maxConRate.setText("25");
|
136 |
rateInterval.setText("5");
|
137 |
} else {
|
138 |
ConnectionThrottle throttle = loadBalancer.getConnectionThrottle(); |
139 |
|
140 |
//restore the current values to the boxes
|
141 |
minCons.setText(throttle.getMinConnections()); |
142 |
maxCons.setText(throttle.getMaxConnections()); |
143 |
maxConRate.setText(throttle.getMaxConnectionRate()); |
144 |
rateInterval.setText(throttle.getRateInterval()); |
145 |
} |
146 |
} |
147 |
|
148 |
private Boolean validText(){ |
149 |
return validEditText(maxCons, 0, 100000, "Max Connections") |
150 |
&& validEditText(minCons, 0, 1000, "Min Connections") |
151 |
&& validEditText(maxConRate, 0, 100000, "Max Connection Rate") |
152 |
&& validEditText(rateInterval, 1, 3600, "Rate Interval"); |
153 |
} |
154 |
|
155 |
private Boolean validEditText(EditText box, int min, int max, String boxName){ |
156 |
String result = box.getText().toString();
|
157 |
if(result.equals("")){ |
158 |
showAlert("Error", "Please enter a value for " + boxName + "."); |
159 |
return false; |
160 |
} else {
|
161 |
try {
|
162 |
int value = Integer.parseInt(result); |
163 |
if(value >= min && value <= max){
|
164 |
return true; |
165 |
} else {
|
166 |
showAlert("Error", boxName + " must be an integer between " + min + " and " + max + " inclusive."); |
167 |
return false; |
168 |
} |
169 |
} catch (NumberFormatException e) { |
170 |
showAlert("Error", boxName + " must be an integer between " + min + " and " + max + " inclusive."); |
171 |
return false; |
172 |
} |
173 |
} |
174 |
} |
175 |
|
176 |
public class UpdateConnectionThrottleTask extends AsyncTask<Void, Void, HttpBundle> { |
177 |
|
178 |
private CloudServersException exception;
|
179 |
|
180 |
@Override
|
181 |
protected void onPreExecute(){ |
182 |
showDialog(); |
183 |
} |
184 |
|
185 |
@Override
|
186 |
protected HttpBundle doInBackground(Void... arg0) { |
187 |
HttpBundle bundle = null;
|
188 |
try {
|
189 |
bundle = (new ConnectionThrottleManager(getContext())).update(loadBalancer, connectionThrottle);
|
190 |
} catch (CloudServersException e) {
|
191 |
exception = e; |
192 |
} |
193 |
return bundle;
|
194 |
} |
195 |
|
196 |
@Override
|
197 |
protected void onPostExecute(HttpBundle bundle) { |
198 |
hideDialog(); |
199 |
HttpResponse response = bundle.getResponse(); |
200 |
if (response != null) { |
201 |
int statusCode = response.getStatusLine().getStatusCode();
|
202 |
if (statusCode == 202 || statusCode == 200) { |
203 |
setResult(Activity.RESULT_OK); |
204 |
finish(); |
205 |
} else {
|
206 |
CloudServersException cse = parseCloudServersException(response); |
207 |
if ("".equals(cse.getMessage())) { |
208 |
showError("There was a problem editing the connection throttle.", bundle);
|
209 |
} else {
|
210 |
showError("There was a problem editing the connection throttle: " + cse.getMessage(), bundle);
|
211 |
} |
212 |
} |
213 |
} else if (exception != null) { |
214 |
showError("There was a problem editing the connection throttle: " + exception.getMessage(), bundle);
|
215 |
} |
216 |
} |
217 |
} |
218 |
|
219 |
public class DeleteConnectionThrottleTask extends AsyncTask<Void, Void, HttpBundle> { |
220 |
|
221 |
private CloudServersException exception;
|
222 |
|
223 |
@Override
|
224 |
protected void onPreExecute(){ |
225 |
showDialog(); |
226 |
} |
227 |
|
228 |
@Override
|
229 |
protected HttpBundle doInBackground(Void... arg0) { |
230 |
HttpBundle bundle = null;
|
231 |
try {
|
232 |
bundle = (new ConnectionThrottleManager(getContext())).delete(loadBalancer);
|
233 |
} catch (CloudServersException e) {
|
234 |
exception = e; |
235 |
} |
236 |
return bundle;
|
237 |
} |
238 |
|
239 |
@Override
|
240 |
protected void onPostExecute(HttpBundle bundle) { |
241 |
hideDialog(); |
242 |
HttpResponse response = bundle.getResponse(); |
243 |
if (response != null) { |
244 |
int statusCode = response.getStatusLine().getStatusCode();
|
245 |
if (statusCode == 202 || statusCode == 200) { |
246 |
setResult(Activity.RESULT_OK); |
247 |
finish(); |
248 |
} else {
|
249 |
CloudServersException cse = parseCloudServersException(response); |
250 |
if ("".equals(cse.getMessage())) { |
251 |
showError("There was a problem editing the connection throttle.", bundle);
|
252 |
} else {
|
253 |
showError("There was a problem editing the connection throttle: " + cse.getMessage(), bundle);
|
254 |
} |
255 |
} |
256 |
} else if (exception != null) { |
257 |
showError("There was a problem editing the connection throttle: " + exception.getMessage(), bundle);
|
258 |
} |
259 |
} |
260 |
} |
261 |
|
262 |
} |