Add echo back in for test
continuous-integration/drone/push Build is passing Details

This commit is contained in:
DutchEllie 2022-05-29 21:11:39 +02:00
parent de1001c794
commit 0bd424fb39
Signed by: DutchEllie
SSH Key Fingerprint: SHA256:UU8yhd1Bh8exU88ev0ej/Dm+CeyfR/ZIDRrpRfYdfbg
1 changed files with 24 additions and 0 deletions

View File

@ -1,12 +1,14 @@
package main
import (
"bufio"
"context"
"fmt"
"log"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"
relayv1 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv1/relay"
libp2pquic "github.com/libp2p/go-libp2p/p2p/transport/quic"
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
@ -54,6 +56,28 @@ func run() {
return
}
h1.SetStreamHandler("/echo/1.0.0", func(s network.Stream) {
log.Println("listener received new stream")
// Read whatever comes in and print it out
// from https://github.com/libp2p/go-libp2p/blob/621eafcecde611b27bbb42dda4b8bbc97b66e907/examples/echo/main.go#L197
if err := func(s network.Stream) error {
buf := bufio.NewReader(s)
str, err := buf.ReadString('\n')
if err != nil {
log.Printf("error reading stream: %v\n", err)
return err
}
log.Printf("read: %s", str)
_, err = s.Write([]byte(str))
return err
}(s); err != nil {
log.Println(err)
s.Reset()
} else {
s.Close()
}
})
fullAddr := func(ha host.Host) string {
// Build host multiaddress
hostAddr, _ := ma.NewMultiaddr(fmt.Sprintf("/p2p/%s", ha.ID().Pretty()))