| 1 | # Copyright (c) 2003, Cornell University | ||
| 2 | # See the file COPYING for the status of this software | ||
| 3 | |||
| 4 | package SOAP::Clean::Server; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use SOAP::Clean::Misc; | ||
| 10 | |||
| 11 | BEGIN { | ||
| 12 | use Exporter (); | ||
| 13 | our (@ISA, @EXPORT); | ||
| 14 | |||
| 15 | @ISA = qw(Exporter); | ||
| 16 | @EXPORT = qw( | ||
| 17 | &file | ||
| 18 | &val | ||
| 19 | &in | ||
| 20 | &in_out | ||
| 21 | &out | ||
| 22 | &optional | ||
| 23 | &default | ||
| 24 | ); | ||
| 25 | } | ||
| 26 | |||
| 27 | ######################################################################## | ||
| 28 | # Parameter constructors | ||
| 29 | ######################################################################## | ||
| 30 | |||
| 31 | sub param_constructor { | ||
| 32 | my $field_name = shift; | ||
| 33 | my $field_val = shift; | ||
| 34 | my $x = shift; | ||
| 35 | if ( !(ref $x) ) { | ||
| 36 | my $name = $x; | ||
| 37 | my $type = shift; | ||
| 38 | $x = { name => $name, type => $type, optional => 0 }; | ||
| 39 | } | ||
| 40 | assert(ref $x eq "HASH"); | ||
| 41 | $$x{$field_name} = $field_val; | ||
| 42 | return $x; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub file { return param_constructor('mech' => 'file',@_); } | ||
| 46 | sub val { return param_constructor('mech' => 'val',@_); } | ||
| 47 | sub in { return param_constructor('direc' => 'in',@_); } | ||
| 48 | sub in_out { return param_constructor('direc' => 'in_out',@_); } | ||
| 49 | sub out { return param_constructor('direc' => 'out',@_); } | ||
| 50 | |||
| 51 | sub optional { return param_constructor('optional' => 1, @_); } | ||
| 52 | |||
| 53 | sub default { | ||
| 54 | my $default = pop @_; | ||
| 55 | return optional(param_constructor('default' => $default, @_)); | ||
| 56 | } | ||
| 57 | |||
| 58 | ######################################################################## | ||
| 59 | # Base Server | ||
| 60 | ######################################################################## | ||
| 61 | |||
| 62 | package SOAP::Clean::Server::Base; | ||
| 63 | |||
| 64 | use warnings; | ||
| 65 | use strict; | ||
| 66 | |||
| 67 | use File::Temp qw/ :POSIX /; | ||
| 68 | use MIME::Base64; | ||
| 69 | use Getopt::Long; | ||
| 70 | |||
| 71 | use SOAP::Clean::Misc; | ||
| 72 | use SOAP::Clean::XML; | ||
| 73 | use SOAP::Clean::Internal; | ||
| 74 | use SOAP::Clean::Processes; | ||
| 75 | use SOAP::Clean::Server; | ||
| 76 | |||
| 77 | # Inheritance | ||
| 78 | our @ISA = qw(SOAP::Clean::Internal::Actor); | ||
| 79 | |||
| 80 | # Constructor | ||
| 81 | |||
| 82 | sub initialize { | ||
| 83 | my ($self) = @_; | ||
| 84 | $self->SUPER::initialize(); | ||
| 85 | $self->{is_server} = 1; | ||
| 86 | $self->{verbose} = $ENV{VERBOSE_SERVER} || 0; | ||
| 87 | $self->{processes} = new SOAP::Clean::Processes::Basic; | ||
| 88 | $self->{urn} = "urn:soap-clean"; | ||
| 89 | $self->{name} = "unnamed_server"; | ||
| 90 | $self->{full_name} = "An unnamed SOAP::Clean server"; | ||
| 91 | } | ||
| 92 | |||
| 93 | ######################################################################## | ||
| 94 | # Methods for modifying object behavior | ||
| 95 | # ie, new SOAP::Clean::CGI()->descr_file(...)->... | ||
| 96 | ######################################################################## | ||
| 97 | |||
| 98 | # Methods for defining and describing the server | ||
| 99 | |||
| 100 | sub descr_file { | ||
| 101 | my ($self,$descr_file) = @_; | ||
| 102 | $self->{descr} = $self->_read_descr($descr_file); | ||
| 103 | return $self; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub descr { | ||
| 107 | my ($self,$descr) = @_; | ||
| 108 | $self->{descr} = $descr; | ||
| 109 | return $self; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub params { | ||
| 113 | my ($self,@params) = @_; | ||
| 114 | $self->{params} = \@params; | ||
| 115 | return $self; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub command { | ||
| 119 | my ($self,$command) = @_; | ||
| 120 | $self->{command} = $command; | ||
| 121 | return $self; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub urn { | ||
| 125 | my ($self,$urn) = @_; | ||
| 126 | $self->{urn} = $urn; | ||
| 127 | return $self; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub name { | ||
| 131 | my ($self,$name) = @_; | ||
| 132 | $self->{name} = $name; | ||
| 133 | return $self; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub full_name { | ||
| 137 | my ($self,$full_name) = @_; | ||
| 138 | $self->{full_name} = $full_name; | ||
| 139 | return $self; | ||
| 140 | } | ||
| 141 | |||
| 142 | sub in_order { | ||
| 143 | my ($self,@in_args) = @_; | ||
| 144 | $self->{in_order} = \@in_args; | ||
| 145 | return $self; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub out_order { | ||
| 149 | my ($self,@out_args) = @_; | ||
| 150 | $self->{out_order} = \@out_args; | ||
| 151 | return $self; | ||
| 152 | } | ||
| 153 | |||
| 154 | ######################################################################## | ||
| 155 | |||
| 156 | sub _dispatch_method { | ||
| 157 | my ($self,$method_name, %request_args) = @_; | ||
| 158 | |||
| 159 | return $self->_Call(%request_args) if ($method_name eq "Call"); | ||
| 160 | return $self->_Results(%request_args) if ($method_name eq "Results"); | ||
| 161 | return $self->_Running(%request_args) if ($method_name eq "Running"); | ||
| 162 | return $self->_Spawn(%request_args) if ($method_name eq "Spawn"); | ||
| 163 | |||
| 164 | die("No such method"); | ||
| 165 | } | ||
| 166 | |||
| 167 | ######################################################################## | ||
| 168 | |||
| 169 | sub _Call { | ||
| 170 | my ($self,%request_args) = @_; | ||
| 171 | |||
| 172 | my $job_info = $self->_do_Spawn(%request_args); | ||
| 173 | while ($self->_do_Running($job_info)) { | ||
| 174 | sleep 1; | ||
| 175 | } | ||
| 176 | return $self->_do_Results($job_info); | ||
| 177 | } | ||
| 178 | |||
| 179 | ######################################################################## | ||
| 180 | |||
| 181 | sub _Spawn { | ||
| 182 | my ($self,%request_args) = @_; | ||
| 183 | |||
| 184 | my $job_info = $self->_do_Spawn(%request_args); | ||
| 185 | my $job_info_str = job_info_to_string($job_info); | ||
| 186 | |||
| 187 | my %response_args = (); | ||
| 188 | $response_args{uid} = arg_encode_string('server:uid',$job_info_str); | ||
| 189 | return %response_args; | ||
| 190 | } | ||
| 191 | |||
| 192 | sub _do_Spawn { | ||
| 193 | my ($self,%request_args) = @_; | ||
| 194 | |||
| 195 | $self->_ensure_interface(); | ||
| 196 | |||
| 197 | my @params = @{$self->{params}}; | ||
| 198 | |||
| 199 | my $out_log = tmpnam(); | ||
| 200 | |||
| 201 | # Process parameters before calling the command | ||
| 202 | my %replacements = (); | ||
| 203 | foreach my $p ( @params ) { | ||
| 204 | my $name = $$p{name}; | ||
| 205 | my $direc = $$p{direc}; | ||
| 206 | my $mech = $$p{mech}; | ||
| 207 | my $type = $$p{type}; | ||
| 208 | my $optional = $$p{optional}; | ||
| 209 | my $default = $$p{default}; | ||
| 210 | |||
| 211 | my $replacement; | ||
| 212 | if ( $direc eq "out" ) { | ||
| 213 | # out parameters | ||
| 214 | ( $mech eq "file" ) | ||
| 215 | || die("Error! file \"".__FILE__."\", line ".__LINE__); | ||
| 216 | $replacement = tmpnam(); | ||
| 217 | } else { | ||
| 218 | # in and in_out parameters | ||
| 219 | # first, make sure that we have a value. | ||
| 220 | my $value; | ||
| 221 | if (defined($request_args{$name})) { | ||
| 222 | $value = $request_args{$name}; | ||
| 223 | } elsif (!$optional) { | ||
| 224 | die("No value for argument $name"); | ||
| 225 | } elsif (defined($default)) { | ||
| 226 | $value = $default; | ||
| 227 | } | ||
| 228 | # second, write the value to a file, if need be. Otherwise, | ||
| 229 | # convert it to the replacement string | ||
| 230 | if ( !defined($value) ) { | ||
| 231 | # No value given, so no replacement. | ||
| 232 | } elsif ( $mech eq "file" ) { | ||
| 233 | $replacement = tmpnam();; | ||
| 234 | $self->_write_to_file($value,$type,$replacement); | ||
| 235 | } else { | ||
| 236 | $replacement = | ||
| 237 | arg_strip_white | ||
| 238 | (arg_decode_to_string($value)); | ||
| 239 | } | ||
| 240 | } | ||
| 241 | |||
| 242 | if ( defined($replacement) ) { | ||
| 243 | $replacements{$name} = $replacement; | ||
| 244 | } | ||
| 245 | |||
| 246 | |||
| 247 | } | ||
| 248 | |||
| 249 | my $command_obj = $self->{command}; | ||
| 250 | my $cmd_line; | ||
| 251 | if (ref $command_obj eq "CODE") { | ||
| 252 | $self->_print(1,"Calling code ref to generate commmand line\n"); | ||
| 253 | $cmd_line = &$command_obj(%replacements); | ||
| 254 | } else { | ||
| 255 | $cmd_line = $command_obj; | ||
| 256 | $self->_print(1,"Raw cmd line: $cmd_line\n"); | ||
| 257 | $cmd_line =~ s/\$\{(\w+)\}/$replacements{$1}/g; | ||
| 258 | } | ||
| 259 | |||
| 260 | $self->_print(1,"Spawning: $cmd_line\n"); | ||
| 261 | my $processes_obj = assert($self->{processes}); | ||
| 262 | my $process_info_str = $processes_obj->process_spawn($cmd_line,$out_log); | ||
| 263 | $self->_print(1,"Spawned\n"); | ||
| 264 | |||
| 265 | my $job_info = [ $process_info_str, $out_log, %replacements ]; | ||
| 266 | return $job_info; | ||
| 267 | } | ||
| 268 | |||
| 269 | ######################################################################## | ||
| 270 | |||
| 271 | sub _Running { | ||
| 272 | my ($self,%request_args) = @_; | ||
| 273 | |||
| 274 | defined($request_args{uid}) || die("Missing uid argument"); | ||
| 275 | my $job_info_str = arg_decode_to_string($request_args{uid}); | ||
| 276 | defined($job_info_str) || die("Can't xlate job_info_str"); | ||
| 277 | |||
| 278 | my $job_info = job_info_from_string($job_info_str); | ||
| 279 | $job_info || die("Invalid job_info string"); | ||
| 280 | |||
| 281 | my $running = $self->_do_Running($job_info); | ||
| 282 | |||
| 283 | my %response_args = (); | ||
| 284 | $response_args{is_running} = arg_encode_bool('server:is_running',$running); | ||
| 285 | return %response_args; | ||
| 286 | } | ||
| 287 | |||
| 288 | sub _do_Running { | ||
| 289 | my ($self,$job_info) = @_; | ||
| 290 | |||
| 291 | my ( $process_info_str, $out_log, %replacements ) = @$job_info; | ||
| 292 | my $processes_obj = assert($self->{processes}); | ||
| 293 | my $running = $processes_obj->process_running($process_info_str); | ||
| 294 | $self->_print(1,$running ? "Still running\n": "Done!\n"); | ||
| 295 | return $running; | ||
| 296 | |||
| 297 | } | ||
| 298 | |||
| 299 | ######################################################################## | ||
| 300 | |||
| 301 | sub _Results { | ||
| 302 | my ($self,%request_args) = @_; | ||
| 303 | |||
| 304 | defined($request_args{uid}) || die("Missing uid argument"); | ||
| 305 | my $job_info_str = arg_decode_to_string($request_args{uid}); | ||
| 306 | defined($job_info_str) || die("Can't xlate job_info_str"); | ||
| 307 | |||
| 308 | my $job_info = job_info_from_string($job_info_str); | ||
| 309 | $job_info || die("Invalid job_info string"); | ||
| 310 | return $self->_do_Results($job_info); | ||
| 311 | } | ||
| 312 | |||
| 313 | sub _do_Results { | ||
| 314 | my ($self,$job_info) = @_; | ||
| 315 | my ( $process_info_str, $out_log, %replacements ) = @$job_info; | ||
| 316 | |||
| 317 | $self->_ensure_interface(); | ||
| 318 | |||
| 319 | my $processes_obj = assert($self->{processes}); | ||
| 320 | my $status = $processes_obj->process_result($process_info_str); | ||
| 321 | $self->_print(1,"Status = $status\n"); | ||
| 322 | my $out_str = ""; | ||
| 323 | open LOG, "<$out_log" || | ||
| 324 | die("The command failed. Couldn't read log file"); | ||
| 325 | while (my $l = <LOG>) { | ||
| 326 | $out_str .= $l; | ||
| 327 | } | ||
| 328 | close LOG; | ||
| 329 | unlink($out_log); | ||
| 330 | if ( $status != 0 ) { | ||
| 331 | die("The command failed. Log follows.\n".$out_str); | ||
| 332 | } | ||
| 333 | $self->_print(1,"Log follows:\n".$out_str); | ||
| 334 | |||
| 335 | # Process parameters after calling the command | ||
| 336 | my @params = @{$self->{params}}; | ||
| 337 | my @tmp_files = (); | ||
| 338 | my %response_args = (); | ||
| 339 | foreach my $p ( @params ) { | ||
| 340 | my $name = $$p{name}; | ||
| 341 | my $direc = $$p{direc}; | ||
| 342 | my $mech = $$p{mech}; | ||
| 343 | my $type = $$p{type}; | ||
| 344 | my $replacement = $replacements{$name}; | ||
| 345 | |||
| 346 | if ( $mech eq "file" ) { | ||
| 347 | push @tmp_files, $replacement; | ||
| 348 | } | ||
| 349 | |||
| 350 | if ( $direc eq "in" ) { | ||
| 351 | # in parameters | ||
| 352 | ; # nothing has to be done. | ||
| 353 | } else { | ||
| 354 | # out and in_out parameters | ||
| 355 | # read the value from a file | ||
| 356 | ( $mech eq "file" ) | ||
| 357 | || die("Error! file \"".__FILE__."\", line ".__LINE__); | ||
| 358 | |||
| 359 | $response_args{$name} = | ||
| 360 | $self->_read_from_file($name,$type,$replacement); | ||
| 361 | } | ||
| 362 | } | ||
| 363 | |||
| 364 | # Delete the temporary files | ||
| 365 | foreach my $f ( @tmp_files ) { unlink $f; } | ||
| 366 | |||
| 367 | return %response_args; | ||
| 368 | } | ||
| 369 | |||
| 370 | ######################################################################## | ||
| 371 | |||
| 372 | sub job_info_to_string { | ||
| 373 | my ($job_info) = @_; | ||
| 374 | my ( $process_info_str, $out_log, %replacements ) = @$job_info; | ||
| 375 | |||
| 376 | my $str = sprintf("{%s}{%s}", | ||
| 377 | $process_info_str,$out_log); | ||
| 378 | foreach my $name ( keys %replacements ) { | ||
| 379 | $str .= sprintf("{%s}[%s]",$name,$replacements{$name}); | ||
| 380 | } | ||
| 381 | my $job_info_str = encode_base64($str); | ||
| 382 | return $job_info_str; | ||
| 383 | } | ||
| 384 | |||
| 385 | sub job_info_from_string { | ||
| 386 | my ($job_info_str) = @_; | ||
| 387 | |||
| 388 | my $str = decode_base64($job_info_str); | ||
| 389 | |||
| 390 | ($str =~ /^\{([^\}]*)\}\{([^\}]*)\}(.*)/) || return 0; | ||
| 391 | my $process_info_str = $1; | ||
| 392 | my $out_log = $2; | ||
| 393 | $str = $3; | ||
| 394 | |||
| 395 | my %replacements = (); | ||
| 396 | while ( $str =~ /^\{([^\}]*)\}\[([^\[]*)\](.*)/ ) { | ||
| 397 | $replacements{$1} = $2; | ||
| 398 | $str = $3; | ||
| 399 | } | ||
| 400 | |||
| 401 | ($str eq "") || return 0; | ||
| 402 | |||
| 403 | my $job_info = [ $process_info_str, $out_log, %replacements ]; | ||
| 404 | return $job_info; | ||
| 405 | } | ||
| 406 | |||
| 407 | ######################################################################## | ||
| 408 | |||
| 409 | # Ensure that $self->{params} is properly set for server objects. | ||
| 410 | sub _ensure_interface { | ||
| 411 | my ($self) = @_; | ||
| 412 | |||
| 413 | if ( defined($self->{interface_ensured}) ) { return; } | ||
| 414 | |||
| 415 | if ( defined($self->{params}) && defined($self->{command}) ) { | ||
| 416 | ; | ||
| 417 | } else { | ||
| 418 | |||
| 419 | assert(!defined($self->{params}), | ||
| 420 | "params() was used, but command() was not"); | ||
| 421 | assert(!defined($self->{command}), | ||
| 422 | "command() was used, but params() was not"); | ||
| 423 | |||
| 424 | defined($self->{descr}) | ||
| 425 | || die("Server descriptor now specified."); | ||
| 426 | |||
| 427 | ($self->{params},$self->{command}) = | ||
| 428 | $self->_parse_descr($self->{descr}); | ||
| 429 | } | ||
| 430 | |||
| 431 | # Sanity check on the parameters. | ||
| 432 | foreach my $p ( @{$self->{params}} ) { | ||
| 433 | my $name = assert($$p{name}); | ||
| 434 | my $direc = assert($$p{direc}); | ||
| 435 | assert($direc eq 'in' || $direc eq 'out' || $direc eq 'in_out'); | ||
| 436 | my $mech = assert($$p{mech}); | ||
| 437 | assert($mech eq 'file' || $mech eq 'val'); | ||
| 438 | my $type = assert($$p{type}); | ||
| 439 | assert($type eq 'bool' | ||
| 440 | || $type eq 'int' | ||
| 441 | || $type eq 'float' | ||
| 442 | || $type eq 'string' | ||
| 443 | || $type eq 'raw' | ||
| 444 | || $type eq 'xml'); | ||
| 445 | if (!defined($$p{optional})) { | ||
| 446 | $$p{optional} = 0; | ||
| 447 | } | ||
| 448 | my $optional = $$p{optional}; | ||
| 449 | assert(!$optional || ($direc eq 'in' || $direc eq 'in_out')); | ||
| 450 | my $default = defined($$p{default}); | ||
| 451 | assert(!$default || $optional); | ||
| 452 | } | ||
| 453 | |||
| 454 | # set in_order, if not already set | ||
| 455 | if ( !defined($self->{in_order}) ) { | ||
| 456 | my @in_order = (); | ||
| 457 | foreach my $param ( @{$self->{params}} ) { | ||
| 458 | if ( $$param{direc} ne "out" ) { | ||
| 459 | push @in_order, $$param{name}; | ||
| 460 | } | ||
| 461 | } | ||
| 462 | $self->{in_order} = \@in_order; | ||
| 463 | } | ||
| 464 | |||
| 465 | # set out_order, if not already set | ||
| 466 | if ( !defined($self->{out_order}) ) { | ||
| 467 | my @out_order = (); | ||
| 468 | foreach my $param ( @{$self->{params}} ) { | ||
| 469 | if ( $$param{direc} ne "in" ) { | ||
| 470 | push @out_order, $$param{name}; | ||
| 471 | } | ||
| 472 | } | ||
| 473 | $self->{out_order} = \@out_order; | ||
| 474 | } | ||
| 475 | |||
| 476 | $self->{interface_ensured} = 1; | ||
| 477 | |||
| 478 | } | ||
| 479 | |||
| 480 | |||
| 481 | ######################################################################## | ||
| 482 | |||
| 483 | sub _write_to_file { | ||
| 484 | my ($self,$value,$type,$file) = @_; | ||
| 485 | |||
| 486 | if ( $type eq "xml" ) { | ||
| 487 | open F,">$file" || | ||
| 488 | die("Error! file \"".__FILE__."\", line ".__LINE__); | ||
| 489 | if ( !ref($value) ) { | ||
| 490 | print F $value, "\n"; | ||
| 491 | } else { | ||
| 492 | my $t = arg_decode_to_xml($value); | ||
| 493 | xml_to_fh($t,\*F); | ||
| 494 | } | ||
| 495 | close F || | ||
| 496 | die("Error! file \"".__FILE__."\", line ".__LINE__); | ||
| 497 | } elsif ( $type eq "raw" ) { | ||
| 498 | open F,">$file" || | ||
| 499 | die("Error! file \"".__FILE__."\", line ".__LINE__); | ||
| 500 | binmode F; | ||
| 501 | syswrite F, arg_decode_to_string($value); | ||
| 502 | close F || | ||
| 503 | die("Error! Line ".__LINE__); | ||
| 504 | |||
| 505 | } else { | ||
| 506 | $self->_print(1,"type=$type\n"); | ||
| 507 | die("Error! file \"".__FILE__."\", line ".__LINE__); | ||
| 508 | } | ||
| 509 | } | ||
| 510 | |||
| 511 | ######################################################################## | ||
| 512 | |||
| 513 | sub _read_from_file { | ||
| 514 | my ($self,$name,$type,$file) = @_; | ||
| 515 | my $result; | ||
| 516 | |||
| 517 | open F, "<".$file | ||
| 518 | || die("Error! file \"".__FILE__."\", line ".__LINE__); | ||
| 519 | |||
| 520 | if ($type eq "xml") { | ||
| 521 | my $out_doc = xml_from_fh(\*F); | ||
| 522 | my ($out_node) = xml_get_children($out_doc); | ||
| 523 | $result = arg_encode_xml('server:'.$name,$out_node); | ||
| 524 | } elsif ($type eq "raw") { | ||
| 525 | binmode F; | ||
| 526 | my $out_len = sysseek F,0,2; # how long is the file. | ||
| 527 | sysseek F,0,0; # jump back to the beginning. | ||
| 528 | my $out_str; | ||
| 529 | sysread F,$out_str,$out_len; | ||
| 530 | $result = arg_encode_raw('server:'.$name,$out_str); | ||
| 531 | } else { | ||
| 532 | # A basic type. | ||
| 533 | my $str = ""; | ||
| 534 | while (<F>) { $str .= $_; } | ||
| 535 | if ($type eq "int") { | ||
| 536 | $result = arg_encode_int('server:'.$name,$str); | ||
| 537 | } elsif ($type eq "string") { | ||
| 538 | $result = arg_encode_string('server:'.$name,$str); | ||
| 539 | } else { | ||
| 540 | $self->_print(1,"name=$name\n"); | ||
| 541 | $self->_print(1,"type=$type\n"); | ||
| 542 | $self->_print(1,"file=$file\n"); | ||
| 543 | die("Error! file \"".__FILE__."\", line ".__LINE__); | ||
| 544 | } | ||
| 545 | } | ||
| 546 | close F; | ||
| 547 | return $result; | ||
| 548 | } | ||
| 549 | |||
| 550 | ######################################################################## | ||
| 551 | # Generate WSDL | ||
| 552 | ######################################################################## | ||
| 553 | |||
| 554 | # Generate the basic WSDL result, modulo the 'service' element. | ||
| 555 | |||
| 556 | sub _wsdl_generate { | ||
| 557 | my ($self,$service_func) = @_; | ||
| 558 | |||
| 559 | $self->_ensure_interface(); | ||
| 560 | my $params_refs_ref = $self->{params}; | ||
| 561 | my @params_refs = @$params_refs_ref; | ||
| 562 | |||
| 563 | my $server_urn = $self->{urn}; | ||
| 564 | defined($server_urn) || die("No server URN"); | ||
| 565 | my $server_name = $self->{name}; | ||
| 566 | defined($server_name) || die("No server name"); | ||
| 567 | |||
| 568 | my $in_order = $self->{in_order}; assert(defined($in_order)); | ||
| 569 | my $out_order = $self->{out_order}; assert(defined($out_order)); | ||
| 570 | |||
| 571 | #################################################################### | ||
| 572 | # types | ||
| 573 | #################################################################### | ||
| 574 | |||
| 575 | my %params_by_name = (); | ||
| 576 | foreach my $param ( @params_refs ) { | ||
| 577 | $params_by_name{$$param{name}} = $param; | ||
| 578 | } | ||
| 579 | |||
| 580 | # in parameters type | ||
| 581 | my @in_elements = (); | ||
| 582 | foreach my $param_name ( @$in_order ) { | ||
| 583 | my $param = $params_by_name{$param_name}; | ||
| 584 | assert(defined($param),"No such parameter $param"); | ||
| 585 | assert( $$param{direc} ne "out", | ||
| 586 | "$param_name, which appears in in_order(), is an output parameter"); | ||
| 587 | push @in_elements, | ||
| 588 | $self->_wsdl_parameter_type($$param{name},$$param{type},$$param{optional}); | ||
| 589 | } | ||
| 590 | my $in_type = element("xsd:complexType", | ||
| 591 | element("xsd:sequence", | ||
| 592 | @in_elements)); | ||
| 593 | |||
| 594 | # out parameters type | ||
| 595 | my @out_elements = (); | ||
| 596 | foreach my $param_name ( @$out_order ) { | ||
| 597 | my $param = $params_by_name{$param_name}; | ||
| 598 | assert(defined($param),"No such parameter $param"); | ||
| 599 | assert( $$param{direc} ne "in", | ||
| 600 | "$param_name, which appears in out_order(), is an input parameter"); | ||
| 601 | push @out_elements, | ||
| 602 | $self->_wsdl_parameter_type($$param{name},$$param{type},$$param{optional}); | ||
| 603 | } | ||
| 604 | my $out_type = element("xsd:complexType", | ||
| 605 | element("xsd:sequence", | ||
| 606 | @out_elements)); | ||
| 607 | |||
| 608 | # UID paramater type | ||
| 609 | my $uid_type = | ||
| 610 | element("xsd:complexType", | ||
| 611 | element("xsd:sequence", | ||
| 612 | $self->_wsdl_parameter_type("uid","string",0))); | ||
| 613 | |||
| 614 | # is_running:bool parameter type | ||
| 615 | my $bool_type = | ||
| 616 | element("xsd:complexType", | ||
| 617 | element("xsd:sequence", | ||
| 618 | $self->_wsdl_parameter_type("is_running","bool",0))); | ||
| 619 | |||
| 620 | # The types for the method arguments and results | ||
| 621 | my $call_request_type_name = "Call"; | ||
| 622 | my $call_response_type_name = "CallResult"; | ||
| 623 | my $spawn_request_type_name = "Spawn"; | ||
| 624 | my $spawn_response_type_name = "SpawnResult"; | ||
| 625 | my $running_request_type_name = "Running"; | ||
| 626 | my $running_response_type_name = "RunningResult"; | ||
| 627 | my $results_request_type_name = "Results"; | ||
| 628 | my $results_response_type_name = "ResultsResult"; | ||
| 629 | |||
| 630 | my $types = | ||
| 631 | element | ||
| 632 | ("types", | ||
| 633 | element | ||
| 634 | ("xsd:schema", | ||
| 635 | attr("elementFormDefault","qualified"), | ||
| 636 | attr("targetNamespace",$server_urn), | ||
| 637 | $self->_wsdl_generate_types($call_request_type_name, | ||
| 638 | $call_response_type_name, | ||
| 639 | $in_type,$out_type), | ||
| 640 | $self->_wsdl_generate_types($spawn_request_type_name, | ||
| 641 | $spawn_response_type_name, | ||
| 642 | $in_type,$uid_type), | ||
| 643 | $self->_wsdl_generate_types($running_request_type_name, | ||
| 644 | $running_response_type_name, | ||
| 645 | $uid_type,$bool_type), | ||
| 646 | $self->_wsdl_generate_types($results_request_type_name, | ||
| 647 | $results_response_type_name, | ||
| 648 | $uid_type,$out_type), | ||
| 649 | )); | ||
| 650 | |||
| 651 | #################################################################### | ||
| 652 | # messages | ||
| 653 | #################################################################### | ||
| 654 | |||
| 655 | my $call_request_msg_name = "CallRequest"; | ||
| 656 | my $call_response_msg_name = "CallResponse"; | ||
| 657 | my $spawn_request_msg_name = "SpawnRequest"; | ||
| 658 | my $spawn_response_msg_name = "SpawnResponse"; | ||
| 659 | my $running_request_msg_name = "RunningRequest"; | ||
| 660 | my $running_response_msg_name = "RunningResponse"; | ||
| 661 | my $results_request_msg_name = "ResultsRequest"; | ||
| 662 | my $results_response_msg_name = "ResultsResponse"; | ||
| 663 | my $fault_msg_name = "Fault"; | ||
| 664 | |||
| 665 | my @messages = | ||
| 666 | ( | ||
| 667 | |||
| 668 | $self->_wsdl_generate_messages | ||
| 669 | ($call_request_type_name,$call_response_type_name, | ||
| 670 | $call_request_msg_name,$call_response_msg_name), | ||
| 671 | $self->_wsdl_generate_messages | ||
| 672 | ($spawn_request_type_name,$spawn_response_type_name, | ||
| 673 | $spawn_request_msg_name,$spawn_response_msg_name), | ||
| 674 | $self->_wsdl_generate_messages | ||
| 675 | ($running_request_type_name,$running_response_type_name, | ||
| 676 | $running_request_msg_name,$running_response_msg_name), | ||
| 677 | $self->_wsdl_generate_messages | ||
| 678 | ($results_request_type_name,$results_response_type_name, | ||
| 679 | $results_request_msg_name,$results_response_msg_name), | ||
| 680 | |||
| 681 | element("message", | ||
| 682 | attr("name",$fault_msg_name), | ||
| 683 | element("part", | ||
| 684 | attr("name","faultmess"), | ||
| 685 | attr("type","xsd:string"))) | ||
| 686 | |||
| 687 | ); | ||
| 688 | |||
| 689 | #################################################################### | ||
| 690 | # porttype's | ||
| 691 | #################################################################### | ||
| 692 | |||
| 693 | my $porttype_name = $server_name."PortType"; | ||
| 694 | |||
| 695 | my $porttype = | ||
| 696 | element("portType", | ||
| 697 | attr("name",$porttype_name), | ||
| 698 | $self->_wsdl_generate_porttype_operation("Call",$call_request_msg_name, | ||
| 699 | $call_response_msg_name), | ||
| 700 | $self->_wsdl_generate_porttype_operation("Spawn",$spawn_request_msg_name, | ||
| 701 | $spawn_response_msg_name), | ||
| 702 | $self->_wsdl_generate_porttype_operation("Running",$running_request_msg_name, | ||
| 703 | $running_response_msg_name), | ||
| 704 | $self->_wsdl_generate_porttype_operation("Results",$results_request_msg_name, | ||
| 705 | $results_response_msg_name)); | ||
| 706 | |||
| 707 | #################################################################### | ||
| 708 | # bindings | ||
| 709 | #################################################################### | ||
| 710 | |||
| 711 | my $binding_name = $server_name."Binding"; | ||
| 712 | |||
| 713 | my $binding = | ||
| 714 | element | ||
| 715 | ("binding", | ||
| 716 | attr("name",$binding_name), | ||
| 717 | attr("type","server:".$porttype_name), | ||
| 718 | element("soap:binding", | ||
| 719 | attr("style","document"), | ||
| 720 | attr("transport", | ||
| 721 | "http://schemas.xmlsoap.org/soap/http")), | ||
| 722 | $self->_wsdl_generate_binding_operation("Call"), | ||
| 723 | $self->_wsdl_generate_binding_operation("Spawn"), | ||
| 724 | $self->_wsdl_generate_binding_operation("Running"), | ||
| 725 | $self->_wsdl_generate_binding_operation("Results")); | ||
| 726 | |||
| 727 | |||
| 728 | #################################################################### | ||
| 729 | # Make the service element | ||
| 730 | #################################################################### | ||
| 731 | my $service = &$service_func($binding_name); | ||
| 732 | |||
| 733 | #################################################################### | ||
| 734 | # Put it all together into the WSDL document. | ||
| 735 | #################################################################### | ||
| 736 | |||
| 737 | my $d = | ||
| 738 | document | ||
| 739 | (element("definitions", | ||
| 740 | namespace(0,$wsdl), | ||
| 741 | namespace("soap",$wsdl_soap), | ||
| 742 | namespace("SOAP-ENC",$SOAP_ENC), | ||
| 743 | namespace("xsd",$xsd), | ||
| 744 | namespace("server",$server_urn), | ||
| 745 | attr("targetNamespace",$server_urn), | ||
| 746 | $types, | ||
| 747 | @messages, | ||
| 748 | $porttype, | ||
| 749 | $binding, | ||
| 750 | $service, | ||
| 751 | )); | ||
| 752 | |||
| 753 | return $d; | ||
| 754 | } | ||
| 755 | |||
| 756 | ######################################################################## | ||
| 757 | |||
| 758 | # Generate type declarations | ||
| 759 | sub _wsdl_generate_types { | ||
| 760 | my ($self,$request_type_name,$response_type_name, | ||
| 761 | $request_base_type,$response_base_type) = @_; | ||
| 762 | |||
| 763 | assert($request_type_name); | ||
| 764 | assert($response_type_name); | ||
| 765 | assert($request_base_type); | ||
| 766 | assert($response_base_type); | ||
| 767 | |||
| 768 | my $request_type = | ||
| 769 | element("xsd:element", | ||
| 770 | attr("name",$request_type_name), | ||
| 771 | $request_base_type); | ||
| 772 | my $response_type = | ||
| 773 | element("xsd:element", | ||
| 774 | attr("name",$response_type_name), | ||
| 775 | $response_base_type); | ||
| 776 | |||
| 777 | return ($request_type,$response_type); | ||
| 778 | } | ||
| 779 | |||
| 780 | # Generate message declarations | ||
| 781 | sub _wsdl_generate_messages { | ||
| 782 | my ($self,$request_type_name,$response_type_name, | ||
| 783 | $request_msg_name,$response_msg_name) = @_; | ||
| 784 | |||
| 785 | my $request_msg = | ||
| 786 | element("message", | ||
| 787 | attr("name",$request_msg_name), | ||
| 788 | element("part", | ||
| 789 | attr("name","parameters"), | ||
| 790 | attr("element","server:".$request_type_name))); | ||
| 791 | |||
| 792 | my $response_msg = | ||
| 793 | element("message", | ||
| 794 | attr("name",$response_msg_name), | ||
| 795 | element("part", | ||
| 796 | attr("name","parameters"), | ||
| 797 | attr("element","server:".$response_type_name))); | ||
| 798 | |||
| 799 | return ($request_msg,$response_msg); | ||
| 800 | } | ||
| 801 | |||
| 802 | |||
| 803 | # Generate porttype declarations | ||
| 804 | sub _wsdl_generate_porttype_operation { | ||
| 805 | my ($self,$method_name,$request_msg_name,$response_msg_name) = @_; | ||
| 806 | |||
| 807 | my $operation = | ||
| 808 | element("operation", | ||
| 809 | attr("name",$method_name), | ||
| 810 | element("input", attr("message", "server:".$request_msg_name)), | ||
| 811 | element("output", attr("message","server:".$response_msg_name))); | ||
| 812 | |||
| 813 | return $operation; | ||
| 814 | } | ||
| 815 | |||
| 816 | # Generate binding declarations | ||
| 817 | sub _wsdl_generate_binding_operation { | ||
| 818 | my ($self,$method_name) = @_; | ||
| 819 | |||
| 820 | my $server_urn = $self->{urn}; | ||
| 821 | defined($server_urn) || die("No server URN"); | ||
| 822 | |||
| 823 | my $operation = | ||
| 824 | element | ||
| 825 | ("operation", | ||
| 826 | attr("name",$method_name), | ||
| 827 | element("soap:operation", | ||
| 828 | attr("soapAction",$server_urn."#".$method_name), | ||
| 829 | attr("style","document")), | ||
| 830 | element("input", | ||
| 831 | element("soap:body",attr("use","literal"))), | ||
| 832 | element("output", | ||
| 833 | element("soap:body",attr("use","literal"))) | ||
| 834 | ); | ||
| 835 | |||
| 836 | return $operation; | ||
| 837 | } | ||
| 838 | |||
| 839 | # Generate a type element for a single parameter. | ||
| 840 | sub _wsdl_parameter_type { | ||
| 841 | my ($self,$name,$type,$optional) = @_; | ||
| 842 | |||
| 843 | my $type_info; | ||
| 844 | if ( $type eq "xml" ) { | ||
| 845 | $type_info = | ||
| 846 | element("xsd:complexType", | ||
| 847 | attr("mixed","true"), | ||
| 848 | element("xsd:sequence", | ||
| 849 | element("xsd:any"))); | ||
| 850 | } else { | ||
| 851 | my $xsd_type = 0; | ||
| 852 | if ( $type eq "bool" ) { | ||
| 853 | $xsd_type = "xsd:boolean"; | ||
| 854 | } elsif ( $type eq "int" ) { | ||
| 855 | $xsd_type = "xsd:int"; | ||
| 856 | } elsif ( $type eq "float" ) { | ||
| 857 | $xsd_type = "xsd:float"; | ||
| 858 | } elsif ( $type eq "string" ) { | ||
| 859 | $xsd_type = "xsd:string"; | ||
| 860 | } elsif ( $type eq "raw" ) { | ||
| 861 | $xsd_type = "xsd:base64Binary"; | ||
| 862 | } else { | ||
| 863 | die("Error! file \"".__FILE__."\", line ".__LINE__); | ||
| 864 | } | ||
| 865 | $type_info = attr("type",$xsd_type); | ||
| 866 | } | ||
| 867 | |||
| 868 | return element("xsd:element", | ||
| 869 | attr("minOccurs",$optional ? "0" : "1"), | ||
| 870 | attr("maxOccurs","1"), | ||
| 871 | attr("name",$name), | ||
| 872 | $type_info); | ||
| 873 | |||
| 874 | } | ||
| 875 | |||
| 876 | ######################################################################## | ||
| 877 | # Functions for reading and parsing command descriptors. | ||
| 878 | ######################################################################## | ||
| 879 | |||
| 880 | # Parse a .descr file. Return a "parse object" that describes the contents | ||
| 881 | # of the file. | ||
| 882 | |||
| 883 | sub _read_descr { | ||
| 884 | my ($self,$descr_file_name) = @_; | ||
| 885 | |||
| 886 | my $descr = ""; | ||
| 887 | # read the file. Strip spaces and comments; | ||
| 888 | open DESCR_FILE, "<$descr_file_name" || | ||
| 889 | die("Error! file \"".__FILE__."\", line ".__LINE__); | ||
| 890 | while (<DESCR_FILE>) { | ||
| 891 | chomp; | ||
| 892 | # remove comments from the end of the line. | ||
| 893 | s/\#[^\"]*$//; | ||
| 894 | $descr .= $_; | ||
| 895 | } | ||
| 896 | close DESCR_FILE || | ||
| 897 | die("Error! file \"".__FILE__."\", line ".__LINE__); | ||
| 898 | |||
| 899 | return $descr; | ||
| 900 | } | ||
| 901 | |||
| 902 | # Returns a list of parameter objects. Each parameter object has these fields, | ||
| 903 | # | ||
| 904 | # name - the name of the parameter | ||
| 905 | # direc - one of in, out, in_out | ||
| 906 | # mech - How is the parameter communicated to the command? | ||
| 907 | # One of val or file | ||
| 908 | # type - One of bool, int, float, raw, xml | ||
| 909 | # type_arg - Optional argument to the type. Only for type==xml. | ||
| 910 | # optional - True if paramater is optional | ||
| 911 | # default - Optional, default value | ||
| 912 | |||
| 913 | sub _parse_descr { | ||
| 914 | my ($self,$descr) = @_; | ||
| 915 | |||
| 916 | |||
| 917 | my @params = (); | ||
| 918 | my $cmd_skel = $descr; | ||
| 919 | |||
| 920 | while ( $cmd_skel =~ '(.*)\[([^\]]*)\](.*)' ) { | ||
| 921 | my ($cmd_prefix, $orig_annot, $cmd_suffix) = ($1,$2,$3); | ||
| 922 | |||
| 923 | my $annot = $orig_annot; | ||
| 924 | |||
| 925 | my %param = (); | ||
| 926 | |||
| 927 | ( $annot =~ '^(in|out|in_out)\s+(.*)' ) | ||
| 928 | || die('Invalid parameter kind - '.$orig_annot); | ||
| 929 | ($param{direc},$annot) = ($1,$2); | ||
| 930 | |||
| 931 | if ( $annot =~ '^(val|file)\s+(.*)' ) { | ||
| 932 | ($param{mech},$annot) = ($1,$2); | ||
| 933 | } else { | ||
| 934 | $param{mech} = "val"; | ||
| 935 | } | ||
| 936 | |||
| 937 | ( $annot =~ '([a-zA-Z0-9_]+)\s*:\s*(.*)' ) | ||
| 938 | || die('Cannot find parameter name - '.$orig_annot); | ||
| 939 | my $param_name; | ||
| 940 | ($param_name,$annot) = ($1,$2); | ||
| 941 | |||
| 942 | ( $annot =~ '([^=]+)(.*)' ) | ||
| 943 | || die('Cannot find type - '.$orig_annot); | ||
| 944 | ($param{type},$annot) = ($1,$2); | ||
| 945 | $param{type} =~ s/\s//g; | ||
| 946 | # type_name("url") | ||
| 947 | if ( $param{type} =~ /^([a-zA-Z0-9_-]+)\(\"(.*)\"\)$/ ) { | ||
| 948 | ($param{type},$param{type_arg}) = ($1,$2); | ||
| 949 | } | ||
| 950 | |||
| 951 | if ( $annot =~ '^=\s*(.*)\s*' ) { | ||
| 952 | $param{optional} = 1; | ||
| 953 | if ( $1 ne "" ) { | ||
| 954 | $param{default} = $1; | ||
| 955 | } | ||
| 956 | } else { | ||
| 957 | $param{optional} = 0; | ||
| 958 | } | ||
| 959 | |||
| 960 | $param{name} = $param_name; | ||
| 961 | push @params, \%param; | ||
| 962 | $cmd_skel = $cmd_prefix.'${'.$param_name.'}'.$cmd_suffix; | ||
| 963 | } | ||
| 964 | |||
| 965 | return (\@params,$cmd_skel); | ||
| 966 | } | ||
| 967 | |||
| 968 | ######################################################################## | ||
| 969 | |||
| 970 | 1; |