--
--	socktcpcli.occ - simple TCP client
--	Copyright (C) 2000-2008 Fred Barnes <frmb@kent.ac.uk>
--      Modification (C) 2008 Tom Carlson
--
--	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.
--

#INCLUDE "sock.module"
#INCLUDE "course.module"

--{{{  PROC random.writer (SOCKET sock, INT res)
PROC random.writer (SOCKET sock, INT res)
  VAL [][]BYTE messages IS ["Hello, new world (from occam)  ",
    "Just testing the sockets stuff ",
    "A slice of pizza please        ",
    "No, I don*'t have any salt.     ",
    "A great man once said something",
    "It takes two to tango          ",
    "Stop that, it*'s silly          ",
    "This socket thing works ?      ",
    "Eric the half-a-bee            ",
    "                               "]:
  TIMER tim:
  INT seed, i:
  SEQ
    tim ? seed
    IF
      seed < 0
        seed := (-seed)
      seed = 0
        seed := 1
      TRUE
        SKIP
    i, res := 0, 1
    WHILE (i < (SIZE messages)) AND (res > 0)
      INT val, t:
      SEQ
        socket.write (sock, messages[i], res)
        val, seed := random (3000, seed)
        IF
          val < 250
            val := 250
          TRUE
            SKIP
        tim ? t
        tim ? AFTER (t PLUS (val * 1000))
        i := (i + 1)
    res := 0
:
--}}} 

PROC sock.tcp.cli (CHAN BYTE kyb?, scr!, err!) --{{{
  --{{{  Start here
  [128]BYTE hostname:
  INT host.len, res:
  INT remote.addr, remote.port:
  SOCKET sock:
  SEQ
    --{{{  get host/port from the user
    out.string ("Hostname ?: ", 0, scr!)
    scr ! FLUSH
    in.string (hostname, host.len, (SIZE hostname), kyb?, scr!)
    scr ! '*n'
    socket.addr.of.host ([hostname FOR host.len], remote.addr, res)
    IF
      res < 0
        SEQ
          out.string ("Unable to determine address of ", 0, scr!)
          out.string ([hostname FOR host.len], 0, scr!)
          out.string ("*n", 0, scr!)
          STOP
      TRUE
        SKIP
    out.string ("Port ?: ", 0, scr!)
    scr ! FLUSH
    in.int (remote.port, 5, kyb?, scr!)
    scr ! '*n'
    --}}}  
    --{{{  create and connect socket
    socket.create.connect.tcp (sock, remote.addr, remote.port, res)
    IF
      res < 0
        SEQ
          out.string ("Unable to connect to ", 0, scr!)
          out.string ([hostname FOR host.len], 0, scr!)
          out.string (" port ", 0, scr!)
          out.int (remote.port, 0, scr!)
          out.string ("*n", 0, scr!)
          STOP
      TRUE
        SKIP
    --}}}  
    --{{{  loop writing messages
    res := 0
    WHILE res = 0
      random.writer (sock, res)
    --}}}  
    --{{{  close socket
    socket.close (sock)
    --}}}  
  --}}}  
:
--}}}  

