Skip to content
Browse files

CL-64: To tear down TCP connection upon unsubscribe

  • Loading branch information...
1 parent b31de70 commit 2266fb249a594a7b042fe3981666087510927a92 raju committed Aug 2, 2012
View
2 android/PubnubAndroidTest/AndroidManifest.xml
@@ -7,6 +7,8 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
+ <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
+ <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".PubnubTestActivity"
View
1 android/PubnubAndroidTest/lint.xml
@@ -1,3 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<lint>
+ <issue id="ParserError" severity="ignore" />
</lint>
View
78 android/PubnubAndroidTest/src/com/fbt/Pubnub.java
@@ -6,7 +6,9 @@
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
+import java.util.Enumeration;
import java.util.HashMap;
+import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
@@ -21,10 +23,14 @@
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.params.BasicHttpParams;
+import org.apache.http.params.HttpConnectionParams;
+import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;
import pubnub.PubnubCrypto;
+import android.content.Context;
/**
* PubNub 3.1 Real-time Push Cloud API
@@ -44,6 +50,8 @@
boolean connected, first;
}
private List<ChannelStatus> subscriptions;
+ private Hashtable<String, Object> connection = new Hashtable<String, Object>();
+ private Context mContext;
/**
* PubNub 3.1 with Cipher Key
@@ -247,7 +255,7 @@ public JSONArray publish( HashMap<String, Object> args ) {
url.add(message.toString());
// Return JSONArray
- return _request(url);
+ return _request(url, channel);
}
/**
@@ -353,7 +361,7 @@ private void _subscribe( HashMap<String, Object> args ) {
return;
// Wait for Message
- JSONArray response = _request(url);
+ JSONArray response = _request(url, channel);
// Stop Connection?
for (ChannelStatus it : subscriptions) {
@@ -500,9 +508,9 @@ public JSONArray history( HashMap<String, Object> args ) {
if (this.CIPHER_KEY.length() > 0) {
// Decrpyt Messages
PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY);
- return pc.decryptJSONArray(_request(url));
+ return pc.decryptJSONArray(_request(url, channel));
} else {
- return _request(url);
+ return _request(url, channel);
}
}
@@ -519,7 +527,7 @@ public double time() {
url.add("time");
url.add("0");
- JSONArray response = _request(url);
+ JSONArray response = _request(url, null);
return response.optDouble(0);
}
@@ -551,6 +559,18 @@ public void unsubscribe( HashMap<String, Object> args ) {
break;
}
}
+ Enumeration<String> e = connection.keys();
+ while (e.hasMoreElements()) {
+
+ String ch = (String) e.nextElement();
+ if (ch.equals(channel))
+ {
+ HttpClient http = (HttpClient) connection.get(ch);
+ http.getConnectionManager().shutdown();
+ if (connection.containsKey(ch))
+ connection.remove(ch);
+ }
+ }
}
/**
@@ -559,7 +579,7 @@ public void unsubscribe( HashMap<String, Object> args ) {
* @param List<String> request of url directories.
* @return JSONArray from JSON response.
*/
- private JSONArray _request(List<String> url_components) {
+ private JSONArray _request(List<String> url_components, String channel) {
String json = "";
StringBuilder url = new StringBuilder();
Iterator<String> url_iterator = url_components.iterator();
@@ -574,15 +594,16 @@ private JSONArray _request(List<String> url_components) {
} catch (Exception e) {
e.printStackTrace();
JSONArray jsono = new JSONArray();
- try { jsono.put("Failed UTF-8 Encoding URL."); }
+ try {
+ jsono.put(0);
+ jsono.put("Failed UTF-8 Encoding URL."); }
catch (Exception jsone) {}
return jsono;
}
}
try {
-
- PubnubHttpRequest request = new PubnubHttpRequest(url.toString());
+ PubnubHttpRequest request = new PubnubHttpRequest(url.toString(),channel);
FutureTask<String> task = new FutureTask<String>(request);
Thread t = new Thread(task);
t.start();
@@ -592,12 +613,12 @@ private JSONArray _request(List<String> url_components) {
JSONArray jsono = new JSONArray();
try {
- jsono.put("Failed to Concurrent HTTP Request.");
+ jsono.put(0);
+ jsono.put("Request failed due to missing Internet connection.");
} catch (Exception jsone) {
}
- e.printStackTrace();
- System.out.println(e);
+ System.out.println(e.getMessage());
return jsono;
}
@@ -606,10 +627,12 @@ private JSONArray _request(List<String> url_components) {
JSONArray jsono = new JSONArray();
- try { jsono.put("Failed JSONP HTTP Request."); }
+ try {
+ jsono.put(0);
+ jsono.put("Failed JSONP HTTP Request."); }
catch (Exception jsone) {}
- System.out.println(e);
+ System.out.println(e.getMessage());
return jsono;
}
@@ -619,11 +642,12 @@ private JSONArray _request(List<String> url_components) {
catch (Exception e) {
JSONArray jsono = new JSONArray();
- try { jsono.put("Failed JSON Parsing."); }
+ try {
+ jsono.put(0);
+ jsono.put("Failed JSON Parsing."); }
catch (Exception jsone) {}
- e.printStackTrace();
- System.out.println(e);
+ System.out.println(e.getMessage());
// Return Failure to Parse
return jsono;
@@ -632,22 +656,28 @@ private JSONArray _request(List<String> url_components) {
private class PubnubHttpRequest implements Callable<String> {
- String url;
+ String url, channel;
- public PubnubHttpRequest(String url) {
+ public PubnubHttpRequest(String url, String channel) {
this.url = url;
+ this.channel = channel;
}
@Override
public String call() throws Exception {
// Prepare request
String line = "", json = "";
- HttpClient httpclient = new DefaultHttpClient();
+ HttpParams httpParams = new BasicHttpParams();
+ HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
+ HttpConnectionParams.setSoTimeout(httpParams, 310000);
+ HttpClient httpclient = new DefaultHttpClient(httpParams);
HttpUriRequest request = new HttpGet(url);
request.setHeader("V", "3.1");
request.setHeader("User-Agent", "Java-Android");
request.setHeader("Accept-Encoding", "gzip");
- httpclient.getParams().setParameter("http.connection.timeout", 310000);
+ if (channel != null) {
+ connection.put(channel, httpclient);
+ }
// Execute request
HttpResponse response;
@@ -674,8 +704,10 @@ public String call() throws Exception {
}
reader.close();
}
-
- return json;
+ if (channel != null) {
+ connection.remove(channel);
+ }
+ return json;
}
}
View
1,018 android/PubnubAndroidTest/src/com/fbt/PubnubTestActivity.java
@@ -1,498 +1,520 @@
-package com.fbt;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-
-import org.json.JSONArray;
-import org.json.JSONObject;
-
-import android.app.Activity;
-import android.app.AlertDialog;
-import android.content.DialogInterface;
-import android.os.AsyncTask;
-import android.os.Bundle;
-import android.os.Handler;
-import android.os.Message;
-import android.util.Log;
-import android.view.View;
-import android.view.View.OnClickListener;
-import android.widget.Button;
-import android.widget.EditText;
-import android.widget.Toast;
-
-/**
- * PubnubTestActivity
- *
- */
-public class PubnubTestActivity extends Activity {
-
- Pubnub pubnub;
- String myMessage = "", channel = "hello_world";
- EditText ed;
- RefreshHandler r = new RefreshHandler();
- int limit = 5;
-
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- ed = (EditText) findViewById(R.id.editText1);
-
- // Android: (Init)
- pubnub = new Pubnub("demo", // PUBLISH_KEY
- "demo", // SUBSCRIBE_KEY
- "demo", // SECRET_KEY
- "", // CIPHER_KEY
- true // SSL_ON?
- );
-
- XMLDownloader d = new XMLDownloader();
- d.execute("xml");
-
- Button publishBtn = (Button) findViewById(R.id.publishBtn);
- publishBtn.setOnClickListener(new OnClickListener() {
-
- public void onClick(View v) {
-
- // Android: (Publish)
-
- // Create JSON Message
- JSONObject message = new JSONObject();
- try {
- if (ed.getText().toString() != null
- && !ed.getText().toString().equals(""))
- message.put("Message", ed.getText().toString());
- } catch (org.json.JSONException jsonError) {
- }
-
- // Publish Message
- HashMap<String, Object> args = new HashMap<String, Object>(2);
- args.put("channel", channel); // Channel Name
- args.put("message", message); // JSON Message
- JSONArray info = pubnub.publish(args);
-
- // Print Response from PubNub JSONP REST Service
- System.out.println(info);
- }
- });
-
- Button unsubscribeBtn = (Button) findViewById(R.id.unsubscribeBtn);
- unsubscribeBtn.setOnClickListener(new OnClickListener() {
-
- public void onClick(View v) {
-
- // Android: (Unsubscribe/disconnect)
- HashMap<String, Object> args = new HashMap<String, Object>(1);
- args.put("channel", channel);
- pubnub.unsubscribe(args);
- }
- });
-
- Button subscribeBtn = (Button) findViewById(R.id.subscribeBtn);
- subscribeBtn.setOnClickListener(new OnClickListener() {
-
- public void onClick(View v) {
-
- // Android: (Subscribe)
- XMLDownloader d = new XMLDownloader();
- d.execute("xml");
- }
- });
-
- Button historyBtn = (Button) findViewById(R.id.historyBtn);
- historyBtn.setOnClickListener(new OnClickListener() {
-
- public void onClick(View v) {
-
- // Android: (History)
-
- System.out.print(" HISTORY: ");
- HashMap<String, Object> args = new HashMap<String, Object>(2);
- args.put("channel", channel);
- args.put("limit", limit);
-
- // Get History
- JSONArray response = pubnub.history(args);
-
- // Print Response from PubNub JSONP REST Service
- try {
- if (response != null) {
- StringBuffer messages = new StringBuffer("");
- for (int i = 0; i < response.length(); i++) {
- Object o = response.get(i);
- String message = o.toString() + "\n\n";
- messages.append(message);
- }
- final AlertDialog.Builder b = new AlertDialog.Builder(
- PubnubTestActivity.this);
- b.setIcon(android.R.drawable.ic_dialog_alert);
- b.setTitle("History: ");
- b.setMessage(messages.toString());
- b.setNegativeButton("OK",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog,
- int which) {
- }
- });
- b.show();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
-
- Button uuidBtn = (Button) findViewById(R.id.uuidBtn);
- uuidBtn.setOnClickListener(new OnClickListener() {
-
- public void onClick(View v) {
-
- // Android: (UUID)
-
- String uuid = Pubnub.uuid();
- System.out.println(" UUID: " + uuid);
- final AlertDialog.Builder b = new AlertDialog.Builder(
- PubnubTestActivity.this);
- b.setIcon(android.R.drawable.ic_dialog_alert);
- b.setTitle("UUID: ");
- b.setMessage(uuid);
- b.setNegativeButton("OK",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog,
- int which) {
- }
- });
- b.show();
- }
- });
-
- Button timeBtn = (Button) findViewById(R.id.timeBtn);
- timeBtn.setOnClickListener(new OnClickListener() {
-
- public void onClick(View v) {
-
- // Android: (Time)
-
- double time = pubnub.time();
- System.out.println(" Time: " + Double.toString(time));
- final AlertDialog.Builder b = new AlertDialog.Builder(
- PubnubTestActivity.this);
- b.setIcon(android.R.drawable.ic_dialog_alert);
- b.setTitle("Time: ");
- b.setMessage(Double.toString(time));
- b.setNegativeButton("OK",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog,
- int which) {
- }
- });
- b.show();
- }
- });
- }
-
- public void allMessageClick(View v) {
- JSONObject message = new JSONObject();
- try {
- message.put("some_val", "Hello World! --> ɂ顶@#$%^&*()!");
- /*
- * message.put( "title", "Android PubNub"); message.put( "text",
- * "This is a push to all users! woot!"); message.put( "url",
- * "http://www.pubnub.com");
- */
- } catch (org.json.JSONException jsonError) {
- }
-
- // Publish
- HashMap<String, Object> args = new HashMap<String, Object>(2);
- args.put("channel", channel);
- args.put("message", message);
- JSONArray response = null;
- response = pubnub.publish(args);
- System.out.println(response);
-
- args = new HashMap<String, Object>(2);
- args.put("channel", channel);
- args.put("message", "Hello World");
-
- response = pubnub.publish(args);
- System.out.println(response);
-
- JSONArray array = new JSONArray();
- array.put("Sunday");
- array.put("Monday");
- array.put("Tuesday");
- array.put("Wednesday");
- array.put("Thursday");
- array.put("Friday");
- array.put("Saturday");
-
- args = new HashMap<String, Object>(2);
- args.put("channel", channel);
- args.put("message", array);
-
- response = pubnub.publish(args);
- System.out.println(response);
- }
-
- class RefreshHandler extends Handler {
- @Override
- public void handleMessage(Message msg) {
- Log.v("IN", "HANDLER");
- Toast.makeText(PubnubTestActivity.this, "You got message",
- Toast.LENGTH_LONG).show();
- final AlertDialog.Builder b = new AlertDialog.Builder(
- PubnubTestActivity.this);
- b.setIcon(android.R.drawable.ic_dialog_alert);
- b.setTitle("PUBNUB");
- b.setMessage(myMessage);
-
- b.setNegativeButton("OK", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- }
- });
- b.show();
- }
- };
-
- class XMLDownloader extends AsyncTask<String, Void, Boolean> {
-
- @Override
- protected Boolean doInBackground(String... params) {
- try {
- // Android: (Subscribe)
-
- class Receiver implements Callback {
- public boolean subscribeCallback(String channel,
- Object message) {
- Log.i("Message Received", message.toString());
- myMessage = message.toString();
- r.sendEmptyMessage(0);
- return true;
- }
-
- @Override
- public void errorCallback(String channel, Object message) {
- Log.e("ErrorCallback", "Channel:" + channel + "-"
- + message.toString());
- }
-
- @Override
- public void connectCallback(String channel) {
- Log.i("ConnectCallback", "Connected to channel :"
- + channel);
- }
-
- @Override
- public void reconnectCallback(String channel) {
- Log.i("ReconnectCallback", "Reconnected to channel :"
- + channel);
- }
-
- @Override
- public void disconnectCallback(String channel) {
- Log.i("DisconnectCallback", "Disconnected to channel :"
- + channel);
- }
- }
-
- // Listen for Messages (Subscribe)
- HashMap<String, Object> args = new HashMap<String, Object>(2);
- args.put("channel", channel); // Channel Name
- args.put("callback", new Receiver()); // Callback to get response
- pubnub.subscribe(args);
-
- } catch (Exception e) {
- e.printStackTrace();
- Log.v("ERROR", "While downloading");
- }
-
- return Boolean.TRUE;
- }
-
- @Override
- protected void onPreExecute() {
- }
-
- protected void onPostExecute(Boolean result) {
- }
- }
-
- String publish_key = "demo", subscribe_key = "demo";
- String secret_key = "demo", cipher_key = "";
- Boolean ssl_on = false;
-
- // User Supplied Options PubNub
- Pubnub pubnub_user_supplied_options = new Pubnub(publish_key, // OPTIONAL (supply "" to disable)
- subscribe_key, // REQUIRED
- secret_key, // OPTIONAL (supply "" to disable)
- cipher_key, // OPTIONAL (supply "" to disable)
- ssl_on // OPTIONAL (supply "" to disable)
- );
-
- // High Security PubNub
- Pubnub pubnub_high_security = new Pubnub(
- // Publish Key
- "pub-c-a30c030e-9f9c-408d-be89-d70b336ca7a0",
-
- // Subscribe Key
- "sub-c-387c90f3-c018-11e1-98c9-a5220e0555fd",
-
- // Secret Key
- "sec-c-MTliNDE0NTAtYjY4Ni00MDRkLTllYTItNDhiZGE0N2JlYzBl",
-
- // Cipher Key
- "YWxzamRmbVjFaa05HVnGFqZHM3NXRBS73jxmhVMkjiwVVXV1d5UrXR1JLSkZFRr"
- + "WVd4emFtUm1iR0TFpUZvbiBoYXMgYmVlbxWkhNaF3uUi8kM0YkJTEVlZYVFjBYi"
- + "jFkWFIxSkxTa1pGUjd874hjklaTFpUwRVuIFNob3VsZCB5UwRkxUR1J6YVhlQWa"
- + "V1ZkNGVH32mDkdho3pqtRnRVbTFpUjBaeGUgYXNrZWQtZFoKjda40ZWlyYWl1eX"
- + "U4RkNtdmNub2l1dHE2TTA1jd84jkdJTbFJXYkZwWlZtRnKkWVrSRhhWbFpZVmFz"
- + "c2RkZmTFpUpGa1dGSXhTa3hUYTFwR1Vpkm9yIGluZm9ybWFNfdsWQdSiiYXNWVX"
- + "RSblJWYlRGcFVqQmFlRmRyYUU0MFpXbHlZV2wxZVhVNFJrTnR51YjJsMWRIRTJU"
- + "W91ciBpbmZvcm1hdGliBzdWJtaXR0ZWQb3UZSBhIHJlc3BvbnNlLCB3ZWxsIHJl"
- + "VEExWdHVybiB0am0aW9uIb24gYXMgd2UgcG9zc2libHkgY2FuLuhcFe24ldWVns"
- + "dSaTFpU3hVUjFKNllWaFdhRmxZUWpCaQo34gcmVxdWlGFzIHNveqQl83snBfVl3",
-
- // 2048bit SSL ON - ENABLED TRUE
- false);
-
- // Channel | Message Test Data (UTF-8)
- String message = " ~`â¦â§!@#$%^&*(???)+=[]\\{}|;\':,./<>?abcd";
- Pubnub _pubnub;
- ArrayList<String> many_channels = new ArrayList<String>();
- HashMap<String, Object> status = new HashMap<String, Object>(3);
- HashMap<String, Object> threads = new HashMap<String, Object>(4);
- int max_retries = 10;
-
- // Full Unit Test
- public void runUnitTest(View v) {
- /*final AlertDialog.Builder b = new AlertDialog.Builder(
- PubnubTestActivity.this);
- b.setIcon(android.R.drawable.ic_dialog_alert);
- b.setTitle("Unit Test");
- b.setMessage("Please see the logs (Info) for detailed result.");
- b.setNegativeButton("OK",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog,
- int which) {
- }
- });
- b.show();*/
- unitTest(pubnub_user_supplied_options);
- unitTest(pubnub_high_security);
- }
-
- private void unitTest(Pubnub pubnub) {
- _pubnub = pubnub;
- for (int i = 0; i < max_retries; i++) {
- many_channels.add("channel_" + i);
- }
-
- status.put("sent", 0);
- status.put("received", 0);
- status.put("connections", 0);
-
- for (final String _channel : many_channels) {
- Thread t = new Thread() {
- public void run() {
- HashMap<String, Object> args = new HashMap<String, Object>(2);
- args.put("channel", _channel);
- args.put("callback", new ReceivedMessage()); // Callback to get response
-
- // Listen for Messages (Subscribe)
- _pubnub.subscribe(args);
- };
- };
- t.start();
- threads.put(_channel, t);
-
- try {
- Thread.sleep(2000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
-
- // Callback Interface when a Message is Received
- class ReceivedMessage implements Callback {
-
- @Override
- public boolean subscribeCallback(String channel, Object message) {
- Integer sent = (Integer) status.get("sent");
- Integer received = (Integer) status.get("received");
-
- test(received <= sent, "many sends");
- status.remove(received);
- status.put("received", received.intValue() + 1);
- HashMap<String, Object> args = new HashMap<String, Object>(1);
- args.put("channel", channel);
- _pubnub.unsubscribe(args);
-
- HashMap<String, Object> argsHistory = new HashMap<String, Object>(2);
- argsHistory.put("channel", channel);
- argsHistory.put("limit", 2);
-
- // Get History
- JSONArray response = _pubnub.history(argsHistory);
- if (response != null) {
- test(true, " History with channel " + channel);
- }
- return true;
- }
-
- @Override
- public void errorCallback(String channel, Object message) {
- Log.e("Pubnub", "Channel:" + channel + "-" + message.toString());
- }
-
- @Override
- public void connectCallback(String channel) {
- Log.e("Pubnub", "Connected to channel :" + channel);
-
- Integer connections = (Integer) status.get("connections");
- status.remove(connections);
- status.put("connections", connections.intValue() + 1);
-
- JSONArray array = new JSONArray();
- array.put("Sunday");
- array.put("Monday");
- array.put("Tuesday");
- array.put("Wednesday");
- array.put("Thursday");
- array.put("Friday");
- array.put("Saturday");
-
- HashMap<String, Object> args = new HashMap<String, Object>(2);
- args.put("channel", channel);
- args.put("message", array);
-
- JSONArray response = _pubnub.publish(args);
- Integer sent = (Integer) status.get("sent");
- status.remove(sent);
- status.put("sent", sent.intValue() + 1);
-
- test(true, "publish complete");
- test(true, "publish response" + response);
- }
-
- @Override
- public void reconnectCallback(String channel) {
- Log.e("Pubnub", "Reconnected to channel :" + channel);
- }
-
- @Override
- public void disconnectCallback(String channel) {
- Log.e("Pubnub", "Disconnected to channel :" + channel);
- }
- }
-
- private void test(Boolean trial, String name) {
- if (trial)
- Log.e("PASS ", name);
- else
- Log.e("- FAIL - ", name);
- }
-}
+package com.fbt;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+
+import org.json.JSONArray;
+import org.json.JSONObject;
+
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.content.DialogInterface;
+import android.os.AsyncTask;
+import android.os.Bundle;
+import android.os.Handler;
+import android.os.Message;
+import android.util.Log;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.widget.Button;
+import android.widget.EditText;
+import android.widget.Toast;
+
+/**
+ * PubnubTestActivity
+ *
+ */
+public class PubnubTestActivity extends Activity {
+
+ Pubnub pubnub;
+ String myMessage = "", channel = "hello_world";
+ EditText ed;
+ RefreshHandler r = new RefreshHandler();
+ int limit = 5;
+
+ /** Called when the activity is first created. */
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.main);
+
+ ed = (EditText) findViewById(R.id.editText1);
+
+ // Android: (Init)
+ pubnub = new Pubnub("demo", // PUBLISH_KEY
+ "demo", // SUBSCRIBE_KEY
+ "demo", // SECRET_KEY
+ "", // CIPHER_KEY
+ true // SSL_ON?
+ );
+ // pubnub.startPubnub(this);
+ XMLDownloader d = new XMLDownloader();
+ d.execute("xml");
+
+ Button publishBtn = (Button) findViewById(R.id.publishBtn);
+ publishBtn.setOnClickListener(new OnClickListener() {
+
+ public void onClick(View v) {
+
+ // Android: (Publish)
+
+ // Create JSON Message
+ JSONObject message = new JSONObject();
+ try {
+ if (ed.getText().toString() != null
+ && !ed.getText().toString().equals(""))
+ message.put("Message", ed.getText().toString());
+ } catch (org.json.JSONException jsonError) {
+ }
+
+ // Publish Message
+ HashMap<String, Object> args = new HashMap<String, Object>(2);
+ args.put("channel", channel); // Channel Name
+ args.put("message", message); // JSON Message
+ JSONArray info = pubnub.publish(args);
+
+ // Print Response from PubNub JSONP REST Service
+ System.out.println(info);
+ Log.e("Publish", info.toString());
+ }
+ });
+
+ Button unsubscribeBtn = (Button) findViewById(R.id.unsubscribeBtn);
+ unsubscribeBtn.setOnClickListener(new OnClickListener() {
+
+ public void onClick(View v) {
+
+ // Android: (Unsubscribe/disconnect)
+ HashMap<String, Object> args = new HashMap<String, Object>(1);
+ args.put("channel", channel);
+ pubnub.unsubscribe(args);
+ }
+ });
+
+ Button subscribeBtn = (Button) findViewById(R.id.subscribeBtn);
+ subscribeBtn.setOnClickListener(new OnClickListener() {
+
+ public void onClick(View v) {
+
+ // Android: (Subscribe)
+ XMLDownloader d = new XMLDownloader();
+ d.execute("xml");
+ }
+ });
+
+ Button historyBtn = (Button) findViewById(R.id.historyBtn);
+ historyBtn.setOnClickListener(new OnClickListener() {
+
+ public void onClick(View v) {
+
+ // Android: (History)
+
+ System.out.print(" HISTORY: ");
+ HashMap<String, Object> args = new HashMap<String, Object>(2);
+ args.put("channel", channel);
+ args.put("limit", limit);
+
+ // Get History
+ JSONArray response = pubnub.history(args);
+
+ // Print Response from PubNub JSONP REST Service
+ try {
+ if (response != null) {
+ StringBuffer messages = new StringBuffer("");
+ for (int i = 0; i < response.length(); i++) {
+ Object o = response.get(i);
+ String message = o.toString() + "\n\n";
+ messages.append(message);
+ }
+ final AlertDialog.Builder b = new AlertDialog.Builder(
+ PubnubTestActivity.this);
+ b.setIcon(android.R.drawable.ic_dialog_alert);
+ b.setTitle("History: ");
+ b.setMessage(messages.toString());
+ b.setNegativeButton("OK",
+ new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog,
+ int which) {
+ }
+ });
+ b.show();
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+ });
+
+ Button uuidBtn = (Button) findViewById(R.id.uuidBtn);
+ uuidBtn.setOnClickListener(new OnClickListener() {
+
+ public void onClick(View v) {
+
+ // Android: (UUID)
+
+ String uuid = Pubnub.uuid();
+ System.out.println(" UUID: " + uuid);
+ final AlertDialog.Builder b = new AlertDialog.Builder(
+ PubnubTestActivity.this);
+ b.setIcon(android.R.drawable.ic_dialog_alert);
+ b.setTitle("UUID: ");
+ b.setMessage(uuid);
+ b.setNegativeButton("OK",
+ new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog,
+ int which) {
+ }
+ });
+ b.show();
+ }
+ });
+
+ Button timeBtn = (Button) findViewById(R.id.timeBtn);
+ timeBtn.setOnClickListener(new OnClickListener() {
+
+ public void onClick(View v) {
+
+ // Android: (Time)
+
+ double time = pubnub.time();
+ System.out.println(" Time: " + Double.toString(time));
+ final AlertDialog.Builder b = new AlertDialog.Builder(
+ PubnubTestActivity.this);
+ b.setIcon(android.R.drawable.ic_dialog_alert);
+ b.setTitle("Time: ");
+ b.setMessage(Double.toString(time));
+ b.setNegativeButton("OK",
+ new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog,
+ int which) {
+ }
+ });
+ b.show();
+ }
+ });
+ }
+
+ @Override
+ protected void onDestroy() {
+ // TODO Auto-generated method stub
+ super.onDestroy();
+ // pubnub.stopPubnub();
+ }
+
+ @Override
+ protected void onPause() {
+ // TODO Auto-generated method stub
+ super.onPause();
+ // pubnub.stopPubnub();
+ }
+
+ @Override
+ protected void onResume() {
+ // TODO Auto-generated method stub
+ super.onResume();
+ // pubnub.startPubnub(this);
+ }
+
+ public void allMessageClick(View v) {
+ JSONObject message = new JSONObject();
+ try {
+ message.put("some_val", "Hello World! --> ɂ顶@#$%^&*()!");
+ /*
+ * message.put( "title", "Android PubNub"); message.put( "text",
+ * "This is a push to all users! woot!"); message.put( "url",
+ * "http://www.pubnub.com");
+ */
+ } catch (org.json.JSONException jsonError) {
+ }
+
+ // Publish
+ HashMap<String, Object> args = new HashMap<String, Object>(2);
+ args.put("channel", channel);
+ args.put("message", message);
+ JSONArray response = null;
+ response = pubnub.publish(args);
+ System.out.println(response);
+
+ args = new HashMap<String, Object>(2);
+ args.put("channel", channel);
+ args.put("message", "Hello World");
+
+ response = pubnub.publish(args);
+ System.out.println(response);
+
+ JSONArray array = new JSONArray();
+ array.put("Sunday");
+ array.put("Monday");
+ array.put("Tuesday");
+ array.put("Wednesday");
+ array.put("Thursday");
+ array.put("Friday");
+ array.put("Saturday");
+
+ args = new HashMap<String, Object>(2);
+ args.put("channel", channel);
+ args.put("message", array);
+
+ response = pubnub.publish(args);
+ System.out.println(response);
+ }
+
+ class RefreshHandler extends Handler {
+ @Override
+ public void handleMessage(Message msg) {
+ Log.v("IN", "HANDLER");
+ Toast.makeText(PubnubTestActivity.this, "You got message",
+ Toast.LENGTH_LONG).show();
+ final AlertDialog.Builder b = new AlertDialog.Builder(
+ PubnubTestActivity.this);
+ b.setIcon(android.R.drawable.ic_dialog_alert);
+ b.setTitle("PUBNUB");
+ b.setMessage(myMessage);
+
+ b.setNegativeButton("OK", new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog, int which) {
+ }
+ });
+ b.show();
+ }
+ };
+
+ class XMLDownloader extends AsyncTask<String, Void, Boolean> {
+
+ @Override
+ protected Boolean doInBackground(String... params) {
+ try {
+ // Android: (Subscribe)
+
+ class Receiver implements Callback {
+ public boolean subscribeCallback(String channel,
+ Object message) {
+ Log.e("Message Received", message.toString());
+ myMessage = message.toString();
+ r.sendEmptyMessage(0);
+ return true;
+ }
+
+ @Override
+ public void errorCallback(String channel, Object message) {
+ Log.e("ErrorCallback", "Channel:" + channel + "-"
+ + message.toString());
+ }
+
+ @Override
+ public void connectCallback(String channel) {
+ Log.e("ConnectCallback", "Connected to channel :"
+ + channel);
+ }
+
+ @Override
+ public void reconnectCallback(String channel) {
+ Log.e("ReconnectCallback", "Reconnecting to channel :"
+ + channel);
+ }
+
+ @Override
+ public void disconnectCallback(String channel) {
+ Log.e("DisconnectCallback", "Disconnected to channel :"
+ + channel);
+ }
+ }
+
+ // Listen for Messages (Subscribe)
+ HashMap<String, Object> args = new HashMap<String, Object>(2);
+ args.put("channel", channel); // Channel Name
+ args.put("callback", new Receiver()); // Callback to get response
+ pubnub.subscribe(args);
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ Log.v("ERROR", "While downloading");
+ }
+
+ return Boolean.TRUE;
+ }
+
+ @Override
+ protected void onPreExecute() {
+ }
+
+ protected void onPostExecute(Boolean result) {
+ }
+ }
+
+ String publish_key = "demo", subscribe_key = "demo";
+ String secret_key = "demo", cipher_key = "";
+ Boolean ssl_on = false;
+
+ // User Supplied Options PubNub
+ Pubnub pubnub_user_supplied_options = new Pubnub(publish_key, // OPTIONAL (supply "" to disable)
+ subscribe_key, // REQUIRED
+ secret_key, // OPTIONAL (supply "" to disable)
+ cipher_key, // OPTIONAL (supply "" to disable)
+ ssl_on // OPTIONAL (supply "" to disable)
+ );
+
+ // High Security PubNub
+ Pubnub pubnub_high_security = new Pubnub(
+ // Publish Key
+ "pub-c-a30c030e-9f9c-408d-be89-d70b336ca7a0",
+
+ // Subscribe Key
+ "sub-c-387c90f3-c018-11e1-98c9-a5220e0555fd",
+
+ // Secret Key
+ "sec-c-MTliNDE0NTAtYjY4Ni00MDRkLTllYTItNDhiZGE0N2JlYzBl",
+
+ // Cipher Key
+ "YWxzamRmbVjFaa05HVnGFqZHM3NXRBS73jxmhVMkjiwVVXV1d5UrXR1JLSkZFRr"
+ + "WVd4emFtUm1iR0TFpUZvbiBoYXMgYmVlbxWkhNaF3uUi8kM0YkJTEVlZYVFjBYi"
+ + "jFkWFIxSkxTa1pGUjd874hjklaTFpUwRVuIFNob3VsZCB5UwRkxUR1J6YVhlQWa"
+ + "V1ZkNGVH32mDkdho3pqtRnRVbTFpUjBaeGUgYXNrZWQtZFoKjda40ZWlyYWl1eX"
+ + "U4RkNtdmNub2l1dHE2TTA1jd84jkdJTbFJXYkZwWlZtRnKkWVrSRhhWbFpZVmFz"
+ + "c2RkZmTFpUpGa1dGSXhTa3hUYTFwR1Vpkm9yIGluZm9ybWFNfdsWQdSiiYXNWVX"
+ + "RSblJWYlRGcFVqQmFlRmRyYUU0MFpXbHlZV2wxZVhVNFJrTnR51YjJsMWRIRTJU"
+ + "W91ciBpbmZvcm1hdGliBzdWJtaXR0ZWQb3UZSBhIHJlc3BvbnNlLCB3ZWxsIHJl"
+ + "VEExWdHVybiB0am0aW9uIb24gYXMgd2UgcG9zc2libHkgY2FuLuhcFe24ldWVns"
+ + "dSaTFpU3hVUjFKNllWaFdhRmxZUWpCaQo34gcmVxdWlGFzIHNveqQl83snBfVl3",
+
+ // 2048bit SSL ON - ENABLED TRUE
+ false);
+
+ // Channel | Message Test Data (UTF-8)
+ String message = " ~`â¦â§!@#$%^&*(???)+=[]\\{}|;\':,./<>?abcd";
+ Pubnub _pubnub;
+ ArrayList<String> many_channels = new ArrayList<String>();
+ HashMap<String, Object> status = new HashMap<String, Object>(3);
+ HashMap<String, Object> threads = new HashMap<String, Object>(4);
+ int max_retries = 10;
+
+ // Full Unit Test
+ public void runUnitTest(View v) {
+ /*final AlertDialog.Builder b = new AlertDialog.Builder(
+ PubnubTestActivity.this);
+ b.setIcon(android.R.drawable.ic_dialog_alert);
+ b.setTitle("Unit Test");
+ b.setMessage("Please see the logs (Info) for detailed result.");
+ b.setNegativeButton("OK",
+ new DialogInterface.OnClickListener() {
+ public void onClick(DialogInterface dialog,
+ int which) {
+ }
+ });
+ b.show();*/
+ unitTest(pubnub_user_supplied_options);
+ unitTest(pubnub_high_security);
+ }
+
+ private void unitTest(Pubnub pubnub) {
+ _pubnub = pubnub;
+ for (int i = 0; i < max_retries; i++) {
+ many_channels.add("channel_" + i);
+ }
+
+ status.put("sent", 0);
+ status.put("received", 0);
+ status.put("connections", 0);
+
+ for (final String _channel : many_channels) {
+ Thread t = new Thread() {
+ public void run() {
+ HashMap<String, Object> args = new HashMap<String, Object>(2);
+ args.put("channel", _channel);
+ args.put("callback", new ReceivedMessage()); // Callback to get response
+
+ // Listen for Messages (Subscribe)
+ _pubnub.subscribe(args);
+ };
+ };
+ t.start();
+ threads.put(_channel, t);
+
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ // Callback Interface when a Message is Received
+ class ReceivedMessage implements Callback {
+
+ @Override
+ public boolean subscribeCallback(String channel, Object message) {
+ Integer sent = (Integer) status.get("sent");
+ Integer received = (Integer) status.get("received");
+
+ test(received <= sent, "many sends");
+ status.remove(received);
+ status.put("received", received.intValue() + 1);
+ HashMap<String, Object> args = new HashMap<String, Object>(1);
+ args.put("channel", channel);
+ _pubnub.unsubscribe(args);
+
+ HashMap<String, Object> argsHistory = new HashMap<String, Object>(2);
+ argsHistory.put("channel", channel);
+ argsHistory.put("limit", 2);
+
+ // Get History
+ JSONArray response = _pubnub.history(argsHistory);
+ if (response != null) {
+ test(true, " History with channel " + channel);
+ }
+ return true;
+ }
+
+ @Override
+ public void errorCallback(String channel, Object message) {
+ Log.e("Pubnub", "Channel:" + channel + "-" + message.toString());
+ }
+
+ @Override
+ public void connectCallback(String channel) {
+ Log.e("Pubnub", "Connected to channel :" + channel);
+
+ Integer connections = (Integer) status.get("connections");
+ status.remove(connections);
+ status.put("connections", connections.intValue() + 1);
+
+ JSONArray array = new JSONArray();
+ array.put("Sunday");
+ array.put("Monday");
+ array.put("Tuesday");
+ array.put("Wednesday");
+ array.put("Thursday");
+ array.put("Friday");
+ array.put("Saturday");
+
+ HashMap<String, Object> args = new HashMap<String, Object>(2);
+ args.put("channel", channel);
+ args.put("message", array);
+
+ JSONArray response = _pubnub.publish(args);
+ Integer sent = (Integer) status.get("sent");
+ status.remove(sent);
+ status.put("sent", sent.intValue() + 1);
+
+ test(true, "publish complete");
+ test(true, "publish response" + response);
+ }
+
+ @Override
+ public void reconnectCallback(String channel) {
+ Log.e("Pubnub", "Reconnected to channel :" + channel);
+ }
+
+ @Override
+ public void disconnectCallback(String channel) {
+ Log.e("Pubnub", "Disconnected to channel :" + channel);
+ }
+ }
+
+ private void test(Boolean trial, String name) {
+ if (trial)
+ Log.e("PASS ", name);
+ else
+ Log.e("- FAIL - ", name);
+ }
+}

0 comments on commit 2266fb2

Please sign in to comment.
Something went wrong with that request. Please try again.