Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
TATAUFO committed Jul 3, 2024
1 parent b01c8e1 commit 4db4e34
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 104 deletions.
104 changes: 0 additions & 104 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package node

import (
"bufio"
"bytes"
"context"
"crypto/ed25519"
"embed"
Expand All @@ -29,18 +27,13 @@ import (
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"

"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
"github.com/multiformats/go-multiaddr"
"github.com/pdupub/go-pdu/core"
"github.com/pdupub/go-pdu/node/db"
)
Expand Down Expand Up @@ -139,75 +132,6 @@ func (n *Node) handleInterrupt() {
}()
}

func (n *Node) handleStream(s network.Stream) {
fmt.Println("Got a new stream!")
defer s.Close()

remotePeerID := s.Conn().RemotePeer()
remotePeerAddr := s.Conn().RemoteMultiaddr()

remoteFullAddr := fmt.Sprintf("%s/p2p/%s", remotePeerAddr, remotePeerID)
fmt.Printf("Connected to peer: %s\n", remoteFullAddr)

buf := new(bytes.Buffer)
buf.ReadFrom(s)
message := buf.String()
fmt.Printf("Received message: %s\n", message)

// save peer info into ndb

info := strings.Split(remoteFullAddr, "/")
peer := db.Peer{
ID: info[len(info)-1],
Address: remoteFullAddr,
Status: "connected",
LastConnected: time.Now(),
}
if err := n.ndb.AddPeer(peer); err != nil {
log.Printf("Failed to add peer to database: %s", err)
}

}

func (n *Node) sendMessage(peerID peer.ID, message string) error {
s, err := n.Host.NewStream(n.Ctx, peerID, protocol.ID(protocolID))
if err != nil {
return err
}
defer s.Close()

_, err = s.Write([]byte(message))
return err
}

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

peerinfo, err := peer.AddrInfoFromP2pAddr(maddr)
if err != nil {
log.Fatalf("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)
}

peer := db.Peer{
ID: peerinfo.ID.String(),
Address: peerAddr,
Status: "connected",
LastConnected: time.Now(),
}
if err := n.ndb.AddPeer(peer); err != nil {
log.Printf("Failed to add peer to database: %s", err)
}

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

func (n *Node) startWebServer(port int) {
subFS, err := fs.Sub(staticFiles, "static")
if err != nil {
Expand All @@ -234,34 +158,6 @@ func (n *Node) startRPCServer(port int) {
log.Fatal(server.ListenAndServe())
}

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")
}
} else {
fmt.Printf("Failed to get peer info: %s\n", err)
}
}
}
}

func (n *Node) Run(webPort, rpcPort int) {
n.handleInterrupt()

Expand Down
114 changes: 114 additions & 0 deletions node/p2p_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package node

import (
"bufio"
"bytes"
"fmt"
"log"
"os"
"strings"
"time"

"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/multiformats/go-multiaddr"
"github.com/pdupub/go-pdu/node/db"
)

func (n *Node) handleStream(s network.Stream) {
fmt.Println("Got a new stream!")
defer s.Close()

remotePeerID := s.Conn().RemotePeer()
remotePeerAddr := s.Conn().RemoteMultiaddr()

remoteFullAddr := fmt.Sprintf("%s/p2p/%s", remotePeerAddr, remotePeerID)
fmt.Printf("Connected to peer: %s\n", remoteFullAddr)

buf := new(bytes.Buffer)
buf.ReadFrom(s)
message := buf.String()
fmt.Printf("Received message: %s\n", message)

// save peer info into ndb

info := strings.Split(remoteFullAddr, "/")
peer := db.Peer{
ID: info[len(info)-1],
Address: remoteFullAddr,
Status: "connected",
LastConnected: time.Now(),
}
if err := n.ndb.AddPeer(peer); err != nil {
log.Printf("Failed to add peer to database: %s", err)
}

}

func (n *Node) sendMessage(peerID peer.ID, message string) error {
s, err := n.Host.NewStream(n.Ctx, peerID, protocol.ID(protocolID))
if err != nil {
return err
}
defer s.Close()

_, err = s.Write([]byte(message))
return err
}

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

peerinfo, err := peer.AddrInfoFromP2pAddr(maddr)
if err != nil {
log.Fatalf("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)
}

peer := db.Peer{
ID: peerinfo.ID.String(),
Address: peerAddr,
Status: "connected",
LastConnected: time.Now(),
}
if err := n.ndb.AddPeer(peer); err != nil {
log.Printf("Failed to add peer to database: %s", err)
}

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

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")
}
} else {
fmt.Printf("Failed to get peer info: %s\n", err)
}
}
}
}

0 comments on commit 4db4e34

Please sign in to comment.