Pages

Saturday, January 4, 2014

JS: Communication Gateway

There is always a need of a single point of contact in every communication matter in real life. In the same way, in coding also when this technique is applied, it makes the communication easier to handle and to track the information flow. So here is an implementation of the same real life scenario in javascript. This technique can be extended or implemented in any language wherever there is a need of controlled communication.

It's working is very straight forward. All the request calls are pushed into the queue. Queue works on First-In-First-Out (FIFO) logic. So as the requests get processed, it would be removed from the queue and the next request would be sent fro processing. This works automatically until the request queue become empty.
/* 
@file communicator.js
@author Abhishek Kumar
@email akbittu@gmail.com
*/

// Basic

function Communicator() {

    var requests = [];

    this.request = function (url, data, listener) {
        var package = {
            url: url,
            data: data,
            listener: listener
        };
        requests.push(package);
        if (requests.length == 1) {
            this.call();
        }
    }

    this.call = function () {
        var package = requests.shift();
        $.ajax({
            type: "POST",
            url: package.url,
            data: package.data,
        }).done(function (response) {
            console.log("Success: " + response);
            if (requests.length > 0) {
                this.call();
            }
            package.listener(response);
        }).fail(function (jqXHR, textStatus) {
            console.log("Failed: " + textStatus);
        });
    }

}

// Advanced

function Communicator() {

    var requests = [];
    
    var serialize = function(obj) {
        var str = [];
        for (var p in obj) {
            if (obj.hasOwnProperty(p)) {
                str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
            }
        }
        return str.join("&");
    }

    this.request = function (url, data, listener) {
        if (typeof (data) == 'object') {
            data = serialize(data);
        }
        var package = {
            url: url,
            data: data,
            listener: listener
        };
        requests.push(package);
        if (requests.length == 1) {
            this.call();
        }
    }

    this.call = function () {
        var package = requests.shift();        
        $.ajax({
            type: "POST",
            url: package.url,
            data: package.data,
            cache: false,
            success: function(response) {
                console.log("Success: " + response);
                if (requests.length > 0) {
                    this.call();
                }
                package.listener(response);
            },
            error: function(jqXHR, textStatus){
                console.log("Failed: " + textStatus);
            }
        });
    }
}

// Usage

var oCommunicator = new Communicator();
oCommunicator.request('services.php', {
    cmd: 'login',
    arg: '["user1","pass1"]'
}, function (response) {
    console.log("Success: " + response);
});

No comments:

Post a Comment