#!/usr/bin/env tclsh
source streamtestlib.tcl

# Define records, protocol and startup (text goes to files)
# The asynPort "device" is connected to a network TCP socket
# Talk to the socket with send/receive/assure
# Send commands to the ioc shell with ioccmd

set records {
    record (stringin, "DZ:request")
    {
        field (DTYP, "stream")
        field (INP,  "@test.proto read device")
    }
    record (stringout, "DZ:echo")
    {
        field (DTYP, "stream")
        field (DOL,  "DZ:request")
        field (OMSL, "closed_loop")
        field (OUT,  "@test.proto printstr device")
    }
    record (stringin, "DZ:spy")
    {
        field (DTYP, "stream")
        field (INP,  "@test.proto readintr device")
        field (SCAN, "I/O Intr")
        field (FLNK, "DZ:count")
    }
    record (calc, "DZ:count")
    {
        field (INPA, "DZ:count")
        field (CALC, "A+1")
    }
    record (longout, "DZ:countout")
    {
        field (DTYP, "stream")
        field (DOL,  "DZ:count")
        field (OMSL, "closed_loop")
        field (OUT,  "@test.proto printnum device")
    }
    record (stringout, "DZ:stringout")
    {
        field (DTYP, "stream")
        field (DOL,  "DZ:spy")
        field (OMSL, "closed_loop")
        field (OUT,  "@test.proto printstr device")
    }
}

set protocol {
    Terminator = LF;
    read {extraInput=ignore; InTerminator = ""; out "Give input"; in "%/.*253.*/"; }
    readintr {extraInput=ignore; in "%39c"; }
    printnum {out "%d";}
    printstr {out "%s";}
}

set startup {
    var streamDebug 1
}

set debug 0
set rep 500

startioc

ioccmd {dbpf "DZ:request.PROC" 1}
assure "Give input\n"
for {set i 1} {$i <= $rep} {incr i} {
    append output "This is line $i.\n"
}
send $output
after 2000
ioccmd {dbpf "DZ:echo.PROC" 1}
assure "This is line 253.\n"
ioccmd {dbpf "DZ:stringout.PROC" 1}
assure "This is line $rep.\n"
ioccmd {dbpf "DZ:countout.PROC" 1}
assure "$rep\n"

finish
