-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresponseHandling.go
61 lines (57 loc) · 1.79 KB
/
responseHandling.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
package main
import (
"fmt"
"github.com/gorilla/websocket"
"github.com/richiMarchi/latency-tester/enhanced-client/client/serialization/protobuf"
"google.golang.org/protobuf/proto"
"log"
"os"
"strconv"
"strings"
"time"
)
func readDispatcher(
c *websocket.Conn,
done chan struct{},
toolRtt *os.File,
reset chan *websocket.Conn) {
for {
// Read all incoming messages
_, message, err := c.ReadMessage()
if err != nil {
if strings.Contains(err.Error(), "1000") {
fmt.Println("read: ", err)
close(done)
return
} else {
log.Println("Reader thread: waiting for connection to reset...")
c = <-reset
log.Println("Reader thread: connection reset signaled")
continue
}
}
handleMessage(&message, toolRtt)
}
}
// Deserialize the message received and store data in the file
func handleMessage(message *[]byte, toolRtt *os.File) {
jsonMap := &protobuf.DataJSON{}
_ = proto.Unmarshal(*message, jsonMap)
if jsonMap.Id == 0 {
log.Println("Connection Reset")
toolRtt.WriteString(strconv.FormatInt(jsonMap.ClientTimestamp.AsTime().UnixNano(), 10))
toolRtt.WriteString(",")
toolRtt.WriteString(strconv.FormatInt(jsonMap.ServerTimestamp.AsTime().UnixNano(), 10))
toolRtt.WriteString(",-1\n")
} else {
latency := getTimestamp().Sub(jsonMap.ClientTimestamp.AsTime())
fmt.Printf("%d.\t%f ms\n", jsonMap.Id, float64(latency.Nanoseconds())/float64(time.Millisecond.Nanoseconds()))
toolRtt.WriteString(strconv.FormatInt(jsonMap.ClientTimestamp.AsTime().UnixNano(), 10))
toolRtt.WriteString(",")
toolRtt.WriteString(strconv.FormatInt(jsonMap.ServerTimestamp.AsTime().UnixNano(), 10))
toolRtt.WriteString(",")
toolRtt.WriteString(strconv.FormatFloat(
float64(latency.Nanoseconds())/float64(time.Millisecond.Nanoseconds()), 'f', -1, 64))
toolRtt.WriteString("\n")
}
}