Skip to content

Commit

Permalink
update node and add Multiaddr (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
AstaFrode authored Apr 7, 2023
1 parent 6d305f9 commit d215ce8
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 38 deletions.
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
// package.
type Config struct {
Workspace string
ListenAddrs []ma.Multiaddr
ListenAddrs ma.Multiaddr
ConnManager connmgr.ConnManager
}

Expand Down
11 changes: 9 additions & 2 deletions core/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"context"
"crypto/rand"
"errors"
"fmt"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -48,6 +49,7 @@ type Node struct {
host host.Host // lib-p2p host
workspace string // data
privatekeyPath string
multiaddr string
}

// NewBasicNode constructs a new *Node
Expand All @@ -57,7 +59,7 @@ type Node struct {
// privatekeypath: private key file
// If it's empty, automatically created in the program working directory
// If it's a directory, it will be created in the specified directory
func NewBasicNode(multiaddr []ma.Multiaddr, workspace string, privatekeypath string) (*Node, error) {
func NewBasicNode(multiaddr ma.Multiaddr, workspace string, privatekeypath string) (*Node, error) {
if multiaddr == nil || workspace == "" {
return nil, errors.New("invalid parameter")
}
Expand All @@ -68,7 +70,7 @@ func NewBasicNode(multiaddr []ma.Multiaddr, workspace string, privatekeypath str
}

host, err := libp2p.New(
libp2p.ListenAddrs(multiaddr...),
libp2p.ListenAddrs(multiaddr),
libp2p.Identity(prvKey),
yamuxOpt,
mplexOpt,
Expand All @@ -86,6 +88,7 @@ func NewBasicNode(multiaddr []ma.Multiaddr, workspace string, privatekeypath str
host: host,
workspace: workspace,
privatekeyPath: privatekeypath,
multiaddr: fmt.Sprintf("%s/p2p/%s", multiaddr.String(), host.ID()),
}
return n, nil
}
Expand Down Expand Up @@ -127,6 +130,10 @@ func (n Node) Workspace() string {
return n.workspace
}

func (n Node) Multiaddr() string {
return n.multiaddr
}

func (n Node) ID() peer.ID {
return n.host.ID()
}
Expand Down
17 changes: 3 additions & 14 deletions defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,13 @@ import (
"os"

"github.com/libp2p/go-libp2p/p2p/net/connmgr"
"github.com/multiformats/go-multiaddr"
)

// DefaultListenAddrs configures libp2p to use default listen address.
var DefaultListenAddrs = func(cfg *Config) error {
addrs := []string{
"/ip4/0.0.0.0/tcp/0",
"/ip6/::/tcp/0",
}
listenAddrs := make([]multiaddr.Multiaddr, 0, len(addrs))
for _, s := range addrs {
addr, err := multiaddr.NewMultiaddr(s)
if err != nil {
return err
}
listenAddrs = append(listenAddrs, addr)
}
return cfg.Apply(ListenAddrs(listenAddrs...))
ip := "0.0.0.0"
port := 80
return cfg.Apply(ListenAddrStrings(ip, port))
}

// DefaultListenAddrs configures libp2p to use default listen address.
Expand Down
8 changes: 2 additions & 6 deletions examples/readfile/example_readfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ func main() {
// To construct a simple host with all the default settings, just use `New`
h1, err := p2pgo.New(
".private1",
p2pgo.ListenAddrStrings(
fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", *sourcePort1), // regular tcp connections
),
p2pgo.ListenAddrStrings("0.0.0.0", *sourcePort1), // regular tcp connections
)
if err != nil {
panic(err)
Expand All @@ -42,9 +40,7 @@ func main() {
// To construct a simple host with all the default settings, just use `New`
h2, err := p2pgo.New(
".private2",
p2pgo.ListenAddrStrings(
fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", *sourcePort2), // regular tcp connections
),
p2pgo.ListenAddrStrings("0.0.0.0", *sourcePort2), // regular tcp connections
)
if err != nil {
panic(err)
Expand Down
8 changes: 2 additions & 6 deletions examples/writefile/example_writefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ func main() {
// To construct a simple host with all the default settings, just use `New`
h1, err := p2pgo.New(
".private1",
p2pgo.ListenAddrStrings(
fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", *sourcePort1), // regular tcp connections
),
p2pgo.ListenAddrStrings("0.0.0.0", *sourcePort1), // regular tcp connections
)
if err != nil {
panic(err)
Expand All @@ -42,9 +40,7 @@ func main() {
// To construct a simple host with all the default settings, just use `New`
h2, err := p2pgo.New(
".private2",
p2pgo.ListenAddrStrings(
fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", *sourcePort2), // regular tcp connections
),
p2pgo.ListenAddrStrings("0.0.0.0", *sourcePort2), // regular tcp connections
)
if err != nil {
panic(err)
Expand Down
16 changes: 7 additions & 9 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,21 @@ import (

// ListenAddrStrings configures libp2p to listen on the given (unparsed)
// addresses.
func ListenAddrStrings(s ...string) Option {
func ListenAddrStrings(ip string, port int) Option {
return func(cfg *Config) error {
for _, addrstr := range s {
a, err := ma.NewMultiaddr(addrstr)
if err != nil {
return err
}
cfg.ListenAddrs = append(cfg.ListenAddrs, a)
a, err := ma.NewMultiaddr(fmt.Sprintf("/ip4/%s/tcp/%d", ip, port))
if err != nil {
return err
}
cfg.ListenAddrs = a
return nil
}
}

// ListenAddrs configures libp2p to listen on the given addresses.
func ListenAddrs(addrs ...ma.Multiaddr) Option {
func ListenAddrs(addrs ma.Multiaddr) Option {
return func(cfg *Config) error {
cfg.ListenAddrs = append(cfg.ListenAddrs, addrs...)
cfg.ListenAddrs = addrs
return nil
}
}
Expand Down

0 comments on commit d215ce8

Please sign in to comment.