X-Git-Url: http://git.rot13.org/?p=safeq;a=blobdiff_plain;f=terminal-server.pl;h=6ea1a23ba3831c683d7045a91e81b5f7f42e6f0b;hp=4b964ed8d12107eae56d34249d1dd700c4cf3082;hb=83c5b5d051461cc72a23eda9a2028eff5cf8edb7;hpb=26b71ac4b95c2bb1b9bd2d49bc04931f42dafc48 diff --git a/terminal-server.pl b/terminal-server.pl index 4b964ed..6ea1a23 100755 --- a/terminal-server.pl +++ b/terminal-server.pl @@ -1,10 +1,12 @@ #!/usr/bin/perl use warnings; use strict; +use autodie; use Data::Dump qw(dump); use IO::Socket::INET; +use Time::HiRes; $| = 1; @@ -25,6 +27,9 @@ my $prices = { DUPLEX => -0.05, }; + +my $next_nop_t = time() + 5; + while(1) { our $client_socket = $socket->accept(); @@ -35,11 +40,34 @@ while(1) { } sub client_line { - my $line = <$client_socket>; + #my $line = <$client_socket>; + + my $line; + my $timeout = $next_nop_t - time(); + if ( $timeout > 0 ) { + eval { + local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required + alarm $timeout; + warn "# NOP alarm $timeout"; + $line = <$client_socket>; + alarm 0; + }; + if ($@) { + # timed out + client_send ".NOP"; + $line = <$client_socket>; + } + } else { + $line = <$client_socket>; + } + if ( defined $line ) { $line =~ s/[\r\n]+$//; warn "<< $line\n"; + } else { + warn "<< NULL ", $client_socket->connected ? '' : 'NOT ', "connected"; } + return $line; } @@ -61,6 +89,7 @@ while(1) { while ($client_socket->connected) { my $line = client_line; + last if ! defined $line; if ( $line =~ m/^\.SQ ([\d\.]+) (\S+)/ ) { my ($version,$serial) = ($1,$2); @@ -97,34 +126,86 @@ while(1) { $total_charged += $charge; $total_pages++; client_send ".CREDIT ".credit; - client_send ".COPY 1"; # I verified that you are allowed to copy 1 page? + client_send ".COPY $total_pages"; # page copied client_send ".NOP"; } elsif ( $line =~ m/\.ACTION LIST/ ) { + + client_send "2"; # nr of items in list + # status: 0 - pendng/3 - printed + # | pages + # | | title + # | | | queue + client_send "3|1|Koha online catalog|XWC7232"; + client_send "0|1|Koha online catalog|XWC5225"; # FIXME - } elsif ( $line =~ m/\.ACTION PRINT ALL/ ) { + } elsif ( $line =~ m/\.ACTION PRINT (ALL|\d+)/ ) { + my $what = $1; + my $job = $1 if $1 =~ m/^\d+$/; # 0 means print all? + + my $charge = $prices->{'A4'} || die "no A4 price"; + + my $nr_jobs = 2; + + if ( $nr_jobs == 0 ) { + client_send ".ACTION NOJOB Nema se Å¡ta tiskat"; + next; + } + # FIXME + warn "FIXME $line\n"; + client_send ".ACTION PRINT"; # device locked from terminal screen? + + # check if printer ready + my $printer_ready = 0; + if ( ! $printer_ready ) { + client_send ".WARN 1/1|The printer is not ready|job has been suspended ... (1x)"; + next; + } + + my $send = 0; # 0 .. 100 + my $printed = 0; # 0 .. nr pages + + # total pages in batch + # | page/batch + # | | title + # | | | + client_send ".PRINT 1|1/1|Microsoft Word - molba_opca"; + client_send ".NOP S $send C 0"; + + # open 10.60.3.25:9100 + $send = 100; + + client_send ".NOP S $send C 0"; + client_send ".MSG Please check display of device" if $send == 100; + + # check smtp counters to be sure page is printed + + $credit -= $charge; + $total_charged += $charge; + $total_pages++; + + client_send ".DONE $nr_jobs $total_pages ".credit($total_charged); } elsif ( $line =~ m/^\.NOP/ ) { # XXX it's important to sleep, before sending response or # interface on terminal device will be unresponsive - sleep 1; - client_send ".NOP"; + $next_nop_t = time() + 5; # NOP every 5s? } elsif ( $line =~ m/^\.END/ ) { - client_send ".DONE BLK WAIT"; - client_send ".NOP"; - my $nop = client_line; + client_send ".DONE BLK WAIT"; + client_send ".NOP"; + my $nr_jobs = 0; # FIXME client_send ".DONE $total_pages ".credit($total_charged); - warn "expected NOP got: $nop" unless $nop =~ m/NOP/; - my $null = client_line; - $client_socket->close; - } else { +# $client_socket->close; + } elsif (defined $line) { warn "UNKNOWN: ",dump($line); print "Response>"; my $r = ; chomp $r; client_send $r; + } else { + warn "NULL line ", $client_socket->connected ? '' : 'NOT ', "connected"; } } warn "# return to accept";