# This script tests using the shell command asynOctetWriteRead to write/read to ipEchoServer

< envPaths

dbLoadDatabase("../../dbd/testIPServer.dbd")
testIPServer_registerRecordDeviceDriver(pdbbase)

#The following command starts a server on port 5001
drvAsynIPServerPortConfigure("P5001","localhost:5001",2,0,0,0)

asynSetTraceIOMask("P5001",0,0x2)
#asynSetTraceMask("P5001",0,0xff)

# The following command creates an echo driver that listens for connections on port 5001
# and echoes back all strings received.
# This can be accessed for example with "telnet localhost 5001"
ipEchoServer("P5001", -1)

# Create a client port that connects to the echo server
drvAsynIPPortConfigure("client5001","localhost:5001",0,0,0)

asynSetTraceIOMask("client5001",0,0x2)
#asynSetTraceMask("client5001",0,255)
asynOctetSetOutputEos("client5001",0,"\r\n")
asynOctetSetInputEos("client5001",0,"\r\n")

# Connect to that port from iocsh
asynOctetConnect("client5001","client5001")

# Write to the port, should get echo back
asynOctetWriteRead("client5001","hello world")

# Create an asynPortDriver connected to that client port
# This port driver implements asynInt32, asynFloat64, and asynOctet interfaces.
# The writeInt32 function converts a number to a string, sends it to the echo server,
# reads back the response string, converts to a number and divides by 2 for an input record.
# The writeFloat64 function converts a number to a string, sends it to the echo server,
# reads back the response string, converts to a number and divides by 3.0 for an input record.
# The writeOctet function sends the string to the echo server,
# reads back the response string, and converts to upper case for an input record.
asynPortTestConfigure("AP5001","client5001")
asynSetTraceIOMask("AP5001",0,0x2)
#asynSetTraceMask("AP5001",0,255)

dbLoadRecords("../../db/testIPServer.db", "P=testIPServer:")
dbLoadRecords("../../db/asynPortTest.db", "P=testIPServer:,PORT=AP5001")

iocInit()

# Sleep so the next operations are after everything above is done, so we can decipher asynTrace output
epicsThreadSleep(5)
asynOctetConnect AP5001 AP5001 0 1 80 OCTET_DATA
asynOctetWrite AP5001 "Hello again world"
asynOctetRead AP5001

