fleshed out some commands (particularly flag setting and unsetting)
[spider.git] / perl / client.pl
index c508d942e90cab67e8d43cc02c599b4cd61a3799..c5b4bbec805e2ef04a8ae01c081286e1b5eb46b1 100755 (executable)
@@ -9,16 +9,23 @@
 #
 # Copyright (c) 1998 Dirk Koopman G1TLH
 #
+# $Id$
 # 
 
+BEGIN {
+  unshift @INC, "/spider/local";
+  unshift @INC, "/spider/perl";
+}
+
 use Msg;
 use DXVars;
 
-$mode = 1;                      # 1 - \n = \r as EOL, 2 - \n = \n, 0 - transparent
+$mode = 2;                      # 1 - \n = \r as EOL, 2 - \n = \n, 0 - transparent
 $call = "";                     # the callsign being used
 @stdoutq = ();                  # the queue of stuff to send out to the user
 $conn = 0;                      # the connection object for the cluster
 $lastbit = "";                  # the last bit of an incomplete input line
+$mynl = "\n";                   # standard terminator
 
 # cease communications
 sub cease
@@ -36,6 +43,16 @@ sub sig_term
   cease(1);
 }
 
+sub setmode
+{
+  if ($mode == 1) {
+    $mynl = "\r";
+  } else {
+       $mynl = "\n";
+  }
+  $/ = $mynl;
+}
+
 # handle incoming messages
 sub rec_socket
 {
@@ -44,16 +61,19 @@ sub rec_socket
     cease(1);
   }
   if (defined $msg) {
-    my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)|(.*)$/;
+    my ($sort, $call, $line) = $msg =~ /^(\w)(\S+)\|(.*)$/;
        
        if ($sort eq 'D') {
-          my $nl = ($mode == 1) ? "\r" : "\n";
-          $nl = "" if $mode == 0;
-          $line =~ s/\n/\r/o if $mode == 1;
-          print $line, $nl;
+          my $snl = $mynl;
+          $snl = "" if $mode == 0;
+          $snl = ' ' if ($mode && $line =~ />$/);
+          $line =~ s/\n/\r/og if $mode == 1;
+          #my $p = qq($line$snl);
+          print $line, $snl;
        } elsif ($sort eq 'M') {
          $mode = $line;               # set new mode from cluster
-       } elsif ($sort eq 'Z') {       # end, disconnect, go, away .....
+      setmode();
+    } elsif ($sort eq 'Z') {       # end, disconnect, go, away .....
          cease(0);
     }    
   } 
@@ -72,7 +92,7 @@ sub rec_stdin
 #  print "sys: $r $buf";
   if ($r > 0) {
     if ($mode) {
-         $buf =~ s/\r/\n/o if $mode == 1;
+         $buf =~ s/\r/\n/og if $mode == 1;
          $dangle = !($buf =~ /\n$/);
          @lines = split /\n/, $buf;
          if ($dangle) {                # pull off any dangly bits
@@ -94,18 +114,33 @@ sub rec_stdin
   }
 }
 
-$call = uc $ARGV[0];
-die "client.pl <call> [<mode>]\r\n" if (!$call);
-$mode = $ARGV[1] if (@ARGV > 1);
+$call = uc shift @ARGV;
+$call = uc $mycall if !$call; 
+$connsort = lc shift @ARGV;
+$connsort = 'local' if !$connsort;
+$mode = ($connsort =~ /^ax/o) ? 1 : 2;
+setmode();
 
-select STDOUT; $| = 1;
+#select STDOUT; $| = 1;
+STDOUT->autoflush(1);
 
 $SIG{'INT'} = \&sig_term;
 $SIG{'TERM'} = \&sig_term;
 $SIG{'HUP'} = \&sig_term;
 
 $conn = Msg->connect("$clusteraddr", $clusterport, \&rec_socket);
-$conn->send_now("A$call|start");
+$conn->send_now("A$call|$connsort");
 Msg->set_event_handler(\*STDIN, "read" => \&rec_stdin);
-Msg->event_loop();
+
+$lasttime = time;
+for (;;) {
+  my $t;
+  Msg->event_loop(1, 0.010);
+  $t = time;
+  if (t > $lasttime+660 && $connsort =~ /^ax/o) {            # every e
+    print pack('xx');
+       STDOUT->fflush();
+       $lasttime = $t;
+  }
+}