Browse files

refactoring for common code base

  • Loading branch information...
1 parent 68ffd3b commit 41b6b6772ed628f4dcc46ac3dbd609cd813d762d @devendram devendram committed Dec 28, 2012
View
14 blackberry/3.4/examples/PubnubExample/src/com/pubnub/examples/blackberry/PubnubExample.java
@@ -3,7 +3,6 @@
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
-import com.pubnub.api.Callback;
import com.pubnub.api.Pubnub;
/**
@@ -18,20 +17,9 @@
*/
public static void main(String[] args)
{
- Pubnub pubnub = new Pubnub("demo", "demo", "demo", false);
-
- pubnub.publish("hello_world1","I am blackberry client" , new Callback() {
- public void successCallback(String channel, Object message) {
- System.out.println(message.toString());
- }
-
- public void errorCallback(String channel, Object message) {
- System.out.println(message.toString());
- }
-
- });
Pubnub.startHeartbeat(5000);
+
// Create a new instance of the application and make the currently
// running thread the application's event dispatch thread.
PubnubExample theApp = new PubnubExample();
View
28 ...ry/3.4/examples/PubnubExample/src/com/pubnub/examples/blackberry/PubnubExampleScreen.java
@@ -32,6 +32,10 @@ public PubnubExampleScreen()
// Set the displayed title of the screen
setTitle("PubnubExample");
add(new LabelField("Please select an item from the menu"));
+ add(new LabelField("Subscribe will listen on following channels: "));
+ add(new LabelField("hello_world1, hello_world2, hello_world3, hello_world4"));
+ add(new LabelField("Publish, history, detailedHistory, hereNow, Presence use hello_world"));
+
addMenuItem(new TimeMenuItem());
addMenuItem(new PublishMenuItem());
@@ -58,7 +62,7 @@ public void successCallback(String channel, Object message) {
}
public void errorCallback(String channel, Object message) {
- PubnubExample.alertDialog(channel + " : " + message.toString());
+ PubnubExample.alertDialog(message.toString());
}
});
}
@@ -76,11 +80,11 @@ public void execute(ReadOnlyCommandMetadata metadata, Object context) {
_pubnub.publish(channel, "Blackberry says hello world", new Callback() {
public void successCallback(String channel, Object message) {
- PubnubExample.alertDialog(message.toString());
+ PubnubExample.alertDialog("Channel : " + channel + ", " + message.toString());
}
public void errorCallback(String channel, Object message) {
- PubnubExample.alertDialog(channel + " : " + message.toString());
+ PubnubExample.alertDialog("Channel : " + channel + ", " + message.toString());
}
});
@@ -98,11 +102,11 @@ public void execute(ReadOnlyCommandMetadata metadata, Object context)
{
_pubnub.hereNow(channel, new Callback() {
public void successCallback(String channel, Object message) {
- PubnubExample.alertDialog(message.toString());
+ PubnubExample.alertDialog("Channel : " + channel + ", " + message.toString());
}
public void errorCallback(String channel, Object message) {
- PubnubExample.alertDialog(channel + " : " + message.toString());
+ PubnubExample.alertDialog("Channel : " + channel + ", " + message.toString());
}
});
}
@@ -120,11 +124,11 @@ public void execute(ReadOnlyCommandMetadata metadata, Object context)
{
_pubnub.history(channel, 1, new Callback() {
public void successCallback(String channel, Object message) {
- PubnubExample.alertDialog(message.toString());
+ PubnubExample.alertDialog("Channel : " + channel + ", " + message.toString());
}
public void errorCallback(String channel, Object message) {
- PubnubExample.alertDialog(channel + " : " + message.toString());
+ PubnubExample.alertDialog("Channel : " + channel + ", " + message.toString());
}
});
}
@@ -142,11 +146,11 @@ public void execute(ReadOnlyCommandMetadata metadata, Object context)
{
_pubnub.detailedHistory(channel, 1, new Callback() {
public void successCallback(String channel, Object message) {
- PubnubExample.alertDialog(message.toString());
+ PubnubExample.alertDialog("Channel : " + channel + ", " + message.toString());
}
public void errorCallback(String channel, Object message) {
- PubnubExample.alertDialog(channel + " : " + message.toString());
+ PubnubExample.alertDialog("Channel : " + channel + ", " + message.toString());
}
});
}
@@ -179,7 +183,7 @@ public void reconnectCallback(String channel) {
}
public void successCallback(String channel, Object message) {
- PubnubExample.alertDialog(channel + " " + message.toString());
+ PubnubExample.alertDialog("Channel : " + channel + ", " + message.toString());
}
});
@@ -202,11 +206,11 @@ public void execute(ReadOnlyCommandMetadata metadata, Object context)
try {
_pubnub.presence(channel, new Callback() {
public void successCallback(String channel, Object message) {
- PubnubExample.alertDialog(message.toString());
+ PubnubExample.alertDialog("Channel : " + channel + ", " + message.toString());
}
public void errorCallback(String channel, Object message) {
- PubnubExample.alertDialog(channel + " : " + message.toString());
+ PubnubExample.alertDialog("Channel : " + channel + ", " + message.toString());
}
});
} catch (PubnubException e) {
View
1,872 j2me/3.4/src/com/pubnub/api/Pubnub.java
@@ -1,936 +1,936 @@
-package com.pubnub.api;
-
-import java.util.Hashtable;
-
-import org.bouncycastle.util.SecureRandom;
-import org.json.me.JSONArray;
-import org.json.me.JSONException;
-import org.json.me.JSONObject;
-
-import com.pubnub.crypto.me.PubnubCrypto;
-import com.pubnub.http.HttpManager;
-import com.pubnub.http.HttpRequest;
-import com.pubnub.http.ResponseHandler;
-
-/**
- * Pubnub object facilitates querying channels for messages and listening on
- * channels for presence/message events
- *
- * @author Pubnub
- *
- */
-
-public class Pubnub {
-
- private String ORIGIN = "pubsub.pubnub.com";
- private String PUBLISH_KEY = "";
- private String SUBSCRIBE_KEY = "";
- private String SECRET_KEY = "";
- private String CIPHER_KEY = "";
- private boolean SSL = false;
- private String UUID = null;
- private Hashtable _headers;
- private Subscriptions subscriptions;
-
- private HttpManager longPollConnManager;
- private HttpManager simpleConnManager;
-
- /**
- * UUID
- *
- * 32 digit UUID generation at client side.
- *
- * @return String uuid.
- */
- public static String uuid() {
- String valueBeforeMD5;
- String valueAfterMD5;
- SecureRandom mySecureRand = new SecureRandom();
- String s_id = System.getProperty("microedition.platform");
- StringBuffer sbValueBeforeMD5 = new StringBuffer();
- try {
- long time = System.currentTimeMillis();
- long rand = 0;
- rand = mySecureRand.nextLong();
- sbValueBeforeMD5.append(s_id);
- sbValueBeforeMD5.append(":");
- sbValueBeforeMD5.append(Long.toString(time));
- sbValueBeforeMD5.append(":");
- sbValueBeforeMD5.append(Long.toString(rand));
- valueBeforeMD5 = sbValueBeforeMD5.toString();
- byte[] array = PubnubCrypto.md5(valueBeforeMD5);
- StringBuffer sb = new StringBuffer();
- for (int j = 0; j < array.length; ++j) {
- int b = array[j] & 0xFF;
- if (b < 0x10) {
- sb.append('0');
- }
- sb.append(Integer.toHexString(b));
- }
- valueAfterMD5 = sb.toString();
- String raw = valueAfterMD5.toUpperCase();
- sb = new StringBuffer();
- sb.append(raw.substring(0, 8));
- sb.append("-");
- sb.append(raw.substring(8, 12));
- sb.append("-");
- sb.append(raw.substring(12, 16));
- sb.append("-");
- sb.append(raw.substring(16, 20));
- sb.append("-");
- sb.append(raw.substring(20));
- return sb.toString();
- } catch (Exception e) {
- return null;
- }
- }
-
- /**
- *
- * Constructor for Pubnub Class
- *
- * @param publish_key
- * Publish Key
- * @param subscribe_key
- * Subscribe Key
- * @param secret_key
- * Secret Key
- * @param cipher_key
- * Cipher Key
- * @param ssl_on
- * SSL enabled ?
- */
-
- public Pubnub(String publish_key, String subscribe_key, String secret_key,
- String cipher_key, boolean ssl_on) {
- this.init(publish_key, subscribe_key, secret_key, cipher_key, ssl_on);
- }
-
- /**
- *
- * Constructor for Pubnub Class
- *
- * @param publish_key
- * Publish Key
- * @param subscribe_key
- * Subscribe Key
- * @param secret_key
- * Secret Key
- * @param ssl_on
- * SSL enabled ?
- */
-
- public Pubnub(String publish_key, String subscribe_key, String secret_key,
- boolean ssl_on) {
- this.init(publish_key, subscribe_key, secret_key, "", ssl_on);
- }
-
- /**
- *
- * Constructor for Pubnub Class
- *
- * @param publish_key
- * Publish Key
- * @param subscribe_key
- * Subscribe Key
- */
-
- public Pubnub(String publish_key, String subscribe_key) {
- this.init(publish_key, subscribe_key, "", "", false);
- }
-
- /**
- *
- * Constructor for Pubnub Class
- *
- * @param publish_key
- * Publish Key
- * @param subscribe_key
- * Subscribe Key
- * @param secret_key
- * Secret Key
- */
- public Pubnub(String publish_key, String subscribe_key, String secret_key) {
- this.init(publish_key, subscribe_key, secret_key, "", false);
- }
-
- /**
- *
- * Initialize PubNub Object State.
- *
- * @param publish_key
- * @param subscribe_key
- * @param secret_key
- * @param cipher_key
- * @param ssl_on
- */
- 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;
- this.SECRET_KEY = secret_key;
- this.CIPHER_KEY = cipher_key;
- this.SSL = ssl_on;
-
- // SSL On?
- if (this.SSL) {
- this.ORIGIN = "https://" + this.ORIGIN;
- } else {
- this.ORIGIN = "http://" + this.ORIGIN;
- }
-
- if (UUID == null)
- UUID = uuid();
-
- if (subscriptions == null)
- subscriptions = new Subscriptions();
-
- if (longPollConnManager == null)
- longPollConnManager = new HttpManager("Long Poll");
-
- if (simpleConnManager == null)
- simpleConnManager = new HttpManager("Simple");
-
- _headers = new Hashtable();
- _headers.put("V", "3.3");
- _headers.put("User-Agent", "J2ME");
- _headers.put("Accept-Encoding", "gzip");
- _headers.put("Connection", "close");
- }
-
- /**
- * Start heartbeat thread to check connectivity to pubnub servers Calls to
- * pubnub api's will return network error, when heartbeat is on and pubnub
- * servers are not reachable
- *
- * @param interval
- */
- public static void startHeartbeat(int interval) {
- HttpManager.startHeartbeat("http://pubsub.pubnub.com/time/0",
- interval);
- }
-
- /**
- * Send a message to a channel.
- *
- * @param channel
- * Channel name
- * @param message
- * JSONObject to be published
- * @param callback
- * Callback
- */
- public void publish(String channel, JSONObject message, Callback callback) {
- Hashtable args = new Hashtable();
- args.put("channel", channel);
- args.put("message", message);
- args.put("callback", callback);
- publish(args);
- }
-
- /**
- * Send a message to a channel.
- *
- * @param channel
- * Channel name
- * @param message
- * JSONObject to be published
- * @param callback
- * Callback
- */
- public void publish(String channel, JSONArray message, Callback callback) {
- Hashtable args = new Hashtable();
- args.put("channel", channel);
- args.put("message", message);
- args.put("callback", callback);
- publish(args);
- }
-
- /**
- * Send a message to a channel.
- *
- * @param channel
- * Channel name
- * @param message
- * JSONObject to be published
- * @param callback
- * Callback
- */
- public void publish(String channel, String message, Callback callback) {
- Hashtable args = new Hashtable();
- args.put("channel", channel);
- args.put("message", message);
- args.put("callback", callback);
- publish(args);
- }
-
- /**
- * Send a message to a channel.
- *
- * @param args
- * Hashtable containing channel name, message.
- * @param callback
- * Callback
- */
- public void publish(Hashtable args, Callback callback) {
- args.put("callback", callback);
- publish(args);
- }
-
- /**
- * Send a message to a channel.
- *
- * @param args
- * Hashtable containing channel name, message, callback
- */
- public void publish(Hashtable args) {
-
- final String channel = (String) args.get("channel");
- Object message = args.get("message");
- final Callback callback = (Callback) args.get("callback");
- if (message instanceof JSONObject) {
- JSONObject obj = (JSONObject) message;
-
- if (this.CIPHER_KEY.length() > 0) {
- // Encrypt Message
- PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY);
- try {
- message = pc.encrypt(obj);
- } catch (Exception e) {
- JSONArray jsarr;
- jsarr = new JSONArray();
- jsarr.put("0").put("Error: Encryption Failure");
- callback.errorCallback(channel, jsarr);
- }
- } else {
- message = obj;
- }
- } else if (message instanceof String) {
- String obj = (String) message;
- if (this.CIPHER_KEY.length() > 0) {
- // Encrypt Message
- PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY);
- try {
- message = pc.encrypt(obj);
- } catch (Exception e) {
- JSONArray jsarr;
- jsarr = new JSONArray();
- jsarr.put("0").put("Error: Encryption Failure");
- callback.errorCallback(channel, jsarr);
- }
- } else {
- message = obj;
- }
- message = "\"" + message + "\"";
-
- } else if (message instanceof JSONArray) {
- JSONArray obj = (JSONArray) message;
-
- if (this.CIPHER_KEY.length() > 0) {
- // Encrypt Message
- PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY);
- try {
- message = pc.encryptJSONArray(obj);
- } catch (Exception e) {
- JSONArray jsarr;
- jsarr = new JSONArray();
- jsarr.put("0").put("Error: Encryption Failure");
- callback.errorCallback(channel, jsarr);
- }
-
- } else {
- message = obj;
- }
- }
-
- // Generate String to Sign
- String signature = "0";
-
- if (this.SECRET_KEY.length() > 0) {
- StringBuffer string_to_sign = new StringBuffer();
- string_to_sign.append(this.PUBLISH_KEY).append('/')
- .append(this.SUBSCRIBE_KEY).append('/')
- .append(this.SECRET_KEY).append('/').append(channel)
- .append('/').append(message.toString());
-
- // Sign Message
- signature = PubnubCrypto.getHMacSHA256(this.SECRET_KEY,
- string_to_sign.toString());
- }
-
- String[] urlComponents = { this.ORIGIN, "publish", this.PUBLISH_KEY,
- this.SUBSCRIBE_KEY, signature, channel, "0",
- PubnubUtil.urlEncode(message.toString()) };
-
- PubnubRequest req = new PubnubRequest(urlComponents, channel,
- new ResponseHandler() {
- public void handleResponse(String response) {
- JSONArray jsarr;
- try {
- jsarr = new JSONArray(response);
- } catch (JSONException e) {
- handleError(response);
- return;
- }
- callback.successCallback(channel, jsarr);
- }
-
- public void handleError(String response) {
- JSONArray jsarr;
- try {
- jsarr = new JSONArray(response);
- } catch (JSONException e) {
- jsarr = new JSONArray();
- jsarr.put("0").put(
- "Error: Failed JSON HTTP PubnubRequest");
- }
- callback.errorCallback(channel, jsarr);
-
- }
- });
-
- _request(req, simpleConnManager);
- }
-
- /**
- *
- * Listen for presence of subscribers on a channel
- *
- * @param channel
- * Name of the channel on which to listen for join/leave i.e.
- * presence events
- * @param callback
- * Callback
- * @exception PubnubException
- * Throws PubnubException if Callback is null
- */
- public void presence(String channel, Callback callback)
- throws PubnubException {
- Hashtable args = new Hashtable(2);
- args.put("channel", channel + "-pnpres");
- args.put("callback", callback);
- subscribe(args);
- }
-
- /**
- *
- * Read presence information from a channel
- *
- * @param channel
- * Channel name
- * @param requestTimeout
- * timeout in milliseconds for this request
- */
- public void hereNow(final String channel, final Callback callback) {
-
- String[] urlargs = { this.ORIGIN, "v2", "presence", "sub_key",
- this.SUBSCRIBE_KEY, "channel", channel };
-
- PubnubRequest req = new PubnubRequest(urlargs, (String) null,
- new ResponseHandler() {
-
- public void handleResponse(String response) {
- callback.successCallback(channel, response);
-
- }
-
- public void handleError(String response) {
- callback.errorCallback(channel, response);
-
- }
-
- });
-
- _request(req, simpleConnManager);
- }
-
- /**
- *
- * Read history from a channel.
- *
- * @param channel
- * Channel Name
- * @param limit
- * Upper limit on number of messages in response
- * @param requestTimeout
- * timeout in milliseconds for this request
- * @return JSONArray of message history on a channel.
- */
- public void history(String channel, int limit, Callback callback) {
- Hashtable args = new Hashtable(2);
- args.put("channel", channel);
- args.put("limit", String.valueOf(limit));
- args.put("callback", callback);
- history(args);
- }
-
- /**
- *
- * Read history from a channel.
- *
- * @param args
- * HashMap of <String, Object> containing channel name, limit
- * history count
- * @param requestTimeout
- * timeout in milliseconds for this request
- * @return JSONArray of history.
- */
- private void history(Hashtable args) {
-
- final String channel = (String) args.get("channel");
- String limit = (String) args.get("limit");
- final Callback callback = (Callback) args.get("callback");
-
- String[] urlargs = { this.ORIGIN, "history", this.SUBSCRIBE_KEY,
- channel, "0", limit };
-
- PubnubRequest req = new PubnubRequest(urlargs, channel, new ResponseHandler() {
-
- public void handleResponse(String response) {
- callback.successCallback(channel, response);
-
- }
-
- public void handleError(String response) {
- callback.errorCallback(channel, response);
-
- }
-
- });
- _request(req, simpleConnManager);
- }
-
- /**
- *
- * Read DetailedHistory for a channel.
- *
- * @param channel
- * Channel name for which detailed history is required
- * @param start
- * Start time
- * @param end
- * End time
- * @param count
- * Upper limit on number of messages to be returned
- * @param reverse
- * True if messages need to be in reverse order
- * @return JSONArray of detailed history.
- */
- public void detailedHistory(final String channel, long start, long end,
- int count, boolean reverse, final Callback callback) {
- Hashtable parameters = new Hashtable();
- if (count == -1)
- count = 100;
-
- parameters.put("count", String.valueOf(count));
- parameters.put("reverse", String.valueOf(reverse));
-
- if (start != -1)
- parameters.put("start", Long.toString(start).toLowerCase());
-
- if (end != -1)
- parameters.put("end", Long.toString(end).toLowerCase());
-
- String[] urlargs = { this.ORIGIN, "v2", "history", "sub-key",
- this.SUBSCRIBE_KEY, "channel", channel };
-
- PubnubRequest req = new PubnubRequest(urlargs, parameters, channel,
- new ResponseHandler() {
-
- public void handleResponse(String response) {
- callback.successCallback(channel, response);
-
- }
-
- public void handleError(String response) {
- callback.errorCallback(channel, response);
-
- }
-
- });
- _request(req, simpleConnManager);
- }
-
- /**
- *
- * Read DetailedHistory for a channel.
- *
- * @param channel
- * Channel name for which detailed history is required
- * @param start
- * Start time
- * @param reverse
- * True if messages need to be in reverse order
- * @return JSONArray of detailed history.
- */
- public void detailedHistory(String channel, long start, boolean reverse,
- Callback callback) {
- detailedHistory(channel, start, -1, -1, reverse, callback);
- }
-
- /**
- *
- * Read DetailedHistory for a channel.
- *
- * @param channel
- * Channel name for which detailed history is required
- * @param start
- * Start time
- * @param end
- * End time
- * @return JSONArray of detailed history.
- */
- public void detailedHistory(String channel, long start, long end,
- Callback callback) {
- detailedHistory(channel, start, end, -1, false, callback);
- }
-
- /**
- *
- * Read DetailedHistory for a channel.
- *
- * @param channel
- * Channel name for which detailed history is required
- * @param start
- * Start time
- * @param end
- * End time
- * @param reverse
- * True if messages need to be in reverse order
- * @return JSONArray of detailed history.
- */
- public void detailedHistory(String channel, long start, long end,
- boolean reverse, Callback callback) {
- detailedHistory(channel, start, end, -1, reverse, callback);
- }
-
- /**
- *
- * Read DetailedHistory for a channel.
- *
- * @param channel
- * Channel name for which detailed history is required
- * @param count
- * Upper limit on number of messages to be returned
- * @param reverse
- * True if messages need to be in reverse order
- * @return JSONArray of detailed history.
- */
- public void detailedHistory(String channel, int count, boolean reverse,
- Callback callback) {
- detailedHistory(channel, -1, -1, count, reverse, callback);
- }
-
- /**
- *
- * Read DetailedHistory for a channel.
- *
- * @param channel
- * Channel name for which detailed history is required
- * @param reverse
- * True if messages need to be in reverse order
- * @return JSONArray of detailed history.
- */
- public void detailedHistory(String channel, boolean reverse,
- Callback callback) {
- detailedHistory(channel, -1, -1, -1, reverse, callback);
- }
-
- /**
- *
- * Read DetailedHistory for a channel.
- *
- * @param channel
- * Channel name for which detailed history is required
- * @param reverse
- * True if messages need to be in reverse order
- * @return JSONArray of detailed history.
- */
- public void detailedHistory(String channel, int count, Callback callback) {
- detailedHistory(channel, -1, -1, count, false, callback);
- }
-
- /**
- * Read current time from PubNub Cloud.
- *
- * @return current timestamp.
- */
- public void time(final Callback cb) {
-
- String[] url = { this.ORIGIN, "time", "0" };
- PubnubRequest req = new PubnubRequest(url, (String) null, new ResponseHandler() {
-
- public void handleResponse(String response) {
- cb.successCallback(null, response);
- }
-
- public void handleError(String response) {
- cb.errorCallback(null, response);
- }
-
- });
-
- _request(req, simpleConnManager);
- }
-
- private boolean inputsValid(Hashtable args) throws PubnubException {
- boolean channelMissing;
- if (((Callback) args.get("callback")) == null) {
- throw new PubnubException("Invalid Callback");
- }
- Object _channels = args.get("channels");
- Object _channel = args.get("channel");
-
- channelMissing = ((_channel == null || _channel.equals("")) && (_channels == null || _channels
- .equals(""))) ? true : false;
-
- if (channelMissing) {
- throw new PubnubException("Channel Missing");
- }
- return true;
- }
-
- /**
- * Unsubscribe/Disconnect from channel.
- *
- * @param channels
- * String[] array containing channel names as string.
- */
- public void unsubscribe(String[] channels) {
- for (int i = 0; i < channels.length; i++) {
- subscriptions.removeChannel(channels[i]);
- }
- }
-
- /**
- * Unsubscribe/Disconnect from channel.
- *
- * @param channel
- * channel name as String.
- */
- public void unsubscribe(String channel) {
- unsubscribe(new String[] { channel });
- }
-
- /**
- * Unsubscribe/Disconnect from channel.
- *
- * @param args
- * Hashtable containing channel name.
- */
- public void unsubscribe(Hashtable args) {
- String[] channelList = (String[]) args.get("channels");
- if (channelList == null) {
- channelList = new String[] { (String) args.get("channel") };
- }
- unsubscribe(channelList);
- }
-
- /**
- *
- * Listen for a message on a channel.
- *
- * @param args
- * Hashtable containing channel name
- * @param callback
- * Callback
- * @exception PubnubException
- * Throws PubnubException if Callback is null
- */
- public void subscribe(Hashtable args, Callback callback)
- throws PubnubException {
-
- args.put("callback", callback);
-
- if (!inputsValid(args)) {
- return;
- }
- args.put("timetoken", "0");
- _subscribe(args);
- }
-
- /**
- *
- * Listen for a message on a channel.
- *
- * @param args
- * Hashtable containing channel name, callback
- * @exception PubnubException
- * Throws PubnubException if Callback is null
- */
- public void subscribe(Hashtable args) throws PubnubException {
-
- if (!inputsValid(args)) {
- return;
- }
-
- args.put("timetoken", "0");
- _subscribe(args);
- }
-
- /**
- *
- * Listen for a message on a channel.
- *
- * @param channelsArr
- * Array of channel names (string) to listen on
- * @param callback
- * Callback
- * @exception PubnubException
- * Throws PubnubException if Callback is null
- */
- public void subscribe(String[] channelsArr, Callback callback)
- throws PubnubException {
-
- Hashtable args = new Hashtable();
-
- args.put("channels", channelsArr);
- args.put("callback", callback);
- subscribe(args);
- }
-
- private void callErrorCallbacks(String[] channelList, String message) {
- for (int i = 0; i < channelList.length; i++) {
- Callback cb = ((Channel) subscriptions.getChannel(channelList[i])).callback;
- cb.errorCallback(channelList[i], message);
- }
- }
-
- /**
- * @param args
- * Hashtable
- */
- private void _subscribe(Hashtable args) {
-
- String[] channelList = (String[]) args.get("channels");
- if (channelList == null) {
- channelList = new String[] { (String) args.get("channel") };
- }
- Callback callback = (Callback) args.get("callback");
- String timetoken = (String) args.get("timetoken");
-
- /*
- * Scan through the channels array. If a channel does not exist in
- * hashtable create a new entry with default values. If already exists
- * and connected, then return
- */
-
- for (int i = 0; i < channelList.length; i++) {
- String channel = channelList[i];
- Channel channelObj = (Channel) subscriptions.getChannel(channel);
-
- if (channelObj == null) {
- Channel ch = new Channel();
- ch.name = channel;
- ch.connected = false;
- ch.callback = callback;
- subscriptions.addChannel(ch);
- } else if (channelObj.connected) {
-
- return;
- }
- }
- _subscribe_base(timetoken);
- }
-
- /**
- * @param timetoken
- * , Timetoken to be used
- */
- private void _subscribe_base(String timetoken) {
- String channelString = subscriptions.getChannelString();
- String[] channelsArray = subscriptions.getChannelNames();
-
- if (channelString == null) {
- callErrorCallbacks(channelsArray, "Parsing Error");
- return;
- }
-
- String[] urlComponents = { Pubnub.this.ORIGIN, "subscribe",
- Pubnub.this.SUBSCRIBE_KEY, channelString, "0", timetoken };
-
- Hashtable params = new Hashtable();
- params.put("uuid", uuid());
-
- PubnubRequest req = new PubnubRequest(urlComponents, params, channelsArray,
- new ResponseHandler() {
- String _timetoken = "0";
-
- public void handleResponse(String response) {
-
- subscriptions.invokeConnectCallbackOnChannels();
-
- /*
- * Check if response has channel names. A JSON response
- * with more than 2 items means the response contains
- * the channel names as well. The channel names are in a
- * comma delimted string. Call success callback on all
- * he channels passing the corresponding response
- * message.
- */
-
- JSONArray jsa;
- try {
- jsa = new JSONArray(response);
- String _timetoken = jsa.get(1).toString();
- JSONArray messages = new JSONArray(jsa.get(0)
- .toString());
-
- if (jsa.length() > 2) {
- /*
- * Response has multiple channels
- */
-
- String[] _channels = PubnubUtil.splitString(
- jsa.getString(2), ",");
-
- for (int i = 0; i < _channels.length; i++) {
- Channel _channel = (Channel) subscriptions
- .getChannel(_channels[i]);
- if (_channel != null)
- _channel.callback.successCallback(
- _channels[i], messages.get(i));
- }
-
- } else {
- /*
- * Response for single channel Callback on
- * single channel
- */
- Channel _channel = subscriptions
- .getFirstChannel();
-
- if (_channel != null) {
- for (int i = 0; i < messages.length(); i++) {
- _channel.callback.successCallback(
- _channel.name, messages.get(i));
- }
- }
-
- }
- _subscribe_base(_timetoken);
- } catch (JSONException e) {
- _subscribe_base(_timetoken);
- }
-
- }
-
- public void handleError(String response) {
- subscriptions.invokeDisconnectCallbackOnChannels();
- _subscribe_base(_timetoken);
- }
- });
-
- _request(req, longPollConnManager);
- }
-
- /**
- * @param req
- * @param connManager
- */
- private void _request(final PubnubRequest req, HttpManager connManager) {
-
- HttpRequest hreq = new HttpRequest(req.getUrl(), _headers, req.responseHandler);
- connManager.queue(hreq);
- }
-}
+package com.pubnub.api;
+
+import java.util.Hashtable;
+
+import org.bouncycastle.util.SecureRandom;
+import org.json.me.JSONArray;
+import org.json.me.JSONException;
+import org.json.me.JSONObject;
+
+import com.pubnub.crypto.me.PubnubCrypto;
+import com.pubnub.http.HttpManager;
+import com.pubnub.http.HttpRequest;
+import com.pubnub.http.ResponseHandler;
+
+/**
+ * Pubnub object facilitates querying channels for messages and listening on
+ * channels for presence/message events
+ *
+ * @author Pubnub
+ *
+ */
+
+public class Pubnub {
+
+ private String ORIGIN = "pubsub.pubnub.com";
+ private String PUBLISH_KEY = "";
+ private String SUBSCRIBE_KEY = "";
+ private String SECRET_KEY = "";
+ private String CIPHER_KEY = "";
+ private boolean SSL = true;
+ private String UUID = null;
+ private Hashtable _headers;
+ private Subscriptions subscriptions;
+
+ private HttpManager longPollConnManager;
+ private HttpManager simpleConnManager;
+
+ /**
+ * UUID
+ *
+ * 32 digit UUID generation at client side.
+ *
+ * @return String uuid.
+ */
+ public static String uuid() {
+ String valueBeforeMD5;
+ String valueAfterMD5;
+ SecureRandom mySecureRand = new SecureRandom();
+ String s_id = System.getProperty("microedition.platform");
+ StringBuffer sbValueBeforeMD5 = new StringBuffer();
+ try {
+ long time = System.currentTimeMillis();
+ long rand = 0;
+ rand = mySecureRand.nextLong();
+ sbValueBeforeMD5.append(s_id);
+ sbValueBeforeMD5.append(":");
+ sbValueBeforeMD5.append(Long.toString(time));
+ sbValueBeforeMD5.append(":");
+ sbValueBeforeMD5.append(Long.toString(rand));
+ valueBeforeMD5 = sbValueBeforeMD5.toString();
+ byte[] array = PubnubCrypto.md5(valueBeforeMD5);
+ StringBuffer sb = new StringBuffer();
+ for (int j = 0; j < array.length; ++j) {
+ int b = array[j] & 0xFF;
+ if (b < 0x10) {
+ sb.append('0');
+ }
+ sb.append(Integer.toHexString(b));
+ }
+ valueAfterMD5 = sb.toString();
+ String raw = valueAfterMD5.toUpperCase();
+ sb = new StringBuffer();
+ sb.append(raw.substring(0, 8));
+ sb.append("-");
+ sb.append(raw.substring(8, 12));
+ sb.append("-");
+ sb.append(raw.substring(12, 16));
+ sb.append("-");
+ sb.append(raw.substring(16, 20));
+ sb.append("-");
+ sb.append(raw.substring(20));
+ return sb.toString();
+ } catch (Exception e) {
+ return null;
+ }
+ }
+
+ /**
+ *
+ * Constructor for Pubnub Class
+ *
+ * @param publish_key
+ * Publish Key
+ * @param subscribe_key
+ * Subscribe Key
+ * @param secret_key
+ * Secret Key
+ * @param cipher_key
+ * Cipher Key
+ * @param ssl_on
+ * SSL enabled ?
+ */
+
+ public Pubnub(String publish_key, String subscribe_key, String secret_key,
+ String cipher_key, boolean ssl_on) {
+ this.init(publish_key, subscribe_key, secret_key, cipher_key, ssl_on);
+ }
+
+ /**
+ *
+ * Constructor for Pubnub Class
+ *
+ * @param publish_key
+ * Publish Key
+ * @param subscribe_key
+ * Subscribe Key
+ * @param secret_key
+ * Secret Key
+ * @param ssl_on
+ * SSL enabled ?
+ */
+
+ public Pubnub(String publish_key, String subscribe_key, String secret_key,
+ boolean ssl_on) {
+ this.init(publish_key, subscribe_key, secret_key, "", ssl_on);
+ }
+
+ /**
+ *
+ * Constructor for Pubnub Class
+ *
+ * @param publish_key
+ * Publish Key
+ * @param subscribe_key
+ * Subscribe Key
+ */
+
+ public Pubnub(String publish_key, String subscribe_key) {
+ this.init(publish_key, subscribe_key, "", "", false);
+ }
+
+ /**
+ *
+ * Constructor for Pubnub Class
+ *
+ * @param publish_key
+ * Publish Key
+ * @param subscribe_key
+ * Subscribe Key
+ * @param secret_key
+ * Secret Key
+ */
+ public Pubnub(String publish_key, String subscribe_key, String secret_key) {
+ this.init(publish_key, subscribe_key, secret_key, "", false);
+ }
+
+ /**
+ *
+ * Initialize PubNub Object State.
+ *
+ * @param publish_key
+ * @param subscribe_key
+ * @param secret_key
+ * @param cipher_key
+ * @param ssl_on
+ */
+ 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;
+ this.SECRET_KEY = secret_key;
+ this.CIPHER_KEY = cipher_key;
+ this.SSL = ssl_on;
+
+ // SSL On?
+ if (this.SSL) {
+ this.ORIGIN = "https://" + this.ORIGIN;
+ } else {
+ this.ORIGIN = "http://" + this.ORIGIN;
+ }
+
+ if (UUID == null)
+ UUID = uuid();
+
+ if (subscriptions == null)
+ subscriptions = new Subscriptions();
+
+ if (longPollConnManager == null)
+ longPollConnManager = new HttpManager("Long Poll");
+
+ if (simpleConnManager == null)
+ simpleConnManager = new HttpManager("Simple");
+
+ _headers = new Hashtable();
+ _headers.put("V", "3.3");
+ _headers.put("User-Agent", "J2ME");
+ _headers.put("Accept-Encoding", "gzip");
+ _headers.put("Connection", "close");
+ }
+
+ /**
+ * Start heartbeat thread to check connectivity to pubnub servers Calls to
+ * pubnub api's will return network error, when heartbeat is on and pubnub
+ * servers are not reachable
+ *
+ * @param interval
+ */
+ public static void startHeartbeat(int interval) {
+ HttpManager.startHeartbeat("https://pubsub.pubnub.com/time/0",
+ interval);
+ }
+
+ /**
+ * Send a message to a channel.
+ *
+ * @param channel
+ * Channel name
+ * @param message
+ * JSONObject to be published
+ * @param callback
+ * Callback
+ */
+ public void publish(String channel, JSONObject message, Callback callback) {
+ Hashtable args = new Hashtable();
+ args.put("channel", channel);
+ args.put("message", message);
+ args.put("callback", callback);
+ publish(args);
+ }
+
+ /**
+ * Send a message to a channel.
+ *
+ * @param channel
+ * Channel name
+ * @param message
+ * JSONObject to be published
+ * @param callback
+ * Callback
+ */
+ public void publish(String channel, JSONArray message, Callback callback) {
+ Hashtable args = new Hashtable();
+ args.put("channel", channel);
+ args.put("message", message);
+ args.put("callback", callback);
+ publish(args);
+ }
+
+ /**
+ * Send a message to a channel.
+ *
+ * @param channel
+ * Channel name
+ * @param message
+ * JSONObject to be published
+ * @param callback
+ * Callback
+ */
+ public void publish(String channel, String message, Callback callback) {
+ Hashtable args = new Hashtable();
+ args.put("channel", channel);
+ args.put("message", message);
+ args.put("callback", callback);
+ publish(args);
+ }
+
+ /**
+ * Send a message to a channel.
+ *
+ * @param args
+ * Hashtable containing channel name, message.
+ * @param callback
+ * Callback
+ */
+ public void publish(Hashtable args, Callback callback) {
+ args.put("callback", callback);
+ publish(args);
+ }
+
+ /**
+ * Send a message to a channel.
+ *
+ * @param args
+ * Hashtable containing channel name, message, callback
+ */
+ public void publish(Hashtable args) {
+
+ final String channel = (String) args.get("channel");
+ Object message = args.get("message");
+ final Callback callback = (Callback) args.get("callback");
+ if (message instanceof JSONObject) {
+ JSONObject obj = (JSONObject) message;
+
+ if (this.CIPHER_KEY.length() > 0) {
+ // Encrypt Message
+ PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY);
+ try {
+ message = pc.encrypt(obj);
+ } catch (Exception e) {
+ JSONArray jsarr;
+ jsarr = new JSONArray();
+ jsarr.put("0").put("Error: Encryption Failure");
+ callback.errorCallback(channel, jsarr);
+ }
+ } else {
+ message = obj;
+ }
+ } else if (message instanceof String) {
+ String obj = (String) message;
+ if (this.CIPHER_KEY.length() > 0) {
+ // Encrypt Message
+ PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY);
+ try {
+ message = pc.encrypt(obj);
+ } catch (Exception e) {
+ JSONArray jsarr;
+ jsarr = new JSONArray();
+ jsarr.put("0").put("Error: Encryption Failure");
+ callback.errorCallback(channel, jsarr);
+ }
+ } else {
+ message = obj;
+ }
+ message = "\"" + message + "\"";
+
+ } else if (message instanceof JSONArray) {
+ JSONArray obj = (JSONArray) message;
+
+ if (this.CIPHER_KEY.length() > 0) {
+ // Encrypt Message
+ PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY);
+ try {
+ message = pc.encryptJSONArray(obj);
+ } catch (Exception e) {
+ JSONArray jsarr;
+ jsarr = new JSONArray();
+ jsarr.put("0").put("Error: Encryption Failure");
+ callback.errorCallback(channel, jsarr);
+ }
+
+ } else {
+ message = obj;
+ }
+ }
+
+ // Generate String to Sign
+ String signature = "0";
+
+ if (this.SECRET_KEY.length() > 0) {
+ StringBuffer string_to_sign = new StringBuffer();
+ string_to_sign.append(this.PUBLISH_KEY).append('/')
+ .append(this.SUBSCRIBE_KEY).append('/')
+ .append(this.SECRET_KEY).append('/').append(channel)
+ .append('/').append(message.toString());
+
+ // Sign Message
+ signature = PubnubCrypto.getHMacSHA256(this.SECRET_KEY,
+ string_to_sign.toString());
+ }
+
+ String[] urlComponents = { this.ORIGIN, "publish", this.PUBLISH_KEY,
+ this.SUBSCRIBE_KEY, signature, channel, "0",
+ PubnubUtil.urlEncode(message.toString()) };
+
+ PubnubRequest req = new PubnubRequest(urlComponents, channel,
+ new ResponseHandler() {
+ public void handleResponse(String response) {
+ JSONArray jsarr;
+ try {
+ jsarr = new JSONArray(response);
+ } catch (JSONException e) {
+ handleError(response);
+ return;
+ }
+ callback.successCallback(channel, jsarr);
+ }
+
+ public void handleError(String response) {
+ JSONArray jsarr;
+ try {
+ jsarr = new JSONArray(response);
+ } catch (JSONException e) {
+ jsarr = new JSONArray();
+ jsarr.put("0").put(
+ "Error: Failed JSON HTTP PubnubRequest");
+ }
+ callback.errorCallback(channel, jsarr);
+
+ }
+ });
+
+ _request(req, simpleConnManager);
+ }
+
+ /**
+ *
+ * Listen for presence of subscribers on a channel
+ *
+ * @param channel
+ * Name of the channel on which to listen for join/leave i.e.
+ * presence events
+ * @param callback
+ * Callback
+ * @exception PubnubException
+ * Throws PubnubException if Callback is null
+ */
+ public void presence(String channel, Callback callback)
+ throws PubnubException {
+ Hashtable args = new Hashtable(2);
+ args.put("channel", channel + "-pnpres");
+ args.put("callback", callback);
+ subscribe(args);
+ }
+
+ /**
+ *
+ * Read presence information from a channel
+ *
+ * @param channel
+ * Channel name
+ * @param requestTimeout
+ * timeout in milliseconds for this request
+ */
+ public void hereNow(final String channel, final Callback callback) {
+
+ String[] urlargs = { this.ORIGIN, "v2", "presence", "sub_key",
+ this.SUBSCRIBE_KEY, "channel", channel };
+
+ PubnubRequest req = new PubnubRequest(urlargs, (String) null,
+ new ResponseHandler() {
+
+ public void handleResponse(String response) {
+ callback.successCallback(channel, response);
+
+ }
+
+ public void handleError(String response) {
+ callback.errorCallback(channel, response);
+
+ }
+
+ });
+
+ _request(req, simpleConnManager);
+ }
+
+ /**
+ *
+ * Read history from a channel.
+ *
+ * @param channel
+ * Channel Name
+ * @param limit
+ * Upper limit on number of messages in response
+ * @param requestTimeout
+ * timeout in milliseconds for this request
+ * @return JSONArray of message history on a channel.
+ */
+ public void history(String channel, int limit, Callback callback) {
+ Hashtable args = new Hashtable(2);
+ args.put("channel", channel);
+ args.put("limit", String.valueOf(limit));
+ args.put("callback", callback);
+ history(args);
+ }
+
+ /**
+ *
+ * Read history from a channel.
+ *
+ * @param args
+ * HashMap of <String, Object> containing channel name, limit
+ * history count
+ * @param requestTimeout
+ * timeout in milliseconds for this request
+ * @return JSONArray of history.
+ */
+ private void history(Hashtable args) {
+
+ final String channel = (String) args.get("channel");
+ String limit = (String) args.get("limit");
+ final Callback callback = (Callback) args.get("callback");
+
+ String[] urlargs = { this.ORIGIN, "history", this.SUBSCRIBE_KEY,
+ channel, "0", limit };
+
+ PubnubRequest req = new PubnubRequest(urlargs, channel, new ResponseHandler() {
+
+ public void handleResponse(String response) {
+ callback.successCallback(channel, response);
+
+ }
+
+ public void handleError(String response) {
+ callback.errorCallback(channel, response);
+
+ }
+
+ });
+ _request(req, simpleConnManager);
+ }
+
+ /**
+ *
+ * Read DetailedHistory for a channel.
+ *
+ * @param channel
+ * Channel name for which detailed history is required
+ * @param start
+ * Start time
+ * @param end
+ * End time
+ * @param count
+ * Upper limit on number of messages to be returned
+ * @param reverse
+ * True if messages need to be in reverse order
+ * @return JSONArray of detailed history.
+ */
+ public void detailedHistory(final String channel, long start, long end,
+ int count, boolean reverse, final Callback callback) {
+ Hashtable parameters = new Hashtable();
+ if (count == -1)
+ count = 100;
+
+ parameters.put("count", String.valueOf(count));
+ parameters.put("reverse", String.valueOf(reverse));
+
+ if (start != -1)
+ parameters.put("start", Long.toString(start).toLowerCase());
+
+ if (end != -1)
+ parameters.put("end", Long.toString(end).toLowerCase());
+
+ String[] urlargs = { this.ORIGIN, "v2", "history", "sub-key",
+ this.SUBSCRIBE_KEY, "channel", channel };
+
+ PubnubRequest req = new PubnubRequest(urlargs, parameters, channel,
+ new ResponseHandler() {
+
+ public void handleResponse(String response) {
+ callback.successCallback(channel, response);
+
+ }
+
+ public void handleError(String response) {
+ callback.errorCallback(channel, response);
+
+ }
+
+ });
+ _request(req, simpleConnManager);
+ }
+
+ /**
+ *
+ * Read DetailedHistory for a channel.
+ *
+ * @param channel
+ * Channel name for which detailed history is required
+ * @param start
+ * Start time
+ * @param reverse
+ * True if messages need to be in reverse order
+ * @return JSONArray of detailed history.
+ */
+ public void detailedHistory(String channel, long start, boolean reverse,
+ Callback callback) {
+ detailedHistory(channel, start, -1, -1, reverse, callback);
+ }
+
+ /**
+ *
+ * Read DetailedHistory for a channel.
+ *
+ * @param channel
+ * Channel name for which detailed history is required
+ * @param start
+ * Start time
+ * @param end
+ * End time
+ * @return JSONArray of detailed history.
+ */
+ public void detailedHistory(String channel, long start, long end,
+ Callback callback) {
+ detailedHistory(channel, start, end, -1, false, callback);
+ }
+
+ /**
+ *
+ * Read DetailedHistory for a channel.
+ *
+ * @param channel
+ * Channel name for which detailed history is required
+ * @param start
+ * Start time
+ * @param end
+ * End time
+ * @param reverse
+ * True if messages need to be in reverse order
+ * @return JSONArray of detailed history.
+ */
+ public void detailedHistory(String channel, long start, long end,
+ boolean reverse, Callback callback) {
+ detailedHistory(channel, start, end, -1, reverse, callback);
+ }
+
+ /**
+ *
+ * Read DetailedHistory for a channel.
+ *
+ * @param channel
+ * Channel name for which detailed history is required
+ * @param count
+ * Upper limit on number of messages to be returned
+ * @param reverse
+ * True if messages need to be in reverse order
+ * @return JSONArray of detailed history.
+ */
+ public void detailedHistory(String channel, int count, boolean reverse,
+ Callback callback) {
+ detailedHistory(channel, -1, -1, count, reverse, callback);
+ }
+
+ /**
+ *
+ * Read DetailedHistory for a channel.
+ *
+ * @param channel
+ * Channel name for which detailed history is required
+ * @param reverse
+ * True if messages need to be in reverse order
+ * @return JSONArray of detailed history.
+ */
+ public void detailedHistory(String channel, boolean reverse,
+ Callback callback) {
+ detailedHistory(channel, -1, -1, -1, reverse, callback);
+ }
+
+ /**
+ *
+ * Read DetailedHistory for a channel.
+ *
+ * @param channel
+ * Channel name for which detailed history is required
+ * @param reverse
+ * True if messages need to be in reverse order
+ * @return JSONArray of detailed history.
+ */
+ public void detailedHistory(String channel, int count, Callback callback) {
+ detailedHistory(channel, -1, -1, count, false, callback);
+ }
+
+ /**
+ * Read current time from PubNub Cloud.
+ *
+ * @return current timestamp.
+ */
+ public void time(final Callback cb) {
+
+ String[] url = { this.ORIGIN, "time", "0" };
+ PubnubRequest req = new PubnubRequest(url, (String) null, new ResponseHandler() {
+
+ public void handleResponse(String response) {
+ cb.successCallback(null, response);
+ }
+
+ public void handleError(String response) {
+ cb.errorCallback(null, response);
+ }
+
+ });
+
+ _request(req, simpleConnManager);
+ }
+
+ private boolean inputsValid(Hashtable args) throws PubnubException {
+ boolean channelMissing;
+ if (((Callback) args.get("callback")) == null) {
+ throw new PubnubException("Invalid Callback");
+ }
+ Object _channels = args.get("channels");
+ Object _channel = args.get("channel");
+
+ channelMissing = ((_channel == null || _channel.equals("")) && (_channels == null || _channels
+ .equals(""))) ? true : false;
+
+ if (channelMissing) {
+ throw new PubnubException("Channel Missing");
+ }
+ return true;
+ }
+
+ /**
+ * Unsubscribe/Disconnect from channel.
+ *
+ * @param channels
+ * String[] array containing channel names as string.
+ */
+ public void unsubscribe(String[] channels) {
+ for (int i = 0; i < channels.length; i++) {
+ subscriptions.removeChannel(channels[i]);
+ }
+ }
+
+ /**
+ * Unsubscribe/Disconnect from channel.
+ *
+ * @param channel
+ * channel name as String.
+ */
+ public void unsubscribe(String channel) {
+ unsubscribe(new String[] { channel });
+ }
+
+ /**
+ * Unsubscribe/Disconnect from channel.
+ *
+ * @param args
+ * Hashtable containing channel name.
+ */
+ public void unsubscribe(Hashtable args) {
+ String[] channelList = (String[]) args.get("channels");
+ if (channelList == null) {
+ channelList = new String[] { (String) args.get("channel") };
+ }
+ unsubscribe(channelList);
+ }
+
+ /**
+ *
+ * Listen for a message on a channel.
+ *
+ * @param args
+ * Hashtable containing channel name
+ * @param callback
+ * Callback
+ * @exception PubnubException
+ * Throws PubnubException if Callback is null
+ */
+ public void subscribe(Hashtable args, Callback callback)
+ throws PubnubException {
+
+ args.put("callback", callback);
+
+ if (!inputsValid(args)) {
+ return;
+ }
+ args.put("timetoken", "0");
+ _subscribe(args);
+ }
+
+ /**
+ *
+ * Listen for a message on a channel.
+ *
+ * @param args
+ * Hashtable containing channel name, callback
+ * @exception PubnubException
+ * Throws PubnubException if Callback is null
+ */
+ public void subscribe(Hashtable args) throws PubnubException {
+
+ if (!inputsValid(args)) {
+ return;
+ }
+
+ args.put("timetoken", "0");
+ _subscribe(args);
+ }
+
+ /**
+ *
+ * Listen for a message on a channel.
+ *
+ * @param channelsArr
+ * Array of channel names (string) to listen on
+ * @param callback
+ * Callback
+ * @exception PubnubException
+ * Throws PubnubException if Callback is null
+ */
+ public void subscribe(String[] channelsArr, Callback callback)
+ throws PubnubException {
+
+ Hashtable args = new Hashtable();
+
+ args.put("channels", channelsArr);
+ args.put("callback", callback);
+ subscribe(args);
+ }
+
+ private void callErrorCallbacks(String[] channelList, String message) {
+ for (int i = 0; i < channelList.length; i++) {
+ Callback cb = ((Channel) subscriptions.getChannel(channelList[i])).callback;
+ cb.errorCallback(channelList[i], message);
+ }
+ }
+
+ /**
+ * @param args
+ * Hashtable
+ */
+ private void _subscribe(Hashtable args) {
+
+ String[] channelList = (String[]) args.get("channels");
+ if (channelList == null) {
+ channelList = new String[] { (String) args.get("channel") };
+ }
+ Callback callback = (Callback) args.get("callback");
+ String timetoken = (String) args.get("timetoken");
+
+ /*
+ * Scan through the channels array. If a channel does not exist in
+ * hashtable create a new entry with default values. If already exists
+ * and connected, then return
+ */
+
+ for (int i = 0; i < channelList.length; i++) {
+ String channel = channelList[i];
+ Channel channelObj = (Channel) subscriptions.getChannel(channel);
+
+ if (channelObj == null) {
+ Channel ch = new Channel();
+ ch.name = channel;
+ ch.connected = false;
+ ch.callback = callback;
+ subscriptions.addChannel(ch);
+ } else if (channelObj.connected) {
+
+ return;
+ }
+ }
+ _subscribe_base(timetoken);
+ }
+
+ /**
+ * @param timetoken
+ * , Timetoken to be used
+ */
+ private void _subscribe_base(String timetoken) {
+ String channelString = subscriptions.getChannelString();
+ String[] channelsArray = subscriptions.getChannelNames();
+
+ if (channelString == null) {
+ callErrorCallbacks(channelsArray, "Parsing Error");
+ return;
+ }
+
+ String[] urlComponents = { Pubnub.this.ORIGIN, "subscribe",
+ Pubnub.this.SUBSCRIBE_KEY, channelString, "0", timetoken };
+
+ Hashtable params = new Hashtable();
+ params.put("uuid", uuid());
+
+ PubnubRequest req = new PubnubRequest(urlComponents, params, channelsArray,
+ new ResponseHandler() {
+ String _timetoken = "0";
+
+ public void handleResponse(String response) {
+
+ subscriptions.invokeConnectCallbackOnChannels();
+
+ /*
+ * Check if response has channel names. A JSON response
+ * with more than 2 items means the response contains
+ * the channel names as well. The channel names are in a
+ * comma delimted string. Call success callback on all
+ * he channels passing the corresponding response
+ * message.
+ */
+
+ JSONArray jsa;
+ try {
+ jsa = new JSONArray(response);
+ String _timetoken = jsa.get(1).toString();
+ JSONArray messages = new JSONArray(jsa.get(0)
+ .toString());
+
+ if (jsa.length() > 2) {
+ /*
+ * Response has multiple channels
+ */
+
+ String[] _channels = PubnubUtil.splitString(
+ jsa.getString(2), ",");
+
+ for (int i = 0; i < _channels.length; i++) {
+ Channel _channel = (Channel) subscriptions
+ .getChannel(_channels[i]);
+ if (_channel != null)
+ _channel.callback.successCallback(
+ _channels[i], messages.get(i));
+ }
+
+ } else {
+ /*
+ * Response for single channel Callback on
+ * single channel
+ */
+ Channel _channel = subscriptions
+ .getFirstChannel();
+
+ if (_channel != null) {
+ for (int i = 0; i < messages.length(); i++) {
+ _channel.callback.successCallback(
+ _channel.name, messages.get(i));
+ }
+ }
+
+ }
+ _subscribe_base(_timetoken);
+ } catch (JSONException e) {
+ _subscribe_base(_timetoken);
+ }
+
+ }
+
+ public void handleError(String response) {
+ subscriptions.invokeDisconnectCallbackOnChannels();
+ _subscribe_base(_timetoken);
+ }
+ });
+
+ _request(req, longPollConnManager);
+ }
+
+ /**
+ * @param req
+ * @param connManager
+ */
+ private void _request(final PubnubRequest req, HttpManager connManager) {
+
+ HttpRequest hreq = new HttpRequest(req.getUrl(), _headers, req.responseHandler);
+ connManager.queue(hreq);
+ }
+}
View
89 j2me/3.4/src/com/pubnub/api/Request.java
@@ -1,89 +0,0 @@
-package com.pubnub.api;
-
-import java.util.Enumeration;
-import java.util.Hashtable;
-
-/**
- *
- * @author Pubnub
- */
-class Request {
-
- private String[] urlComponents;
- private Hashtable params;
- private String url;
- private String[] channels;
- ResponseHandler responseHandler;
-
- public String[] getChannels() {
-
- return this.channels;
- }
-
- public String getUrl() {
-
- if (url != null) {
- return url;
- }
-
- String url = PubnubUtil.joinString(urlComponents, "/");
-
- if (this.params != null) {
- StringBuffer sb = new StringBuffer();
- sb.append(url).append("?");
-
- Enumeration paramsKeys = this.params.keys();
- boolean first = true;
- while (paramsKeys.hasMoreElements()) {
- if (!first) {
- sb.append("&");
- } else
- first = false;
-
- String key = (String) paramsKeys.nextElement();
- sb.append(PubnubUtil.urlEncode((String) key))
- .append("=")
- .append(PubnubUtil.urlEncode((String) this.params
- .get(key)));
- }
-
- url = sb.toString();
- }
- this.url = url;
-
- return this.url;
- }
-
- public Request(String[] urlComponents, String[] channels,
- ResponseHandler responseHandler) {
- this.channels = channels;
- this.responseHandler = responseHandler;
- this.urlComponents = urlComponents;
-
- }
-
- public Request(String[] urlComponents, Hashtable params, String[] channels,
- ResponseHandler responseHandler) {
- this.channels = channels;
- this.responseHandler = responseHandler;
- this.params = params;
- this.urlComponents = urlComponents;
- }
-
- public Request(String[] urlComponents, String channel,
- ResponseHandler responseHandler) {
-
- this.channels = new String[] { channel };
- this.responseHandler = responseHandler;
- this.urlComponents = urlComponents;
- }
-
- public Request(String[] urlComponents, Hashtable params, String channel,
- ResponseHandler responseHandler) {
-
- this.channels = new String[] { channel };
- this.responseHandler = responseHandler;
- this.params = params;
- this.urlComponents = urlComponents;
- }
-}
View
11 j2me/3.4/src/com/pubnub/api/ResponseHandler.java
@@ -1,11 +0,0 @@
-package com.pubnub.api;
-
-/**
- * @author Pubnub
- */
-
-interface ResponseHandler {
- public abstract void handleResponse(String response);
-
- public abstract void handleError(String response);
-}
View
26 j2me/3.4/src/com/pubnub/asynchttp/AsyncHttpCallback.java
@@ -1,26 +0,0 @@
-package com.pubnub.asynchttp;
-
-import java.io.IOException;
-import java.util.Hashtable;
-import javax.microedition.io.HttpConnection;
-
-public interface AsyncHttpCallback {
-
- String startingCall();
-
- boolean checkResponse(HttpConnection conn) throws IOException;
-
- void endingCall(HttpConnection conn) throws IOException;
-
- void cancelingCall(HttpConnection conn) throws IOException;
-
- Hashtable getHeaderFields();
-
- public HttpConnection getConnection();
-
- public void setConnection(HttpConnection connection);
-
- public void setConnManager(AsyncHttpManager connManager);
-
- public void errorCall(HttpConnection hconn, int statusCode, String response);
-}
View
361 j2me/3.4/src/com/pubnub/asynchttp/AsyncHttpManager.java
@@ -1,361 +0,0 @@
-package com.pubnub.asynchttp;
-
-import java.io.IOException;
-import java.util.Enumeration;
-import java.util.Hashtable;
-import java.util.Vector;
-
-import javax.microedition.io.Connector;
-import javax.microedition.io.HttpConnection;
-
-public class AsyncHttpManager {
-
- private static int _maxWorkers = 1;
- private Vector _waiting = new Vector();
- private Worker _workers[];
- private static Network network;
-
- private class Network {
- private boolean available = true;
-
- public synchronized boolean isAvailable() {
- return available;
- }
-
- public synchronized void available() {
- available = true;
- this.notifyAll();
- }
-
- public synchronized void unavailable() {
- available = false;
- }
- }
-
- public static void startHeartbeat(String url, int interval) {
- new Thread(new Heartbeat(url, interval), "heartbeat").start();
- }
-
- public void cancel(HttpCallback cb) {
- for (int i = 0; i < _workers.length; ++i) {
- if (_workers[i].asyncConnection != null) {
- if (!_workers[i].getDie()) {
-
- cancel(_workers[i].asyncConnection);
- _workers[i].asyncConnection = null;
-
- }
- }
- }
-
- }
-
- private void cancel(AsyncConnection conn) {
- AsyncHttpCallback cb = conn.getCallback();
-
- try {
- close(conn);
- cb.cancelingCall(conn.getHttpConnection());
- } catch (IOException ignore) {
- } finally {
- close(conn);
-
- }
- }
-
- public void cancelAll() {
- synchronized (_waiting) {
-
- for (int i = 0; i < _workers.length; ++i) {
- _workers[i].die();
- }
-
- while (_waiting.size() != 0) {
- AsyncConnection conn = (AsyncConnection) _waiting
- .firstElement();
- _waiting.removeElementAt(0);
-
- cancel(conn);
- }
- _workers = null;
- _waiting.notifyAll();
-
- }
- }
-
- private void close(AsyncConnection conn) {
- if (conn != null) {
- close(conn.getHttpConnection());
- conn.setHttpConnection(null);
- }
- }
-
- private static void close(HttpConnection hc) {
- if (hc != null) {
- try {
- hc.close();
- } catch (IOException e) {
-
- }
- }
- }
-
- public static int getWorkerCount() {
- return _maxWorkers;
- }
-
- private void init(int maxCalls, String name) {
- if (maxCalls < 1) {
- maxCalls = 1;
- }
- _workers = new Worker[maxCalls];
- for (int i = 0; i < maxCalls; ++i) {
- Worker w = new Worker();
- _workers[i] = w;
- new Thread(w, name).start();
- }
- if (network == null) {
- network = new Network();
- }
- }
-
- public AsyncHttpManager(String name) {
- init(_maxWorkers, name);
- }
-
- public static boolean isRedirect(int rc) {
- return (rc == HttpConnection.HTTP_MOVED_PERM
- || rc == HttpConnection.HTTP_MOVED_TEMP
- || rc == HttpConnection.HTTP_SEE_OTHER || rc == HttpConnection.HTTP_TEMP_REDIRECT);
- }
-
- public void queue(AsyncHttpCallback request) {
- queue(request, null);
- }
-
- public void queue(AsyncHttpCallback cb, HttpConnection hc) {
-
- if (!network.isAvailable()) {
- cb.errorCall(hc, 0, "[0,'Network Error']");
- return;
- }
- cb.setConnManager(this);
- synchronized (_waiting) {
- AsyncConnection conn = new AsyncConnection(cb, hc);
- _waiting.addElement(conn);
- _waiting.notifyAll();
- }
- }
-
- public static void setWorkerCount(int count) {
- _maxWorkers = count;
- }
-
- private static class AsyncConnection {
-
- AsyncConnection(AsyncHttpCallback cb, HttpConnection hc) {
- _callback = cb;
- _httpconn = hc;
- }
-
- AsyncHttpCallback getCallback() {
- return _callback;
- }
-
- HttpConnection getHttpConnection() {
- return _httpconn;
- }
-
- void setHttpConnection(HttpConnection hc) {
- _httpconn = hc;
- }
-
- private AsyncHttpCallback _callback;
- private HttpConnection _httpconn;
- }
-
- private static class Heartbeat implements Runnable {
- private boolean runHeartbeat = true;
- private String heartbeatUrl;
- private int heartbeatInterval;
-
- public Heartbeat(String url, int interval) {
- this.heartbeatUrl = url;
- this.heartbeatInterval = interval;
- }
-
- public void run() {
- while (runHeartbeat) {
- HttpConnection hc = null;
- try {
-
- hc = (HttpConnection) Connector.open(heartbeatUrl,
- Connector.READ_WRITE, true);
- hc.setRequestMethod(HttpConnection.GET);
-
- int rc = hc.getResponseCode();
- if (rc == HttpConnection.HTTP_OK) {
- network.available();
- } else {
- network.unavailable();
- }
- close(hc);
-
- } catch (IOException e) {
- network.unavailable();
-
- } finally {
- if (hc != null)
- close(hc);
- }
- try {
- Thread.sleep(heartbeatInterval);
- } catch (InterruptedException e) {
-
- }
- }
- }
-
- }
-
- private class Worker implements Runnable {
-
- public void die() {
- _die = true;
- }
-
- public boolean getDie() {
- return _die;
- }
-
- private void process(AsyncConnection conn) {
-
- AsyncHttpCallback cb = conn.getCallback();
- String url = null;
- HttpConnection hc = null;
- try {
- hc = conn.getHttpConnection();
-
- if (hc == null) {
- url = cb.startingCall();
- if (url == null) {
- cancel(conn);
- return;
- }
- }
-
- int follow = 5;
-
- while (follow-- > 0) {
- hc = conn.getHttpConnection();
-
- if (hc == null) {
-
- try {
- hc = (HttpConnection) Connector.open(url,
- Connector.READ_WRITE, true);
- hc.setRequestMethod(HttpConnection.GET);
- Hashtable headers = cb.getHeaderFields();
- Enumeration en = headers.keys();
- while (en.hasMoreElements()) {
- String key = (String) en.nextElement();