mirror of
https://github.com/cwinfo/envayasms.git
synced 2024-12-04 20:45:32 +00:00
use http connection pooling to improve performance (especially over ssl)
This commit is contained in:
parent
eb808372f8
commit
a793a5f2e3
@ -18,12 +18,23 @@ import android.text.SpannableStringBuilder;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import org.apache.http.client.HttpClient;
|
||||||
|
import org.apache.http.conn.params.ConnManagerParams;
|
||||||
|
import org.apache.http.conn.scheme.PlainSocketFactory;
|
||||||
|
import org.apache.http.conn.scheme.Scheme;
|
||||||
|
import org.apache.http.conn.scheme.SchemeRegistry;
|
||||||
|
import org.apache.http.conn.ssl.SSLSocketFactory;
|
||||||
|
import org.apache.http.impl.client.DefaultHttpClient;
|
||||||
|
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
|
||||||
import org.apache.http.message.BasicNameValuePair;
|
import org.apache.http.message.BasicNameValuePair;
|
||||||
|
import org.apache.http.params.BasicHttpParams;
|
||||||
|
import org.apache.http.params.HttpConnectionParams;
|
||||||
|
import org.apache.http.params.HttpParams;
|
||||||
|
import org.apache.http.params.HttpProtocolParams;
|
||||||
import org.envaya.kalsms.receiver.OutgoingMessagePoller;
|
import org.envaya.kalsms.receiver.OutgoingMessagePoller;
|
||||||
import org.envaya.kalsms.task.HttpTask;
|
import org.envaya.kalsms.task.HttpTask;
|
||||||
import org.envaya.kalsms.task.PollerTask;
|
import org.envaya.kalsms.task.PollerTask;
|
||||||
@ -83,13 +94,19 @@ public final class App extends Application {
|
|||||||
private SharedPreferences settings;
|
private SharedPreferences settings;
|
||||||
private MmsObserver mmsObserver;
|
private MmsObserver mmsObserver;
|
||||||
private SpannableStringBuilder displayedLog = new SpannableStringBuilder();
|
private SpannableStringBuilder displayedLog = new SpannableStringBuilder();
|
||||||
private long lastLogTime;
|
private long lastLogTime;
|
||||||
|
|
||||||
|
// list of package names (e.g. org.envaya.kalsms, or org.envaya.kalsms.packXX)
|
||||||
|
// for this package and all expansion packs
|
||||||
private List<String> outgoingMessagePackages = new ArrayList<String>();
|
private List<String> outgoingMessagePackages = new ArrayList<String>();
|
||||||
|
|
||||||
|
// count to provide round-robin selection of expansion packs
|
||||||
private int outgoingMessageCount = -1;
|
private int outgoingMessageCount = -1;
|
||||||
|
|
||||||
|
// map of package name => sorted list of timestamps of outgoing messages
|
||||||
private HashMap<String, ArrayList<Long>> outgoingTimestamps
|
private HashMap<String, ArrayList<Long>> outgoingTimestamps
|
||||||
= new HashMap<String, ArrayList<Long>>();
|
= new HashMap<String, ArrayList<Long>>();
|
||||||
|
|
||||||
private MmsUtils mmsUtils;
|
private MmsUtils mmsUtils;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -147,6 +164,7 @@ public final class App extends Application {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ArrayList<Long> sent = outgoingTimestamps.get(packageName);
|
ArrayList<Long> sent = outgoingTimestamps.get(packageName);
|
||||||
|
|
||||||
Long ct = System.currentTimeMillis();
|
Long ct = System.currentTimeMillis();
|
||||||
|
|
||||||
//log(packageName + " SMS send size=" + sent.size());
|
//log(packageName + " SMS send size=" + sent.size());
|
||||||
@ -599,5 +617,34 @@ public final class App extends Application {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private HttpClient httpClient;
|
||||||
|
|
||||||
|
public synchronized HttpClient getHttpClient()
|
||||||
|
{
|
||||||
|
if (httpClient == null)
|
||||||
|
{
|
||||||
|
// via http://thinkandroid.wordpress.com/2009/12/31/creating-an-http-client-example/
|
||||||
|
// also http://hc.apache.org/httpclient-3.x/threading.html
|
||||||
|
|
||||||
|
HttpParams httpParams = new BasicHttpParams();
|
||||||
|
HttpConnectionParams.setConnectionTimeout(httpParams, 8000);
|
||||||
|
HttpConnectionParams.setSoTimeout(httpParams, 8000);
|
||||||
|
HttpProtocolParams.setContentCharset(httpParams, "utf-8");
|
||||||
|
|
||||||
|
SchemeRegistry registry = new SchemeRegistry();
|
||||||
|
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
|
||||||
|
|
||||||
|
final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
|
||||||
|
sslSocketFactory.setHostnameVerifier(SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
|
||||||
|
|
||||||
|
registry.register(new Scheme("https", sslSocketFactory, 443));
|
||||||
|
|
||||||
|
ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(httpParams, registry);
|
||||||
|
|
||||||
|
httpClient = new DefaultHttpClient(manager, httpParams);
|
||||||
|
}
|
||||||
|
return httpClient;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,9 +14,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
import javax.xml.parsers.DocumentBuilder;
|
import javax.xml.parsers.DocumentBuilder;
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
import javax.xml.parsers.DocumentBuilderFactory;
|
||||||
import javax.xml.parsers.ParserConfigurationException;
|
import javax.xml.parsers.ParserConfigurationException;
|
||||||
@ -25,15 +23,10 @@ import org.apache.http.client.HttpClient;
|
|||||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||||
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.client.methods.HttpPost;
|
||||||
import org.apache.http.entity.mime.FormBodyPart;
|
import org.apache.http.entity.mime.FormBodyPart;
|
||||||
import org.apache.http.entity.mime.HttpMultipartMode;
|
|
||||||
import org.apache.http.entity.mime.MultipartEntity;
|
import org.apache.http.entity.mime.MultipartEntity;
|
||||||
import org.apache.http.entity.mime.content.ContentBody;
|
|
||||||
import org.apache.http.entity.mime.content.StringBody;
|
import org.apache.http.entity.mime.content.StringBody;
|
||||||
import org.apache.http.impl.client.DefaultHttpClient;
|
|
||||||
import org.apache.http.message.BasicNameValuePair;
|
import org.apache.http.message.BasicNameValuePair;
|
||||||
import org.apache.http.params.BasicHttpParams;
|
import org.apache.http.util.EntityUtils;
|
||||||
import org.apache.http.params.HttpConnectionParams;
|
|
||||||
import org.apache.http.params.HttpParams;
|
|
||||||
import org.envaya.kalsms.App;
|
import org.envaya.kalsms.App;
|
||||||
import org.envaya.kalsms.Base64Coder;
|
import org.envaya.kalsms.Base64Coder;
|
||||||
import org.envaya.kalsms.OutgoingMessage;
|
import org.envaya.kalsms.OutgoingMessage;
|
||||||
@ -53,6 +46,8 @@ public class HttpTask extends AsyncTask<String, Void, HttpResponse> {
|
|||||||
private List<FormBodyPart> formParts;
|
private List<FormBodyPart> formParts;
|
||||||
private boolean useMultipartPost = false;
|
private boolean useMultipartPost = false;
|
||||||
|
|
||||||
|
private HttpPost post;
|
||||||
|
|
||||||
public HttpTask(App app, BasicNameValuePair... paramsArr)
|
public HttpTask(App app, BasicNameValuePair... paramsArr)
|
||||||
{
|
{
|
||||||
super();
|
super();
|
||||||
@ -67,15 +62,7 @@ public class HttpTask extends AsyncTask<String, Void, HttpResponse> {
|
|||||||
{
|
{
|
||||||
useMultipartPost = true;
|
useMultipartPost = true;
|
||||||
this.formParts = formParts;
|
this.formParts = formParts;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpClient getHttpClient()
|
|
||||||
{
|
|
||||||
HttpParams httpParameters = new BasicHttpParams();
|
|
||||||
HttpConnectionParams.setConnectionTimeout(httpParameters, 8000);
|
|
||||||
HttpConnectionParams.setSoTimeout(httpParameters, 8000);
|
|
||||||
return new DefaultHttpClient(httpParameters);
|
|
||||||
}
|
|
||||||
|
|
||||||
private String getSignature()
|
private String getSignature()
|
||||||
throws NoSuchAlgorithmException, UnsupportedEncodingException
|
throws NoSuchAlgorithmException, UnsupportedEncodingException
|
||||||
@ -110,16 +97,15 @@ public class HttpTask extends AsyncTask<String, Void, HttpResponse> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected HttpResponse doInBackground(String... ignored) {
|
protected HttpResponse doInBackground(String... ignored) {
|
||||||
|
if (url.length() == 0) {
|
||||||
|
app.log("Can't contact server; Server URL not set");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
post = new HttpPost(url);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (url.length() == 0) {
|
|
||||||
app.log("Can't contact server; Server URL not set");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
HttpPost post = new HttpPost(url);
|
|
||||||
|
|
||||||
|
|
||||||
if (useMultipartPost)
|
if (useMultipartPost)
|
||||||
{
|
{
|
||||||
MultipartEntity entity = new MultipartEntity();//HttpMultipartMode.BROWSER_COMPATIBLE);
|
MultipartEntity entity = new MultipartEntity();//HttpMultipartMode.BROWSER_COMPATIBLE);
|
||||||
@ -144,9 +130,9 @@ public class HttpTask extends AsyncTask<String, Void, HttpResponse> {
|
|||||||
|
|
||||||
post.setHeader("X-Kalsms-Signature", signature);
|
post.setHeader("X-Kalsms-Signature", signature);
|
||||||
|
|
||||||
HttpClient client = getHttpClient();
|
HttpClient client = app.getHttpClient();
|
||||||
HttpResponse response = client.execute(post);
|
HttpResponse response = client.execute(post);
|
||||||
|
|
||||||
int statusCode = response.getStatusLine().getStatusCode();
|
int statusCode = response.getStatusLine().getStatusCode();
|
||||||
if (statusCode == 200)
|
if (statusCode == 200)
|
||||||
{
|
{
|
||||||
@ -154,29 +140,32 @@ public class HttpTask extends AsyncTask<String, Void, HttpResponse> {
|
|||||||
}
|
}
|
||||||
else if (statusCode == 403)
|
else if (statusCode == 403)
|
||||||
{
|
{
|
||||||
|
response.getEntity().consumeContent();
|
||||||
app.log("Failed to authenticate to server");
|
app.log("Failed to authenticate to server");
|
||||||
app.log("(Phone number or password may be incorrect)");
|
app.log("(Phone number or password may be incorrect)");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
response.getEntity().consumeContent();
|
||||||
app.log("Received HTTP " + statusCode + " from server");
|
app.log("Received HTTP " + statusCode + " from server");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (IOException ex)
|
catch (IOException ex)
|
||||||
{
|
{
|
||||||
|
post.abort();
|
||||||
app.logError("Error while contacting server", ex);
|
app.logError("Error while contacting server", ex);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
catch (Throwable ex)
|
catch (Throwable ex)
|
||||||
{
|
{
|
||||||
|
post.abort();
|
||||||
app.logError("Unexpected error while contacting server", ex, true);
|
app.logError("Unexpected error while contacting server", ex, true);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String getDefaultToAddress()
|
protected String getDefaultToAddress()
|
||||||
{
|
{
|
||||||
return "";
|
return "";
|
||||||
@ -224,6 +213,7 @@ public class HttpTask extends AsyncTask<String, Void, HttpResponse> {
|
|||||||
}
|
}
|
||||||
catch (Throwable ex)
|
catch (Throwable ex)
|
||||||
{
|
{
|
||||||
|
post.abort();
|
||||||
app.logError("Error processing server response", ex);
|
app.logError("Error processing server response", ex);
|
||||||
handleFailure();
|
handleFailure();
|
||||||
}
|
}
|
||||||
@ -236,6 +226,10 @@ public class HttpTask extends AsyncTask<String, Void, HttpResponse> {
|
|||||||
|
|
||||||
protected void handleResponse(HttpResponse response) throws Exception
|
protected void handleResponse(HttpResponse response) throws Exception
|
||||||
{
|
{
|
||||||
|
if (response != null)
|
||||||
|
{
|
||||||
|
response.getEntity().consumeContent();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void handleFailure()
|
protected void handleFailure()
|
||||||
|
Loading…
Reference in New Issue
Block a user