crossroads

Git mirror of https://crossroads.e-tunity.com/
git clone git://git.finwo.net/app/crossroads
Log | Files | Refs

dispatcher-roundrobin (2291B)


      1 #!/usr/bin/perl
      2 
      3 use strict;
      4 
      5 # Example of a round-robin external dispatcher. This is totally
      6 # superfluous, Crossroads has this on-board; if you use the external
      7 # program for determining round-robin dispatching, then you'll only
      8 # slow things down. This script is just meant as an example.
      9 
     10 # Globals / configuration
     11 # -----------------------
     12 my $log = '/tmp/exthandler.log';    # Debug log, set to /dev/null to suppress
     13 my $statefile = '/tmp/rr.last';	    # Where we keep the last used
     14 
     15 # Logging
     16 # -------
     17 sub msg {
     18     return if ($log eq '/dev/null' or $log eq '');
     19     open (my $of, ">>$log") or return;
     20     print $of (scalar(localtime()), ' ', @_);
     21 }
     22 
     23 # Read the last used back end
     24 # ---------------------------
     25 sub readlast() {
     26     my $ret;
     27     
     28     if (open (my $if, $statefile)) {
     29 	$ret = <$if>;
     30 	chomp ($ret);
     31 	close ($if);
     32 	msg ("Last used back end: $ret\n");
     33 	return ($ret);
     34     }
     35     msg ("No last-used back end (yet)\n");
     36     return (undef);    
     37 }
     38 
     39 # Write back the last used back end, reply to Crossroads and stop
     40 # ---------------------------------------------------------------
     41 sub reply ($) {
     42     my $last = shift;
     43 
     44     if (open (my $of, ">$statefile")) {
     45 	print $of ("$last\n");
     46     }
     47     print ("$last\n");
     48     exit (0);
     49 }
     50 
     51 # Main starts here
     52 # ----------------
     53 
     54 # Collect the cmdline arguments. We expect pairs of backend-name /
     55 # backend-availablility, and we'll store only the available ones.
     56 msg ("Dispatch request received\n");
     57 my @backend;
     58 for (my $i = 0; $i <= $#ARGV; $i += 2) {
     59     push (@backend,  $ARGV[$i]) if ($ARGV[$i + 1]);
     60 }
     61 msg ("Available back ends: @backend\n");
     62 
     63 # Let's see what the last one is. If none found, then we return the
     64 # first available back end. Otherwise we need to go thru the list of
     65 # back ends, and return the next one in line.
     66 my $last = readlast();
     67 if ($last eq '') {
     68     msg ("Returning first available back end $backend[0]\n");
     69     reply ($backend[0]);
     70 }
     71 
     72 # There **was** a last back end.  Try to match it in the list,
     73 # then return the next-in-line.
     74 for (my $i = 0; $i < $#backend; $i++) {
     75     if ($last eq $backend[$i]) {
     76 	msg ("Returning next back end ", $backend[$i + 1], "\n");
     77 	reply ($backend[$i + 1]);
     78     }
     79 }
     80 
     81 # No luck.. run back to the first one.
     82 msg ("Returning first back end $backend[0]\n");
     83 reply ($backend[0]);
     84