func writeHC(b *bytes.Buffer, hc *HostConfig, p *sign.Node) error { // Node{name, pubkey, x_hat, children} if p == nil { return errors.New("node does not exist") } prk, _ := p.PrivKey.MarshalBinary() pbk, _ := p.PubKey.MarshalBinary() fmt.Fprint(b, "{\"name\":", "\""+p.Name()+"\",") fmt.Fprint(b, "\"prikey\":", "\""+string(hex.EncodeToString(prk))+"\",") fmt.Fprint(b, "\"pubkey\":", "\""+string(hex.EncodeToString(pbk))+"\",") // recursively format children fmt.Fprint(b, "\"children\":[") i := 0 for _, n := range p.Children(0) { if i != 0 { b.WriteString(", ") } c := hc.Hosts[n.Name()] err := writeHC(b, hc, c) if err != nil { b.WriteString("\"" + n.Name() + "\"") } i++ } fmt.Fprint(b, "]}") return nil }
// dijkstra is actually implemented as BFS right now because it is equivalent // when edge weights are all 1. func dijkstra(m map[string]*sign.Node, root *sign.Node) { l := list.New() visited := make(map[string]bool) l.PushFront(root) visited[root.Name()] = true for e := l.Front(); e != nil; e = l.Front() { l.Remove(e) sn := e.Value.(*sign.Node) // make all unvisited peers children // and mark them as visited for name, conn := range sn.Peers() { // visited means it is already on the tree. if visited[name] { continue } visited[name] = true // add the associated peer/connection as a child sn.AddChildren(0, conn.Name()) cn, ok := m[name] if !ok { panic("error getting connection from map") } peers := cn.Peers() pconn, ok := peers[sn.Name()] if !ok { panic("parent connection doesn't exist: not bi-directional") } cn.AddParent(0, pconn.Name()) l.PushFront(cn) } } }