swarm/network: Pass p2p.Server to Hive.Start

Signed-off-by: Lewis Marshall <lewis@lmars.net>
This commit is contained in:
Lewis Marshall 2017-04-27 09:06:47 +01:00
parent c3dc00336e
commit 24dcc8d6e1
3 changed files with 9 additions and 23 deletions

View file

@ -22,6 +22,7 @@ import (
"time"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/adapters"
"github.com/ethereum/go-ethereum/p2p/discover"
)
@ -92,7 +93,7 @@ func NewHive(params *HiveParams, overlay Overlay) *Hive {
// connectPeer is a function to connect to a peer based on its NodeID or enode URL
// these are called on the p2p.Server which runs on the node
// af() returns an arbitrary ticker channel
func (self *Hive) Start(connectPeer func(string) error, af func() <-chan time.Time) error {
func (self *Hive) Start(server p2p.Server, af func() <-chan time.Time) error {
self.toggle = make(chan bool)
self.more = make(chan bool, 1)
@ -113,9 +114,11 @@ func (self *Hive) Start(connectPeer func(string) error, af func() <-chan time.Ti
if addr != nil {
log.Info(fmt.Sprintf("========> connect to bee %v", addr))
err := connectPeer(NodeId(addr).NodeID.String())
if err != nil {
log.Error(fmt.Sprintf("===X====> connect to bee %v failed: %v", addr, err))
node, err := discover.ParseNode(NodeId(addr).NodeID.String())
if err == nil {
server.AddPeer(node)
} else {
log.Error(fmt.Sprintf("===X====> connect to bee %v failed: invalid node URL: %v", addr, err))
}
} else {
log.Trace("cannot suggest peers")

View file

@ -13,7 +13,6 @@ import (
p2pnode "github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/adapters"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/p2p/simulations"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/swarm/network"
@ -246,15 +245,7 @@ func (n *node) APIs() []rpc.API {
}
func (n *node) Start(server p2p.Server) error {
connectPeer := func(url string) error {
node, err := discover.ParseNode(url)
if err != nil {
return fmt.Errorf("invalid node URL: %v", err)
}
server.AddPeer(node)
return nil
}
return n.Hive.Start(connectPeer, n.hiveKeepAlive)
return n.Hive.Start(server, n.hiveKeepAlive)
}
func (n *node) Stop() error {

View file

@ -50,15 +50,7 @@ func af() <-chan time.Time {
// Start() starts up the hive
// makes SimNode implement node.Service
func (self *SimNode) Start(server p2p.Server) error {
connectPeer := func(url string) error {
node, err := discover.ParseNode(url)
if err != nil {
return fmt.Errorf("invalid node URL: %v", err)
}
server.AddPeer(node)
return nil
}
return self.hive.Start(connectPeer, af)
return self.hive.Start(server, af)
}
// Stop() shuts down the hive