[LON-CAPA-cvs] cvs: loncom / lond

foxr lon-capa-cvs@mail.lon-capa.org
Wed, 28 Jul 2004 21:33:22 -0000


foxr		Wed Jul 28 17:33:22 2004 EDT

  Modified files:              
    /loncom	lond 
  Log:
  handlerize load and userload requests.
  
  
Index: loncom/lond
diff -u loncom/lond:1.216 loncom/lond:1.217
--- loncom/lond:1.216	Tue Jul 27 07:34:49 2004
+++ loncom/lond	Wed Jul 28 17:33:22 2004
@@ -2,7 +2,7 @@
 # The LearningOnline Network
 # lond "LON Daemon" Server (port "LOND" 5663)
 #
-# $Id: lond,v 1.216 2004/07/27 11:34:49 foxr Exp $
+# $Id: lond,v 1.217 2004/07/28 21:33:22 foxr Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -56,7 +56,7 @@
 my $status='';
 my $lastlog='';
 
-my $VERSION='$Revision: 1.216 $'; #' stupid emacs
+my $VERSION='$Revision: 1.217 $'; #' stupid emacs
 my $remoteVERSION;
 my $currenthostid="default";
 my $currentdomainid;
@@ -1138,6 +1138,72 @@
 &register_handler("ekey", \&establish_key_handler, 0, 1,1);
 
 
+#     Handler for the load command.  Returns the current system load average
+#     to the requestor.
+#
+# Parameters:
+#      $cmd    - the actual keyword that invoked us.
+#      $tail   - the tail of the request that invoked us.
+#      $replyfd- File descriptor connected to the client
+#  Implicit Inputs:
+#      $currenthostid - Global variable that carries the name of the host
+#                       known as.
+#      $clientname    - Global variable that carries the name of the hsot we're connected to.
+#  Returns:
+#      1       - Ok to continue processing.
+#      0       - Program should exit.
+#  Side effects:
+#      Reply information is sent to the client.
+sub load_handler {
+    my ($cmd, $tail, $replyfd) = @_;
+
+   # Get the load average from /proc/loadavg and calculate it as a percentage of
+   # the allowed load limit as set by the perl global variable lonLoadLim
+
+    my $loadavg;
+    my $loadfile=IO::File->new('/proc/loadavg');
+   
+    $loadavg=<$loadfile>;
+    $loadavg =~ s/\s.*//g;                      # Extract the first field only.
+   
+    my $loadpercent=100*$loadavg/$perlvar{'lonLoadLim'};
+
+    &Reply( $replyfd, "$loadpercent\n", "$cmd:$tail");
+   
+    return 1;
+}
+register_handler("load", \&load_handler, 0, 1, 0);
+
+#
+#   Process the userload request.  This sub returns to the client the current
+#  user load average.  It can be invoked either by clients or managers.
+#
+# Parameters:
+#      $cmd    - the actual keyword that invoked us.
+#      $tail   - the tail of the request that invoked us.
+#      $replyfd- File descriptor connected to the client
+#  Implicit Inputs:
+#      $currenthostid - Global variable that carries the name of the host
+#                       known as.
+#      $clientname    - Global variable that carries the name of the hsot we're connected to.
+#  Returns:
+#      1       - Ok to continue processing.
+#      0       - Program should exit
+# Implicit inputs:
+#     whatever the userload() function requires.
+#  Implicit outputs:
+#     the reply is written to the client.
+#
+sub user_load_handler {
+    my ($cmd, $tail, $replyfd) = @_;
+
+    my $userloadpercent=&userload();
+    &Reply($replyfd, "$userloadpercent\n", "$cmd:$tail");
+    
+    return 1;
+}
+register_handler("userload", \&user_load_handler, 0, 1, 0);
+
 
 #---------------------------------------------------------------
 #
@@ -1252,35 +1318,8 @@
 
 #------------------- Commands not yet in spearate handlers. --------------
 
-# ------------------------------------------------------------------------ load
-    if ($userinput =~ /^load/) { # client only
-	if (isClient) {
-	    my $loadavg;
-	    {
-		my $loadfile=IO::File->new('/proc/loadavg');
-		$loadavg=<$loadfile>;
-	    }
-	    $loadavg =~ s/\s.*//g;
-	    my $loadpercent=100*$loadavg/$perlvar{'lonLoadLim'};
-	    print $client "$loadpercent\n";
-	} else {
-	    Reply($client, "refused\n", $userinput);
-	    
-	}
-# -------------------------------------------------------------------- userload
-    } elsif ($userinput =~ /^userload/) { # client only
-	if(isClient) {
-	    my $userloadpercent=&userload();
-	    print $client "$userloadpercent\n";
-	} else {
-	    Reply($client, "refused\n", $userinput);
-	    
-	}
-#
-#        Transactions requiring encryption:
-#
 # ----------------------------------------------------------------- currentauth
-    } elsif ($userinput =~ /^currentauth/) {
+    if ($userinput =~ /^currentauth/) {
 	if (($wasenc==1)  && isClient) { # Encoded & client only.
 	    my ($cmd,$udom,$uname)=split(/:/,$userinput);
 	    my $result = GetAuthType($udom, $uname);