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

View of /trunk/webwork2/lib/WebworkWebservice.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6242 - (download) (as text) (annotate)
Fri May 14 00:58:02 2010 UTC (3 years ago) by gage
File size: 8063 byte(s)
minor changes to configuration section

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9