5 use Data::Dump qw(dump);
11 my $socket = IO::Socket::INET->new(
18 print "$0 waiting for client connection on port 4096\n";
23 BW => 0.0, # just paper cost
24 COLOR => 3.99, # FIXME
29 our $client_socket = $socket->accept();
32 my $text = join('', @_);
34 print $client_socket "$text\r\n";
38 my $line = <$client_socket>;
39 if ( defined $line ) {
40 $line =~ s/[\r\n]+$//;
46 # get the host and port number of newly connected client.
47 my $peer_address = $client_socket->peerhost();
48 my $peer_port = $client_socket->peerport();
50 print "Connection from: $peer_address:$peer_port\n";
53 my $total_charged = 0.00;
57 $v = $_[0] if defined $_[0];
58 return sprintf "%1.2f kn", $v;
61 while ($client_socket->connected) {
63 my $line = client_line;
65 if ( $line =~ m/^\.SQ ([\d\.]+) (\S+)/ ) {
66 my ($version,$serial) = ($1,$2);
68 #client_send ".SQ FAILED message";
69 } elsif ( $line =~ m/^\.CFG/ ) {
70 client_send ".CFG OK %s";
71 } elsif ( $line =~ m/\.SERVER LIST/ ) {
72 client_send ".ERROR NO-ENTERPRISE";
73 } elsif ( $line =~ m/\.CARD (\S+)/ ) {
75 client_send ".CARD OK Ime Prezime (nobody\@example.com)";
76 } elsif ( $line =~ m/\.PIN (\S+)/ ) {
78 client_send ".PIN OK Ime Pinzime (nobody\@example.com)";
79 } elsif ( $line =~ m/\.ACTION$/ ) {
80 # CMENUS0 - no printer
81 client_send ".ACTION CMENUS68"; # FIXME can be CMENUS2
83 } elsif ( $line =~ m/\.ACTION COPY/ ) {
84 client_send ".ACTION COPY"; # safeq sends this twice
85 client_send ".COPY Mozete kopirati |".credit;
87 client_send ".CREDIT ".credit;
88 } elsif ( $line =~ m/\.COPY (.+)/ ) {
91 foreach ( split(/,/,$1) ) {
92 die "can't find [$_] in prices=",dump($prices) unless exists $prices->{$_};
93 $charge += $prices->{$_};
95 warn "CHARGE: $charge\n";
97 $total_charged += $charge;
99 client_send ".CREDIT ".credit;
100 client_send ".COPY 1"; # I verified that you are allowed to copy 1 page?
103 } elsif ( $line =~ m/\.ACTION LIST/ ) {
106 } elsif ( $line =~ m/\.ACTION PRINT ALL/ ) {
109 } elsif ( $line =~ m/^\.NOP/ ) {
110 # XXX it's important to sleep, before sending response or
111 # interface on terminal device will be unresponsive
114 } elsif ( $line =~ m/^\.END/ ) {
115 client_send ".DONE BLK WAIT";
117 my $nop = client_line;
118 client_send ".DONE $total_pages ".credit($total_charged);
119 warn "expected NOP got: $nop" unless $nop =~ m/NOP/;
120 my $null = client_line;
121 $client_socket->close;
123 warn "UNKNOWN: ",dump($line);
130 warn "# return to accept";