diag.go 870 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11
package dht

import (
	"encoding/json"
	"time"

	peer "github.com/jbenet/go-ipfs/peer"
)

type connDiagInfo struct {
	Latency time.Duration
12
	ID      peer.ID
13 14 15
}

type diagInfo struct {
16
	ID          peer.ID
17
	Connections []connDiagInfo
Jeromy's avatar
Jeromy committed
18 19
	Keys        []string
	LifeSpan    time.Duration
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
	CodeVersion string
}

func (di *diagInfo) Marshal() []byte {
	b, err := json.Marshal(di)
	if err != nil {
		panic(err)
	}
	//TODO: also consider compressing this. There will be a lot of these
	return b
}

func (dht *IpfsDHT) getDiagInfo() *diagInfo {
	di := new(diagInfo)
	di.CodeVersion = "github.com/jbenet/go-ipfs"
35
	di.ID = dht.self.ID()
36 37 38
	di.LifeSpan = time.Since(dht.birth)
	di.Keys = nil // Currently no way to query datastore

39
	for _, p := range dht.routingTable.ListPeers() {
40 41
		d := connDiagInfo{p.GetLatency(), p.ID()}
		di.Connections = append(di.Connections, d)
42 43 44
	}
	return di
}