Skip to content

Commit

Permalink
Create p2p_node.go
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 7, 2024
1 parent 659ca38 commit 7aba246
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions network/p2p/p2p_node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package p2p

import (
"context"
"fmt"
"log"
"net"

"github.com/KOSASIH/pi-nexus-autonomous-banking-network/network/p2p/pb"
"google.golang.org/grpc"
)

type P2PNode struct {
pb.UnimplementedP2PNodeServer
nodeID string
peers map[string]*grpc.ClientConn
}

func (n *P2PNode) Connect(ctx context.Context, req *pb.ConnectRequest) (*pb.ConnectResponse, error) {
// TO DO: implement peer connection logic
log.Printf("Received connect request from %s", req.NodeID)
n.peers[req.NodeID] = req.Conn
return &pb.ConnectResponse{Result: "success"}, nil
}

func (n *P2PNode) Disconnect(ctx context.Context, req *pb.DisconnectRequest) (*pb.DisconnectResponse, error) {
// TO DO: implement peer disconnection logic
log.Printf("Received disconnect request from %s", req.NodeID)
delete(n.peers, req.NodeID)
return &pb.DisconnectResponse{Result: "success"}, nil
}

func (n *P2PNode) SendMessage(ctx context.Context, req *pb.SendMessageRequest) (*pb.SendMessageResponse, error) {
// TO DO: implement message sending logic
log.Printf("Received send message request from %s", req.NodeID)
return &pb.SendMessageResponse{Result: "success"}, nil
}

func main() {
lis, err := net.Listen("tcp", ":50054")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}

srv := grpc.NewServer()
pb.RegisterP2PNodeServer(srv, &P2PNode{
nodeID: "node-1",
peers: make(map[string]*grpc.ClientConn),
})

log.Println("P2P node listening on port 50054")
srv.Serve(lis)
}

0 comments on commit 7aba246

Please sign in to comment.