PROXY  WHOIS  RQUOTE  TEXTS  SOFT  FOREX  BBOARD
 Music  Philosophy  Code  Literature  Russian

= ROOT|Technical|Code_Examples|Perl|site_perl|JUNOS|Device.pm =

page 3 of 11



}

#
# Send a request to the JUNOScript server and return the result.
# Since the normal DOM top level will always contain only the
# node for the rpc-reply, we assume the caller really just wants
# that node. If called in an scalar context, we only return this
# node. In an array context, we return the document and the node
# as an array.
#
sub request
{
    my $rpc;
    my($self, $request) = @_;

    $self->clear_errors() if caller() ne __PACKAGE__;

    tracept("Request");

    $self->connect() || return unless $self->{JUNOS_Connected};

    my $conn = $self->{JUNOS_Conn};

    # Catches calling 'request_end_session' twice amongst other calamities
    if ($conn->{seen_eof}) {
	$self->report_error("connection ended unexpectedly");
	return;
    }
    
    # If the caller gives us an object, turn it into a string.
    if (ref($request)) {
	$rpc = $request->toString;
    } else {
	$rpc = $request;
    }
    trace("Trace", "starting rpc; ", ref($request), "sending::\n", $rpc);

    trace("Verbose", "--- begin request---\n",
	  $rpc, ($rpc =~ /\n$/) ? "" : "\n", "--- end request ---\n");

    # Send the request to the JUNOScript server
    my $back =  $conn->send($rpc);

    # Pull data off the server until we get a complete reply (or eof).
    until ($self->{JUNOS_Reply}) {
	my $in = $conn->recv();
	trace("Trace", "during rpc; got::\n	$in");

	if ($conn->eof) {
	    # Make sure it's legit before handing it off
	    $self->parse_done($in) if (contains_end_tag($in));
	    $conn->{seen_eof} = 1;
	    last;
	} else {
	    # This is for the case when using telnet & after we're done
	    #   running 'junoscript' we get the shell char back
	    if ($self->parse_more($in))
	    {
		$conn->{seen_eof} = 1;
		last;
	    }
	}

    }

    tracept("Request");

    # Fetch the XML::DOM::Document, as saved by replyHandler()
    my $doc = $self->{JUNOS_Reply};
    trace("Trace", "reply is ", ref($doc) || "empty", "::", $doc);
    unless ($doc) {
	$self->report_error("reply is empty");
	return;
    }

    # Clear the reply
    undef $self->{JUNOS_Reply};

    # Turn the response into a JUNOS::Response, allowing us
    # to add methods on top of the DOM methods.
    my $response = JUNOS::Response->new($doc->getDocumentElement());

    trace("Verbose", "--- begin reply---\n",
	  $response->toString, ($response =~ /\n$/) ? "" : "\n",
	  "--- end reply ---\n");

    # If the caller wants an array, we give them both the document and
    # the response, which they must dispose of.
    return ($doc, $response) if wantarray;

    # Otherwise dispose of the document, which was only needed for DOM.
    if ($#$response >= 0) {
	$doc->removeChild($response);
	$response->setOwnerDocument(undef);
	$doc->dispose;
    }

    return $response;
}

=3=

1|2| < PREV = PAGE 3 = NEXT > |4|5|6|7|8|9|10|11

UP TO ROOT | UP TO DIR | TO FIRST PAGE

Google
 


E-mail Facebook Google Digg del.icio.us BlinkList Fark Furl Ma.gnolia Netscape NewsVine Reddit Slashdot Spurl StumbleUpon Technorati YahooMyWeb LiveJournal Blogmarks TwitThis Live News2.ru BobrDobr.ru Memori.ru MoeMesto.ru

0.00579691 wallclock secs ( 0.01 usr + 0.00 sys = 0.01 CPU)