Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

CL-198: J2ME should reconnect after connection loss

  • Loading branch information...
commit f34f16fa42eed16f68c94740aad965ce96c7ea37 1 parent 261e1d5
@tanaji-kadam tanaji-kadam authored
View
BIN  j2me/3.3/PubnubUnitTest/libs/async-http.jar
Binary file not shown
View
6 j2me/3.3/PubnubUnitTest/src/pubnub/Callback.java
@@ -1,15 +1,10 @@
package pubnub;
-
-import org.json.me.JSONArray;
-
public interface Callback {
-
public abstract void publishCallback(String channel,Object message,Object responce);
public abstract void subscribeCallback(String channel,Object message);
public abstract void historyCallback(String channel,Object message);
public abstract void errorCallback(String channel, Object message);
-
public abstract void connectCallback(String channel);
public abstract void reconnectCallback(String channel);
public abstract void disconnectCallback(String channel);
@@ -17,4 +12,3 @@
public abstract void presenceCallback(String channel,Object message);
public abstract void detailedHistoryCallback(String channel,Object message);
}
-
View
127 j2me/3.3/PubnubUnitTest/src/pubnub/Pubnub.java
@@ -2,21 +2,14 @@
import com.tinyline.util.GZIPInputStream;
import java.io.*;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
-import pubnub.crypto.PubnubCrypto;
-
-
import java.util.Enumeration;
import java.util.Hashtable;
-import java.util.Random;
import java.util.Vector;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.ShortBufferException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
-
import javax.microedition.io.HttpsConnection;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.InvalidCipherTextException;
@@ -24,6 +17,7 @@
import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;
+import pubnub.crypto.PubnubCrypto;
import pubnub.util.AsyncHttpManager;
import pubnub.util.HttpCallback;
@@ -114,7 +108,7 @@ public Pubnub(String publish_key, String subscribe_key, String secret_key) {
* @param String Cipher Key.
* @param boolean SSL Enabled.
*/
- public void init(String publish_key, String subscribe_key,
+ private void init(String publish_key, String subscribe_key,
String secret_key, String cipher_key, boolean ssl_on) {
this.PUBLISH_KEY = publish_key;
this.SUBSCRIBE_KEY = subscribe_key;
@@ -257,8 +251,6 @@ public void publish(Hashtable args) {
public void subscribe(final Hashtable args) {
args.put("timetoken", "0");
_subscribe(args);
-
-
}
private void _subscribe_base(Hashtable args) {
@@ -286,10 +278,8 @@ private void _subscribe_base(Hashtable args) {
if (it.channel.equals(channel)) {
if (!it.connected) {
if (_callback != null) {
-
_callback.disconnectCallback(channel);
}
-
is_disconnect = true;
break;
}
@@ -300,8 +290,6 @@ private void _subscribe_base(Hashtable args) {
}
}
_request(url, channel,null);
-
-
} catch (Exception e) {
try {
Thread.sleep(1000);
@@ -365,7 +353,6 @@ private void _subscribe(Hashtable args) {
}
}
_subscribe_base(args);
-
}
/**
@@ -385,19 +372,14 @@ public long time() {
String res = getViaHttpsConnection(getURL(url));
JSONArray response = new JSONArray(res);
try {
- //return (Double.parseDouble(response.get(0).toString()));
+
return Long.parseLong(response.get(0).toString());
} catch (JSONException ex) {
ex.printStackTrace();
}
-
- } catch (JSONException ex) {
- ex.printStackTrace();
- } catch (IOException ex) {
- ex.printStackTrace();
- } catch (ServiceProviderException ex) {
- ex.printStackTrace();
- }
+ } catch (Exception ex) {
+ //ex.printStackTrace();
+ }
return 0;
}
@@ -426,18 +408,9 @@ public void presence(String channel) {
Hashtable args = new Hashtable(6);
args.put("channel", channel + "-pnpres");
subscribe(args);
-
}
-//[1:02:44 PM] Tanaji Kadam: - (void)presence:(NSString *)channel
-//{
-// if(channel == nil || channel ==@"")
-// {
-// NSLog(@"Missing channel");
-// return;
-// }
-// [self subscribe:[NSString stringWithFormat:@"%@-pnpres", channel]];
-//}
+
/**
* UUID
*
@@ -525,7 +498,6 @@ public void history(Hashtable args) {
url.addElement(channel);
url.addElement("0");
url.addElement(limit.toString());
-
_request(url, channel,null);
}
@@ -539,24 +511,18 @@ public void history(Hashtable args) {
* @return JSONArray of history.
*/
public void detailedHistory(Hashtable args) {
-
String channel = (String) args.get("channel");
-
if (channel == null || channel.equals("")) {
-
System.out.println("Missing Channel");
return;
}
-
StringBuffer parameter = new StringBuffer("?");
int count = 100;
if (args.get("count") != null) {
count = Integer.parseInt(args.get("count").toString());
}
-
parameter.append("count=").append(count);
-
if (args.get("reverse") != null) {
Boolean reverse = (Boolean) args.get("reverse");
if (reverse.booleanValue()) {
@@ -565,11 +531,9 @@ public void detailedHistory(Hashtable args) {
parameter.append("&reverse=false");
}
}
-
if (args.get("start") != null) {
parameter.append("&start=").append((String) args.get("start"));
}
-
if (args.get("end") != null) {
parameter.append("&end=").append((String) args.get("end"));
}
@@ -583,8 +547,6 @@ public void detailedHistory(Hashtable args) {
url.addElement(channel);
url.addElement(parameter.toString());
_request(url, channel,null);
-
-
}
/**
@@ -602,22 +564,17 @@ public void unsubscribe(Hashtable args) {
if (it.channel.equals(channel) && it.connected) {
it.connected = false;
// it.first = false;
-
break;
}
-
}
-
for (int i = 0; i < _connection.size(); i++) {
HttpCallback cb = (HttpCallback) _connection.elementAt(i);
if (cb.getChannel().equals(channel)) {
HttpConnection con = cb.getConnection();
if (con != null) {
-
//con.close();
cb.cancelRequest(cb);
_connection.removeElement(cb);
-
}
}
}
@@ -639,7 +596,6 @@ private String getURL(Vector url_components) {
url.append("/").append(encode(url_bit, "UTF-8"));
}
} catch (Exception e) {
- // e.printStackTrace();
JSONArray jsono = new JSONArray();
try {
jsono.put("Failed UTF-8 Encoding URL.");
@@ -661,22 +617,16 @@ private String getURL(Vector url_components) {
* @return JSONArray from JSON response.
*/
private void _request(Vector url_components, final String channel1,Object message) {
-
String request_for = (String) url_components.elementAt(0);
-
if (request_for.equals("subscribe")) {
current_timetoken = (String) url_components.elementAt(4);
}
-
-
-
if (request_for.endsWith("v2")) {
request_for = (String) url_components.elementAt(1);
if (request_for.endsWith("history")) {
request_for = "detailedHistory";
}
}
-
String url = getURL(url_components);
if (request_for.equals("subscribe")) {
url = url + "/?uuid=" + UUID;
@@ -687,26 +637,16 @@ private void _request(Vector url_components, final String channel1,Object messag
_headers.put("Accept-Encoding", "gzip");
_headers.put("Connection", "close");
-
-
HttpCallback callback = new HttpCallback(url.toString(), _headers, request_for) {
-
public void processResponse(HttpConnection conn, Object cookie) throws IOException {
}
-
public void OnComplet(HttpConnection hc, String response, String req_for, String channel) throws IOException {
try {
-
-
- //response=response.replace('+', ' ');
-
JSONArray out = null;
-
if (response != null) {
if (!req_for.equals("presence")) {
out = new JSONArray(response);
}
-
} else {
out = null;
}
@@ -718,7 +658,6 @@ public void OnComplet(HttpConnection hc, String response, String req_for, String
} else if (req_for.equals("detailedHistory")) {
detailedHistoryComplet(out, channel);
} else if (req_for.equals("history")) {
-
if (CIPHER_KEY.length() > 0) {
try {
// Decrpyt Messages
@@ -731,9 +670,7 @@ public void OnComplet(HttpConnection hc, String response, String req_for, String
if (_callback != null) {
_callback.historyCallback(channel, out);
}
-
} else if (req_for.equals("publish")) {
-
if (out == null) {
JSONArray arr = new JSONArray();
arr.put("0");
@@ -746,18 +683,15 @@ public void OnComplet(HttpConnection hc, String response, String req_for, String
}
}
}
-
} catch (Exception ex) {
ex.printStackTrace();
}
-
if (_connection.contains(this)) {
_connection.removeElement(this);
}
}
public void errorCall(HttpConnection conn, Object message) throws IOException {
- System.out.println("Error:" + message.toString());
if (_connection.contains(this)) {
_connection.removeElement(this);
}
@@ -765,7 +699,6 @@ public void errorCall(HttpConnection conn, Object message) throws IOException {
void detailedHistoryComplet(JSONArray responce, String channel) {
if (CIPHER_KEY.length() > 0) {
-
try {
JSONArray messageArray = responce.getJSONArray(0);
PubnubCrypto pc = new PubnubCrypto(CIPHER_KEY);
@@ -792,7 +725,6 @@ void subscribeComplet(JSONArray out, String channel) {
if (channel.endsWith("-pnpres")) {
isPresence = true;
}
- System.out.println("subscribeComplet:" + out + " isPresent:" + isPresence);
String timetoken = "0";
if (!isPresence) {
ChannelStatus it;
@@ -804,31 +736,32 @@ void subscribeComplet(JSONArray out, String channel) {
if (!it.connected && it.first) {
subscriptions.removeElement(it);
if (_callback != null) {
-
_callback.disconnectCallback(channel);
+ it.connected = false;
}
is_disconnect = true;
break;
}
}
}
-
if (is_disconnect) {
return;
}
-
-
// Problem?
if (out == null || out.optInt(1) == 0) {
-
for (int i = 0; i < subscriptions.size(); i++) {
it = (ChannelStatus) subscriptions.elementAt(i);
if (it.channel.equals(channel)) {
if (_callback != null) {
_callback.disconnectCallback(channel);
+ is_disconnect = true;
+ it.connected = false;
}
}
}
+ if (is_disconnect) {
+ // return;
+ }
// Ensure Connected (Call Time Function)
boolean is_reconnected = false;
while (true) {
@@ -842,31 +775,39 @@ void subscribeComplet(JSONArray out, String channel) {
} else {
if (_callback != null) {
_callback.reconnectCallback(channel);
+ for (int i = 0; i < subscriptions.size(); i++) {
+ it = (ChannelStatus) subscriptions.elementAt(i);
+ if (it.channel.equals(channel)) {
+ //if (_callback != null) {
+ //_callback.disconnectCallback(channel);
+ //is_disconnect = true;
+ it.connected = true;
+ //}
+ }
+ }
}
Hashtable args = new Hashtable();
args.put("channel", channel);
-
if (current_timetoken.equals("0")) {
args.put("timetoken", time_token + "");
} else {
args.put("timetoken", current_timetoken + "");
-
}
-
- _subscribe(args);
+ _subscribe_base(args);
is_reconnected = true;
break;
}
}
+ if(is_reconnected){
+ return;
+ }
} else {
-
for (int i = 0; i < subscriptions.size(); i++) {
it = (ChannelStatus) subscriptions.elementAt(i);
if (it.channel.equals(channel)) {
// Connect Callback
if (!it.first) {
it.first = true;
-
if (_callback != null) {
_callback.connectCallback(channel);
}
@@ -876,9 +817,7 @@ void subscribeComplet(JSONArray out, String channel) {
}
}
}
-
JSONArray messages = out.optJSONArray(0);
-
// Update TimeToken
if (out.optString(1).length() > 0) {
timetoken = out.optString(1);
@@ -887,7 +826,6 @@ void subscribeComplet(JSONArray out, String channel) {
for (int i = 0; messages.length() > i; i++) {
JSONObject message = messages.optJSONObject(i);
if (message != null) {
-
if (CIPHER_KEY.length() > 0) {
// Decrypt Message
PubnubCrypto pc = new PubnubCrypto(CIPHER_KEY);
@@ -930,8 +868,6 @@ void subscribeComplet(JSONArray out, String channel) {
}
}
}
-
-
Hashtable args = new Hashtable();
args.put("channel", channel);
args.put("timetoken", timetoken + "");
@@ -974,7 +910,6 @@ private void hereNowComplet(String response, String channel) {
callback.setChannel(channel1);
}
-
if (request_for.equals("publish")) {
callback.setMessage(message);
}
@@ -992,7 +927,7 @@ public final String getViaHttpsConnection(String url)
InputStream dis = null;
OutputStream os = null;
int rc;
- String respBody = new String(""); // return empty string on bad things
+ String respBody = ""; // return empty string on bad things
try {
if (SSL) {
c = (HttpsConnection) Connector.open(url, Connector.READ_WRITE,
@@ -1047,12 +982,9 @@ public String encode(String s, String enc)
int maxBytesPerChar = 10; // rather arbitrary limit, but safe for now
StringBuffer out = new StringBuffer(s.length());
ByteArrayOutputStream buf = new ByteArrayOutputStream(maxBytesPerChar);
-
OutputStreamWriter writer = new OutputStreamWriter(buf, enc);
-
for (int i = 0; i < s.length(); i++) {
int c = (int) s.charAt(i);
- //System.out.println("Examining character: " + c);
if (dontNeedEncoding(c)) {
if (c == ' ') {
out.append('%');
@@ -1061,7 +993,6 @@ public String encode(String s, String enc)
needToChange = true;
} else {
- //System.out.println("Storing: " + c);
out.append((char) c);
wroteUnencodedChar = true;
}
@@ -1133,7 +1064,6 @@ public String encode(String s, String enc)
}
static class CCharacter {
-
public static char forDigit(int digit, int radix) {
if ((digit >= radix) || (digit < 0)) {
return '\0';
@@ -1157,7 +1087,6 @@ public static boolean dontNeedEncoding(int ch) {
break;
}
}
-
return en;
}
//private static final int caseDiff = ('a' - 'A');
View
BIN  j2me/3.3/j2me/libs/async-http.jar
Binary file not shown
View
69 j2me/3.3/j2me/src/pubnub/Pubnub.java
@@ -2,9 +2,6 @@
import com.tinyline.util.GZIPInputStream;
import java.io.*;
-import pubnub.crypto.PubnubCrypto;
-
-
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
@@ -13,7 +10,6 @@
import javax.crypto.ShortBufferException;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
-
import javax.microedition.io.HttpsConnection;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.InvalidCipherTextException;
@@ -21,6 +17,7 @@
import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;
+import pubnub.crypto.PubnubCrypto;
import pubnub.util.AsyncHttpManager;
import pubnub.util.HttpCallback;
@@ -111,7 +108,7 @@ public Pubnub(String publish_key, String subscribe_key, String secret_key) {
* @param String Cipher Key.
* @param boolean SSL Enabled.
*/
- public void init(String publish_key, String subscribe_key,
+ private void init(String publish_key, String subscribe_key,
String secret_key, String cipher_key, boolean ssl_on) {
this.PUBLISH_KEY = publish_key;
this.SUBSCRIBE_KEY = subscribe_key;
@@ -254,8 +251,6 @@ public void publish(Hashtable args) {
public void subscribe(final Hashtable args) {
args.put("timetoken", "0");
_subscribe(args);
-
-
}
private void _subscribe_base(Hashtable args) {
@@ -358,7 +353,6 @@ private void _subscribe(Hashtable args) {
}
}
_subscribe_base(args);
-
}
/**
@@ -383,14 +377,9 @@ public long time() {
} catch (JSONException ex) {
ex.printStackTrace();
}
-
- } catch (JSONException ex) {
- ex.printStackTrace();
- } catch (IOException ex) {
- ex.printStackTrace();
- } catch (ServiceProviderException ex) {
- ex.printStackTrace();
- }
+ } catch (Exception ex) {
+ //ex.printStackTrace();
+ }
return 0;
}
@@ -522,13 +511,11 @@ public void history(Hashtable args) {
* @return JSONArray of history.
*/
public void detailedHistory(Hashtable args) {
-
String channel = (String) args.get("channel");
if (channel == null || channel.equals("")) {
System.out.println("Missing Channel");
return;
}
-
StringBuffer parameter = new StringBuffer("?");
int count = 100;
@@ -609,7 +596,6 @@ private String getURL(Vector url_components) {
url.append("/").append(encode(url_bit, "UTF-8"));
}
} catch (Exception e) {
- // e.printStackTrace();
JSONArray jsono = new JSONArray();
try {
jsono.put("Failed UTF-8 Encoding URL.");
@@ -641,7 +627,6 @@ private void _request(Vector url_components, final String channel1,Object messag
request_for = "detailedHistory";
}
}
-
String url = getURL(url_components);
if (request_for.equals("subscribe")) {
url = url + "/?uuid=" + UUID;
@@ -658,7 +643,6 @@ public void processResponse(HttpConnection conn, Object cookie) throws IOExcepti
public void OnComplet(HttpConnection hc, String response, String req_for, String channel) throws IOException {
try {
JSONArray out = null;
-
if (response != null) {
if (!req_for.equals("presence")) {
out = new JSONArray(response);
@@ -674,7 +658,6 @@ public void OnComplet(HttpConnection hc, String response, String req_for, String
} else if (req_for.equals("detailedHistory")) {
detailedHistoryComplet(out, channel);
} else if (req_for.equals("history")) {
-
if (CIPHER_KEY.length() > 0) {
try {
// Decrpyt Messages
@@ -687,9 +670,7 @@ public void OnComplet(HttpConnection hc, String response, String req_for, String
if (_callback != null) {
_callback.historyCallback(channel, out);
}
-
} else if (req_for.equals("publish")) {
-
if (out == null) {
JSONArray arr = new JSONArray();
arr.put("0");
@@ -702,18 +683,15 @@ public void OnComplet(HttpConnection hc, String response, String req_for, String
}
}
}
-
} catch (Exception ex) {
ex.printStackTrace();
}
-
if (_connection.contains(this)) {
_connection.removeElement(this);
}
}
public void errorCall(HttpConnection conn, Object message) throws IOException {
- System.out.println("Error:" + message.toString());
if (_connection.contains(this)) {
_connection.removeElement(this);
}
@@ -721,7 +699,6 @@ public void errorCall(HttpConnection conn, Object message) throws IOException {
void detailedHistoryComplet(JSONArray responce, String channel) {
if (CIPHER_KEY.length() > 0) {
-
try {
JSONArray messageArray = responce.getJSONArray(0);
PubnubCrypto pc = new PubnubCrypto(CIPHER_KEY);
@@ -760,28 +737,31 @@ void subscribeComplet(JSONArray out, String channel) {
subscriptions.removeElement(it);
if (_callback != null) {
_callback.disconnectCallback(channel);
+ it.connected = false;
}
is_disconnect = true;
break;
}
}
}
-
if (is_disconnect) {
return;
}
-
// Problem?
if (out == null || out.optInt(1) == 0) {
-
for (int i = 0; i < subscriptions.size(); i++) {
it = (ChannelStatus) subscriptions.elementAt(i);
if (it.channel.equals(channel)) {
if (_callback != null) {
_callback.disconnectCallback(channel);
+ is_disconnect = true;
+ it.connected = false;
}
}
}
+ if (is_disconnect) {
+ // return;
+ }
// Ensure Connected (Call Time Function)
boolean is_reconnected = false;
while (true) {
@@ -795,30 +775,39 @@ void subscribeComplet(JSONArray out, String channel) {
} else {
if (_callback != null) {
_callback.reconnectCallback(channel);
+ for (int i = 0; i < subscriptions.size(); i++) {
+ it = (ChannelStatus) subscriptions.elementAt(i);
+ if (it.channel.equals(channel)) {
+ //if (_callback != null) {
+ //_callback.disconnectCallback(channel);
+ //is_disconnect = true;
+ it.connected = true;
+ //}
+ }
+ }
}
Hashtable args = new Hashtable();
args.put("channel", channel);
-
if (current_timetoken.equals("0")) {
args.put("timetoken", time_token + "");
} else {
args.put("timetoken", current_timetoken + "");
-
}
- _subscribe(args);
+ _subscribe_base(args);
is_reconnected = true;
break;
}
}
+ if(is_reconnected){
+ return;
+ }
} else {
-
for (int i = 0; i < subscriptions.size(); i++) {
it = (ChannelStatus) subscriptions.elementAt(i);
if (it.channel.equals(channel)) {
// Connect Callback
if (!it.first) {
it.first = true;
-
if (_callback != null) {
_callback.connectCallback(channel);
}
@@ -829,7 +818,6 @@ void subscribeComplet(JSONArray out, String channel) {
}
}
JSONArray messages = out.optJSONArray(0);
-
// Update TimeToken
if (out.optString(1).length() > 0) {
timetoken = out.optString(1);
@@ -838,7 +826,6 @@ void subscribeComplet(JSONArray out, String channel) {
for (int i = 0; messages.length() > i; i++) {
JSONObject message = messages.optJSONObject(i);
if (message != null) {
-
if (CIPHER_KEY.length() > 0) {
// Decrypt Message
PubnubCrypto pc = new PubnubCrypto(CIPHER_KEY);
@@ -881,8 +868,6 @@ void subscribeComplet(JSONArray out, String channel) {
}
}
}
-
-
Hashtable args = new Hashtable();
args.put("channel", channel);
args.put("timetoken", timetoken + "");
@@ -942,7 +927,7 @@ public final String getViaHttpsConnection(String url)
InputStream dis = null;
OutputStream os = null;
int rc;
- String respBody = new String(""); // return empty string on bad things
+ String respBody = ""; // return empty string on bad things
try {
if (SSL) {
c = (HttpsConnection) Connector.open(url, Connector.READ_WRITE,
@@ -1079,7 +1064,6 @@ public String encode(String s, String enc)
}
static class CCharacter {
-
public static char forDigit(int digit, int radix) {
if ((digit >= radix) || (digit < 0)) {
return '\0';
@@ -1103,7 +1087,6 @@ public static boolean dontNeedEncoding(int ch) {
break;
}
}
-
return en;
}
//private static final int caseDiff = ('a' - 'A');
Please sign in to comment.
Something went wrong with that request. Please try again.