home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / slip-cm.zip / CManager.CMD < prev    next >
OS/2 REXX Batch file  |  1994-04-06  |  14KB  |  531 lines

  1. /*************************************************************/
  2. /* CMANAGER                                                  */
  3. /*           This is a TCP/IP connection manager.  It will   */
  4. /*           start or quiesce an Internet connection using   */
  5. /*           SLIP capabilities provided in TCP/IP V2 for     */
  6. /*           OS/2.                                           */
  7. /*************************************************************/
  8.  
  9. /*************************************************************/
  10. /* Queues:                                                   */
  11. /*           CMCommandQueue   (IPC queue for commands)       */
  12. /*                                                           */
  13. /* Commands:                                                 */
  14. /*           START            (activate connection)          */
  15. /*                                                           */
  16. /*           STOP             (deactivate connection after   */
  17. /*                             current activity completed)   */
  18. /*                                                           */
  19. /*           FORCE            (deactivate connection         */
  20. /*                             immediately)                  */
  21. /*                                                           */
  22. /*           EXIT             (terminate the connection      */
  23. /*                             manager)                      */
  24. /*                                                           */
  25. /*************************************************************/
  26.  
  27. trace OFF
  28.  
  29.   signal on HALT name CLEANUP
  30.   signal on FAILURE name CLEANUP
  31.   signal on SYNTAX name CLEANUP
  32.  
  33.   address CMD
  34.  
  35.   call initialize
  36.  
  37.   call emit '001I connection manager started'
  38.  
  39.   do while \stopped()
  40.      command = get_command()
  41.  
  42.      if (command = '') & c_quiesced() then do
  43.         if inactive() then
  44.           call force
  45.         else
  46.           call syssleep 10
  47.      end /* if */
  48.      
  49.      else do
  50.        parse upper var command verb '15'x id '15'x queue
  51.  
  52.        verb = strip(verb)
  53.        id = strip(id)
  54.        queue = strip(queue)
  55.  
  56.        call accept queue
  57.  
  58.        select
  59.          when (verb == 'START') then call do_start(id)
  60.          when (verb == 'STOP') then call do_quiesce(id)
  61.          when (verb == 'FORCE') then call force(id)
  62.          when (verb == 'EXIT') then result = do_exit(id)
  63.          otherwise call emit '002E unknown command:' command
  64.        end /* select */
  65.      end /* if */
  66.  
  67.      G._cs = G._ns
  68.   end /* while */
  69.  
  70. /*************************************************************/
  71. /* Cleanup gracefully after exit or abort                    */
  72. /*************************************************************/
  73.  
  74. CLEANUP:
  75.  
  76.   if \datatype(RC, numeric) then
  77.     error = 'not set'
  78.   else
  79.     error = RC
  80.  
  81.   if \datatype(SIGL, numeric) then
  82.     line = 'not set'
  83.   else
  84.     line = SIGL
  85.  
  86.   call emit '003I terminating, RC='error', line='line
  87.  
  88.   call flush_queue G._cq
  89.  
  90.   if inactive() then
  91.     call force
  92.  
  93. exit (RC <> 0)
  94.  
  95. /*************************************************************/
  96. /* Perform environment initialization at startup             */
  97. /*************************************************************/
  98.  
  99. initialize: procedure expose G.
  100.  
  101.   G._fax = 1
  102.   G._fax_on = '@d:\software\package\faxworks\fxrcv -on 1> nul 2> nul'
  103.   G._fax_off = '@d:\software\package\faxworks\fxrcv -off 1> nul 2> nul'
  104.   
  105.   G._cq = CMCommandQueue
  106.   G._starts = 0
  107.  
  108.   call rxqueue delete, G._cq
  109.   call rxqueue create, G._cq
  110.  
  111.   G._s2r.UP = 0
  112.   G._s2r.QUIESCED = 1
  113.   G._s2r.DOWN = 2
  114.  
  115.   G._r2s.0 = 'UP'
  116.   G._r2s.1 = 'QUIESCED'
  117.   G._r2s.2 = 'DOWN'
  118.  
  119.   G._driver = 'SLIP'
  120.  
  121.   if task_active(G._driver, tasks()) then
  122.     G._cs = G._s2r.UP
  123.   else
  124.     G._cs = G._s2r.DOWN
  125.  
  126.   G._ns = G._cs
  127.  
  128.   call emit '017I connection is initially' state_name(G._cs)
  129.  
  130.   call flush_queue G._cq
  131.  
  132.   call rxfuncadd syssleep, rexxutil, syssleep
  133.  
  134. return
  135.  
  136. /*************************************************************/
  137. /* Get rid of anything sitting in a queue                    */
  138. /*************************************************************/
  139.  
  140. flush_queue: procedure expose G.
  141.  
  142.   parse arg queues
  143.  
  144.   do while (queues <> "")
  145.     parse var queues queue queues
  146.  
  147.     previous = rxqueue(set, queue)
  148.  
  149.     do while (queued() > 0)
  150.       pull line
  151.  
  152.       call emit '004I flushed' '"'line'"' 'from' queue
  153.     end /* while */
  154.  
  155.     previous = rxqueue(set, previous)
  156.   end /* while */
  157.  
  158. return
  159.  
  160. /*************************************************************/
  161. /* Get command from command queue (exit immediately if the   */
  162. /* quiesced state is set and nothing is in the command queue)*/
  163. /*************************************************************/
  164.  
  165. get_command: procedure expose G.
  166.  
  167.   previous = rxqueue(set, G._cq)
  168.  
  169.   if \c_quiesced() | (queued() > 0) then do
  170.     command = linein('QUEUE:')
  171.     command = strip(space(command, 1))
  172.  
  173.     call emit '005I received command' '"'command'"'
  174.   end /* if */  
  175.  
  176.   else do
  177.     command = ""
  178.  
  179.     call emit '006W no command in queue'
  180.   end /* if */
  181.  
  182.   previous = rxqueue(set, previous)
  183.  
  184. return (command)
  185.  
  186. /*************************************************************/
  187. /* Perform EXIT operation                                    */
  188. /*************************************************************/
  189.  
  190. do_exit: procedure expose G.
  191.  
  192.   parse arg id
  193.  
  194.   G._stopped = 1
  195.  
  196. return (0)
  197.  
  198. /*************************************************************/
  199. /* Perform START operation, including starting SLIP driver   */
  200. /*************************************************************/
  201.  
  202. do_start: procedure expose G.
  203.  
  204.   parse arg id
  205.  
  206.   call emit '007I' id(id) 'requesting START'
  207.  
  208.   select
  209.     when c_up() then G._ns = up()
  210.  
  211.     when c_quiesced() then G._ns = up()
  212.  
  213.     otherwise do
  214.       if task_active(G._driver, tasks()) then
  215.         G._ns = up()
  216.       else do
  217.         call emit '008I starting' G._driver'...' 
  218.  
  219.         call fax_off
  220.  
  221.        '@start "IP Connection Driver" /min /n' G._driver
  222.         sRC = RC
  223.  
  224.        '@SLIPWAIT 240 > nul'
  225.         wRC = RC
  226.  
  227.         if (rc == 0) & task_active(G._driver, tasks()) then do 
  228.           G._ns = up()
  229.  
  230.           call emit '009I driver successfully started'
  231.         end /* if */
  232.  
  233.         else do
  234.           G._ns = down()
  235.           
  236.           call emit '010E driver did not start, SRC='sRC', wRC='wRC
  237.         end /* else */
  238.       end /* else */
  239.     end /* otherwise */
  240.   end /* select */
  241.  
  242.   if c_up(G._ns) then
  243.     call new_start
  244.  
  245. return
  246.  
  247. /*************************************************************/
  248. /* Set quiesced mode if no more pending STARTs               */
  249. /*************************************************************/
  250.  
  251. do_quiesce: procedure expose G.
  252.  
  253.   parse arg id
  254.  
  255.   call emit '016I' id(id) 'requesting STOP'
  256.  
  257.   call new_stop
  258.  
  259.   if \starts() then do
  260.     select
  261.       when c_quiesced() then G._ns = quiesced()
  262.       when c_down() then G._ns = down()
  263.       otherwise G._ns = quiesced()
  264.     end
  265.   end /* if */
  266.  
  267. return
  268.  
  269. /*************************************************************/
  270. /* Force an immediate shutdown of the SLIP driver            */
  271. /*************************************************************/
  272.  
  273. force: procedure expose G.
  274.  
  275.   parse arg id
  276.  
  277.   call emit '011I' id(id) 'terminating' G._driver
  278.  
  279.  '@killem' G._driver '> nul'
  280.   result = RC
  281.  
  282.   if \task_active(G._driver, tasks()) then do
  283.     G._ns = down()
  284.  
  285.     call emit '012I driver successfully terminated'
  286.  
  287.     call fax_on
  288.   end /* if */
  289.       
  290.   else do
  291.     G._ns = G._cs
  292.  
  293.     call emit '013E driver not terminated, RC='result
  294.   end /* else */
  295.  
  296. return
  297.  
  298. /*************************************************************/
  299. /* Display and optionally log a message w/ timestamp         */
  300. /*************************************************************/
  301.  
  302. emit: procedure expose G.
  303.  
  304.   parse arg message
  305.  
  306.   say (date(USA) time() message)
  307.  
  308. return 
  309.  
  310. /*************************************************************/
  311. /* Return 1 if there are no active sockets                   */
  312. /*************************************************************/
  313.  
  314. inactive: procedure expose G.
  315.  
  316.    current = rxqueue(get)
  317.  
  318.    call flush_queue current
  319.  
  320.   '@netstat -s | rxqueue' current
  321.  
  322.    sockets = 0
  323.  
  324.    do scan = 1 to queued()
  325.      pull information
  326.       
  327.      parse var information socket type . . host state .
  328.  
  329.      active =,
  330.        (type = 'DGRAM') & (host <> '0.0.0.0') |,
  331.        (type = 'STREAM') & (state <> 'LISTEN')
  332.  
  333.      if (active == 1) then do
  334.        call emit '014I Waiting on' type 'socket' socket 'to' host
  335.  
  336.        sockets = sockets + 1
  337.      end /* if */
  338.    end scan
  339.  
  340.   call emit '115I' sockets 'active sockets'
  341.  
  342. return (sockets <= 0)
  343.  
  344. /*************************************************************/
  345. /* Return 1 if the "stopped" flag has been set               */
  346. /*************************************************************/
  347.  
  348. stopped: procedure expose G.
  349.  
  350. return (G._stopped == 1)
  351.  
  352. /*************************************************************/
  353. /* Return 1 if the specified state is UP                     */
  354. /*************************************************************/
  355.  
  356. c_up: procedure expose G.
  357.  
  358.   parse arg state
  359.  
  360.   if (state = '') then
  361.     state = G._cs
  362.  
  363. return (state = G._s2r.UP)
  364.  
  365. /*************************************************************/
  366. /* Return 1 if the specified state is DOWN                   */
  367. /*************************************************************/
  368.  
  369. c_down: procedure expose G.
  370.  
  371.   parse arg state
  372.  
  373.   if (state = '') then
  374.     state = G._cs
  375.  
  376. return (state = G._s2r.DOWN)
  377.  
  378. /*************************************************************/
  379. /* Return 1 if the specified state is QUIESCED               */
  380. /*************************************************************/
  381.  
  382. c_quiesced: procedure expose G.
  383.  
  384.   parse arg state
  385.  
  386.   if (state = '') then
  387.     state = G._cs
  388.  
  389. return (state = G._s2r.QUIESCED)
  390.  
  391. /*************************************************************/
  392. /* Return the numeric state for UP                           */
  393. /*************************************************************/
  394.  
  395. up: procedure expose G.
  396.  
  397. return (G._s2r.UP)
  398.  
  399. /*************************************************************/
  400. /* Return the numeric state for DOWN                         */
  401. /*************************************************************/
  402.  
  403. down: procedure expose G.
  404.  
  405. return (G._s2r.DOWN)
  406.  
  407. /*************************************************************/
  408. /* Return the numeric state for QUIESCED                     */
  409. /*************************************************************/
  410.  
  411. quiesced: procedure expose G.
  412.  
  413. return (G._s2r.QUIESCED)
  414.  
  415. /*************************************************************/
  416. /* Return a generic program id if the id was omitted         */
  417. /*************************************************************/
  418.  
  419. id: procedure expose G.
  420.  
  421.   parse arg id
  422.  
  423.   if (id = '') then
  424.     id = 'program'
  425.  
  426. return (id)
  427.  
  428. /*************************************************************/
  429. /* Return the name corresponding to a numeric state          */
  430. /*************************************************************/
  431.  
  432. state_name: procedure expose G.
  433.  
  434.   parse arg state
  435.  
  436. return (G._r2s.state)
  437.  
  438. /*************************************************************/
  439. /* Register START request by incrementing active start count */
  440. /*************************************************************/
  441.  
  442. new_start: procedure expose G.
  443.  
  444.   starts = G._starts + 1
  445.  
  446.   call emit '018I' starts 'active START requests'  
  447.  
  448.   G._starts = starts
  449.  
  450. return
  451.  
  452. /*************************************************************/
  453. /* Register STOP request by decrementing active start count  */
  454. /*************************************************************/
  455.  
  456. new_stop: procedure expose G.
  457.  
  458.   starts = G._starts - 1
  459.  
  460.   If (starts < 0) then
  461.     starts = 0
  462.  
  463.   call emit '019I' starts 'active START requests'  
  464.  
  465.   G._starts = starts
  466.  
  467. return
  468.  
  469. /*************************************************************/
  470. /* Return 1 if there are any active START requests           */
  471. /*************************************************************/
  472.  
  473. starts: procedure expose G.
  474.  
  475. return (G._starts > 0)
  476.  
  477. /*************************************************************/
  478. /* Turn on fax receive                                       */
  479. /*************************************************************/
  480.  
  481. fax_on: procedure expose G.
  482.  
  483.   if (G._fax == 1) then do
  484.     address CMD G._fax_on
  485.  
  486.     if (RC == 0) then 
  487.       call emit '020I Fax receive enabled'
  488.     else
  489.       call emit '021W Fax receive not enabled, RC='RC
  490.   end /* if */
  491.  
  492. return
  493.  
  494. /*************************************************************/
  495. /* Turn off fax receive                                      */
  496. /*************************************************************/
  497.  
  498. fax_off: procedure expose G.
  499.  
  500.   if (G._fax == 1) then do
  501.     address CMD G._fax_off
  502.  
  503.     if (RC == 0) then 
  504.       call emit '022I Fax receive DISABLED'
  505.     else
  506.       call emit '023W Fax receive not disabled, RC='RC
  507.   end /* if */
  508.  
  509. return
  510.  
  511. /*************************************************************/
  512. /* Send an acceptance notification to the caller's response  */
  513. /* queue (if any)                                            */
  514. /*************************************************************/
  515.  
  516. accept: procedure expose G.
  517.  
  518.   parse arg response
  519.  
  520.   if (queue <> "") then do
  521.     call emit '024I sending acknowledgement on queue' response
  522.  
  523.     previous = rxqueue(set, response)
  524.  
  525.     queue (1)
  526.  
  527.     call rxqueue set, previous
  528.   end /* if */
  529.  
  530. return
  531.