2021-06-20 04:33:20 +00:00
|
|
|
## nim-ws
|
|
|
|
## Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
## Licensed under either of
|
|
|
|
## * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
## * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
## at your option.
|
|
|
|
## This file may not be copied, modified, or distributed except according to
|
|
|
|
## those terms.
|
|
|
|
|
2021-05-26 17:07:57 +00:00
|
|
|
import
|
|
|
|
std/[strutils],
|
|
|
|
pkg/[chronos, chronicles, stew/byteutils],
|
|
|
|
../ws/[ws, types, frame]
|
|
|
|
|
2021-06-12 00:54:38 +00:00
|
|
|
const
|
|
|
|
clientFlags = {NoVerifyHost, NoVerifyServerName}
|
|
|
|
|
|
|
|
const agent = when defined tls:
|
|
|
|
"nim-ws-tls-client"
|
|
|
|
else:
|
|
|
|
"nim-ws-client"
|
|
|
|
const secure = defined tls
|
|
|
|
|
|
|
|
proc connectServer(path: string): Future[WSSession] {.async.} =
|
|
|
|
let ws = await WebSocket.connect(
|
|
|
|
host = "127.0.0.1",
|
|
|
|
port = Port(9001),
|
|
|
|
path = path,
|
|
|
|
secure=secure,
|
|
|
|
flags=clientFlags
|
|
|
|
)
|
|
|
|
return ws
|
|
|
|
|
|
|
|
proc getCaseCount(): Future[int] {.async.} =
|
2021-05-26 17:07:57 +00:00
|
|
|
var caseCount = 0
|
|
|
|
block:
|
|
|
|
try:
|
2021-06-12 00:54:38 +00:00
|
|
|
let ws = await connectServer("/getCaseCount")
|
2021-05-26 17:07:57 +00:00
|
|
|
let buff = await ws.recv()
|
|
|
|
let dataStr = string.fromBytes(buff)
|
|
|
|
caseCount = parseInt(dataStr)
|
|
|
|
await ws.close()
|
|
|
|
break
|
|
|
|
except WebSocketError as exc:
|
|
|
|
error "WebSocket error", exception = exc.msg
|
|
|
|
except ValueError as exc:
|
|
|
|
error "ParseInt error", exception = exc.msg
|
|
|
|
|
|
|
|
return caseCount
|
|
|
|
|
2021-06-12 00:54:38 +00:00
|
|
|
proc generateReport() {.async.} =
|
2021-05-26 17:07:57 +00:00
|
|
|
try:
|
2021-06-12 07:58:28 +00:00
|
|
|
trace "request autobahn server to generate report"
|
2021-06-12 00:54:38 +00:00
|
|
|
let ws = await connectServer("/updateReports?agent=" & agent)
|
2021-05-26 17:07:57 +00:00
|
|
|
while true:
|
|
|
|
let buff = await ws.recv()
|
|
|
|
if buff.len <= 0:
|
|
|
|
break
|
|
|
|
await ws.close()
|
|
|
|
except WebSocketError as exc:
|
|
|
|
error "WebSocket error", exception = exc.msg
|
|
|
|
|
|
|
|
proc main() {.async.} =
|
2021-06-12 00:54:38 +00:00
|
|
|
let caseCount = await getCaseCount()
|
|
|
|
trace "case count", count=caseCount
|
2021-05-26 17:07:57 +00:00
|
|
|
|
|
|
|
for i in 1..caseCount:
|
2021-06-12 07:58:28 +00:00
|
|
|
trace "runcase", no=i
|
2021-06-12 00:54:38 +00:00
|
|
|
let path = "/runCase?case=$1&agent=$2" % [$i, agent]
|
2021-05-26 17:07:57 +00:00
|
|
|
try:
|
2021-06-12 00:54:38 +00:00
|
|
|
let ws = await connectServer(path)
|
2021-06-12 07:58:28 +00:00
|
|
|
|
|
|
|
while ws.readystate != ReadyState.Closed:
|
|
|
|
# echo back
|
|
|
|
let data = await ws.recv()
|
|
|
|
let opCode = if ws.binary:
|
|
|
|
Opcode.Binary
|
|
|
|
else:
|
|
|
|
Opcode.Text
|
|
|
|
|
|
|
|
if ws.readyState == ReadyState.Closed:
|
|
|
|
break
|
|
|
|
|
|
|
|
await ws.send(data, opCode)
|
|
|
|
|
2021-05-26 17:07:57 +00:00
|
|
|
except WebSocketError as exc:
|
|
|
|
error "WebSocket error", exception = exc.msg
|
|
|
|
|
2021-06-12 00:54:38 +00:00
|
|
|
await generateReport()
|
2021-05-26 17:07:57 +00:00
|
|
|
|
|
|
|
waitFor main()
|