}
#
# 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= |