Skip to content

Commit

Permalink
Merge branch 'json_time2' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
jaekwon committed Sep 15, 2015
2 parents 47eee5d + 726f547 commit 7876808
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
20 changes: 16 additions & 4 deletions rpc/core_client/ws_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core_client
import (
"net/http"
"strings"
"time"

"github.com/tendermint/tendermint/Godeps/_workspace/src/github.com/gorilla/websocket"
. "github.com/tendermint/tendermint/common"
Expand All @@ -12,8 +13,11 @@ import (
"github.com/tendermint/tendermint/wire"
)

const wsEventsChannelCapacity = 10
const wsResultsChannelCapacity = 10
const (
wsEventsChannelCapacity = 10
wsResultsChannelCapacity = 10
wsWriteTimeoutSeconds = 10
)

type WSClient struct {
QuitService
Expand Down Expand Up @@ -53,6 +57,14 @@ func (wsc *WSClient) dial() error {
if err != nil {
return err
}
// Set the ping/pong handlers
con.SetPingHandler(func(m string) error {
con.WriteControl(websocket.PongMessage, []byte(m), time.Now().Add(time.Second*wsWriteTimeoutSeconds))
return nil
})
con.SetPongHandler(func(m string) error {
return nil
})
wsc.Conn = con
return nil
}
Expand All @@ -65,14 +77,14 @@ func (wsc *WSClient) receiveEventsRoutine() {
for {
_, data, err := wsc.ReadMessage()
if err != nil {
log.Info(Fmt("WSClient failed to read message: %v", err))
log.Info("WSClient failed to read message", "error", err, "data", string(data))
wsc.Stop()
break
} else {
var response ctypes.Response
wire.ReadJSON(&response, data, &err)
if err != nil {
log.Info(Fmt("WSClient failed to parse message: %v", err))
log.Info("WSClient failed to parse message", "error", err)
wsc.Stop()
break
}
Expand Down
8 changes: 4 additions & 4 deletions wire/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ var (
)

const (
rfc2822 = "Mon Jan 02 15:04:05 -0700 2006"
iso8601 = "2006-01-02T15:04:05.000Z" // forced microseconds
)

// NOTE: do not access typeInfos directly, but call GetTypeInfo()
Expand Down Expand Up @@ -731,7 +731,7 @@ func readReflectJSON(rv reflect.Value, rt reflect.Type, o interface{}, err *erro
return
}
log.Info(Fmt("Read time: %v", str))
t, err_ := time.Parse(rfc2822, str)
t, err_ := time.Parse(iso8601, str)
if err_ != nil {
*err = err_
return
Expand Down Expand Up @@ -908,8 +908,8 @@ func writeReflectJSON(rv reflect.Value, rt reflect.Type, w io.Writer, n *int64,
case reflect.Struct:
if rt == timeType {
// Special case: time.Time
t := rv.Interface().(time.Time)
str := t.Format(rfc2822)
t := rv.Interface().(time.Time).UTC()
str := t.Format(iso8601)
jsonBytes, err_ := json.Marshal(str)
if err_ != nil {
*err = err_
Expand Down
7 changes: 4 additions & 3 deletions wire/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ var _ = RegisterInterface(
ConcreteType{&Viper{}, AnimalTypeViper},
)

// TODO: add assertions here ...
func TestAnimalInterface(t *testing.T) {
var foo Animal

Expand Down Expand Up @@ -100,7 +101,7 @@ func constructBasic() interface{} {
SimpleStruct{
String: "String",
Bytes: []byte("Bytes"),
Time: time.Unix(123, 0),
Time: time.Unix(123, 456789999),
},
}
return cat
Expand All @@ -118,8 +119,8 @@ func validateBasic(o interface{}, t *testing.T) {
if string(cat.Bytes) != "Bytes" {
t.Errorf("Expected cat.Bytes == 'Bytes', got %X", cat.Bytes)
}
if cat.Time.Unix() != 123 {
t.Errorf("Expected cat.Time == 'Unix(123)', got %v", cat.Time)
if cat.Time.UnixNano() != 123456000000 { // Only milliseconds
t.Errorf("Expected cat.Time.UnixNano() == 123456000000, got %v", cat.Time.UnixNano())
}
}

Expand Down
14 changes: 12 additions & 2 deletions wire/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,25 @@ package wire
import (
"io"
"time"

. "github.com/tendermint/tendermint/common"
)

// Time
/*
Writes nanoseconds since epoch but with millisecond precision.
This is to ease compatibility with Javascript etc.
*/

func WriteTime(t time.Time, w io.Writer, n *int64, err *error) {
WriteInt64(t.UnixNano(), w, n, err)
nanosecs := t.UnixNano()
millisecs := nanosecs / 1000000
WriteInt64(millisecs*1000000, w, n, err)
}

func ReadTime(r io.Reader, n *int64, err *error) time.Time {
t := ReadInt64(r, n, err)
if t%1000000 != 0 {
PanicSanity("Time cannot have sub-millisecond precision")
}
return time.Unix(0, t)
}

0 comments on commit 7876808

Please sign in to comment.