Esempio n. 1
0
// CodecDecodeSelf implements codec.Selfer
func (n *NodeSet) CodecDecodeSelf(decoder *codec.Decoder) {
	in := []Node{}
	if err := decoder.Decode(&in); err != nil {
		return
	}
	*n = NodeSet{}.fromIntermediate(in)
}
Esempio n. 2
0
// CodecDecodeSelf implements codec.Selfer
func (n *PluginSpecs) CodecDecodeSelf(decoder *codec.Decoder) {
	in := []PluginSpec{}
	if err := decoder.Decode(&in); err != nil {
		return
	}
	*n = PluginSpecs{}.fromIntermediate(in)
}
Esempio n. 3
0
// CodecDecodeSelf implements codec.Selfer
func (c *Counters) CodecDecodeSelf(decoder *codec.Decoder) {
	in := map[string]int{}
	if err := decoder.Decode(&in); err != nil {
		return
	}
	*c = Counters{}.fromIntermediate(in)
}
Esempio n. 4
0
// CodecDecodeSelf implements codec.Selfer
func (m *Metric) CodecDecodeSelf(decoder *codec.Decoder) {
	in := WireMetrics{}
	if err := decoder.Decode(&in); err != nil {
		return
	}
	*m = in.FromIntermediate()
}
Esempio n. 5
0
// CodecDecodeSelf implements codec.Selfer
func (m *LatestMap) CodecDecodeSelf(decoder *codec.Decoder) {
	in := map[string]LatestEntry{}
	if err := decoder.Decode(&in); err != nil {
		return
	}
	*m = LatestMap{}.fromIntermediate(in)
}
Esempio n. 6
0
// CodecDecodeSelf implements codec.Selfer
func (s *Sets) CodecDecodeSelf(decoder *codec.Decoder) {
	in := map[string]StringSet{}
	if err := decoder.Decode(&in); err != nil {
		return
	}
	*s = Sets{}.fromIntermediate(in)
}
Esempio n. 7
0
// readUserMsg is used to decode a UserMsg from a TCP stream
func (m *Memberlist) readUserMsg(bufConn io.Reader, dec *codec.Decoder) error {
	// Read the user message header
	var header userMsgHeader
	if err := dec.Decode(&header); err != nil {
		return err
	}

	// Read the user message into a buffer
	var userBuf []byte
	if header.UserMsgLen > 0 {
		userBuf = make([]byte, header.UserMsgLen)
		bytes, err := io.ReadAtLeast(bufConn, userBuf, header.UserMsgLen)
		if err == nil && bytes != header.UserMsgLen {
			err = fmt.Errorf(
				"Failed to read full user message (%d / %d)",
				bytes, header.UserMsgLen)
		}
		if err != nil {
			return err
		}

		d := m.config.Delegate
		if d != nil {
			d.NotifyMsg(userBuf)
		}
	}

	return nil
}
Esempio n. 8
0
// CodecDecodeSelf implements codec.Selfer
func (c *EdgeMetadatas) CodecDecodeSelf(decoder *codec.Decoder) {
	in := map[string]EdgeMetadata{}
	if err := decoder.Decode(&in); err != nil {
		return
	}
	*c = EdgeMetadatas{}.fromIntermediate(in)
}
Esempio n. 9
0
// CodecDecodeSelf is part of the codec.Selfer interface. It is overwritten here to make sure
// that an empty verbs list is not decoded as nil. On the other hand, an undefined verbs list
// will lead to nil because this decoding for Verbs is not invoked.
//
// TODO(sttts): this is due to a ugorji regression: https://github.com/ugorji/go/issues/119. Remove the
// workaround when the regression is fixed.
func (vs *Verbs) CodecDecodeSelf(decoder *codec.Decoder) {
	m := []string{}
	decoder.Decode(&m)
	if len(m) == 0 {
		*vs = []string{}
	} else {
		*vs = m
	}
}
Esempio n. 10
0
// CodecDecodeSelf implements codec.Selfer
func (nc *NodeControls) CodecDecodeSelf(decoder *codec.Decoder) {
	in := WireNodeControls{}
	if err := decoder.Decode(&in); err != nil {
		return
	}
	*nc = NodeControls{
		Timestamp: parseTime(in.Timestamp),
		Controls:  in.Controls,
	}
}
Esempio n. 11
0
// CodecDecodeSelf implements codec.Selfer
func (c *ControlInstance) CodecDecodeSelf(decoder *codec.Decoder) {
	var in wiredControlInstance
	decoder.Decode(&in)
	*c = ControlInstance{
		ProbeID: in.ProbeID,
		NodeID:  in.NodeID,
		Control: report.Control{
			ID:    in.ID,
			Human: in.Human,
			Icon:  in.Icon,
			Rank:  in.Rank,
		},
	}
}
Esempio n. 12
0
// Deserialize is used to deserialize the time table
// and restore the state
func (t *TimeTable) Deserialize(dec *codec.Decoder) error {
	// Decode the table
	var table []TimeTableEntry
	if err := dec.Decode(&table); err != nil {
		return err
	}

	// Witness from oldest to newest
	n := len(table)
	for i := n - 1; i >= 0; i-- {
		t.Witness(table[i].Index, table[i].Time)
	}
	return nil
}
Esempio n. 13
0
func main() {
	log.Println("MsgPack test begin==>")
	bookNode := Book{"web-founding", 36.5}
	for i := 0; i < 200; i++ {
		bookList = append(bookList, bookNode)
	}
	log.Println("json test=>begin")
	jbook, err := json.Marshal(bookList)
	if err != nil {
		log.Println("json failed=>", err)
		return
	}
	log.Println("length[", len(jbook), "]")
	fh, _ := os.OpenFile("json.txt", os.O_CREATE|os.O_WRONLY, 0666)
	nlen, _ := fh.Write(jbook)
	log.Println("acive write byte length[", nlen, "]")
	fh.Close()
	log.Println("json test=>end")
	log.Println("==================================")
	log.Println("msgpack test=>begin")
	log.Println("msgpack encoding...")
	var h codec.Handle = new(codec.MsgpackHandle)
	var buf []byte
	var enc *codec.Encoder = codec.NewEncoderBytes(&buf, h)
	err = enc.Encode(bookList)
	if err != nil {
		log.Println("msgpack encode err:", err)
		return
	}
	// hlen := hex.Encode(dst, buf)
	log.Println("buf:=>", len(hex.EncodeToString(buf)))
	fmsg, _ := os.OpenFile("msg.txt", os.O_CREATE|os.O_WRONLY, 0666)
	nlen, _ = fmsg.Write(buf)
	log.Println("acive write byte length[", nlen, "]")
	fmsg.Close()
	log.Println("msgpack decoding...")
	var dec *codec.Decoder = codec.NewDecoderBytes(buf, h)
	v2 := new([]Book)
	log.Println("src data len=>", len(buf))
	err = dec.Decode(v2)
	if err != nil {
		log.Println("msgpack decode err:", err)
		return
	}
	// log.Println("decode=>", *v2)
	log.Println("msgpack test=>end")
	log.Println("MsgPack test end==>")
}
Esempio n. 14
0
// readRemoteState is used to read the remote state from a connection
func (m *Memberlist) readRemoteState(bufConn io.Reader, dec *codec.Decoder) (bool, []pushNodeState, []byte, error) {
	// Read the push/pull header
	var header pushPullHeader
	if err := dec.Decode(&header); err != nil {
		return false, nil, nil, err
	}

	// Allocate space for the transfer
	remoteNodes := make([]pushNodeState, header.Nodes)

	// Try to decode all the states
	for i := 0; i < header.Nodes; i++ {
		if err := dec.Decode(&remoteNodes[i]); err != nil {
			return false, nil, nil, err
		}
	}

	// Read the remote user state into a buffer
	var userBuf []byte
	if header.UserStateLen > 0 {
		userBuf = make([]byte, header.UserStateLen)
		bytes, err := io.ReadAtLeast(bufConn, userBuf, header.UserStateLen)
		if err == nil && bytes != header.UserStateLen {
			err = fmt.Errorf(
				"Failed to read full user state (%d / %d)",
				bytes, header.UserStateLen)
		}
		if err != nil {
			return false, nil, nil, err
		}
	}

	// For proto versions < 2, there is no port provided. Mask old
	// behavior by using the configured port
	for idx := range remoteNodes {
		if m.ProtocolVersion() < 2 || remoteNodes[idx].Port == 0 {
			remoteNodes[idx].Port = uint16(m.config.BindPort)
		}
	}

	return header.Join, remoteNodes, userBuf, nil
}
Esempio n. 15
0
// CodecDecodeSelf implements codec.Selfer
func (m *MetricRow) CodecDecodeSelf(decoder *codec.Decoder) {
	var in wiredMetricRow
	decoder.Decode(&in)
	w := WireMetrics{
		Samples: in.Samples,
		Min:     in.Min,
		Max:     in.Max,
		First:   in.First,
		Last:    in.Last,
	}
	metric := w.FromIntermediate()
	*m = MetricRow{
		ID:       in.ID,
		Label:    in.Label,
		Format:   in.Format,
		Group:    in.Group,
		Value:    in.Value,
		Priority: in.Priority,
		Metric:   &metric,
	}
}
Esempio n. 16
0
// CodecDecodeSelf  for ugorji.go codec package @todo write test ... not sure if ok
func (n *NullFloat64) CodecDecodeSelf(d *codec.Decoder) {
	if err := d.Decode(&n.Float64); err != nil {
		PkgLog.Debug("dbr.NullFloat64.CodecDecodeSelf", "err", err, "n", n)
	}
}
Esempio n. 17
0
// handleCommand is used to decode and dispatch a single command
func (n *NetworkTransport) handleCommand(r *bufio.Reader, dec *codec.Decoder, enc *codec.Encoder) error {
	// Get the rpc type
	rpcType, err := r.ReadByte()
	if err != nil {
		return err
	}

	// Create the RPC object
	respCh := make(chan RPCResponse, 1)
	rpc := RPC{
		RespChan: respCh,
	}

	// Decode the command
	switch rpcType {
	case rpcAppendEntries:
		var req AppendEntriesRequest
		if err := dec.Decode(&req); err != nil {
			return err
		}
		rpc.Command = &req

	case rpcRequestVote:
		var req RequestVoteRequest
		if err := dec.Decode(&req); err != nil {
			return err
		}
		rpc.Command = &req

	case rpcInstallSnapshot:
		var req InstallSnapshotRequest
		if err := dec.Decode(&req); err != nil {
			return err
		}
		rpc.Command = &req
		rpc.Reader = io.LimitReader(r, req.Size)

	default:
		return fmt.Errorf("unknown rpc type %d", rpcType)
	}

	// Dispatch the RPC
	select {
	case n.consumeCh <- rpc:
	case <-n.shutdownCh:
		return ErrTransportShutdown
	}

	// Wait for response
	select {
	case resp := <-respCh:
		// Send the error first
		respErr := ""
		if resp.Error != nil {
			respErr = resp.Error.Error()
		}
		if err := enc.Encode(respErr); err != nil {
			return err
		}

		// Send the response
		if err := enc.Encode(resp.Response); err != nil {
			return err
		}
	case <-n.shutdownCh:
		return ErrTransportShutdown
	}
	return nil
}
Esempio n. 18
0
func (l *ListLink) Decode(p []byte, ctx *Context) error {
	var dec *codec.Decoder = codec.NewDecoderBytes(p, ctx.mh)
	err := dec.Decode(l)
	return err
}
Esempio n. 19
0
func (cm ChairMessage) DecodeFrom(dec *codec.Decoder) error {
	return dec.Decode(&cm)
}
Esempio n. 20
0
// CodecDecodeSelf  for ugorji.go codec package @todo write test ... not sure if ok
func (n *NullFloat64) CodecDecodeSelf(d *codec.Decoder) {
	if err := d.Decode(&n.Float64); err != nil {
		log.Error("dbr.NullFloat64.CodecEncodeSelf", "err", err, "n", n)
	}
}
Esempio n. 21
0
// CodecDecodeSelf  for ugorji.go codec package @todo write test ... not sure if ok
func (n *NullBool) CodecDecodeSelf(d *codec.Decoder) {
	if err := d.Decode(&n.Bool); err != nil {
		log.Error("dbr.NullBool.CodecEncodeSelf", "err", err, "n", n)
	}
}
Esempio n. 22
0
// CodecDecodeSelf  for ugorji.go codec package @todo write test ... not sure if ok
func (n *NullTime) CodecDecodeSelf(d *codec.Decoder) {
	if err := d.Decode(&n.Time); err != nil {
		PkgLog.Debug("dbr.NullTime.CodecDecodeSelf", "err", err, "n", n)
	}
}
Esempio n. 23
0
// CodecDecodeSelf  for ugorji.go codec package @todo write test ... not sure if ok
func (n *NullString) CodecDecodeSelf(d *codec.Decoder) {
	if err := d.Decode(&n.String); err != nil {
		PkgLog.Debug("dbr.NullString.CodecDecodeSelf", "err", err, "n", n)
	}
	// think about empty string and Valid value ...
}