diff --git a/chord/httpnode/server.go b/chord/httpnode/server.go index 419ed77..7dcf2ef 100644 --- a/chord/httpnode/server.go +++ b/chord/httpnode/server.go @@ -43,6 +43,7 @@ func (n *Node) Router() http.Handler { m := http.NewServeMux() m.HandleFunc("GET /succ", n.successor) m.HandleFunc("POST /pred", n.notify) + m.HandleFunc("GET /neighbors", n.neighbors) return m } @@ -63,7 +64,6 @@ func (n *Node) successor(w http.ResponseWriter, r *http.Request) { } func (n *Node) notify(w http.ResponseWriter, r *http.Request) { - // Another node is telling us they think they're our predecessor. s := r.FormValue("p") addr, err := netip.ParseAddrPort(s) if err != nil { @@ -72,4 +72,19 @@ func (n *Node) notify(w http.ResponseWriter, r *http.Request) { } np := chord.Address(addr) chord.Notify(n.self, np) + w.WriteHeader(http.StatusNoContent) +} + +func (n *Node) neighbors(w http.ResponseWriter, r *http.Request) { + pred, succ := n.self.Neighbors(nil) + _, paddr := pred.Values() + u := neighbors{ + Succ: make([]netip.AddrPort, 0, len(succ)), + Pred: paddr, + } + for _, s := range succ { + _, addr := s.Values() + u.Succ = append(u.Succ, addr) + } + writeOk(w, &u) }