2020-04-08 15:22:31 +03:00
|
|
|
module main
|
|
|
|
|
2020-04-26 14:49:31 +03:00
|
|
|
import net.websocket
|
|
|
|
import eventbus
|
|
|
|
import readline
|
2020-04-08 15:22:31 +03:00
|
|
|
|
|
|
|
const (
|
|
|
|
eb = eventbus.new()
|
|
|
|
)
|
2020-04-08 22:21:58 +03:00
|
|
|
|
2020-05-28 01:38:54 +03:00
|
|
|
#flag -I @VROOT/vlib/net/websocket/examples
|
2020-04-08 15:22:31 +03:00
|
|
|
#include "utf8.h"
|
|
|
|
fn C.utf8_validate_str() bool
|
2020-04-08 22:21:58 +03:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// println(sss)
|
|
|
|
/*
|
|
|
|
for sss in 0..10 {
|
2020-04-08 15:22:31 +03:00
|
|
|
mut bm := benchmark.new_benchmark()
|
|
|
|
for i in 0..10000 {
|
|
|
|
for a, t in tests {
|
|
|
|
ss := ws.utf8_validate(t.str, t.len)
|
|
|
|
if !ss {
|
|
|
|
panic("failed")
|
|
|
|
}
|
|
|
|
//println("${a}:${ss}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bm.stop()
|
2020-04-08 22:21:58 +03:00
|
|
|
println( bm.total_message('remarks about the benchmark') )
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
mut ws := websocket.new('ws://localhost:9001/getCaseCount')
|
2020-06-01 20:37:24 +03:00
|
|
|
// ws.nonce_size = 16 // try this, if it does not work with your server
|
2020-04-08 22:21:58 +03:00
|
|
|
// defer { }
|
|
|
|
ws.subscriber.subscribe('on_open', on_open)
|
|
|
|
ws.subscriber.subscribe('on_message', on_message)
|
|
|
|
ws.subscriber.subscribe('on_error', on_error)
|
|
|
|
ws.subscriber.subscribe('on_close', on_close)
|
|
|
|
// go
|
2020-04-08 20:44:30 +03:00
|
|
|
ws.connect()
|
|
|
|
ws.read()
|
2020-04-08 22:21:58 +03:00
|
|
|
// time.usleep(2000000)
|
|
|
|
// go ws.listen()
|
|
|
|
// term.erase_clear()
|
|
|
|
/*
|
|
|
|
text := read_line("[client]:")
|
|
|
|
if text == "close" {
|
2020-04-08 15:22:31 +03:00
|
|
|
ws.close(1005, "done")
|
|
|
|
time.usleep(1000000)
|
|
|
|
exit(0)
|
|
|
|
}
|
2020-04-08 22:21:58 +03:00
|
|
|
ws.write(text, .text_frame)
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
time.usleep(1000000)
|
|
|
|
ws.read()
|
|
|
|
*/
|
|
|
|
// ws.close(1005, "done") //
|
|
|
|
// ws.close(1005, "done")
|
|
|
|
// read_line("wait")
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn read_line(text string) string {
|
|
|
|
mut r := readline.Readline{}
|
2020-04-08 22:21:58 +03:00
|
|
|
mut output := r.read_line(text + ' ') or {
|
2020-04-08 15:22:31 +03:00
|
|
|
panic(err)
|
|
|
|
}
|
2020-04-08 22:21:58 +03:00
|
|
|
output = output.replace('\n', '')
|
2020-04-08 15:22:31 +03:00
|
|
|
if output.len <= 0 {
|
2020-04-08 22:21:58 +03:00
|
|
|
return ''
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2020-05-28 01:38:54 +03:00
|
|
|
fn on_open(sender voidptr, ws &websocket.Client, x voidptr) {
|
2020-04-08 22:21:58 +03:00
|
|
|
println('websocket opened.')
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
|
|
|
|
2020-05-28 01:38:54 +03:00
|
|
|
fn on_message(sender voidptr, mut ws websocket.Client, msg websocket.Message) {
|
2020-04-08 22:21:58 +03:00
|
|
|
println('Message recieved. Sending it back.')
|
2020-05-28 01:38:54 +03:00
|
|
|
typ := msg.opcode
|
|
|
|
if typ == .text_frame {
|
2020-04-08 22:21:58 +03:00
|
|
|
if ws.uri.ends_with('getCaseCount') {
|
2020-05-28 01:38:54 +03:00
|
|
|
num := int(msg.payload)
|
2020-04-08 22:21:58 +03:00
|
|
|
ws.close(1005, 'done')
|
2020-04-08 15:22:31 +03:00
|
|
|
start_tests(mut ws, num)
|
|
|
|
return
|
|
|
|
}
|
2020-05-28 01:38:54 +03:00
|
|
|
// println("Message: $msg")
|
|
|
|
ws.write(msg.payload, msg.payload_len, .text_frame)
|
2020-04-08 15:22:31 +03:00
|
|
|
} else {
|
2020-04-08 22:21:58 +03:00
|
|
|
println('Binary message.')
|
2020-05-28 01:38:54 +03:00
|
|
|
ws.write(msg.payload, msg.payload_len, .binary_frame)
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 22:21:58 +03:00
|
|
|
fn start_tests(ws mut websocket.Client, num int) {
|
2020-04-08 15:22:31 +03:00
|
|
|
for i := 1; i < num; i++ {
|
2020-04-08 22:21:58 +03:00
|
|
|
println('Running test: ' + i.str())
|
|
|
|
ws.uri = 'ws://localhost:9001/runCase?case=${i.str()}&agent=vws/1.0a'
|
2020-04-08 20:44:30 +03:00
|
|
|
if ws.connect() >= 0 {
|
|
|
|
ws.listen()
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
|
|
|
}
|
2020-04-08 22:21:58 +03:00
|
|
|
println('Done!')
|
|
|
|
ws.uri = 'ws://localhost:9001/updateReports?agent=vws/1.0a'
|
2020-04-08 20:44:30 +03:00
|
|
|
if ws.connect() >= 0 {
|
|
|
|
ws.read()
|
2020-04-08 22:21:58 +03:00
|
|
|
ws.close(1000, 'disconnecting...')
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
|
2020-05-28 01:38:54 +03:00
|
|
|
fn on_close(sender voidptr, ws &websocket.Client, x voidptr) {
|
2020-04-08 22:21:58 +03:00
|
|
|
println('websocket closed.')
|
2020-04-08 15:22:31 +03:00
|
|
|
}
|
|
|
|
|
2020-05-28 01:38:54 +03:00
|
|
|
fn on_error(sender voidptr, ws &websocket.Client, x voidptr) {
|
2020-04-08 22:21:58 +03:00
|
|
|
println('we have an error.')
|
|
|
|
}
|