BGPmon-core-1

 view release on metacpan or  search on metacpan

lib/BGPmon/Fetch/Client.pm  view on Meta::CPAN


    # Socket timed out.
    $error_code{$fname} = SOCKET_TIMEOUT_CODE;
    $error_msg{$fname} = SOCKET_TIMEOUT_MSG;
    $sel->remove($sock);
    close($sock);
    return -5;
}

=head2 read_xml_message

This function reads one xml message at a time from the BGPmon XML stream.

=cut

sub read_xml_message {
    my $complete_xml_msg = "";
    my $fname = 'read_xml_message';

    # Check if we are connected to a server
    unless ($connected == 1) {
        $error_code{$fname} = NOT_CONNECTED_CODE;
        $error_msg{$fname} = NOT_CONNECTED_MSG;
        return undef;
    }

    my $tempbuf;
    my $bytesread = 0;
    my @ready = $sel->can_read($socket_timeout);

    # If there is a socket with data.
    if (scalar(@ready) > 0) {
        while ($read_buffer !~ /<BGP_MESSAGE.*?BGP_MESSAGE>/) {
            $bytesread = sysread($sock, $tempbuf, $chunksize);
            if (!defined $bytesread) {
                $error_code{$fname} = XML_MSG_READ_ERROR_CODE;
                $error_msg{$fname} = XML_MSG_READ_ERROR_MSG;
                close_connection();
                return undef;
            }
            if ($bytesread == 0) {
                $error_code{$fname} = CONNECTION_CLOSED_CODE;
                $error_msg{$fname} = CONNECTION_CLOSED_MSG;
                close_connection();
                return undef;
            }
            $read_buffer .= $tempbuf;
        }

        # at this point I have a complete message OR my socket closed
        if ($read_buffer =~ /^(<BGP_MESSAGE.*?BGP_MESSAGE>)/) {
            $complete_xml_msg = $1;
            $msgs_read++;
            $read_buffer =~ s/^<BGP_MESSAGE.*?BGP_MESSAGE>//;
        } else {
            $error_code{$fname} = UNKNOWN_ERROR_CODE;
            $error_msg{$fname} = UNKNOWN_ERROR_MSG;
        }
        return $complete_xml_msg;
    }
    # Timeout.
    $error_code{$fname} = SOCKET_TIMEOUT_CODE;
    $error_msg{$fname} = SOCKET_TIMEOUT_MSG;
    return undef;
}

=head2 close_connection

Function to close open files and sockets.

=cut

sub close_connection {
    $connected = 0;
    $end_time = time;
    if ($sock) {
        $sel->remove($sock);
        $sock->close();
    }
}

=head2 is_connected

Function to report whether currently connected to BGPmon.

=cut

sub is_connected {
    return $connected;
}

=head2 messages_read

Get number of messages read.

=cut

sub messages_read {
    return $msgs_read;
}

=head2 uptime

Returns number of seconds the connection has been up.
If the connection is down, return 0.

=cut

sub uptime {
    if ($connected) {
        return time() - $start_time;
    }
    return 0;

}

=head2 connection_endtime

Returns the time the connection ended .
If the connection is up, return 0.

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.064 second using v1.00-cache-2.02-grep-82fe00e-cpan-2c419f77a38b )