6 use Data::Dump qw(dump);
13 my $socket = IO::Socket::INET->new(
20 print "$0 waiting for client connection on port 4096\n";
25 BW => 0.0, # just paper cost
26 COLOR => 3.99, # FIXME
31 my $next_nop_t = time() + 5;
34 our $client_socket = $socket->accept();
37 my $text = join('', @_);
39 print $client_socket "$text\r\n";
43 #my $line = <$client_socket>;
46 my $timeout = $next_nop_t - time();
49 local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
51 warn "# NOP alarm $timeout";
52 $line = <$client_socket>;
58 $line = <$client_socket>;
61 $line = <$client_socket>;
64 if ( defined $line ) {
65 $line =~ s/[\r\n]+$//;
68 warn "<< [NULL] connected: ",dump($client_socket), $client_socket->connected;
74 # get the host and port number of newly connected client.
75 my $peer_address = $client_socket->peerhost();
76 my $peer_port = $client_socket->peerport();
78 print "Connection from: $peer_address:$peer_port\n";
81 my $total_charged = 0.00;
85 $v = $_[0] if defined $_[0];
86 return sprintf "%1.2f kn", $v;
89 while ($client_socket->connected) {
91 my $line = client_line;
93 if ( $line =~ m/^\.SQ ([\d\.]+) (\S+)/ ) {
94 my ($version,$serial) = ($1,$2);
96 #client_send ".SQ FAILED message";
97 } elsif ( $line =~ m/^\.CFG/ ) {
98 client_send ".CFG OK %s";
99 } elsif ( $line =~ m/\.SERVER LIST/ ) {
100 client_send ".ERROR NO-ENTERPRISE";
101 } elsif ( $line =~ m/\.CARD (\S+)/ ) {
103 client_send ".CARD OK Ime Prezime (nobody\@example.com)";
104 } elsif ( $line =~ m/\.PIN (\S+)/ ) {
106 client_send ".PIN OK Ime Pinzime (nobody\@example.com)";
107 } elsif ( $line =~ m/\.ACTION$/ ) {
108 # CMENUS0 - no printer
109 client_send ".ACTION CMENUS68"; # FIXME can be CMENUS2
111 } elsif ( $line =~ m/\.ACTION COPY/ ) {
112 client_send ".ACTION COPY"; # safeq sends this twice
113 client_send ".COPY Mozete kopirati |".credit;
115 client_send ".CREDIT ".credit;
116 } elsif ( $line =~ m/\.COPY (.+)/ ) {
119 foreach ( split(/,/,$1) ) {
120 die "can't find [$_] in prices=",dump($prices) unless exists $prices->{$_};
121 $charge += $prices->{$_};
123 warn "CHARGE: $charge\n";
125 $total_charged += $charge;
127 client_send ".CREDIT ".credit;
128 client_send ".COPY 1"; # I verified that you are allowed to copy 1 page?
131 } elsif ( $line =~ m/\.ACTION LIST/ ) {
134 } elsif ( $line =~ m/\.ACTION PRINT ALL/ ) {
137 } elsif ( $line =~ m/^\.NOP/ ) {
138 # XXX it's important to sleep, before sending response or
139 # interface on terminal device will be unresponsive
140 $next_nop_t = time() + 5; # NOP every 5s?
141 } elsif ( $line =~ m/^\.END/ ) {
142 client_send ".DONE BLK WAIT";
143 $client_socket->close;
144 } elsif (defined $line) {
145 warn "UNKNOWN: ",dump($line);
151 warn "NULL line, connected ", $client_socket->connected;
154 warn "# return to accept";