// Note: cl.msgResponses is accessed directly with the assumption that no key will be accessed concurrently func (cl *ChordLibrary) join(addr string) error { //log.Printf("Joining Node %s\n", addr) tcpConn, err := netlib.NewTCPConnection(addr, cl.NodeID, cl.Addr) if err != nil { log.Println("join: getConnNode - Error initiating connection") return err } cl.nodeCache[tcpConn.RemoteNodeID] = tcpConn go cl.handleRead(tcpConn) nextAddr, nextNodeID, lerr := cl.lookupByID(addr, tcpConn.RemoteNodeID, cl.NodeID) if lerr != nil { log.Printf("join: lookupByNodeID failed\n") } prevAddr, prevNodeID, perr := cl.joinAsPrev(nextAddr, nextNodeID) if perr != nil { log.Printf("join: joinAsPrev failed\n") } nerr := cl.joinAsNext(prevAddr, prevNodeID) if nerr != nil { log.Printf("join: joinAsNext failed\n") } log.Printf("Joined the chord with predecessor %d and successor %d\n", cl.Prev.RemoteNodeID, cl.Next.RemoteNodeID) if cl.Prev.RemoteNodeID != cl.Next.RemoteNodeID { backupAddr, backupNodeID, berr := cl.querySuccessor(cl.Next.Addr, cl.Next.RemoteNodeID) if berr != nil { log.Printf("join: backup failed\n") } cl.BackupNext, _ = cl.getConnection(backupNodeID, backupAddr) } return nil }
func (cl *ChordLibrary) getConnection(nodeID uint32, addr string) (*netlib.TCPConnection, error) { ////log.Printf("Getting connection node for %d\n", nodeID) tcpConn, found := cl.nodeCache[nodeID] if !found { ////log.Println("Connection not found; establishing new") tcpConn, err := netlib.NewTCPConnection(addr, cl.NodeID, cl.Addr) if err != nil { return nil, err } cl.nodeCache[tcpConn.RemoteNodeID] = tcpConn go cl.handleRead(tcpConn) return tcpConn, nil } ////log.Println("Connection found in cache") return tcpConn, nil }