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" "time"
"github.com/ethereum/go-ethereum/log" "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/adapters"
"github.com/ethereum/go-ethereum/p2p/discover" "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 // 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 // these are called on the p2p.Server which runs on the node
// af() returns an arbitrary ticker channel // 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.toggle = make(chan bool)
self.more = make(chan bool, 1) 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 { if addr != nil {
log.Info(fmt.Sprintf("========> connect to bee %v", addr)) log.Info(fmt.Sprintf("========> connect to bee %v", addr))
err := connectPeer(NodeId(addr).NodeID.String()) node, err := discover.ParseNode(NodeId(addr).NodeID.String())
if err != nil { if err == nil {
log.Error(fmt.Sprintf("===X====> connect to bee %v failed: %v", addr, err)) server.AddPeer(node)
} else {
log.Error(fmt.Sprintf("===X====> connect to bee %v failed: invalid node URL: %v", addr, err))
} }
} else { } else {
log.Trace("cannot suggest peers") log.Trace("cannot suggest peers")

View file

@ -13,7 +13,6 @@ import (
p2pnode "github.com/ethereum/go-ethereum/node" p2pnode "github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/adapters" "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/p2p/simulations"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/swarm/network" "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 { func (n *node) Start(server p2p.Server) error {
connectPeer := func(url string) error { return n.Hive.Start(server, n.hiveKeepAlive)
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)
} }
func (n *node) Stop() error { func (n *node) Stop() error {

View file

@ -50,15 +50,7 @@ func af() <-chan time.Time {
// Start() starts up the hive // Start() starts up the hive
// makes SimNode implement node.Service // makes SimNode implement node.Service
func (self *SimNode) Start(server p2p.Server) error { func (self *SimNode) Start(server p2p.Server) error {
connectPeer := func(url string) error { return self.hive.Start(server, af)
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)
} }
// Stop() shuts down the hive // Stop() shuts down the hive