#!/usr/local/bin/tops -s /usr/local/tops/sys -u /opt/mytops/usr/
# File lifeline1, January 2005

# Connecting a lifeline to server running at topsdog.

#-----------------------------------------------------------------------

  CATMSG push no catmsg

\-----------------------------------------------------------------------

# Vector output to this log file:
   "/home/dale/lifeline1.log" "LOG" book
   LOG set_sysout

# Lifeline port at topsdog:
   9881 "PORT" book

# Display first lines in the log file:
   "-" 80 cats nl dot nl
   "PID " getpid int$ cat spaced date cat dot nl

#  Allow topsdog to connect:
   topsdog drop "SERVER_ALLOW" "clients" bank

# Word to connect a line to topsdog:
   inline: connect_line ( --- ) \ to topsdog PORT
    \ This word makes the initial connection and starts word "test"
    \ running every 10 seconds in the multitasker.  If the connection
    \ is broken, word test runs this word to reconnect.
      [ 20 "MAX_TRIES" book
        no "TRIES" book
        no "CONNECTED" book
      ]
      date . 
      topsdog drop "PORT" main CLIENT 0>
      IF " connect_line: connected to topsdog"
         one ten slash "test" PLAY \ test connection every 10 seconds
         no "TRIES" book
         yes "CONNECTED" book
      ELSE " connect_line: unable to connect to topsdog"
         CONNECTED 
         IF one TRIES bump TRIES MAX_TRIES > 
            IF once 120 slash "test" RATE THEN \ test every 2 minutes
         ELSE 10 "connect_line" ALARM \ try connecting again
         THEN
      THEN . nl 
   end

# Word to test the local socket connection to topsdog and reconnect:
   inline: test ( --- ) \ test and reconnect
    \ This word runs in the multitasker to test and reconnect if
    \ necessary.
      [ no "BUSY" book ]
      BUSY IF return THEN
      yes "BUSY" book localsockets any?
      IF 1st pry (S) this socket_open 
         IF (S) drop no "BUSY" book 
            yes "connect_line" "CONNECTED" bank return 
         ELSE (S) sclose no "connect_line" "CONNECTED" bank
         THEN
      THEN 
      " test: reconnecting" . nl
      connect_line 
      no "BUSY" book
   end

# Run word connect_line in 10 seconds, after DSERVER has been started:
   10 "connect_line" ALARM

#  Monitor the workload:
   "Starting WORKLOAD" . nl
    once 60 slash "WORKLOAD" PLAY \ log once a minute

#  Monitor Internet speed:
   "Starting SPEED_LOG" . nl
    once 600 slash "SPEED_LOG" PLAY \ log once every 10 minutes

# DSERVER is being run here simply for local maintenance; use PORT to
# listen on and restrict connection to just loopback:
   "127.0.0.1" PORT DSERVER

\-----------------------------------------------------------------------

  pull catmsg
