From 0c5913ddc9324ab0cd210d7b23dd5a97704c02f0 Mon Sep 17 00:00:00 2001 From: Jony Bepary Date: Wed, 6 Dec 2023 20:04:23 +0600 Subject: [PATCH] removed garbage --- internal/network/mdns.go | 31 ------ internal/network/p2p.go | 190 ----------------------------------- internal/network/protocol.go | 11 -- 3 files changed, 232 deletions(-) delete mode 100644 internal/network/mdns.go delete mode 100644 internal/network/p2p.go delete mode 100644 internal/network/protocol.go diff --git a/internal/network/mdns.go b/internal/network/mdns.go deleted file mode 100644 index c1f5590..0000000 --- a/internal/network/mdns.go +++ /dev/null @@ -1,31 +0,0 @@ -package main - -import ( - "github.com/libp2p/go-libp2p/core/host" - "github.com/libp2p/go-libp2p/core/peer" - - "github.com/libp2p/go-libp2p/p2p/discovery/mdns" -) - -type discoveryNotifee struct { - PeerChan chan peer.AddrInfo -} - -// interface to be called when new peer is found -func (n *discoveryNotifee) HandlePeerFound(pi peer.AddrInfo) { - n.PeerChan <- pi -} - -// Initialize the MDNS service -func initMDNS(peerhost host.Host, rendezvous string) chan peer.AddrInfo { - // register with service so that we get notified about peer discovery - n := &discoveryNotifee{} - n.PeerChan = make(chan peer.AddrInfo) - - // An hour might be a long long period in practical applications. But this is fine for us - ser := mdns.NewMdnsService(peerhost, rendezvous, n) - if err := ser.Start(); err != nil { - panic(err) - } - return n.PeerChan -} diff --git a/internal/network/p2p.go b/internal/network/p2p.go deleted file mode 100644 index da656f9..0000000 --- a/internal/network/p2p.go +++ /dev/null @@ -1,190 +0,0 @@ -package main - -import ( - "bufio" - "context" - "encoding/binary" - "encoding/json" - "flag" - "fmt" - "time" - - "github.com/SohelAhmedJoni/Awazz-Backend/internal/model" - "github.com/libp2p/go-libp2p" - pubsub "github.com/libp2p/go-libp2p-pubsub" - "github.com/libp2p/go-libp2p/core/host" - "github.com/libp2p/go-libp2p/core/protocol" - "github.com/syndtr/goleveldb/leveldb" - "google.golang.org/protobuf/proto" -) - -type blockHeader struct { - BlockID string - PScode string -} - -var ( - topicNameFlag = flag.String("topicName", "/block/1.0.0", "name of topic to join") -) - -func p2p_sync() { - cfg := parseFlags() - ctx := context.Background() - - db, err := leveldb.OpenFile("BLOCK", nil) - if err != nil { - panic(err) - } - h, err := libp2p.New(libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/0")) - if err != nil { - panic(err) - } - // h.SetStreamHandler("/block/1.0.0", func(s network.Stream) { - // log.Printf("/block/1.0.0 stream created") - // handleStream(s, db) - // }) - - // go discoverPeers(ctx, h) - go connectPeers(ctx, h, cfg) - - ps, err := pubsub.NewGossipSub(ctx, h) - if err != nil { - panic(err) - } - topic, err := ps.Join(*topicNameFlag) - if err != nil { - panic(err) - } - go streamConsoleTo(ctx, topic, db) - - sub, err := topic.Subscribe() - if err != nil { - panic(err) - } - printMessagesFrom(ctx, sub, h, db) -} -func check(err error) { - if err != nil { - panic(err) - } -} - -func connectPeers(ctx context.Context, h host.Host, cfg *config) { - - fmt.Printf("\n[*] Your Multiaddress Is: /ip4/%s/tcp/%v/p2p/%s\n", cfg.listenHost, cfg.listenPort, h.ID().Pretty()) - - peerChan := initMDNS(h, cfg.RendezvousString) - for { // allows multiple peers to join - peer := <-peerChan // will block untill we discover a peer - fmt.Println("Found peer:", peer, ", connecting") - - if err := h.Connect(ctx, peer); err != nil { - fmt.Println("Connection failed:", err) - continue - } - - } - // fmt.Println("Peer discovery complete") -} - -// publish -func streamConsoleTo(ctx context.Context, topic *pubsub.Topic, db *leveldb.DB) { - lbyte, err := db.Get([]byte("latest"), nil) - check(err) - var latestBlock model.Post - err = proto.Unmarshal(lbyte, &latestBlock) - check(err) - - bit, err := json.Marshal(latestBlock) - check(err) - - // reader := bufio.NewReader(os.Stdin) - for { - // s, err := reader.ReadString('\n') - if err != nil { - panic(err) - } - if err := topic.Publish(ctx, []byte(bit)); err != nil { - fmt.Println("### Publish error:", err) - } - } -} - -func printMessagesFrom(ctx context.Context, sub *pubsub.Subscription, h host.Host, db *leveldb.DB) { - for { - m, err := sub.Next(ctx) - if err != nil { - panic(err) - } - - if m.ReceivedFrom == h.ID() { - continue - } - // wait for 5 seconds - time.Sleep(5 * time.Second) - - fmt.Println(m.ReceivedFrom, ": ", string(m.Message.Data)) - ReceivedBlock := BlockHeader{} - json.Unmarshal(m.Message.Data, &ReceivedBlock) - - latestBlock := LiteBlock{} - lbyte, err := db.Get([]byte("latest"), nil) - check(err) - err = latestBlock.XXX_Unmarshal(lbyte) - check(err) - - if ReceivedBlock.Pscode == latestBlock.Header.Pscode { - if ReceivedBlock.BlockID < latestBlock.Header.BlockID { - stream, err := h.NewStream(ctx, m.ReceivedFrom, protocol.ID("/block/1.0.0")) - if err != nil { - fmt.Println("Stream open failed", err) - } else { - rw := bufio.NewReadWriter(bufio.NewReader(stream), bufio.NewWriter(stream)) - go writeData(rw, ReceivedBlock.BlockID, latestBlock.Header.BlockID, db) - fmt.Println("Connected to:", m.ReceivedFrom.Pretty()) - } - } else if ReceivedBlock.BlockID > latestBlock.Header.BlockID { - stream, err := h.NewStream(ctx, m.ReceivedFrom, protocol.ID("/block/1.0.0")) - if err != nil { - fmt.Println("Stream open failed", err) - } else { - rw := bufio.NewReadWriter(bufio.NewReader(stream), bufio.NewWriter(stream)) - go readData(rw) - fmt.Println("Connected to:", m.ReceivedFrom.Pretty()) - } - } - - } - } -} - -func sendMsg(rw *bufio.ReadWriter, id int64, content []byte) error { - return nil -} - -func readMsg(rw *bufio.ReadWriter) { - for { - // read bytes until new line - msg, err := rw.ReadBytes('\n') - if err != nil { - fmt.Println("Error reading from buffer") - continue - } - - // get the id - id := int64(binary.LittleEndian.Uint64(msg[0:8])) - - // get the content, last index is len(msg)-1 to remove the new line char - content := string(msg[8 : len(msg)-1]) - - if content != "" { - // we print [message ID] content - fmt.Printf("[%d] %s", id, content) - } - - if err := sendMsg(rw, id, []byte("response")); err != nil { - fmt.Println("Err while sending response: ", err) - continue - } - } -} diff --git a/internal/network/protocol.go b/internal/network/protocol.go deleted file mode 100644 index 2b48f45..0000000 --- a/internal/network/protocol.go +++ /dev/null @@ -1,11 +0,0 @@ -package main - -import ( - "bufio" - "encoding/binary" - "fmt" - - "github.com/syndtr/goleveldb/leveldb" -) - -