fix connection end .END/.DONE
[safeq] / terminal-server.pl
index 505023e..992768e 100755 (executable)
@@ -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,9 +40,34 @@ while(1) {
        }
 
        sub client_line {
-               my $line = <$client_socket>;
-               $line =~ s/[\r\n]+$//;
-               warn "<< $line\n";
+               #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] connected: ",dump($client_socket), $client_socket->connected;
+               }
+
                return $line;
        }
 
@@ -51,7 +81,8 @@ while(1) {
        my $total_charged = 0.00;
        my $total_pages   = 0;
        sub credit {
-               my $v = shift || $credit;
+               my $v = $credit;
+               $v = $_[0] if defined $_[0];
                return sprintf "%1.2f kn", $v;
        }
 
@@ -70,8 +101,12 @@ while(1) {
                } elsif ( $line =~ m/\.CARD (\S+)/ ) {
                        my ($rfid_sid) = $1;
                        client_send  ".CARD OK Ime Prezime (nobody\@example.com)";
+               } elsif ( $line =~ m/\.PIN (\S+)/ ) {
+                       my ($pin) = $1;
+                       client_send  ".PIN OK Ime Pinzime (nobody\@example.com)";
                } elsif ( $line =~ m/\.ACTION$/ ) {
-                       client_send  ".ACTION CMENUS0"; # FIXME can be CMENUS2
+                       # CMENUS0 - no printer
+                       client_send  ".ACTION CMENUS68"; # FIXME can be CMENUS2
 
                } elsif ( $line =~ m/\.ACTION COPY/ ) {
                        client_send  ".ACTION COPY";    # safeq sends this twice
@@ -99,22 +134,21 @@ while(1) {
                } elsif ( $line =~ m/\.ACTION PRINT ALL/ ) {
                        # FIXME
 
-               } elsif ( $line =~ m/(\.NOP)/ ) {
-                       client_send  "$1";
+               } elsif ( $line =~ m/^\.NOP/ ) {
+                       # XXX it's important to sleep, before sending response or
+                       # interface on terminal device will be unresponsive
+                       $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 $total_pages ".credit($total_charged);
-                       warn "expected NOP got: $nop" unless $nop =~ m/NOP/;
-                       my $null = client_line;
                        $client_socket->close;
-               } else {
+               } elsif (defined $line) {
                        warn "UNKNOWN: ",dump($line);
                        print "Response>";
                        my $r = <STDIN>;
                        chomp $r;
                        client_send $r;
+               } else {
+                       warn "NULL line, connected ", $client_socket->connected;
                }
        }
        warn "# return to accept";