root / src / com / rackspacecloud / android / ConnectionThrottleActivity.java @ 16615abd
History | View | Annotate | Download (7.6 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 |
protected void restoreState(Bundle state) { |
41 |
super.restoreState(state);
|
42 |
|
43 |
minCons = (EditText)findViewById(R.id.min_connections_text); |
44 |
maxCons = (EditText)findViewById(R.id.max_connections_text); |
45 |
maxConRate = (EditText)findViewById(R.id.max_connection_rate); |
46 |
rateInterval = (EditText)findViewById(R.id.rate_interval); |
47 |
|
48 |
setupButtons(); |
49 |
setupText(); |
50 |
} |
51 |
|
52 |
private void setupButtons(){ |
53 |
Button enable = (Button)findViewById(R.id.enable_throttle_button); |
54 |
if(loadBalancer.getConnectionThrottle() == null){ |
55 |
enable.setText(ENABLE); |
56 |
} else {
|
57 |
enable.setText(DISABLE); |
58 |
} |
59 |
enable.setOnClickListener(new OnClickListener() {
|
60 |
Button enable = (Button)findViewById(R.id.enable_throttle_button); |
61 |
@Override
|
62 |
public void onClick(View v) { |
63 |
if(enable.getText().toString().equals(ENABLE)){
|
64 |
//Turn on EditTexts
|
65 |
minCons.setEnabled(true);
|
66 |
maxCons.setEnabled(true);
|
67 |
maxConRate.setEnabled(true);
|
68 |
rateInterval.setEnabled(true);
|
69 |
enable.setText(DISABLE); |
70 |
} else {
|
71 |
//Turn off EditTexts
|
72 |
minCons.setEnabled(false);
|
73 |
maxCons.setEnabled(false);
|
74 |
maxConRate.setEnabled(false);
|
75 |
rateInterval.setEnabled(false);
|
76 |
enable.setText(ENABLE); |
77 |
} |
78 |
} |
79 |
}); |
80 |
|
81 |
Button submit = (Button)findViewById(R.id.save_throttle_button); |
82 |
submit.setOnClickListener(new OnClickListener() {
|
83 |
|
84 |
Button enable = (Button)findViewById(R.id.enable_throttle_button); |
85 |
|
86 |
@Override
|
87 |
public void onClick(View v) { |
88 |
|
89 |
connectionThrottle = new ConnectionThrottle();
|
90 |
connectionThrottle.setMaxConnectionRate(maxConRate.getText().toString()); |
91 |
connectionThrottle.setMinConnections(minCons.getText().toString()); |
92 |
connectionThrottle.setMaxConnections(maxCons.getText().toString()); |
93 |
connectionThrottle.setRateInterval(rateInterval.getText().toString()); |
94 |
|
95 |
if(enable.getText().toString().equals(DISABLE)){
|
96 |
if(validText()){
|
97 |
new UpdateConnectionThrottleTask().execute();
|
98 |
} |
99 |
} else {
|
100 |
//if there was no connection throttle before
|
101 |
//then no need to delete it
|
102 |
if(loadBalancer.getConnectionThrottle() != null){ |
103 |
new DeleteConnectionThrottleTask().execute();
|
104 |
} else {
|
105 |
finish(); |
106 |
} |
107 |
} |
108 |
} |
109 |
}); |
110 |
} |
111 |
|
112 |
private void setupText(){ |
113 |
if(loadBalancer.getConnectionThrottle() == null){ |
114 |
minCons.setEnabled(false);
|
115 |
maxCons.setEnabled(false);
|
116 |
maxConRate.setEnabled(false);
|
117 |
rateInterval.setEnabled(false);
|
118 |
|
119 |
//Set boxes to default values
|
120 |
minCons.setText("25");
|
121 |
maxCons.setText("100");
|
122 |
maxConRate.setText("25");
|
123 |
rateInterval.setText("5");
|
124 |
} else {
|
125 |
ConnectionThrottle throttle = loadBalancer.getConnectionThrottle(); |
126 |
|
127 |
//restore the current values to the boxes
|
128 |
minCons.setText(throttle.getMinConnections()); |
129 |
maxCons.setText(throttle.getMaxConnections()); |
130 |
maxConRate.setText(throttle.getMaxConnectionRate()); |
131 |
rateInterval.setText(throttle.getRateInterval()); |
132 |
} |
133 |
} |
134 |
|
135 |
private Boolean validText(){ |
136 |
return validEditText(maxCons, 0, 100000, "Max Connections") |
137 |
&& validEditText(minCons, 0, 1000, "Min Connections") |
138 |
&& validEditText(maxConRate, 0, 100000, "Max Connection Rate") |
139 |
&& validEditText(rateInterval, 1, 3600, "Rate Interval"); |
140 |
} |
141 |
|
142 |
private Boolean validEditText(EditText box, int min, int max, String boxName){ |
143 |
String result = box.getText().toString();
|
144 |
if(result.equals("")){ |
145 |
showAlert("Error", "Please enter a value for " + boxName + "."); |
146 |
return false; |
147 |
} else {
|
148 |
try {
|
149 |
int value = Integer.parseInt(result); |
150 |
Log.d("info", min + " <= " + value + " <= " + max); |
151 |
if(value >= min && value <= max){
|
152 |
return true; |
153 |
} else {
|
154 |
showAlert("Error", boxName + " must be an integer between " + min + " and " + max + " inclusive."); |
155 |
return false; |
156 |
} |
157 |
} catch (NumberFormatException e) { |
158 |
showAlert("Error", boxName + " must be an integer between " + min + " and " + max + " inclusive."); |
159 |
return false; |
160 |
} |
161 |
} |
162 |
} |
163 |
|
164 |
public class UpdateConnectionThrottleTask extends AsyncTask<Void, Void, HttpBundle> { |
165 |
|
166 |
private CloudServersException exception;
|
167 |
|
168 |
@Override
|
169 |
protected void onPreExecute(){ |
170 |
showDialog(); |
171 |
} |
172 |
|
173 |
@Override
|
174 |
protected HttpBundle doInBackground(Void... arg0) { |
175 |
HttpBundle bundle = null;
|
176 |
try {
|
177 |
bundle = (new ConnectionThrottleManager(getContext())).update(loadBalancer, connectionThrottle);
|
178 |
} catch (CloudServersException e) {
|
179 |
exception = e; |
180 |
} |
181 |
return bundle;
|
182 |
} |
183 |
|
184 |
@Override
|
185 |
protected void onPostExecute(HttpBundle bundle) { |
186 |
hideDialog(); |
187 |
HttpResponse response = bundle.getResponse(); |
188 |
if (response != null) { |
189 |
int statusCode = response.getStatusLine().getStatusCode();
|
190 |
if (statusCode == 202) { |
191 |
setResult(Activity.RESULT_OK); |
192 |
finish(); |
193 |
} else {
|
194 |
CloudServersException cse = parseCloudServersException(response); |
195 |
if ("".equals(cse.getMessage())) { |
196 |
showError("There was a problem editing the connection throttle.", bundle);
|
197 |
} else {
|
198 |
showError("There was a problem editing the connection throttle: " + cse.getMessage(), bundle);
|
199 |
} |
200 |
} |
201 |
} else if (exception != null) { |
202 |
showError("There was a problem editing the connection throttle: " + exception.getMessage(), bundle);
|
203 |
} |
204 |
} |
205 |
} |
206 |
|
207 |
public class DeleteConnectionThrottleTask extends AsyncTask<Void, Void, HttpBundle> { |
208 |
|
209 |
private CloudServersException exception;
|
210 |
|
211 |
@Override
|
212 |
protected void onPreExecute(){ |
213 |
showDialog(); |
214 |
} |
215 |
|
216 |
@Override
|
217 |
protected HttpBundle doInBackground(Void... arg0) { |
218 |
HttpBundle bundle = null;
|
219 |
try {
|
220 |
bundle = (new ConnectionThrottleManager(getContext())).delete(loadBalancer);
|
221 |
} catch (CloudServersException e) {
|
222 |
exception = e; |
223 |
} |
224 |
return bundle;
|
225 |
} |
226 |
|
227 |
@Override
|
228 |
protected void onPostExecute(HttpBundle bundle) { |
229 |
hideDialog(); |
230 |
HttpResponse response = bundle.getResponse(); |
231 |
if (response != null) { |
232 |
int statusCode = response.getStatusLine().getStatusCode();
|
233 |
if (statusCode == 202) { |
234 |
setResult(Activity.RESULT_OK); |
235 |
finish(); |
236 |
} else {
|
237 |
CloudServersException cse = parseCloudServersException(response); |
238 |
if ("".equals(cse.getMessage())) { |
239 |
showError("There was a problem editing the connection throttle.", bundle);
|
240 |
} else {
|
241 |
showError("There was a problem editing the connection throttle: " + cse.getMessage(), bundle);
|
242 |
} |
243 |
} |
244 |
} else if (exception != null) { |
245 |
showError("There was a problem editing the connection throttle: " + exception.getMessage(), bundle);
|
246 |
} |
247 |
} |
248 |
} |
249 |
|
250 |
} |