--
--	netbar.occ -- client library for netbar
--	Copyright (C) 2005 Fred Barnes <frmb@kent.ac.uk>
--	                   Adam Sampson <ats1@kent.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.
--

#INCLUDE "sock.module"
#INCLUDE "netbar.inc"

PROC netbar.new (VAL []BYTE host, VAL INT port, RESULT NETBAR n)
  INT ret, addr:
  SEQ
    socket.addr.of.host (host, addr, ret)
    ASSERT (ret = 0)
    socket.create.connect.tcp (n[sock], addr, port, ret)
    ASSERT (ret = 0)
    [4]BYTE buf RETYPES n[id]:
    socket.fullread (n[sock], buf, SIZE buf, ret)
    ASSERT (ret = 4)
    [4]BYTE buf RETYPES n[max]:
    socket.fullread (n[sock], buf, SIZE buf, ret)
    ASSERT (ret = 4)
:

PROC netbar.sync (NETBAR n, SHARED CHAN BYTE err!)
  INT ret:
  SEQ
    CLAIM err!
      err ! '['
    socket.fullwrite (n[sock], "x", ret)
    IF
      ret = 1
        SKIP
      TRUE
        SEQ
          CLAIM err!
            err ! 'A'
          STOP
    ASSERT (ret = 1)
    CLAIM err!
      err ! '|'
    [1]BYTE buf:
    socket.fullread (n[sock], buf, 1, ret)
    IF
      ret = 1
        SKIP
      TRUE
        SEQ
          CLAIM err!
            err ! 'B'
          STOP
    ASSERT (ret = 1)
    CLAIM err!
      err ! ']'
:

PROC netbar.close (NETBAR n)
  socket.close (n[sock])
:
