Changeset 18511 for misc/runpugs

Show
Ignore:
Timestamp:
10/20/07 23:21:08 (13 months ago)
Author:
andara
Message:

[runpugs]
Clean-up of internals.

Location:
misc/runpugs/lib
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • misc/runpugs/lib/Repl.pm

    r16617 r18511  
    1414sub run { 
    1515        my $s=shift; 
    16     my $nl=shift || ""; 
     16        my $nl=shift || ""; 
    1717        print $s->motd,"\n",$s->prompt; 
    1818        while (<STDIN>) {                
  • misc/runpugs/lib/Web/Terminal/Dispatcher.pm

    r16595 r18511  
    88 
    99# 
    10 # based on testmsg.pl from "Advanced Perl Programming" 
     10# Vaguely based on testmsg.pl from "Advanced Perl Programming" 
    1111# 
    1212use lib '.', '../..';    #to keep EPIC happy 
     
    2121                                         DEFAULT => [], 
    2222); 
    23 my $v = 1;#(1 - $Web::Terminal::Settings::daemon)*(1-$Web::Terminal::Settings::test); 
     23my $v = 
     24  1; #(1 - $Web::Terminal::Settings::daemon)*(1-$Web::Terminal::Settings::test); 
    2425 
     26# send(id,ip,app,interactive,cmds) 
     27# id is a unique identifier (string) 
     28# ip is the client IP address (string) 
     29# app is a small integer indicating the application to run 
     30# interactive is a flag (1 or 0) indicating if the Server needs to run the app as an interactive session or single-shot 
     31# cmds is the string of commands to be sent to the application 
    2532sub send { 
    2633        my $id          = shift; 
     
    3138        my $host        = $Web::Terminal::Settings::host; 
    3239        my $port        = $Web::Terminal::Settings::port; 
    33         my $cmd         = $cmds;                            #''; 
     40        my $cmd         = $cmds; 
    3441 
    3542        #WV:   We're using PUGS_SAFEMODE=1 instead 
    36         #    if ($Web::Terminal::Settings::filter and 
    37         #    $cmd=~/$Web::Terminal::Settings::filter_pattern/) { 
    38         #    my $offending_command=$1||$2; 
    39         #    return "Sorry, \'$offending_command\' is not 
    40         #    allowed.\n$Web::Terminal::Settings::prompt"; 
    41         #   } else { 
    42         my $conn; 
    43         $conn = Web::Terminal::Msg->connect( $host, $port, \&rcvd_msg_from_server ); 
    44         if ( not $conn ) { 
     43        # But for applications without safe mode, we need this 
     44        if (     $Web::Terminal::Settings::filter 
     45                 and $cmd =~ /$Web::Terminal::Settings::filter_pattern/ ) 
     46        { 
     47                my $offending_command = $1 || $2; 
     48                return "Sorry, \'$offending_command\' is not 
     49            allowed.\n$Web::Terminal::Settings::prompt"; 
     50        } else { 
     51                my $conn; 
     52                $conn = 
     53                  Web::Terminal::Msg->connect( $host, $port, \&rcvd_msg_from_server ); 
     54                if ( not $conn ) { 
    4555 
    46                 #WV: disabled, too dangerous 
    47                 #       system("/usr/bin/perl ../bin/termserv.pl"); 
    48                 #       sleep 5; 
    49                 #    } else {last;} 
    50                 return "Sorry, the pugs server is not running."; 
    51         } else { 
    52                 my $msg = YAML::Syck::Dump( 
    53                                                                         { 
    54                                                                           id  => $id, 
    55                                                                           ip  => $ip, 
    56                                                                           app => $app, 
    57                                                                           ia  => $interactive, 
    58                                                                           cmd => $cmd 
    59                                                                         } 
    60                 ); 
    61                 print STDERR "Sending message to server: $msg\n", '#' x 70, "\n" if $v; 
    62                 $conn->send_now($msg); 
    63                 print STDERR "done\n" if $v; 
    64                 ( my $rmesg, my $err ) = $conn->rcv_now(); 
    65                 print STDERR "Received reply from server: $rmesg (Error msg:$err)\n", 
    66                   '#' x 70, "\n" if $v; 
    67                 my $rmesgref = YAML::Syck::Load($rmesg); 
    68                 my $rid      = $rmesgref->{id}; 
    69                 my $reply    = $rmesgref->{msg}; 
    70                 my $histref  = $rmesgref->{recent}; 
    71                 my $prompt   = $rmesgref->{prompt}; 
    72                 $conn->disconnect(); 
     56                        #WV: disabled, too dangerous 
     57                        #       system("/usr/bin/perl ../bin/termserv.pl"); 
     58                        #       sleep 5; 
     59                        #    } else {last;} 
     60                        return "Sorry, the pugs server is not running."; 
     61                } else { # Create the YAML-encoded message to be sent to the Server 
     62                        my $msg = YAML::Syck::Dump( 
     63                                                                                { 
     64                                                                                  id  => $id, 
     65                                                                                  ip  => $ip, 
     66                                                                                  app => $app, 
     67                                                                                  ia  => $interactive, 
     68                                                                                  cmd => $cmd 
     69                                                                                } 
     70                        ); 
     71                        print STDERR "Sending message to server: $msg\n", '#' x 70, "\n" 
     72                          if $v; 
     73                        # Send it 
     74                        $conn->send_now($msg); 
     75                        print STDERR "done\n" if $v; 
     76                        # Wait for reply 
     77                        ( my $rmesg, my $err ) = $conn->rcv_now(); 
     78                        print STDERR 
     79                          "Received reply from server: $rmesg (Error msg:$err)\n", '#' x 70, 
     80                          "\n" 
     81                          if $v; 
     82                        # Decode reply   
     83                        my $rmesgref = YAML::Syck::Load($rmesg); 
     84                        my $rid      = $rmesgref->{id}; 
     85                        my $reply    = $rmesgref->{msg}; 
     86                        my $histref  = $rmesgref->{recent}; 
     87                        my $prompt   = $rmesgref->{prompt}; 
     88                        # Tear down TCP connection 
     89                        $conn->disconnect(); 
    7390 
    74                 if ( "$id" ne "$rid" ) { 
    75                         print "Terminal server returned wrong id: $rid, should be $id" if $v; 
    76                         return "Sorry, the pugs session died."; 
     91                        if ( "$id" ne "$rid" ) { 
     92                                print "Terminal server returned wrong id: $rid, should be $id" 
     93                                  if $v; 
     94                                return "Sorry, the pugs session died."; 
     95                        } 
     96                        # The next bit is purely for testing 
     97                        if (     $Web::Terminal::Settings::test == 1 
     98                                 and $cmd ne ':A' 
     99                                 and $cmd ne ':q' 
     100                                 and $cmd !~ /Web::Terminal/ ) 
     101                        { 
     102                                my $cmdreply = $reply; 
     103                                $cmdreply =~ s/^.*?called\ with\ //; 
     104                                $cmdreply =~ s/\.\s*$//; 
     105                                if ( $cmdreply ne $cmd and $cmd != 1 ) { 
     106                                        print "D: Application returned '$reply'<>'$cmd':", 
     107                                          ( $reply eq '0' ), "<>", ( $reply == 0 ), ';', $rmesg, 
     108                                          "\n"; 
     109                                } 
     110                        } elsif ( $Web::Terminal::Settings::test == 1 and $cmd eq ':A' ) { 
     111                                print "D: Simulated Abort. Application returned '$reply'\n" 
     112                                  if $v; 
     113                        } # end of testing stuff 
     114                        return ( $reply, $prompt, $histref ); 
    77115                } 
    78                 if($Web::Terminal::Settings::test==1 and $cmd ne ':A' and $cmd ne ':q' and $cmd!~/Web::Terminal/) { 
    79 my $cmdreply=$reply; 
    80 $cmdreply=~s/^.*?called\ with\ //; 
    81 $cmdreply=~s/\.\s*$//; 
    82 if($cmdreply ne $cmd and $cmd!=1 ) { 
    83         print "D: Application returned '$reply'<>'$cmd':",($reply eq '0'),"<>",($reply==0),';',$rmesg,"\n"; 
    84 } 
    85 } elsif ($Web::Terminal::Settings::test==1 and $cmd eq ':A') { 
    86         print "D: Simulated Abort. Application returned '$reply'\n" if $v; 
    87 } 
    88                 return ( $reply, $prompt, $histref ); 
    89116        } 
    90 } 
     117}    # END of send() 
    91118 
    92119sub rcvd_msg_from_server { 
  • misc/runpugs/lib/Web/Terminal/Server.pm

    r16595 r18511  
    11package Web::Terminal::Server; 
    2  
    3 #The inactive sessions queue starts with n_inactive_max sessions. 
    4 #Ideally, once it drops to n_inactive_min, it should gradually create  
    5 #n_inactive_max-n_inactive_min sessions 
    6 #To do this reallu asynchronously, we need to let the child handle this 
    7 #The problem is that the child can't access the counters of the parent. 
    8 #So I'd just have the child create n_inactive_max-n_inactive_min sessions, gradually e.g. one every 5 minutes 
    9 #But that might interfere with the cleanup: 
    10 #The child sleeps for some time, then cleans up 
    11 #So if we use the same time constant (makes sense), 
    12 #the we need a counter which is set by the signal, that's all 
    13 #So child gets a SIGUSR 
    14 #=> it sets n_new_sessions to max-min if n_new_sessions was 0 
    152 
    163use vars qw( $VERSION ); 
    174$VERSION = '0.4.0'; 
    18 # use utf8; # No UTF, sorry 
     5#use utf8; # No UTF, sorry 
    196use strict; 
    207use Carp::Assert; 
     
    3926# The messages contain the session id and the app. 
    4027# If the ID does not exist in %active_sessions, create a new session 
    41 # Otherwise, write to the terminal and send back the result, again 
    42 # with the session id as first line. 
     28# Otherwise, write to the terminal and send back the result,  
     29# again with the session id as first line. 
    4330 
    4431# verbose 
    4532my $v                 = 1;#(1 - $Web::Terminal::Settings::daemon)*(1-$Web::Terminal::Settings::test); 
    4633 
    47 # Datastructures per app, so $session{$app}{...}, $inactive[$app][...] or @{$inactive[$app]} 
    48 my @active_sessions       = (); # id => session_number for active sessions 
    49 my @sessions              = (); # session_number (from stack) =>  actual session object 
    50 my @session_numbers_stack = ();  # stack for session numbers, i.e. those not active or inactive! 
    51 my @inactive_sessions     = ();  # => stack of session numbers for inactive sessions 
     34# Datastructures per app, so $sessions[$app]{...}, $inactive[$app][...] or @{$inactive[$app]} 
     35my @active_sessions       = (); # {id => session_number} for active sessions 
     36my @sessions              = (); # {session_number =>  actual session object} 
     37my @session_numbers_stack = (); # stack for session numbers, i.e. those not active or inactive! 
     38my @inactive_sessions     = (); # stack of session numbers for inactive sessions 
    5239 
    5340# Counters. 
    5441# initialised via init_sessions() 
    55 my @n_sessions          = ();    # total number of sessions 
     42my @n_sessions          = (); # total number of sessions 
    5643my @n_active_sessions   = (); 
    5744my @n_inactive_sessions = (); 
     
    9279                if ( $childpid = fork ) { 
    9380 
    94                         #parent here 
     81                        # Parent here 
     82                        # Open log 
    9583                        my $log = 
    9684"$Web::Terminal::Settings::log_path/$Web::Terminal::Settings::appname.log"; 
     
    9886                                rename $log, $log . '.' . join( "", localtime ); 
    9987                        } 
    100                         open( LOG2, ">$log" ); 
     88                        open( LOG, ">$log" ); 
     89                         
    10190                        print "Parent: init sessions ...\n " if $v; 
    10291                        &init_sessions(); 
     92                         
    10393                        print "Parent: create new server..." if $v; 
    10494                        Web::Terminal::Msg->new_server( $host, $port, \&login_proc ); 
    10595                        print "OK\n" if $v; 
     96                         
     97                        # Wait for messages 
    10698                        Web::Terminal::Msg->event_loop(); 
    10799                } elsif ( defined $childpid ) { 
    108100 
    109                         # child here 
     101                        # Child here                     
    110102                        &init_child(); 
    111103                         
     
    121113                        # The child can restart the parent 
    122114                        if ($Web::Terminal::Settings::test==1) { 
    123                         die "No restarting, test phase\n"; 
     115                                die "No restarting, test phase\n"; 
    124116                        } elsif ($Web::Terminal::Settings::restart_parent==1) { 
    125                         print "Restarting server\n" if $v; 
    126                         chdir $Web::Terminal::Settings::lib_path; 
    127                         exec("$Web::Terminal::Settings::perl $Web::Terminal::Settings::server" 
    128                         ); 
     117                                print "Restarting server\n" if $v; 
     118                                chdir $Web::Terminal::Settings::lib_path; 
     119                                exec("$Web::Terminal::Settings::perl $Web::Terminal::Settings::server" 
     120                                ); 
     121                        } else { 
     122                                die "Server died, please restart manually.\n"; 
    129123                        } 
    130124                } elsif ( $! == EAGAIN ) { 
     
    193187                                my $term = $sessions[$app]{ $active_sessions[$app]{$id} }; 
    194188                                $tpid = $term->{pid}; 
    195                                 print LOG2 scalar(localtime), 
     189                                print LOG scalar(localtime), 
    196190                                  " : $n_sess/$n_active_sess : $ip : $id : $tpid > ", $cmd, 
    197191                                  "\n"; 
     
    434428                        } 
    435429                } else {    # max for ip reached 
    436                         print LOG2 "MAX nsessions for $ip reached\n"; 
     430                        print LOG "MAX nsessions for $ip reached\n"; 
    437431                        print "MAX nsessions for $ip reached\n" if $v; 
    438432                        return 
     
    647641                                                &kill_session( $app, $id, $ip ); 
    648642                                        #} 
    649                                         print LOG2 "Cleaned up $ip : $id : $tpid\n"; 
     643                                        print LOG "Cleaned up $ip : $id : $tpid\n"; 
    650644                                        print "Cleaned up $ip : $id : $tpid\n" if $v; 
    651645                                } 
  • misc/runpugs/lib/Web/Terminal/Server/Session.pm

    r16617 r18511  
    22 
    33use vars qw( $VERSION ); 
    4 $VERSION='0.3.0'; 
     4$VERSION='0.4.0'; 
    55use Moose; 
    6 #use strict; 
    7 #use utf8; 
     6#use utf8; # Sorry, no UTF. Somebofy fix me Net::Telnet 
    87use lib '.','../../..'; 
    98use Web::Terminal::Settings; 
    109 
    11 #A thin wrapper around Net::Telnet 
    12 #new() starts the session; 
    13 #write() sends commands to it. 
     10# A thin wrapper around Net::Telnet 
     11# new() starts the session; 
     12# write() sends commands to it. 
    1413 
    1514$SIG{CHLD}='IGNORE';