Skip to content

Commit af9b99b

Browse files
authored
#20 Switch to using msgpack binary format instead of json (#97)
1 parent 5ca5968 commit af9b99b

File tree

4 files changed

+28
-24
lines changed

4 files changed

+28
-24
lines changed

cse/observers.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import "C"
99
import (
1010
"cse-server-go/structs"
1111
"errors"
12-
"math"
1312
)
1413

1514
func createObserver() (observer *C.cse_observer) {
@@ -40,18 +39,6 @@ func observerDestroy(observer *C.cse_observer) {
4039
C.cse_observer_destroy(observer)
4140
}
4241

43-
func uglyNanFix(value C.double) interface{} {
44-
floatValue := float64(value)
45-
if math.IsNaN(floatValue) {
46-
return "NaN"
47-
} else if math.IsInf(floatValue, 1) {
48-
return "+Inf"
49-
} else if math.IsInf(floatValue, -1) {
50-
return "-Inf"
51-
}
52-
return floatValue
53-
}
54-
5542
func observerGetReals(observer *C.cse_observer, variables []structs.Variable, slaveIndex int) (realSignals []structs.Signal) {
5643
var realValueRefs []C.cse_variable_index
5744
var realVariables []structs.Variable
@@ -75,7 +62,7 @@ func observerGetReals(observer *C.cse_observer, variables []structs.Variable, sl
7562
Name: realVariables[k].Name,
7663
Causality: realVariables[k].Causality,
7764
Type: realVariables[k].Type,
78-
Value: uglyNanFix(realOutVal[k]),
65+
Value: realOutVal[k],
7966
}
8067
}
8168
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ require (
44
github.com/gobuffalo/packr v1.22.0
55
github.com/gorilla/mux v1.7.0
66
github.com/gorilla/websocket v1.4.0
7-
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd
7+
github.com/ugorji/go/codec v1.1.7
88
)

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,11 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
315315
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
316316
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
317317
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
318+
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
319+
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
318320
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
321+
github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs=
322+
github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY=
319323
github.com/unrolled/secure v0.0.0-20180918153822-f340ee86eb8b/go.mod h1:mnPT77IAdsi/kV7+Es7y+pXALeV3h7G6dQF6mNYjcLA=
320324
github.com/unrolled/secure v0.0.0-20181005190816-ff9db2ff917f/go.mod h1:mnPT77IAdsi/kV7+Es7y+pXALeV3h7G6dQF6mNYjcLA=
321325
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
@@ -348,8 +352,6 @@ golang.org/x/net v0.0.0-20181106065722-10aee1819953/go.mod h1:mL1N/T3taQHkDXs73r
348352
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
349353
golang.org/x/net v0.0.0-20181207154023-610586996380/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
350354
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
351-
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd h1:HuTn7WObtcDo9uEEU7rEqL0jYthdXAmZ6PP+meazmaU=
352-
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
353355
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
354356
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
355357
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw=

server/websockets.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,38 @@ package server
22

33
import (
44
"cse-server-go/structs"
5-
"encoding/json"
65
"github.com/gorilla/websocket"
6+
"github.com/ugorji/go/codec"
77
"io"
88
"log"
99
"net/http"
10+
"reflect"
1011
)
1112

1213
type JsonRequest struct {
1314
Command []string `json:"command,omitempty"`
14-
Module string `json:"module,omitempty"`
15-
Modules bool `json:"modules,omitempty"`
16-
Connections bool `json:"connections,omitempty"`
1715
}
1816

1917
var upgrader = websocket.Upgrader{CheckOrigin: func(r *http.Request) bool { return true },}
2018

2119
func commandLoop(command chan []string, conn *websocket.Conn) {
20+
var (
21+
mh codec.MsgpackHandle
22+
)
23+
mh.MapType = reflect.TypeOf(map[string]interface{}(nil))
24+
decoder := codec.NewDecoder(nil, &mh)
25+
2226
for {
2327
data := JsonRequest{}
2428
_, r, err := conn.NextReader()
2529
if err != nil {
2630
log.Println("read error:", err)
2731
break
2832
}
29-
err = json.NewDecoder(r).Decode(&data)
33+
34+
decoder.Reset(r)
35+
err = decoder.Decode(&data)
36+
3037
if err == io.EOF {
3138
// One value is expected in the message.
3239
log.Println("Message was EOF:", err, data)
@@ -40,11 +47,19 @@ func commandLoop(command chan []string, conn *websocket.Conn) {
4047
}
4148

4249
func stateLoop(state chan structs.JsonResponse, conn *websocket.Conn) {
50+
var (
51+
mh codec.MsgpackHandle
52+
)
53+
mh.MapType = reflect.TypeOf(map[string]interface{}(nil))
54+
encoder := codec.NewEncoder(nil, &mh)
4355
for {
4456
latestState := <-state
45-
err := conn.WriteJSON(latestState)
57+
58+
w, err := conn.NextWriter(2)
59+
encoder.Reset(w)
60+
err = encoder.Encode(latestState)
4661
if err != nil {
47-
log.Println("write:", err)
62+
log.Println("write error:", err)
4863
log.Println("latestState:", latestState)
4964
break
5065
}

0 commit comments

Comments
 (0)