func (t *transport) handlePing(ch *e3x.Channel) { var ( err error pkt *lob.Packet id string alive bool ) defer ch.Close() pkt, err = ch.ReadPacket() if err != nil { // log error // tracef("error: %s", err) return } id, _ = pkt.Header().GetString("vn") rpc := t.lookupRPC(id) if rpc == nil { alive = false } else { alive = true } pkt = &lob.Packet{} pkt.Header().SetBool("alive", alive) err = ch.WritePacket(pkt) if err != nil { // log error // tracef("error: %s", err) return } }
// Gets a list of the vnodes on the box func (t *transport) ListVnodes(hn string) ([]*chord.Vnode, error) { var ( addr *e3x.Addr ch *e3x.Channel res []*completeVnode err error ) addr = t.lookupAddr(hashname.H(hn)) if addr == nil { return nil, e3x.ErrNoAddress } ch, err = t.e.Open(addr, "chord.list", true) if err != nil { return nil, err } defer ch.Close() // ch.SetReadDeadline(time.Now().Add(30*time.Second)) // ch.SetWriteDeadline(time.Now().Add(30*time.Second)) err = ch.WritePacket(&lob.Packet{}) if err != nil { return nil, err } err = json.NewDecoder(newStream(ch)).Decode(&res) if err != nil { return nil, err } return t.internalVnodes(res), nil }
// Request a nodes predecessor func (t *transport) GetPredecessor(vn *chord.Vnode) (*chord.Vnode, error) { var ( addr *e3x.Addr ch *e3x.Channel pkt *lob.Packet res *completeVnode err error ) addr = t.lookupAddr(hashname.H(vn.Host)) if addr == nil { return nil, e3x.ErrNoAddress } ch, err = t.e.Open(addr, "chord.predecessor.get", true) if err != nil { return nil, err } defer ch.Close() // ch.SetReadDeadline(time.Now().Add(30*time.Second)) // ch.SetWriteDeadline(time.Now().Add(30*time.Second)) pkt = &lob.Packet{} pkt.Header().SetString("vn", vn.String()) err = ch.WritePacket(pkt) if err != nil { return nil, err } err = json.NewDecoder(newStream(ch)).Decode(&res) if err != nil { return nil, err } if res != nil { // tracef("GetPredecessor(Vnode(%q)) => Vnode(%q)", vn.String(), res.Id) } return t.internalVnode(res), nil }
// Ping a Vnode, check for liveness func (t *transport) Ping(vn *chord.Vnode) (bool, error) { var ( addr *e3x.Addr ch *e3x.Channel pkt *lob.Packet alive bool err error ) addr = t.lookupAddr(hashname.H(vn.Host)) if addr == nil { return false, e3x.ErrNoAddress } ch, err = t.e.Open(addr, "chord.ping", true) if err != nil { return false, err } defer ch.Close() // ch.SetReadDeadline(time.Now().Add(30*time.Second)) // ch.SetWriteDeadline(time.Now().Add(30*time.Second)) pkt = &lob.Packet{} pkt.Header().SetString("vn", vn.String()) err = ch.WritePacket(pkt) if err != nil { return false, err } pkt, err = ch.ReadPacket() if err != nil { return false, err } alive, _ = pkt.Header().GetBool("alive") // tracef("Ping(Vnode(%q)) => %v", vn.String(), alive) return alive, nil }
func (d *dht) handle_see(c *e3x.Channel) { defer c.Close() pkt, err := c.ReadPacket() if err != nil { return } key, ok := pkt.Header().GetString("key") if !ok { return } h := d.localLookup(key) pkt = &lob.Packet{} pkt.Header().Set("see", h) pkt.Header().SetBool("end", true) err = c.WritePacket(pkt) if err != nil { return } }