X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=scripts%2FRFID-JSONP-server.pl;h=215d6cafa94fe3630131469bf3b95b42772498b2;hb=282b54550a8a651fe528648d40aa7c582d288511;hp=4bec23e44d4a0cd995776b23113f6f49fcc43fff;hpb=39a86abcdffdaabdf9622ed878a1664c85af60e6;p=Biblio-RFID.git diff --git a/scripts/RFID-JSONP-server.pl b/scripts/RFID-JSONP-server.pl index 4bec23e..215d6ca 100755 --- a/scripts/RFID-JSONP-server.pl +++ b/scripts/RFID-JSONP-server.pl @@ -78,14 +78,36 @@ sub rfid_borrower { } +sub sip2_socket { + + return $sip2->{sock} if exists $sip2->{sock} && $sip2->{sock}->connected; + + if ( my $server = $sip2->{server} ) { + my $sock = $sip2->{sock} = IO::Socket::INET->new( $server ) || die "can't connect to $server: $!"; + warn "SIP2 server ", $sock->peerhost, ":", $sock->peerport, "\n"; + + # login + if ( sip2_message("9300CN$sip2->{user}|CO$sip2->{password}|")->{fixed} !~ m/^941/ ) { + die "SIP2 login failed"; + } + + } + return $sip2->{sock}; +} + sub sip2_message { my $send = shift; - my $sock = $sip2->{sock} || die "no sip2 socket"; + my $retry = 0; + +send_again: + my $sock = sip2_socket || die "no sip2 socket"; local $/ = "\r"; $send .= "\r" unless $send =~ m/\r$/; + $send .= "\n" unless $send =~ m/\n$/; + warn "SIP2 >>>> ",dump($send), "\n"; print $sock $send; $sock->flush; @@ -93,13 +115,24 @@ sub sip2_message { my $expect = substr($send,0,2) | 0x01; my $in = <$sock>; - $in =~ s/^\n//; warn "SIP2 <<<< ",dump($in), "\n"; - die "expected $expect" unless substr($in,0,2) != $expect; - + $in =~ s/^\n//; $in =~ s/\r$//; + if ( ! $in ) { + $retry++; + warn "empty read from SIP server, retry: $retry\n"; + if ( $retry < 10 ) { + close( $sip2->{sock} ); + goto send_again; + } + die "aborted"; + } + + + die "expected $expect" unless substr($in,0,2) != $expect; + my $hash; if ( $in =~ s/^([0-9\s]+)// ) { $hash->{fixed} = $1; @@ -114,16 +147,6 @@ sub sip2_message { return $hash; } -if ( my $server = $sip2->{server} ) { - my $sock = $sip2->{sock} = IO::Socket::INET->new( $server ) || die "can't connect to $server: $!"; - warn "SIP2 server ", $sock->peerhost, ":", $sock->peerport, "\n"; - - # login - if ( sip2_message("9300CN$sip2->{user}|CO$sip2->{password}|")->{fixed} !~ m/^941/ ) { - die "SIP2 login failed"; - } - -} use lib 'lib'; use Biblio::RFID::RFID501;