Skip to content

Commit

Permalink
load peers data from db, try to connect when run p2p node
Browse files Browse the repository at this point in the history
  • Loading branch information
TATAUFO committed Jul 3, 2024
1 parent 4db4e34 commit 8be6fc6
Showing 1 changed file with 60 additions and 27 deletions.
87 changes: 60 additions & 27 deletions node/p2p_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@ func (n *Node) sendMessage(peerID peer.ID, message string) error {
return err
}

func (n *Node) connectToPeer(peerAddr string) {
func (n *Node) connectToPeer(peerAddr string) (string, error) {
maddr, err := multiaddr.NewMultiaddr(peerAddr)
if err != nil {
log.Fatalf("Invalid multiaddress: %s", err)
return "", fmt.Errorf("invalid multiaddress: %s", err)
}

peerinfo, err := peer.AddrInfoFromP2pAddr(maddr)
if err != nil {
log.Fatalf("Failed to get peer info: %s", err)
return "", fmt.Errorf("failed to get peer info: %s", err)
}

if err := n.Host.Connect(n.Ctx, *peerinfo); err != nil {
log.Fatalf("Failed to connect to peer: %s", err)
return "", fmt.Errorf("failed to connect to peer: %s", err)
}

peer := db.Peer{
Expand All @@ -79,36 +79,69 @@ func (n *Node) connectToPeer(peerAddr string) {
LastConnected: time.Now(),
}
if err := n.ndb.AddPeer(peer); err != nil {
log.Printf("Failed to add peer to database: %s", err)
return "", fmt.Errorf("failed to add peer to database: %s", err)
}

fmt.Printf("Connected to %s\n", peerinfo.ID.String())
return peerinfo.ID.String(), nil
}

func (n *Node) chatWithPeer(peerAddr string) {

fmt.Println("Enter message to send (empty to skip):")
message, _ := bufio.NewReader(os.Stdin).ReadString('\n')
message = strings.TrimSpace(message)

if message != "" {
peerinfo, err := peer.AddrInfoFromP2pAddr(multiaddr.StringCast(peerAddr))
if err == nil {
err = n.sendMessage(peerinfo.ID, message)
if err != nil {
fmt.Printf("Failed to send message: %s\n", err)
} else {
fmt.Println("Message sent successfully")
}
} else {
fmt.Printf("Failed to get peer info: %s\n", err)
}
}
}

func (n *Node) connectPeers() {
fmt.Println("Enter the multiaddr of a peer to connect to (empty to skip):")
peerAddr, _ := bufio.NewReader(os.Stdin).ReadString('\n')
peerAddr = strings.TrimSpace(peerAddr)

if peerAddr != "" {
n.connectToPeer(peerAddr)

fmt.Println("Enter message to send (empty to skip):")
message, _ := bufio.NewReader(os.Stdin).ReadString('\n')
message = strings.TrimSpace(message)

if message != "" {
peerinfo, err := peer.AddrInfoFromP2pAddr(multiaddr.StringCast(peerAddr))
if err == nil {
err = n.sendMessage(peerinfo.ID, message)
if err != nil {
fmt.Printf("Failed to send message: %s\n", err)
} else {
fmt.Println("Message sent successfully")
}

peers, err := n.ndb.GetPeers(0, 10)
peerAddrs := []string{}
if err == nil && len(peers) > 0 {

for _, peer := range peers {
// if peer.Status == "connected" {
// continue
// }
if targetID, err := n.connectToPeer(peer.Address); err != nil {
fmt.Printf("Failed to connect to peer: %s\n", err)
} else {
fmt.Printf("Failed to get peer info: %s\n", err)
fmt.Printf("Connected to peer: %s\n", targetID)
peerAddrs = append(peerAddrs, peer.Address)
n.chatWithPeer(peer.Address)
}
}
}

if len(peerAddrs) == 0 {
fmt.Printf("Failed to get peers from database: %s\n", err)
fmt.Println("Enter the multiaddr of a peer to connect to (empty to skip):")
peerAddr, _ := bufio.NewReader(os.Stdin).ReadString('\n')
peerAddr = strings.TrimSpace(peerAddr)

if peerAddr != "" {
if targetID, err := n.connectToPeer(peerAddr); err != nil {
fmt.Printf("Failed to connect to peer: %s\n", err)
} else {
fmt.Printf("Connected to peer: %s\n", targetID)
peerAddrs = append(peerAddrs, peerAddr)
n.chatWithPeer(peerAddr)

}
}
}

}

0 comments on commit 8be6fc6

Please sign in to comment.