Saturday, September 6, 2014

IP Switching with AJAX-JSON using Recursion

In the last post, I showed an IP switching technique using AJAX-JSON. This checks on the first IP, and if determined not to be live, uses the second IP. This approach works fine if we're sure that at least one of the two IP addresses will always be live.

But what if there's a possibility that both IP addresses will be down? Or what if there's more than two IP addresses to test?

That would put a need for the testing of IP address to be repeated until we get a live IP or run out of IP address to test. The code for testing will be adjusted to apply recursion.

The adjusted code:
<!doctype html>
<html>
  <head>
    <title>IP Switch with JSON</title>
 <script src="jquery-1.11.1.min.js"></script>
 <script>
 
 $(document).ready(function (){
  function test_ip(Ip_arr, ctr, finalUrl){
   if(ctr < Ip_arr.length){
    var param = 'json_test.php?action=test&jsonp=?';
    var test_url = Ip_arr[ctr];
    $.ajax({
     url: test_url + param,
     dataType: 'json',
     timeout: 1000,
     success: function(jdata){
      var offset = ctr +1;
      finalUrl('IP' + offset, Ip_arr[ctr]);      
     },
     error: function(xhr, ajaxOptions, thrownError) {
      ctr += 1;
      test_ip(Ip_arr, ctr, finalUrl);
     }
    });
   }
   else{
    alert("No IP is available");
   }
  }
   
  function start_app(){
   var Ip_arr = ['http://aaa.aa.aa.10/', 'http://zzz.zz.zz.15/'];
   var ctr = 0;
   test_ip(Ip_arr, ctr, function(Ip_mode, ip){
    alert('IP mode= '+Ip_mode+ '/ IP: '+ip);
    //continue with code
   });
  }
  
  start_app();
 })
 
 </script>
  </head>
  <body>
  </body>
</html>

The start_app() function defines a variable ctr. This is the IP array element counter and keeps track of the current element. The initial value is zero, being the first array element in JavaScript. The element counter is passed as a parameter to test_ip() function.



When test_ip() function is called. A check is made if the element counter variable ctr has not yet reached the total array length.This means that there is an IP address to test and the code proceeds with the $.ajax call. Otherwise, the counter has reached the array length and there is no more IP address to test. An alert handles this situation.























If the check determines an IP address to test, a variable test_url is defined and takes the current element of the IP array. The variable is used on the url setting of the $.ajax call.






The error handler of the $.ajax call is triggered when the AJAX call to the url fails to return the JSON response. The variable ctr is incremented by one- this moves to the next element of the IP array. The call to test_ip() function is repeated.
Add caption


















The test_ip() function is repeatedly called until a live IP is found or the testing runs out of IP addresses to test.

Need help on some web coding task? Let me know about it. Email me at jrwxpun.dolor@gmqoapmssail.co57agsblm and I'll be more than happy to blog about it with my thoughts.

No comments:

Post a Comment