-- Copyright: Mario Schweigler, Adam Sampson (C) 2005-2006
-- Institution: Computing Laboratory, University of Kent, Canterbury, UK
-- Description: Testcase for pony: simple channel communications

-- 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., 51 Franklin Street, Fifth Floor, Boston,
-- MA 02110-1301, USA.

#INCLUDE "course.module"
#INCLUDE "pony.module"
#INCLUDE "trace.inc"

CHAN TYPE THING
  MOBILE RECORD
    CHAN INT c?:
:

PROC simpletest (CHAN BYTE key?, SHARED CHAN BYTE scr!, CHAN BYTE err!)
  MOBILE []BYTE ans.name, app.name, node.name:
  INT node.type, own.node.id, result:
  SHARED PONY.NETHANDLE! net.handle:
  PONY.ERRHANDLE! err.handle:
  INT err.point:
  PONY.MSGHANDLE! msg.handle:
  SEQ
    ans.name := ""
    app.name := "simpletest"
    node.name := ""
    CLAIM scr!
      SEQ
        --{{{  Get node-type
        out.string ("Am I [m]aster or [s]lave (default master)? ", 0, scr!)
        scr ! 255
        BYTE any:
        SEQ
          key ? any
          scr ! any
          scr ! '*n'
          CASE any
            's'
              node.type := PONYC.NODETYPE.SLAVEWAIT
            ELSE
              node.type := PONYC.NODETYPE.MASTERRESET
        --}}}
    --{{{  Startup
    pony.startup.snh.ueh.iep.mh (PONYC.MSGTYPE.STATUSERR,
                                 PONYC.NETTYPE.TCPIP, ans.name,
                                 app.name, node.name, node.type,
                                 own.node.id,
                                 net.handle,
                                 err.handle, err.point,
                                 msg.handle, result)
    CLAIM scr!
      SEQ
        out.string ("Result: ", 0, scr!)
        out.int (result, 0, scr!)
        out.string ("*n", 0, scr!)
    --}}}
    IF
      result = PONYC.RESULT.STARTUP.OK
        PAR
          --{{{  Message outputter
          SEQ
            CLAIM scr!
              out.string ("Starting message outputter.*n", 0, scr!)
            pony.msg.out.so.ue (msg.handle, scr!, err!)
            CLAIM scr!
              out.string ("Message outputter finished.*n", 0, scr!)
          --}}}
          SEQ
            CLAIM scr!
              SEQ
                out.string ("Own node-ID: ", 0, scr!)
                out.int (own.node.id, 0, scr!)
                out.string ("*n", 0, scr!)
            CASE node.type
              PONYC.NODETYPE.MASTERRESET
                THING? svr:
                SEQ
                  C.trace ("Master starting, alloc...")
                  CLAIM net.handle!
                    SEQ
                      C.trace ("1")
                      pony.alloc.us (net.handle, "thing", PONYC.SHARETYPE.SHARED, svr, result)
                      C.trace ("2")
                      C.printarg (svr)
                      --pony.alloc.us (net.handle, "thing2", PONYC.SHARETYPE.SHARED, svr, result)
                      C.trace ("3")
                  C.trace ("... done")
                  IF
                    DEFINED svr
                      SKIP
                    TRUE
                      SEQ
                        C.trace ("svr not defined")
                        STOP
                  CLAIM scr!
                    SEQ
                      out.string ("Alloc result: ", 0, scr!)
                      out.int (result, 0, scr!)
                      out.string ("*n", 0, scr!)
                  ASSERT (result = PONYC.RESULT.ALLOC.OK)
                  SEQ i = 0 FOR 10
                    INT n:
                    SEQ
                      C.trace ("Input...")
                      svr[c] ? n
                      C.trace ("... done")
                      CLAIM scr!
                        SEQ
                          out.string ("Input got ", 0, scr!)
                          out.int (n, 0, scr!)
                          out.string (".*n", 0, scr!)
                  C.trace ("Master done")
              PONYC.NODETYPE.SLAVEWAIT
                SHARED THING! cli:
                SEQ
                  C.trace ("Slave starting, alloc...")
                  CLAIM net.handle!
                    pony.alloc.sc (net.handle, "thing", PONYC.SHARETYPE.UNSHARED, cli, result)
                  C.trace ("... done")
                  IF
                    DEFINED cli
                      SKIP
                    TRUE
                      SEQ
                        C.trace ("cli not defined")
                        STOP
                  CLAIM scr!
                    SEQ
                      out.string ("Alloc result: ", 0, scr!)
                      out.int (result, 0, scr!)
                      out.string ("*n", 0, scr!)
                  ASSERT (result = PONYC.RESULT.ALLOC.OK)
                  SEQ i = 0 FOR 10
                    SEQ
                      C.trace ("Output...")
                      CLAIM cli!
                        SEQ
                          C.trace ("... claimed { ...")
                          cli[c] ! i
                          C.trace ("... } ...")
                      C.trace ("... done")
                  C.trace ("Slave done")
              ELSE
                STOP
            CHAN BOOL sync:
            PAR
              --{{{  Shutdown
              SEQ
                CLAIM scr!
                  out.string ("Calling shutdown process.*n", 0, scr!)
                CLAIM net.handle
                  pony.shutdown (net.handle)
                CLAIM scr!
                  out.string ("Shutdown process finished.*n", 0, scr!)
                sync ! TRUE
              --}}}
              --{{{  Errors and shutdown of error-handler
              TIMER tim:
              INT t, result:
              MOBILE []PONY.ERROR err.array:
              SEQ
                tim ? t
                PRI ALT
                  BOOL any:
                  sync ? any
                    CLAIM scr!
                      out.string ("Shutdown sync.*n", 0, scr!)
                  tim ? AFTER t PLUS 10000000
                    CLAIM scr!
                      out.string ("Shutdown timeout.*n", 0, scr!)
                CLAIM scr!
                  out.string ("Getting errors after initial error-point.*n", 0, scr!)
                INITIAL MOBILE []INT null.int.array IS MOBILE [0]INT:
                pony.err.get.errs.after (err.handle, err.point,
                                         TRUE, TRUE, TRUE, null.int.array,
                                         err.array, result)
                CLAIM scr!
                  SEQ
                    out.string ("Number of errors: ", 0, scr!)
                    out.int ((SIZE err.array), 0, scr!)
                    out.string ("*n", 0, scr!)
                    SEQ i = 0 FOR (SIZE err.array)
                      SEQ
                        out.string ("error: ans.concerned:         ", 0, scr!)
                        out.bool (err.array[i][ans.concerned], 0, scr!)
                        out.string ("*n       master.concerned:      ", 0, scr!)
                        out.bool (err.array[i][master.concerned], 0, scr!)
                        out.string ("*n       remote.node.concerned: ", 0, scr!)
                        out.bool (err.array[i][remote.node.concerned], 0, scr!)
                        out.string ("*n       remote.node.id:        ", 0, scr!)
                        out.int (err.array[i][remote.node.id], 0, scr!)
                        out.string ("*n       err.code:              ", 0, scr!)
                        out.int (err.array[i][err.code], 0, scr!)
                        out.string ("*n", 0, scr!)
                    out.string ("Shutting down error-handler.*n", 0, scr!)
                pony.err.shutdown (err.handle)
                CLAIM scr!
                  out.string ("Error-handler shutdown finished.*n", 0, scr!)
              --}}}
      TRUE
        SKIP
    --{{{  Exit message
    CLAIM scr!
      out.string ("Finished! :)*n", 0, scr!)
    --}}}
:

