#!/usr/local/bin/tops -s /usr/local/tops/sys -u /opt/mytops/usr/
#!../src/tops
#!/usr/local/bin/tops -s /usr/local/tops/sys -u /home/dale/mytops/usr/

# This script runs tops as a server, listening on a port.

# The port to use can be specified with command line switch -port:
#    % tserv -port 9880

# Tue Mar 15 14:36:05 PDT 2011.
# Command line switch -ssl will run SERVER_SSL, as in:
#    % tserv -port 9880 -ssl

# Warning: When using switch -ssl, SSL certifications (.pem files) must 
# be at usrpath, defined above by the -u switch.

# The length of time this script runs can be controlled by command line
# switch -exit.  Here, -exit limits this script to 100 seconds run time:
#    % tserv -exit 100

# A file to source at start up can be specified by command line switch
# -source:
#    % tserv -exit 100 -source collect.v -port 9880

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

 \ Do not change directory if -u usrpath is a relative directory and 
 \ SSL certifications (at usrpath) are needed:
   "/tmp" chdir \ running at /tmp 

   "-exit" argv number not
   IF 12 (hours) 3600 (sec) * \ default end of life
   THEN (EOL) "exit" ALARM    \ set alarm to exit in EOL seconds

   "-port" argv number not
   IF def_port nextport THEN (nPort) "PORT" book

   "-source" argv dup chars any
   IF (qS1) dup filefound
      IF (qS1 qFound) lop source
      ELSE (qS) "tserv: file " . chop . " not found" . nl
         "tserv: halting" . nl
         halt
      THEN
   ELSE (qS1) drop
   THEN

   SSL IF ARGV "-ssl" grepr rows any (f) ssl_connect THEN

\  Running SERVER or SERVER_SSL, listening until ALARM:
   "*" PORT SSL_CONNECT IF SERVER_SSL ELSE SERVER THEN

   "tserv: closing SERVER on port " PORT int$ cat . sp sp date . nl
   serverclose

   exit

#-----------------------------------------------------------------------
 
   Example of starting this program on remote host clacker, using
   port 9878:

      % nohup rsh clacker -n /usr/local/tops/usr/tserv -port 9878 &

   The -n option does not allow rsh to read from standard input on 
   the local machine, so your continued typing will not be seen.

   A remote client also running this program can log into the server 
   as follows.  

      [tops@plunger] ready > "clacker" 9878 CLIENT "S" book       

   Word clients shows the connection to the server on port 9878; the
   notation C>S indicates this is the client connected to a server:

      [tops@plunger] ready > clients
       Server local is off
       Clients:
        socket 3, port 9878, conn C>S, 62.64.138.181 gutter plunger

      [tops@plunger] ready > 

   Word remoteprompt allows the client direct interaction with the 
   server:

      [tops@plunger] ready > S remoteprompt
      tops@socket3 > 

   Here are the clients from the perspective of the server.  The no-
   tation S<C indicates this is the server with client connected:

      tops@socket3 > clients
       Server local is listening on port 9878
       Clients:
        socket 1, port 1362, conn S<C, 64.78.14.209 LOGIN gutter plunger
      tops@socket3 > 

   Word tasks shows the default 12 hour exit alarm running on the
   server:

      tops@socket3 > tasks
       Multitasker tasks:
        exit alarm period 43200 seconds; remaining 43146
      tops@socket3 > 

   Pressing Esc-q returns to the local prompt:

      [tops@plunger] ready >
