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

foxr lon-capa-cvs@mail.lon-capa.org
Tue, 27 Jul 2004 10:50:37 -0000


foxr		Tue Jul 27 06:50:37 2004 EDT

  Modified files:              
    /loncom	lond 
  Log:
  Added dispatch logic and comments about how to use it.  For now if
  no match, dispatch logic falls though to the big bad ugly if/elsif chain.
  
  
  
Index: loncom/lond
diff -u loncom/lond:1.212 loncom/lond:1.213
--- loncom/lond:1.212	Tue Jul 27 06:25:07 2004
+++ loncom/lond	Tue Jul 27 06:50:37 2004
@@ -2,7 +2,7 @@
 # The LearningOnline Network
 # lond "LON Daemon" Server (port "LOND" 5663)
 #
-# $Id: lond,v 1.212 2004/07/27 10:25:07 foxr Exp $
+# $Id: lond,v 1.213 2004/07/27 10:50:37 foxr Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -56,7 +56,7 @@
 my $status='';
 my $lastlog='';
 
-my $VERSION='$Revision: 1.212 $'; #' stupid emacs
+my $VERSION='$Revision: 1.213 $'; #' stupid emacs
 my $remoteVERSION;
 my $currenthostid;
 my $currentdomainid;
@@ -1079,6 +1079,66 @@
     }
     Debug("process_request: $userinput\n");
     
+    #  
+    #   The 'correct way' to add a command to lond is now to
+    #   write a sub to execute it and Add it to the command dispatch
+    #   hash via a call to register_handler..  The comments to that
+    #   sub should give you enough to go on to show how to do this
+    #   along with the examples that are building up as this code
+    #   is getting refactored.   Until all branches of the
+    #   if/elseif monster below have been factored out into
+    #   separate procesor subs, if the dispatch hash is missing
+    #   the command keyword, we will fall through to the remainder
+    #   of the if/else chain below in order to keep this thing in 
+    #   working order throughout the transmogrification.
+
+    my ($command, $tail) = split(/:/, $userinput, 2);
+    chomp($command);
+    chomp($tail);
+    $tail =~ s/(\r)//;		# This helps people debugging with e.g. telnet.
+
+    &Debug("Command received: $command, encoded = $wasenc");
+
+    if(defined $Dispatcher{$command}) {
+
+	my $dispatch_info = $Dispatcher{$command};
+	my $handler       = $$dispatch_info[0];
+	my $need_encode   = $$dispatch_info[1];
+	my $client_types  = $$dispatch_info[2];
+	Debug("Matched dispatch hash: mustencode: $need_encode "
+	      ."ClientType $client_types");
+      
+	#  Validate the request:
+      
+	my $ok = 1;
+	my $requesterprivs = 0;
+	if(&isClient()) {
+	    $requesterprivs |= $CLIENT_OK;
+	}
+	if(&isManager()) {
+	    $requesterprivs |= $MANAGER_OK;
+	}
+	if($need_encode && (!$wasenc)) {
+	    Debug("Must encode but wasn't: $need_encode $wasenc");
+	    $ok = 0;
+	}
+	if(($client_types & $requesterprivs) == 0) {
+	    Debug("Client not privileged to do this operation");
+	    $ok = 0;
+	}
+
+	if($ok) {
+	    Debug("Dispatching to handler $command $tail");
+	    my $keep_going = &$handler($command, $tail, $client);
+	    return $keep_going;
+	} else {
+	    Debug("Refusing to dispatch because client did not match requirements");
+	    Failure($client, "refused\n", $userinput);
+	    return 1;
+	}
+
+    }    
+
 # ------------------------------------------------------------- Normal commands
 # ------------------------------------------------------------------------ ping
     if ($userinput =~ /^ping/) {	# client only