This repository has been archived by the owner on Oct 15, 2022. It is now read-only.
/
srunner.go
70 lines (63 loc) · 1.85 KB
/
srunner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Implementation of an echo server based on LSP.
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"example.com/lsp"
"example.com/lspnet"
)
var (
port = flag.Int("port", 9999, "port number")
readDrop = flag.Int("rdrop", 0, "network read drop percent")
writeDrop = flag.Int("wdrop", 0, "network write drop percent")
epochLimit = flag.Int("elim", lsp.DefaultEpochLimit, "epoch limit")
epochMillis = flag.Int("ems", lsp.DefaultEpochMillis, "epoch duration (ms)")
windowSize = flag.Int("wsize", lsp.DefaultWindowSize, "window size")
showLogs = flag.Bool("v", false, "show srunner logs")
)
func init() {
// Display time, file, and line number in log messages.
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
}
func main() {
flag.Parse()
if !*showLogs {
log.SetOutput(ioutil.Discard)
} else {
lspnet.EnableDebugLogs(true)
}
lspnet.SetServerReadDropPercent(*readDrop)
lspnet.SetServerWriteDropPercent(*writeDrop)
params := &lsp.Params{
EpochLimit: *epochLimit,
EpochMillis: *epochMillis,
WindowSize: *windowSize,
}
fmt.Printf("Starting server on port %d...\n", *port)
srv, err := lsp.NewServer(*port, params)
if err != nil {
fmt.Printf("Failed to start Server on port %d: %s\n", *port, err)
return
}
fmt.Println("Server waiting for clients...")
runServer(srv)
}
func runServer(srv lsp.Server) {
for {
// Read message from client.
if id, payload, err := srv.Read(); err != nil {
fmt.Printf("Client %d has died: %s\n", id, err)
} else {
log.Printf("Server received '%s' from client %d\n", string(payload), id)
// Echo message back to client.
if err := srv.Write(id, payload); err != nil {
// Print an error message and continue...
fmt.Printf("Server failed to write to connection %d: %s\n", id, err)
} else {
log.Printf("Server wrote '%s' to client %d\n", string(payload), id)
}
}
}
}