finish node server

This commit is contained in:
Branden J Brown 2025-03-11 09:04:24 -04:00
parent 462a9783e9
commit 54d9536692

View File

@ -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)
}