Browse Source

Allow to join node without bringing up network

Ettore Di Giacinto 3 years ago
parent
commit
8a66d15886
3 changed files with 42 additions and 22 deletions
  1. 3 1
      pkg/edgevpn/config.go
  2. 30 21
      pkg/edgevpn/edgevpn.go
  3. 9 0
      pkg/edgevpn/options.go

+ 3 - 1
pkg/edgevpn/config.go

@@ -49,12 +49,14 @@ type Config struct {
 
 	// Handle is a handle consumed by HumanInterfaces to handle received messages
 	Handle                     func(bool, *hub.Message)
-	StreamHandlers             map[protocol.ID]func(stream network.Stream)
+	StreamHandlers             map[protocol.ID]StreamHandler
 	AdditionalOptions, Options []libp2p.Option
 
 	LedgerSyncronizationTime, LedgerAnnounceTime time.Duration
 }
 
+type StreamHandler func(stream network.Stream)
+
 type Handler func(*hub.Message) error
 
 type ServiceDiscovery interface {

+ 30 - 21
pkg/edgevpn/edgevpn.go

@@ -29,7 +29,7 @@ type EdgeVPN struct {
 
 func New(p ...Option) *EdgeVPN {
 	c := Config{
-		StreamHandlers:           make(map[protocol.ID]func(stream network.Stream)),
+		StreamHandlers:           make(map[protocol.ID]StreamHandler),
 		LedgerAnnounceTime:       5 * time.Second,
 		LedgerSyncronizationTime: 5 * time.Second,
 	}
@@ -60,31 +60,14 @@ func (e *EdgeVPN) adverizer(ip net.IP, ledger *blockchain.Ledger) {
 	}
 }
 
-// Start the vpn. Returns an error in case of failure
-func (e *EdgeVPN) Start() error {
-	ifce, err := e.createInterface()
-	if err != nil {
-		return err
-	}
-	defer ifce.Close()
-
-	mw, err := e.MessageWriter()
-	if err != nil {
-		return err
-	}
-
-	ledger := blockchain.New(mw, e.config.MaxBlockChainLength)
-
+func (e *EdgeVPN) Join(ledger *blockchain.Ledger) error {
 	// Set the handler when we receive messages
 	// The ledger needs to read them and update the internal blockchain
 	e.config.Handlers = append(e.config.Handlers, ledger.Update)
-	// Set the stream handler to get back the packets from the stream to the interface
-	e.config.StreamHandlers[protocol.ID(Protocol)] = streamHandler(ledger, ifce)
 
 	e.config.Logger.Sugar().Info("starting edgevpn background daemon")
-
 	// Startup libp2p network
-	err = e.network()
+	err := e.network()
 	if err != nil {
 		return err
 	}
@@ -104,6 +87,32 @@ func (e *EdgeVPN) Start() error {
 		func() string { return e.host.ID().String() },
 	)
 
+	return nil
+}
+
+// Start the vpn. Returns an error in case of failure
+func (e *EdgeVPN) Start() error {
+	ifce, err := e.createInterface()
+	if err != nil {
+		return err
+	}
+	defer ifce.Close()
+
+	mw, err := e.MessageWriter()
+	if err != nil {
+		return err
+	}
+
+	ledger := blockchain.New(mw, e.config.MaxBlockChainLength)
+
+	// Set the stream handler to get back the packets from the stream to the interface
+	e.config.StreamHandlers[protocol.ID(Protocol)] = streamHandler(ledger, ifce)
+
+	// Join the node to the network, using our ledger
+	if err := e.Join(ledger); err != nil {
+		return err
+	}
+
 	if e.config.NetLinkBootstrap {
 		if err := e.prepareInterface(); err != nil {
 			return err
@@ -201,7 +210,7 @@ func (e *EdgeVPN) network() error {
 	e.host = host
 
 	for pid, strh := range e.config.StreamHandlers {
-		host.SetStreamHandler(pid, strh)
+		host.SetStreamHandler(pid, network.StreamHandler(strh))
 	}
 
 	e.config.Logger.Sugar().Info("Host created. We are:", host.ID())

+ 9 - 0
pkg/edgevpn/options.go

@@ -6,6 +6,7 @@ import (
 
 	"github.com/ipfs/go-log/v2"
 	"github.com/libp2p/go-libp2p"
+	"github.com/libp2p/go-libp2p-core/protocol"
 	discovery "github.com/mudler/edgevpn/pkg/discovery"
 	"github.com/mudler/edgevpn/pkg/utils"
 	"github.com/pkg/errors"
@@ -95,6 +96,14 @@ func Handlers(h ...Handler) func(cfg *Config) error {
 	}
 }
 
+// StreamHandlers adds a handler to the list that is called on each received message
+func WithStreamHandler(id protocol.ID, h StreamHandler) func(cfg *Config) error {
+	return func(cfg *Config) error {
+		cfg.StreamHandlers[id] = h
+		return nil
+	}
+}
+
 // DiscoveryService Adds the service given as argument to the discovery services
 func DiscoveryService(s ...ServiceDiscovery) func(cfg *Config) error {
 	return func(cfg *Config) error {