Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / ConnectionThrottleActivity.java @ 5018a7f8

History | View | Annotate | Download (8.3 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.view.View;
9
import android.view.View.OnClickListener;
10
import android.widget.Button;
11
import android.widget.EditText;
12

    
13
import com.rackspace.cloud.loadbalancer.api.client.ConnectionThrottle;
14
import com.rackspace.cloud.loadbalancer.api.client.ConnectionThrottleManager;
15
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancer;
16
import com.rackspace.cloud.servers.api.client.CloudServersException;
17
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
18

    
19
public class ConnectionThrottleActivity extends CloudActivity{
20

    
21
        private LoadBalancer loadBalancer;
22
        private ConnectionThrottle connectionThrottle;
23
        private EditText minCons;
24
        private EditText maxCons;
25
        private EditText maxConRate;
26
        private EditText rateInterval;
27

    
28
        private final String ENABLE = "Enable Throttle";
29
        private final String DISABLE = "Disable Throttle";
30

    
31
        @Override
32
        public void onCreate(Bundle savedInstanceState) {
33
                super.onCreate(savedInstanceState);
34
                loadBalancer = (LoadBalancer) this.getIntent().getExtras().get("loadBalancer");
35
                setContentView(R.layout.connectionthrottle);
36
                restoreState(savedInstanceState);
37
        }
38

    
39
        @Override
40
        protected void onSaveInstanceState(Bundle outState) {
41
                super.onSaveInstanceState(outState);
42
                outState.putSerializable("loadBalancer", loadBalancer);
43
        }
44

    
45

    
46
        protected void restoreState(Bundle state) {
47
                super.restoreState(state);
48

    
49
                if(state != null && state.containsKey("loadBalancer")){
50
                        loadBalancer = (LoadBalancer)state.getSerializable("loadBalancer");
51
                }
52
                connectionThrottle = loadBalancer.getConnectionThrottle();
53
                minCons = (EditText)findViewById(R.id.min_connections_text);
54
                maxCons = (EditText)findViewById(R.id.max_connections_text);
55
                maxConRate = (EditText)findViewById(R.id.max_connection_rate);
56
                rateInterval = (EditText)findViewById(R.id.rate_interval);
57

    
58
                setupButtons();
59
                setupText();
60
        }
61

    
62
        private void setupButtons(){
63
                Button enable = (Button)findViewById(R.id.enable_throttle_button);
64
                if(loadBalancer.getConnectionThrottle() == null){
65
                        enable.setText(ENABLE);
66
                } else {
67
                        enable.setText(DISABLE);
68
                }
69
                enable.setOnClickListener(new OnClickListener() {
70
                        Button enable = (Button)findViewById(R.id.enable_throttle_button);
71
                        @Override
72
                        public void onClick(View v) {
73
                                if(enable.getText().toString().equals(ENABLE)){
74
                                        ConnectionThrottle connectionThrottle = new ConnectionThrottle();
75
                                        connectionThrottle.setMinConnections("25");
76
                                        connectionThrottle.setMaxConnections("100");
77
                                        connectionThrottle.setMaxConnectionRate("25");
78
                                        connectionThrottle.setRateInterval("5");
79

    
80
                                        loadBalancer.setConnectionThrottle(connectionThrottle);
81
                                        //Turn on EditTexts
82
                                        minCons.setEnabled(true);                        
83
                                        maxCons.setEnabled(true);        
84
                                        maxConRate.setEnabled(true);
85
                                        rateInterval.setEnabled(true);
86
                                        enable.setText(DISABLE);
87
                                } else {
88
                                        loadBalancer.setConnectionThrottle(null);
89
                                        //Turn off EditTexts
90
                                        minCons.setEnabled(false);
91
                                        maxCons.setEnabled(false);
92
                                        maxConRate.setEnabled(false);
93
                                        rateInterval.setEnabled(false);
94
                                        enable.setText(ENABLE);
95
                                }
96
                        }        
97
                });
98

    
99
                Button submit = (Button)findViewById(R.id.save_throttle_button);
100
                submit.setOnClickListener(new OnClickListener() {
101

    
102
                        Button enable = (Button)findViewById(R.id.enable_throttle_button);
103

    
104
                        @Override
105
                        public void onClick(View v) {
106

    
107
                                connectionThrottle = new ConnectionThrottle();
108
                                connectionThrottle.setMaxConnectionRate(maxConRate.getText().toString());
109
                                connectionThrottle.setMinConnections(minCons.getText().toString());
110
                                connectionThrottle.setMaxConnections(maxCons.getText().toString());
111
                                connectionThrottle.setRateInterval(rateInterval.getText().toString());
112

    
113
                                if(enable.getText().toString().equals(DISABLE)){        
114
                                        if(validText()){
115
                                                //trackEvent(GoogleAnalytics.CATEGORY_LOAD_BALANCER, GoogleAnalytics.EVENT_LB_SESSION_PERSISTENCE, "", -1);
116
                                                new UpdateConnectionThrottleTask().execute();
117
                                        }
118
                                } else {
119
                                        //trackEvent(GoogleAnalytics.CATEGORY_LOAD_BALANCER, GoogleAnalytics.EVENT_LB_SESSION_PERSISTENCE, "", -1);
120
                                        new DeleteConnectionThrottleTask().execute();
121
                                }
122
                        }
123
                }); 
124
        }
125

    
126
        private void setupText(){
127
                if(loadBalancer.getConnectionThrottle() == null){
128
                        minCons.setEnabled(false);
129
                        maxCons.setEnabled(false);
130
                        maxConRate.setEnabled(false);
131
                        rateInterval.setEnabled(false);
132

    
133
                        //Set boxes to default values
134
                        minCons.setText("25");
135
                        maxCons.setText("100");
136
                        maxConRate.setText("25");
137
                        rateInterval.setText("5");
138
                } else {
139
                        ConnectionThrottle throttle = loadBalancer.getConnectionThrottle();
140

    
141
                        //restore the current values to the boxes
142
                        minCons.setText(throttle.getMinConnections());
143
                        maxCons.setText(throttle.getMaxConnections());
144
                        maxConRate.setText(throttle.getMaxConnectionRate());
145
                        rateInterval.setText(throttle.getRateInterval());
146
                }
147
        }
148

    
149
        private Boolean validText(){
150
                return validEditText(maxCons, 0, 100000, "Max Connections") 
151
                && validEditText(minCons, 0, 1000, "Min Connections") 
152
                && validEditText(maxConRate, 0, 100000, "Max Connection Rate") 
153
                && validEditText(rateInterval, 1, 3600, "Rate Interval");
154
        }
155

    
156
        private Boolean validEditText(EditText box, int min, int max, String boxName){
157
                String result = box.getText().toString();
158
                if(result.equals("")){
159
                        showAlert("Error", "Please enter a value for " + boxName + ".");
160
                        return false;
161
                } else {
162
                        try {
163
                                int value = Integer.parseInt(result);
164
                                if(value >= min && value <= max){
165
                                        return true;
166
                                } else {
167
                                        showAlert("Error", boxName + " must be an integer between " + min + " and " + max + " inclusive.");
168
                                        return false;
169
                                }
170
                        } catch (NumberFormatException e) {
171
                                showAlert("Error", boxName + " must be an integer between " + min + " and " + max + " inclusive.");
172
                                return false;
173
                        }
174
                }
175
        }
176

    
177
        public class UpdateConnectionThrottleTask extends AsyncTask<Void, Void, HttpBundle> {
178

    
179
                private CloudServersException exception;
180

    
181
                @Override
182
                protected void onPreExecute(){
183
                        showDialog();
184
                }
185

    
186
                @Override
187
                protected HttpBundle doInBackground(Void... arg0) {
188
                        HttpBundle bundle = null;
189
                        try {
190
                                bundle = (new ConnectionThrottleManager(getContext())).update(loadBalancer, connectionThrottle);
191
                        } catch (CloudServersException e) {
192
                                exception = e;
193
                        }
194
                        return bundle;
195
                }
196

    
197
                @Override
198
                protected void onPostExecute(HttpBundle bundle) {
199
                        hideDialog();
200
                        HttpResponse response = bundle.getResponse();
201
                        if (response != null) {
202
                                int statusCode = response.getStatusLine().getStatusCode();
203
                                if (statusCode == 202 || statusCode == 200) {
204
                                        setResult(Activity.RESULT_OK);
205
                                        finish();
206
                                } else {
207
                                        CloudServersException cse = parseCloudServersException(response);
208
                                        if ("".equals(cse.getMessage())) {
209
                                                showError("There was a problem editing the connection throttle.", bundle);
210
                                        } else {
211
                                                showError("There was a problem editing the connection throttle: " + cse.getMessage(), bundle);
212
                                        }
213
                                }
214
                        } else if (exception != null) {
215
                                showError("There was a problem editing the connection throttle: " + exception.getMessage(), bundle);                                
216
                        }                        
217
                }
218
        }
219

    
220
        public class DeleteConnectionThrottleTask extends AsyncTask<Void, Void, HttpBundle> {
221

    
222
                private CloudServersException exception;
223

    
224
                @Override
225
                protected void onPreExecute(){
226
                        showDialog();
227
                }
228

    
229
                @Override
230
                protected HttpBundle doInBackground(Void... arg0) {
231
                        HttpBundle bundle = null;
232
                        try {
233
                                bundle = (new ConnectionThrottleManager(getContext())).delete(loadBalancer);
234
                        } catch (CloudServersException e) {
235
                                exception = e;
236
                        }
237
                        return bundle;
238
                }
239

    
240
                @Override
241
                protected void onPostExecute(HttpBundle bundle) {
242
                        hideDialog();
243
                        HttpResponse response = bundle.getResponse();
244
                        if (response != null) {
245
                                int statusCode = response.getStatusLine().getStatusCode();
246
                                if (statusCode == 202 || statusCode == 200) {
247
                                        setResult(Activity.RESULT_OK);
248
                                        finish();
249
                                } else {
250
                                        CloudServersException cse = parseCloudServersException(response);
251
                                        if ("".equals(cse.getMessage())) {
252
                                                showError("There was a problem editing the connection throttle.", bundle);
253
                                        } else {
254
                                                showError("There was a problem editing the connection throttle: " + cse.getMessage(), bundle);
255
                                        }
256
                                }
257
                        } else if (exception != null) {
258
                                showError("There was a problem editing the connection throttle: " + exception.getMessage(), bundle);                                
259
                        }                        
260
                }
261
        }
262

    
263
}