--
--	socktest.occ -- testing the socket channel stuff
--	Copyright (C) 2002 Fred Barnes <frmb2@ukc.ac.uk>
--
--	This program is free software; you can redistribute it and/or modify
--	it under the terms of the GNU General Public License as published by
--	the Free Software Foundation; either version 2 of the License, or
--	(at your option) any later version.
--
--	This program is distributed in the hope that it will be useful,
--	but WITHOUT ANY WARRANTY; without even the implied warranty of
--	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--	GNU General Public License for more details.
--
--	You should have received a copy of the GNU General Public License
--	along with this program; if not, write to the Free Software
--	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
--

--{{{  includes, uses
#INCLUDE "udc.module"
#INCLUDE "course.module"
--}}}  

--{{{  PROC socktest (CHAN BYTE kyb?, scr!, err!)
-- this program deliberately suffers from deadlock, which can occur
-- if the TCP "echo" service is used (port 7) and data is blased in
-- at high speed.  The channel write will block, and stay blocked.
-- the "echo" program launched by inetd is, at this point, blocked
-- trying to output back down the socket, on which we are not listening.
PROC socktest (CHAN BYTE kyb?, scr!, err!)
  INT addr:
  [64]BYTE hostname:
  INT hostlen:
  INT port:
  SEQ
    out.string ("socktest: testing the socket channel*n", 0, scr!)
    out.string ("hostname ?: ", 0, scr!)
    scr ! FLUSH
    in.string (hostname, hostlen, SIZE hostname, kyb?, scr!)
    out.string ("*nport ?: ", 0, scr!)
    scr ! FLUSH
    in.int (port, 5, kyb?, scr!)
    scr ! '*n'
    -- allocate channel
    udc.sockchan.alloc ([hostname FOR hostlen], port, addr)
    out.string ("socktest: connected to ", 0, scr!)
    out.string ([hostname FOR hostlen], 0, scr!)
    out.string (" port ", 0, scr!)
    out.int (port, 0, scr!)
    scr ! '*n'
    PLACED CHAN BYTE c addr:
    WHILE TRUE
      BYTE b:
      PRI ALT
        --{{{  socket -> screen
        c ? b
          SEQ
            scr ! b
            scr ! FLUSH
        --}}}  
        --{{{  keyboard -> socket
        kyb ? b
          c ! b
        --}}}  
:
--}}}  
