Esempio n. 1
0
func (c *watch) MarshalJSON() ([]byte, os.Error) {
	c.lk.Lock()
	defer c.lk.Unlock()
	var w bytes.Buffer
	if tmath.IsNum(c.MinProb) && tmath.IsNum(c.MaxProb) {
		fmt.Fprintf(&w,
			"{\"MinDRes\":%g,\"MaxDRes\":%g,\"Sources\":%d,\"Neighbors\":[",
			c.MinProb, c.MaxProb, c.Sources)
	} else {
		fmt.Fprintf(&w,
			"{\"Sources\":%d,\"Neighbors\":[", c.Sources)
	}
	comma := false
	for id, n := range c.Neighbors {
		if comma {
			fmt.Fprintf(&w, ",")
		}
		fmt.Fprintf(&w, "{\"Id\":%s,\"Duration\":%d,\"InTraffic\":%d,\"OutTraffic\":%d}",
			id.ToJSON(), time.Nanoseconds()-n.TimeAppeared,
			n.InTraffic, n.OutTraffic)
		comma = true
	}
	fmt.Fprintf(&w, "]}")
	return w.Bytes(), nil
}
Esempio n. 2
0
func (f *watchFwd) toJSON(id sys.Id) string {
	var w bytes.Buffer
	fmt.Fprintf(&w,
		"{\"Id\":%s,\"MyInTraffic\":%d,\"MyOutTraffic\":%d,"+
			"\"BehalfInTraffic\":%d,\"BehalfOutTraffic\":%d",
		id.ToJSON(), f.MyInTraffic, f.MyOutTraffic,
		f.OnBehalfInTraffic, f.OnBehalfOutTraffic)
	lat := f.LatencyPerByte.GetAvg()
	if math.IsNum(lat) {
		fmt.Fprintf(&w, ",\"LatencyPerByte\":%g}", lat)
	} else {
		fmt.Fprintf(&w, "}")
	}
	return w.String()
}