gwt/3.3/src/pubnub/api/Pubnub.java
/**
     * Presence - Private Interface
     *
     * Implemented by Leonardo Redmond on GitHub
     *
     * @param HashMap
     *            <String, Object> containing channel name, function callback,
     *            timetoken.
     */
Private Interface
Implemented by Leonardo Redmond on GitHub

@param HashMap
 containing channel name, function callback,
timetoken.
Private Interface Implemented by Leonardo Redmond on GitHub @param HashMap containing channel name function callback timetoken .
private void _presence(HashMap<String, Object> args) {
        String channel = (String) args.get("channel");
        String timetoken = (String) args.get("timetoken");
        Callback callback;
        if (args.get("callback") != null) {
            callback = (Callback) args.get("callback");
        } else {
            System.out.println("Invalid Callback.");
            return;
        }
        if (channel == null || channel.equals("")) {
            callback.errorCallback(channel, "Invalid Channel.");
            return;
        }
        if (subscriptions != null && subscriptions.size() > 0) {
            boolean channel_exist = false;
            for (ChannelStatus it : subscriptions) {
                if (it.channel.equals(channel)) {
                    channel_exist = true;
                    break;
                }
            }
            if (!channel_exist) {
                ChannelStatus cs = new ChannelStatus();
                cs.channel = channel;
                cs.connected = true;
                subscriptions.add(cs);
            } else {
                callback.errorCallback(channel, "Already Connected");
                return;
            }
        } else {
            ChannelStatus cs = new ChannelStatus();
            cs.channel = channel;
            cs.connected = true;
            subscriptions = new ArrayList<Pubnub.ChannelStatus>();
            subscriptions.add(cs);
        }
        while (true) {
            try {
                List<String> url = java.util.Arrays.asList("subscribe",
                                   this.SUBSCRIBE_KEY, channel, "0", timetoken);
                boolean is_disconnect = false;
                for (ChannelStatus it : subscriptions) {
                    if (it.channel.equals(channel)) {
                        if (!it.connected) {
                            subscriptions.remove(it);
                            callback.disconnectCallback(channel);
                            is_disconnect = true;
                            break;
                        }
                    }
                }
                if (is_disconnect)
                    return;
                JSONArray response = _request(url);
                for (ChannelStatus it : subscriptions) {
                    if (it.channel.equals(channel)) {
                        if (!it.connected) {
                            subscriptions.remove(it);
                            callback.disconnectCallback(channel);
                            is_disconnect = true;
                            break;
                        }
                    }
                }
                if (is_disconnect)
                    return;
                if (response == null || response.optInt(1) == 0) {
                    for (ChannelStatus it : subscriptions) {
                        if (it.channel.equals(channel)) {
                            if (it.connected && it.first) {
                                subscriptions.remove(it);
                                callback.disconnectCallback(channel);
                            } else {
                                subscriptions.remove(it);
                                callback.errorCallback(channel,
                                                       "Lost Network Connection");
                            }
                        }
                    }
                    boolean is_reconnected = false;
                    while (true) {
                        double time_token = this.time();
                        if (time_token == 0.0) {
                            Thread.sleep(5000);
                        } else {
                            callback.reconnectCallback(channel);
                            is_reconnected = true;
                            break;
                        }
                    }
                    if (is_reconnected) {
                        continue;
                    }
                } else {
                    for (ChannelStatus it : subscriptions) {
                        if (it.channel.equals(channel)) {
                            if (!it.first) {
                                it.first = true;
                                callback.connectCallback(channel);
                                break;
                            }
                        }
                    }
                }
                JSONArray messages = response.optJSONArray(0);
                if (response.optString(1).length() > 0)
                    timetoken = response.optString(1);
                for (int i = 0; messages.length() > i; i++) {
                    JSONObject message = messages.optJSONObject(i);
                    if (message != null) {
                        if (this.CIPHER_KEY.length() > 0) {
                            PubnubCrypto pc = new PubnubCrypto(this.CIPHER_KEY);
                            message = pc.decrypt(message);
                        }
                        if (callback != null)
                            if (!callback.presenceCallback(channel, message)) return;
                    } else {
                        JSONArray arr = messages.optJSONArray(i);
                        if (arr != null) {
                            if (this.CIPHER_KEY.length() > 0) {
                                PubnubCrypto pc = new PubnubCrypto(
                                    this.CIPHER_KEY);
                                arr = pc.decryptJSONArray(arr);
                            }
                            if (callback != null)
                                if (!callback.presenceCallback(channel, arr)) return;
                        } else {
                            String msgs = messages.getString(0);
                            if (this.CIPHER_KEY.length() > 0) {
                                PubnubCrypto pc = new PubnubCrypto(
                                    this.CIPHER_KEY);
                                msgs = pc.decrypt(msgs);
                            }
                            if (callback != null)
                                if (!callback.presenceCallback(channel, msgs)) return;
                        }
                    }
                }
            } catch (Exception e) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ie) {
                }
            }
        }
    }
private void _presence ( HashMap < String , Object > args ) { String channel = ( String ) args . get ( "channel" ) ; String timetoken = ( String ) args . get ( "timetoken" ) ; Callback callback ; if ( args . get ( "callback" ) != null ) { callback = ( Callback ) args . get ( "callback" ) ; } else { System . out . println ( "Invalid Callback." ) ; return ; } if ( channel == null || channel . equals ( "" ) ) { callback . errorCallback ( channel , "Invalid Channel." ) ; return ; } if ( subscriptions != null && subscriptions . size ( ) > 0 ) { boolean channel_exist = false ; for ( ChannelStatus it : subscriptions ) { if ( it . channel . equals ( channel ) ) { channel_exist = true ; break ; } } if ( ! channel_exist ) { ChannelStatus cs = new ChannelStatus ( ) ; cs . channel = channel ; cs . connected = true ; subscriptions . add ( cs ) ; } else { callback . errorCallback ( channel , "Already Connected" ) ; return ; } } else { ChannelStatus cs = new ChannelStatus ( ) ; cs . channel = channel ; cs . connected = true ; subscriptions = new ArrayList < Pubnub . ChannelStatus > ( ) ; subscriptions . add ( cs ) ; } while ( true ) { try { List < String > url = java . util . Arrays . asList ( "subscribe" , this . SUBSCRIBE_KEY , channel , "0" , timetoken ) ; boolean is_disconnect = false ; for ( ChannelStatus it : subscriptions ) { if ( it . channel . equals ( channel ) ) { if ( ! it . connected ) { subscriptions . remove ( it ) ; callback . disconnectCallback ( channel ) ; is_disconnect = true ; break ; } } } if ( is_disconnect ) return ; JSONArray response = _request ( url ) ; for ( ChannelStatus it : subscriptions ) { if ( it . channel . equals ( channel ) ) { if ( ! it . connected ) { subscriptions . remove ( it ) ; callback . disconnectCallback ( channel ) ; is_disconnect = true ; break ; } } } if ( is_disconnect ) return ; if ( response == null || response . optInt ( 1 ) == 0 ) { for ( ChannelStatus it : subscriptions ) { if ( it . channel . equals ( channel ) ) { if ( it . connected && it . first ) { subscriptions . remove ( it ) ; callback . disconnectCallback ( channel ) ; } else { subscriptions . remove ( it ) ; callback . errorCallback ( channel , "Lost Network Connection" ) ; } } } boolean is_reconnected = false ; while ( true ) { double time_token = this . time ( ) ; if ( time_token == 0.0 ) { Thread . sleep ( 5000 ) ; } else { callback . reconnectCallback ( channel ) ; is_reconnected = true ; break ; } } if ( is_reconnected ) { continue ; } } else { for ( ChannelStatus it : subscriptions ) { if ( it . channel . equals ( channel ) ) { if ( ! it . first ) { it . first = true ; callback . connectCallback ( channel ) ; break ; } } } } JSONArray messages = response . optJSONArray ( 0 ) ; if ( response . optString ( 1 ) . length ( ) > 0 ) timetoken = response . optString ( 1 ) ; for ( int i = 0 ; messages . length ( ) > i ; i ++ ) { JSONObject message = messages . optJSONObject ( i ) ; if ( message != null ) { if ( this . CIPHER_KEY . length ( ) > 0 ) { PubnubCrypto pc = new PubnubCrypto ( this . CIPHER_KEY ) ; message = pc . decrypt ( message ) ; } if ( callback != null ) if ( ! callback . presenceCallback ( channel , message ) ) return ; } else { JSONArray arr = messages . optJSONArray ( i ) ; if ( arr != null ) { if ( this . CIPHER_KEY . length ( ) > 0 ) { PubnubCrypto pc = new PubnubCrypto ( this . CIPHER_KEY ) ; arr = pc . decryptJSONArray ( arr ) ; } if ( callback != null ) if ( ! callback . presenceCallback ( channel , arr ) ) return ; } else { String msgs = messages . getString ( 0 ) ; if ( this . CIPHER_KEY . length ( ) > 0 ) { PubnubCrypto pc = new PubnubCrypto ( this . CIPHER_KEY ) ; msgs = pc . decrypt ( msgs ) ; } if ( callback != null ) if ( ! callback . presenceCallback ( channel , msgs ) ) return ; } } } } catch ( Exception e ) { try { Thread . sleep ( 1000 ) ; } catch ( InterruptedException ie ) { } } } }
Presence - Private Interface
 Implemented by Leonardo Redmond on GitHub
Presence - Private Interface Implemented by Leonardo Redmond on GitHub
// Validate Arguments // Ensure Single Connection // New Channel // Build URL // Stop Connection? // Wait for Message // Stop Connection? // Problem? // Ensure Connected (Call Time Function) // Reconnect Callback // this._subscribe(args); // Connect Callback // Update TimeToken // Decrypt Message