From 7c505ab2d1e25c076a9b922c04ca8de8a0e45fdd Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Thu, 21 Feb 2019 16:31:26 +0100 Subject: [PATCH] added emulator and improve server to interoperate --- terminal-emulator.pl | 69 ++++++++++++++++++++++++++++++++++++++++++++ terminal-server.pl | 11 ++++--- 2 files changed, 76 insertions(+), 4 deletions(-) create mode 100755 terminal-emulator.pl diff --git a/terminal-emulator.pl b/terminal-emulator.pl new file mode 100755 index 0000000..9a4b485 --- /dev/null +++ b/terminal-emulator.pl @@ -0,0 +1,69 @@ +#!/usr/bin/perl +use warnings; +use strict; + +use Data::Dump qw(dump); + +use IO::Socket::INET; + +my $ip = shift @ARGV || '127.0.0.1'; +my $port = shift @ARGV || 4096; + +my $socket = IO::Socket::INET->new( + PeerAddr => $ip, + PeerPort => $port, + Proto => 'tcp', +) or die "ERROR $ip:$port - $!"; + +warn "# connected to $ip:$port\n"; + +my @send_receive = grep { /^.+$/ } split(/\n/, q{ +.SQ 3.2.9 SQPR8463332F62E +.SQ OK + +.CFG gd lang=HR +.CFG OK %s + +.SERVER LIST +.ERROR NO-ENTERPRISE + +.CARD E009000000009999 +.CARD OK Ime Prezime (nobody@example.com) + +.ACTION +.ACTION CMENUS0 + +.NOP +.NOP + + +.NOP +.NOP + +.NOP +.NOP + +.NOP +.NOP + +.NOP +.NOP + +.END +}); + +#warn "# send_receive=",dump( \@send_receive ); + +while ( @send_receive ) { + my $send = shift @send_receive; + my $expect = shift @send_receive; + warn ">> $send\n"; + print $socket "$send\r\n"; + my $got = <$socket>; + $got =~ s/[\r\n]+$//; +warn dump($send,$expect,$got); + warn "<< $got\n"; + die "ERROR expected [$expect] got [$got]" if $expect ne $got; +} + +$socket->close(); diff --git a/terminal-server.pl b/terminal-server.pl index ca2fd57..3598ce2 100755 --- a/terminal-server.pl +++ b/terminal-server.pl @@ -2,6 +2,8 @@ use warnings; use strict; +use Data::Dump qw(dump); + use IO::Socket::INET; $| = 1; @@ -32,7 +34,8 @@ while(1) { while ($client_socket->connected) { my $line = <$client_socket>; - chomp $line; + $line =~ s/[\r\n]+$//; + warn "<< $line\n"; if ( $line =~ m/^\.SQ ([\d\.]+) (\S+)/ ) { @@ -42,16 +45,16 @@ while(1) { } elsif ( $line =~ m/\.SERVER LIST/ ) { client_send ".ERROR NO-ENTERPRISE"; } elsif ( $line =~ m/\.CARD (\S+)/ ) { - client_send ".CARD OK pero peric (pero\@example.com)"; + client_send ".CARD OK Ime Prezime (nobody\@example.com)"; } elsif ( $line =~ m/\.ACTION$/ ) { - client_send ".ACTION CMENUS2"; + client_send ".ACTION CMENUS0"; # FIXME can be CMENUS2 } elsif ( $line =~ m/\.ACTION COPY/ ) { client_send ".ACTION COPY"; client_send ".COPY Mozete kopirati (pero)"; } elsif ( $line =~ m/(\.NOP)/ ) { client_send "$1"; } else { - die "unknown: $line"; + die "UNKNOWN: ",dump($line); } } } -- 2.20.1