[system] / branches / gage_dev / webwork2 / lib / WebworkWebservice.pm Repository:
ViewVC logotype

View of /branches/gage_dev/webwork2/lib/WebworkWebservice.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6524 - (download) (as text) (annotate)
Sat Nov 13 04:01:46 2010 UTC (2 years, 6 months ago) by gage
File size: 8164 byte(s)
correct spelling


    1 
    2 # FIXME need way to find the basic libraries and such.
    3 
    4 
    5 BEGIN {
    6     $main::VERSION = "2.1";
    7     #use Apache;
    8     use Cwd;
    9   use WeBWorK::PG::Local;
   10 #     warn "my first webwork $webwork_directory";
   11 #
   12 #     my $webwork_directory = $ENV{WEBWORK_ROOT};
   13 #     warn "my first webwork $webwork_directory";
   14 #     $webwork_directory2 =Cwd::cwd();
   15 #     chomp $webwork_directory2;
   16 #     $webwork_directory2 =~ s|/lib/?$||;  # this will usually get the right webwork home directory
   17 #     warn "Assuming webwork directory is |$webwork_directory| and |$webwork_directory2|", $webwork_directory eq $webwork_directory2;
   18 #     #WTF???  why don't these two methods give me the same directory name?
   19 
   20 ###############################################################################
   21 # Configuration -- set to top webwork directory (webwork2) (set in webwork.apache2-config)
   22 # Configuration -- set server name
   23 ###############################################################################
   24 
   25     my $webwork_directory = $WeBWorK::Constants::WEBWORK_DIRECTORY; #'/opt/webwork/webwork2';
   26 
   27   $WebworkWebservice::HOST_NAME     = 'localhost'; # Apache->server->server_hostname;
   28   $WebworkWebservice::HOST_PORT     = '80';        # Apache->server->port;
   29 
   30 ###############################################################################
   31 
   32   eval "use lib '$webwork_directory/lib'"; die $@ if $@;
   33   eval "use WeBWorK::CourseEnvironment"; die $@ if $@;
   34   my $ce = new WeBWorK::CourseEnvironment({ webwork_dir => $webwork_directory });
   35   my $webwork_url = $ce->{webwork_url};
   36   my $pg_dir = $ce->{pg_dir};
   37   eval "use lib '$pg_dir/lib'"; die $@ if $@;
   38 
   39   $WebworkWebservice::WW_DIRECTORY = $webwork_directory;
   40   $WebworkWebservice::PG_DIRECTORY = $pg_dir;
   41   $WebworkWebservice::SeedCE       = $ce;
   42 
   43 ###############################################################################
   44 
   45   $WebworkWebservice::PASSWORD      = 'xmluser';
   46   $WebworkWebservice::COURSENAME    = 'daemon2_course'; # default course
   47 
   48 
   49   print "WebworkWebservice: webwork_directory set to ", $WeBWorK::Constants::WEBWORK_DIRECTORY, " via \$WeBWorK::Constants::WEBWORK_DIRECTORY set in webwork.apache2-config\n";
   50 
   51 }
   52 
   53 
   54 use strict;
   55 
   56 
   57 package WebworkWebservice;
   58 
   59 sub pretty_print_rh {
   60     shift if UNIVERSAL::isa($_[0] => __PACKAGE__);
   61   my $rh = shift;
   62   my $indent = shift || 0;
   63 
   64   my $out = "";
   65   return $out if $indent>10;
   66   my $type = ref($rh);
   67 
   68   if (defined($type) and $type) {
   69     $out .= " type = $type; ";
   70   } elsif ($rh == undef) {
   71     $out .= " type = scalar; ";
   72   }
   73   if ( ref($rh) =~/HASH/ or "$rh" =~/HASH/ ) {
   74       $out .= "{\n";
   75       $indent++;
   76     foreach my $key (sort keys %{$rh})  {
   77       $out .= "  "x$indent."$key => " . pretty_print_rh( $rh->{$key}, $indent ) . "\n";
   78     }
   79     $indent--;
   80     $out .= "\n"."  "x$indent."}\n";
   81 
   82   } elsif (ref($rh)  =~  /ARRAY/ or "$rh" =~/ARRAY/) {
   83       $out .= " ( ";
   84     foreach my $elem ( @{$rh} )  {
   85       $out .= pretty_print_rh($elem, $indent);
   86 
   87     }
   88     $out .=  " ) \n";
   89   } elsif ( ref($rh) =~ /SCALAR/ ) {
   90     $out .= "scalar reference ". ${$rh};
   91   } elsif ( ref($rh) =~/Base64/ ) {
   92     $out .= "base64 reference " .$$rh;
   93   } else {
   94     $out .=  $rh;
   95   }
   96 
   97   return $out." ";
   98 }
   99 
  100 
  101 use WebworkWebservice::RenderProblem;
  102 use WebworkWebservice::LibraryActions;
  103 use WebworkWebservice::MathTranslators;
  104 
  105 ###############################################################################
  106 package WebworkXMLRPC;
  107 use WebworkWebservice;
  108 use base qw(WebworkWebservice);
  109 
  110 
  111 
  112 #  respond to xmlrpc requests
  113 sub listLib {
  114     my $self = shift;
  115     my $in = shift;
  116     return( WebworkWebservice::LibraryActions::listLib($in) );
  117 }
  118 sub listLibraries {
  119     my $self = shift;
  120     my $in = shift;
  121     return( WebworkWebservice::LibraryActions::listLibraries($in) );
  122 }
  123 sub renderProblem {
  124     my $self = shift;
  125     my $in = shift;
  126     return( WebworkWebservice::RenderProblem::renderProblem($in) );
  127 }
  128 sub readFile {
  129     my $self = shift;
  130     my $in   = shift;
  131     return( WebworkWebservice::LibraryActions::readFile($in) );
  132 }
  133 sub tex2pdf {
  134     my $self = shift;
  135     my $in   = shift;
  136     return( WebworkWebservice::MathTranslators::tex2pdf($in) );
  137 }
  138 
  139 # -- SOAP::Lite -- guide.soaplite.com -- Copyright (C) 2001 Paul Kulchenko --
  140 # test responses
  141 
  142 sub hi {   shift if UNIVERSAL::isa($_[0] => __PACKAGE__); # grabs class reference
  143   return "hello, world";
  144 }
  145 sub hello2 { shift if UNIVERSAL::isa($_[0] => __PACKAGE__);
  146   #print "Receiving request for hello world\n";
  147   return "Hello world2";
  148 }
  149 sub bye {shift if UNIVERSAL::isa($_[0] => __PACKAGE__);
  150   return "goodbye, sad cruel world";
  151 }
  152 
  153 sub languages {shift if UNIVERSAL::isa($_[0] => __PACKAGE__);
  154   return ["Perl", "C", "sh"];
  155 }
  156 
  157 sub echo_self {
  158   my $self = shift;
  159 }
  160 
  161 sub echo {
  162     return join("|",("begin ", WebworkWebservice::pretty_print_rh(\@_), " end") );
  163 }
  164 
  165 sub pretty_print_rh {
  166   WebworkWebservice::pretty_print_rh(@_);
  167 }
  168 
  169 
  170 sub tth {shift if UNIVERSAL::isa($_[0] => __PACKAGE__);
  171   my $in = shift;
  172   my $tthpath = "/usr/local/bin/tth";
  173     # $tthpath -L -f5 -r 2>/dev/null " . $inputString;
  174     return $in;
  175 
  176 }
  177 
  178 
  179 
  180 
  181 package WWd;
  182 
  183 #use lib '/home/gage/webwork/xmlrpc/daemon';
  184 #use WebworkXMLRPC;
  185 
  186 
  187 
  188 
  189 ############utilities
  190 
  191 sub echo {
  192     return "WWd package ".join("|",("begin ", WebworkWebservice::pretty_print_rh(\@_), " end") );
  193 }
  194 
  195 sub listLib {
  196     shift if UNIVERSAL::isa($_[0] => __PACKAGE__);
  197     my $in = shift;
  198     return( Webwork::listLib($in) );
  199 }
  200 sub renderProblem {
  201     shift if UNIVERSAL::isa($_[0] => __PACKAGE__);
  202     my $in = shift;
  203     return( Filter::filterObject( Webwork::renderProblem($in) ) );
  204 }
  205 sub readFile {
  206     shift if UNIVERSAL::isa($_[0] => __PACKAGE__);
  207     my $in = shift;
  208     return( Webwork::readFile($in) );
  209 }
  210 # sub hello {
  211 #   shift if UNIVERSAL::isa($_[0] => __PACKAGE__);
  212 #   print "Receiving request for hello world\n";
  213 #   return "Hello world?";
  214 # }
  215 
  216 
  217 # sub tth {
  218 #   shift if UNIVERSAL::isa($_[0] => __PACKAGE__);
  219 #   my $in = shift;
  220 #   my $tthpath = "/usr/local/bin/tth";
  221 #     my $out;
  222 #     $inputString    = "<<END_OF_TTH_INPUT_STRING;\n\n\n" . $in . "\nEND_OF_TTH_INPUT_STRING\necho \"\" >/dev/null";
  223 #     #it's not clear why another command is needed.
  224 #
  225 #     if (-x $tthpath ) {
  226 #       my $tthcmd      = "$tthpath -L -f5 -r 2>/dev/null " . $inputString;
  227 #       if (open(TTH, "$tthcmd   |")) {
  228 #           local($/);
  229 #       $/ = undef;
  230 #       $out = <TTH>;
  231 #       $/ = "\n";
  232 #       close(TTH);
  233 #       }else {
  234 #           $out = "<BR>there has been an error in executing $tthcmd<BR>";
  235 #       }
  236 #   } else {
  237 #     $out = "<BR> Can't execute the program tth at |$tthpath|<BR>";
  238 #     }
  239 #
  240 #     #return "<!-- \r\n" . $in . "\r\n-->\r\n\r\n" . $out . "\r\n\r\n";
  241 #     return $out;
  242 #
  243 # }
  244 
  245 package Filter;
  246 
  247 
  248 sub is_hash_ref {
  249   my $in =shift;
  250   my $save_SIG_die_trap = $SIG{__DIE__};
  251     $SIG{__DIE__} = sub {CORE::die(@_) };
  252   my $out = eval{  %{   $in  }  };
  253   $out = ($@ eq '') ? 1 : 0;
  254   $@='';
  255   $SIG{__DIE__} = $save_SIG_die_trap;
  256   $out;
  257 }
  258 sub is_array_ref {
  259   my $in =shift;
  260   my $save_SIG_die_trap = $SIG{__DIE__};
  261     $SIG{__DIE__} = sub {CORE::die(@_) };
  262   my $out = eval{  @{   $in  }  };
  263   $out = ($@ eq '') ? 1 : 0;
  264   $@='';
  265   $SIG{__DIE__} = $save_SIG_die_trap;
  266   $out;
  267 }
  268 sub filterObject {
  269 
  270     my $is_hash = 0;
  271     my $is_array =0;
  272   my $obj = shift;
  273   #print "Enter filterObject ", ref($obj), "\n";
  274   my $type = ref($obj);
  275   unless ($type) {
  276     #print "leave filterObject with nothing\n";
  277     return($obj);
  278   }
  279 
  280 
  281   if ( is_hash_ref($obj)  ) {
  282       #print "enter hash ", %{$obj},"\n";
  283       my %obj_container= %{$obj};
  284     foreach my $key (keys %obj_container) {
  285       $obj_container{$key} = filterObject( $obj_container{$key} );
  286       #print $key, "  ",  ref($obj_container{$key}),"   ", $obj_container{$key}, "\n";
  287     }
  288     #print "leave filterObject with HASH\n";
  289     return( bless(\%obj_container,'HASH'));
  290   };
  291 
  292 
  293 
  294   if ( is_array_ref($obj)  ) {
  295     #print "enter array ( ", @{$obj}," )\n";
  296     my @obj_container= @{$obj};
  297     foreach my $i (0..$#obj_container) {
  298       $obj_container[$i] = filterObject( $obj_container[$i] );
  299       #print "\[$i\]  ",  ref($obj_container[$i]),"   ", $obj_container[$i], "\n";
  300     }
  301     #print "leave filterObject with ARRAY\n";
  302     return( bless(\@obj_container,'ARRAY'));
  303   };
  304 
  305 }
  306 
  307 
  308 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9