2008/08/28

The Labs.Com Issue_03_Penguin
Last update 1999/02/20

TPJ: Issue_03_Penguin

This is a collection of programs published by The Perl Journal. You can download all source-code also from TPJ: Programs.
  1. chick
  2. burroughs
  3. More Samples on Penguin
Issue_03_Penguin
1. chick
Download chick

 #!/usr/bin/perl 
 use Penguin; 
 use Penguin::Rights; 
 use Penguin::Frame::Code; 
 use Penguin::Frame::Data; 
 use Penguin::Wrapper::Transparent;  # we're not going to PGP-sign our \ 
 frames 
 use Penguin::Compartment; 
 use Penguin::Channel::TCP::Client; 
 $targethost = shift; 
 $targetport = shift; 
  
 $mychannel = new Penguin::Channel::TCP::Client Peer => $targethost, 
                                                Port => $targetport; 
 $mychannel->open() or die "couldn't connect there.\n"; 
 $frame = new Penguin::Frame::Code Wrapper => 'Penguin::Wrapper::Tra\ 
 nsparent'; 
  
 $frame->assemble( Password => '', # frames not signed, password \ 
 not needed 
                   Text     => "&getparagraphs()\n"; 
                   Title    => "Chick", 
                   Name     => "Joe Random User"); 
 $mychannel->putframe(Frame => $frame); 
 $returnframe = $mychannel->getframe(); 
 $results = $frame1->disassemble(Password => ''); 
 print "Paragraphs from $targethost, $targetport\n"; 
 print "Enter your paragraph following the second line.\n"; 
 print "Terminate it with a blank line.\n"; 
 print "--------------\n"; 
 print $results, "\n"; 
 print "--------------\n"; 
  
 $paragraph = ""; 
  
 while(chomp($line = <STDIN>)) { 
     last if ($line eq ''); 
     $paragraph = $paragraph . $line . "\n"; 
 } 
 $paragraph =~ s/\'/\\\'/; # make minor attempt to escape single quotes 
                           # to prevent agent from screwing up 
  
 $mychannel->close(); 
  
 $mychannel = new Penguin::Channel::TCP::Client Peer => $targethost, 
                                                Port => $portnumber; 
 $frame = new Penguin::Frame::Code Wrapper => 'Penguin::Wrapper::Tra\ 
 nsparent'; 
 $frame->assemble( Password => '', # frames not signed, password \ 
 not needed 
                   Text     => "&putparagraph('$paragraph')\n"; 
                   Title    => "Chick", 
                   Name     => "Joe Random User"); 
 $mychannel->putframe(Frame => $frame); 
 $returnframe = $mychannel->getframe(); 
 $results = $frame1->disassemble(Password => ''); 
 print "Server says: $results\n"; 

Issue_03_Penguin
2. burroughs

Download burroughs

 #!/usr/bin/perl  
 use Penguin;              
 use Penguin::Rights; 
 use Penguin::Frame::Code;            # we want to be able to receive c\ 
 ode 
 use Penguin::Frame::Data;            # we want to be able to send data 
 use Penguin::Wrapper::PGP;           # we don't mind PGP-signed frames 
 use Penguin::Wrapper::Transparent;   # but we'll accept transparent on\ 
 es too 
 use Penguin::Compartment;            # required for execution of forei\ 
 gn code 
 use Penguin::Channel::TCP::Server;   # we'll serve Penguin on a TCP po\ 
 rt 
 require 'paragraph_db_funcs.pl'; # where getparagraphs, etc. are imple\ 
 mented 
 $PGP_PASSWORD = "kwisatz haderach"; 
 $rightsdb = new Penguin::Rights; 
 $rightsdb->get(); 
 $mychannel = new Penguin::Channel::TCP::Server Bind => 8585, 
                                                Listen => 5; 
  
 while(1) { 
     $mychannel->open(); # blocks waiting for an agent to connect 
     ($remotehost, $remoteport) = $mychannel->getinfo(); 
     print "agent arrived from $remotehost, " . `date` . "\n"; 
  
     $frame = $mychannel->getframe(); # blocks waiting for a frame 
  
     if ($frame eq undef) { 
         print "agent was confused.\n"; 
         $mychannel->close(); 
         next; 
     } 
  
     ($title, $signer, $wrapmethod, $code) =  
         $frame->disassemble(Password => $PGP_PASSWORD); 
     $userrights = $rightsdb->getrights(User => $signer); 
     print "agent is $title from $signer, rights: $userrights\n"; 
     $compartment = new Penguin::Compartment; 
     $compartment->initialize(Operations => $userrights); 
  
     $compartment->register(Share => "getparagraphs"); 
     $compartment->register(Share => "putparagraph"); 
     $result = $compartment->execute(Code => $code); 
     if ($@) { # illegal code tried to execute 
  
         $result = $@; 
         print "agent overstepped: $code\n"; 
     } 
  
     $resultframe = new Penguin::Frame::Data; 
     $resultframe->assemble(Text => $result); # Data frames alway\ 
 s Transparent 
     $mychannel->putframe(Frame => $resultframe); 
     $mychannel->close(); 
 } 

Issue_03_Penguin
3. More Samples on Penguin

  • Issue_03_Penguin

                                                                                                                                   

Last update 1999/02/20

All Rights Reserved - (C) 1997 - 2008 by The Labs.Com

Top of Page

The Labs.Com