Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Get examples running with the latest version of libp2p #195

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ os:
language: go

go:
- 1.13.x
- 1.14.x

env:
global:
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ To obtain a clean `$GOPATH` execute the following:
> mkdir /tmp/libp2p-examples
> export GOPATH=/tmp/libp2p/examples
```

### Tests
`go test ./...` will run all tests in the repo
2 changes: 1 addition & 1 deletion chat-with-mdns/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func parseFlags() *config {
flag.StringVar(&c.RendezvousString, "rendezvous", "meetme", "Unique string to identify group of nodes. Share this with your friends to let them connect with you")
flag.StringVar(&c.listenHost, "host", "0.0.0.0", "The bootstrap node host listen address\n")
flag.StringVar(&c.ProtocolID, "pid", "/chat/1.1.0", "Sets a protocol id for stream headers")
flag.IntVar(&c.listenPort, "port", 4001, "node listen port")
flag.IntVar(&c.listenPort, "port", 0, "node listen port")

flag.Parse()
return c
Expand Down
9 changes: 5 additions & 4 deletions chat-with-mdns/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ func main() {
os.Exit(0)
}

fmt.Printf("[*] Listening on: %s with port: %d\n", cfg.listenHost, cfg.listenPort)

ctx := context.Background()
r := rand.Reader

Expand Down Expand Up @@ -112,8 +110,11 @@ func main() {
// Set a function as stream handler.
// This function is called when a peer initiates a connection and starts a stream with this peer.
host.SetStreamHandler(protocol.ID(cfg.ProtocolID), handleStream)

fmt.Printf("\n[*] Your Multiaddress Is: /ip4/%s/tcp/%v/p2p/%s\n", cfg.listenHost, cfg.listenPort, host.ID().Pretty())
fmt.Println("\n Host ID is", host.ID().Pretty())
fmt.Printf("\n Listening on multiaddresses:\n")
for _, a := range host.Addrs() {
fmt.Println(a)
}

peerChan := initMDNS(ctx, host, cfg.RendezvousString)

Expand Down
13 changes: 13 additions & 0 deletions chat-with-mdns/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"testing"
"time"
)

//"Chat with mdns" will poll for input forever, here just run the main logic to see if there are any runtime errors
func TestMain(t *testing.M) {
timer1 := time.NewTimer(2 * time.Second)
go main()
<-timer1.C
}
1 change: 0 additions & 1 deletion chat-with-rendezvous/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ This program demonstrates a simple p2p chat application. You will learn how to d
From the `go-libp2p-examples` directory run the following:

```
> make deps
> cd chat-with-rendezvous/
> go build -o chat
```
Expand Down
14 changes: 14 additions & 0 deletions chat-with-rendezvous/chat-with-rendezvous_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"testing"
"time"
)

//"Chat with rendezvous" will poll for input forever, here just run the main logic to see if there are any runtime errors
//FIXME: As of the time of writing, Chat with rendezvous doesn't work properly, yet this test does not fail.
func TestMain(t *testing.M) {
timer1 := time.NewTimer(2 * time.Second)
go main()
<-timer1.C
}
3 changes: 1 addition & 2 deletions chat-with-rendezvous/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

dht "github.com/libp2p/go-libp2p-kad-dht"
multiaddr "github.com/multiformats/go-multiaddr"
logging "github.com/whyrusleeping/go-logging"

"github.com/ipfs/go-log"
)
Expand Down Expand Up @@ -80,7 +79,7 @@ func writeData(rw *bufio.ReadWriter) {
}

func main() {
log.SetAllLoggers(logging.WARNING)
log.SetAllLoggers(log.LevelError) // INFO or DEBUG for more information.
log.SetLogLevel("rendezvous", "info")
help := flag.Bool("h", false, "Display Help")
config, err := ParseFlags()
Expand Down
1 change: 0 additions & 1 deletion chat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Usage: Run `./chat -sp <SOURCE_PORT>` on host 'B' where <SOURCE_PORT> can be any
From the `go-libp2p-examples` directory run the following:

```
> make deps
> cd chat
> go build
```
Expand Down
13 changes: 13 additions & 0 deletions chat/chat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"testing"
"time"
)

//"Chat" will poll for input forever, here just run the main logic to see if there are any runtime errors
func TestMain(t *testing.M) {
timer1 := time.NewTimer(2 * time.Second)
go main()
<-timer1.C
}
1 change: 0 additions & 1 deletion echo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ In dial mode, the node will start up, connect to the given address, open a strea
From the `go-libp2p-examples` directory run the following:

```
> make deps
> cd echo/
> go build
```
Expand Down
55 changes: 30 additions & 25 deletions echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

golog "github.com/ipfs/go-log"
ma "github.com/multiformats/go-multiaddr"
gologging "github.com/whyrusleeping/go-logging"
)

// makeBasicHost creates a LibP2P host with a random peer ID listening on the
Expand Down Expand Up @@ -76,28 +75,7 @@ func makeBasicHost(listenPort int, insecure bool, randseed int64) (host.Host, er
return basicHost, nil
}

func main() {
// LibP2P code uses golog to log messages. They log with different
// string IDs (i.e. "swarm"). We can control the verbosity level for
// all loggers with:
golog.SetAllLoggers(gologging.INFO) // Change to DEBUG for extra info

// Parse options from the command line
listenF := flag.Int("l", 0, "wait for incoming connections")
target := flag.String("d", "", "target peer to dial")
insecure := flag.Bool("insecure", false, "use an unencrypted connection")
seed := flag.Int64("seed", 0, "set random seed for id generation")
flag.Parse()

if *listenF == 0 {
log.Fatal("Please provide a port to bind on with -l")
}

// Make a host that listens on the given multiaddress
ha, err := makeBasicHost(*listenF, *insecure, *seed)
if err != nil {
log.Fatal(err)
}
func echo(target string, ha host.Host) {

// Set a stream handler on host A. /echo/1.0.0 is
// a user-defined protocol name.
Expand All @@ -111,15 +89,15 @@ func main() {
}
})

if *target == "" {
if target == "" {
log.Println("listening for connections")
select {} // hang forever
}
/**** This is where the listener code ends ****/

// The following code extracts target's the peer ID from the
// given multiaddress
ipfsaddr, err := ma.NewMultiaddr(*target)
ipfsaddr, err := ma.NewMultiaddr(target)
if err != nil {
log.Fatalln(err)
}
Expand Down Expand Up @@ -165,6 +143,33 @@ func main() {
log.Printf("read reply: %q\n", out)
}

func main() {
// LibP2P code uses golog to log messages. They log with different
// string IDs (i.e. "swarm"). We can control the verbosity level for
// all loggers with:
golog.SetAllLoggers(golog.LevelInfo) // Change to DEBUG for extra info

// Parse options from the command line
listenF := flag.Int("l", 0, "wait for incoming connections")
target := flag.String("d", "", "target peer to dial")
insecure := flag.Bool("insecure", false, "use an unencrypted connection")
seed := flag.Int64("seed", 0, "set random seed for id generation")
flag.Parse()

if *listenF == 0 {
log.Fatal("Please provide a port to bind on with -l")
}

// Make a host that listens on the given multiaddress
ha, err := makeBasicHost(*listenF, *insecure, *seed)
if err != nil {
log.Fatal(err)
}

echo(*target, ha)

}

// doEcho reads a line of data a stream and writes it back
func doEcho(s network.Stream) error {
buf := bufio.NewReader(s)
Expand Down
29 changes: 29 additions & 0 deletions echo/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"fmt"
ma "github.com/multiformats/go-multiaddr"
"log"
"testing"
)

// send an echo from port 7001 to port 7000
func TestMain(m *testing.M) {

listenHost, err := makeBasicHost(7000, false, 0)
if err != nil {
log.Fatal(err)
}
// Build host multiaddress
hostAddr, _ := ma.NewMultiaddr(fmt.Sprintf("/ipfs/%s", listenHost.ID().Pretty()))
addr := listenHost.Addrs()[0]
fullAddr := addr.Encapsulate(hostAddr)

pingHost, err := makeBasicHost(7001, false, 0)
if err != nil {
log.Fatal(err)
}

go echo("", listenHost)
echo(fullAddr.String(), pingHost)
}
39 changes: 19 additions & 20 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
module github.com/libp2p/go-libp2p-examples

go 1.14

require (
github.com/gogo/protobuf v1.3.1
github.com/google/uuid v1.1.1
github.com/ipfs/go-datastore v0.4.2
github.com/ipfs/go-log v0.0.1
github.com/libp2p/go-libp2p v0.5.2
github.com/libp2p/go-libp2p-autonat-svc v0.1.0
github.com/libp2p/go-libp2p-circuit v0.1.4
github.com/libp2p/go-libp2p-connmgr v0.2.1
github.com/libp2p/go-libp2p-core v0.3.0
github.com/libp2p/go-libp2p-discovery v0.2.0
github.com/libp2p/go-libp2p-kad-dht v0.5.0
github.com/libp2p/go-libp2p-quic-transport v0.2.3
github.com/libp2p/go-libp2p-routing v0.1.0
github.com/libp2p/go-libp2p-secio v0.2.1
github.com/libp2p/go-libp2p-swarm v0.2.2
github.com/gogo/protobuf v1.3.2
github.com/google/uuid v1.1.5
github.com/ipfs/go-datastore v0.4.5
github.com/ipfs/go-log v1.0.4
github.com/libp2p/go-libp2p v0.13.0
github.com/libp2p/go-libp2p-circuit v0.4.0
github.com/libp2p/go-libp2p-connmgr v0.2.4
github.com/libp2p/go-libp2p-core v0.8.0
github.com/libp2p/go-libp2p-discovery v0.5.0
github.com/libp2p/go-libp2p-kad-dht v0.11.1
github.com/libp2p/go-libp2p-quic-transport v0.10.0
github.com/libp2p/go-libp2p-swarm v0.4.0
github.com/libp2p/go-libp2p-tls v0.1.3
github.com/multiformats/go-multiaddr v0.2.0
github.com/multiformats/go-multiaddr-net v0.1.2
github.com/whyrusleeping/go-logging v0.0.1
github.com/multiformats/go-multiaddr v0.3.1
github.com/multiformats/go-multiaddr-net v0.2.0
github.com/stretchr/testify v1.6.1
golang.org/x/sys v0.0.0-20210113181707-4bcb84eeeb78 // indirect
golang.org/x/text v0.3.5 // indirect
)

go 1.13
Loading