5 use Data::Dump qw(dump);
12 my $socket = IO::Socket::INET->new(
19 print "$0 waiting for client connection on port 4096\n";
24 BW => 0.0, # just paper cost
25 COLOR => 3.99, # FIXME
30 my $next_nop_t = time() + 5;
33 our $client_socket = $socket->accept();
36 my $text = join('', @_);
38 print $client_socket "$text\r\n";
42 #my $line = <$client_socket>;
45 my $timeout = $next_nop_t - time();
48 local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
50 warn "# NOP alarm $timeout";
51 $line = <$client_socket>;
57 $line = <$client_socket>;
60 $line = <$client_socket>;
63 $line =~ s/[\r\n]+$//;
69 # get the host and port number of newly connected client.
70 my $peer_address = $client_socket->peerhost();
71 my $peer_port = $client_socket->peerport();
73 print "Connection from: $peer_address:$peer_port\n";
76 my $total_charged = 0.00;
80 $v = $_[0] if defined $_[0];
81 return sprintf "%1.2f kn", $v;
84 while ($client_socket->connected) {
86 my $line = client_line;
88 if ( $line =~ m/^\.SQ ([\d\.]+) (\S+)/ ) {
89 my ($version,$serial) = ($1,$2);
91 #client_send ".SQ FAILED message";
92 } elsif ( $line =~ m/^\.CFG/ ) {
93 client_send ".CFG OK %s";
94 } elsif ( $line =~ m/\.SERVER LIST/ ) {
95 client_send ".ERROR NO-ENTERPRISE";
96 } elsif ( $line =~ m/\.CARD (\S+)/ ) {
98 client_send ".CARD OK Ime Prezime (nobody\@example.com)";
99 } elsif ( $line =~ m/\.PIN (\S+)/ ) {
101 client_send ".PIN OK Ime Pinzime (nobody\@example.com)";
102 } elsif ( $line =~ m/\.ACTION$/ ) {
103 # CMENUS0 - no printer
104 client_send ".ACTION CMENUS68"; # FIXME can be CMENUS2
106 } elsif ( $line =~ m/\.ACTION COPY/ ) {
107 client_send ".ACTION COPY"; # safeq sends this twice
108 client_send ".COPY Mozete kopirati |".credit;
110 client_send ".CREDIT ".credit;
111 } elsif ( $line =~ m/\.COPY (.+)/ ) {
114 foreach ( split(/,/,$1) ) {
115 die "can't find [$_] in prices=",dump($prices) unless exists $prices->{$_};
116 $charge += $prices->{$_};
118 warn "CHARGE: $charge\n";
120 $total_charged += $charge;
122 client_send ".CREDIT ".credit;
123 client_send ".COPY 1"; # I verified that you are allowed to copy 1 page?
126 } elsif ( $line =~ m/\.ACTION LIST/ ) {
129 } elsif ( $line =~ m/\.ACTION PRINT ALL/ ) {
132 } elsif ( $line =~ m/^\.NOP/ ) {
133 # XXX it's important to sleep, before sending response or
134 # interface on terminal device will be unresponsive
135 $next_nop_t = time() + 5; # NOP every 5s?
136 } elsif ( $line =~ m/^\.END/ ) {
137 client_send ".DONE BLK WAIT";
139 my $nop = client_line;
140 client_send ".DONE $total_pages ".credit($total_charged);
141 warn "expected NOP got: $nop" unless $nop =~ m/NOP/;
142 my $null = client_line;
143 $client_socket->close;
145 warn "UNKNOWN: ",dump($line);
152 warn "# return to accept";