github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/network/node/node_example_test.go (about)

     1  package node_test
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/neatlab/neatio/network/node"
     8  	"github.com/neatlab/neatio/network/p2p"
     9  	"github.com/neatlab/neatio/network/rpc"
    10  )
    11  
    12  type SampleService struct{}
    13  
    14  func (s *SampleService) Protocols() []p2p.Protocol { return nil }
    15  func (s *SampleService) APIs() []rpc.API           { return nil }
    16  func (s *SampleService) Start(*p2p.Server) error   { fmt.Println("Service starting..."); return nil }
    17  func (s *SampleService) Stop() error               { fmt.Println("Service stopping..."); return nil }
    18  
    19  func ExampleService() {
    20  
    21  	stack, err := node.New(&node.Config{})
    22  	if err != nil {
    23  		log.Fatalf("Failed to create network node: %v", err)
    24  	}
    25  
    26  	constructor := func(context *node.ServiceContext) (node.Service, error) {
    27  		return new(SampleService), nil
    28  	}
    29  	if err := stack.Register(constructor); err != nil {
    30  		log.Fatalf("Failed to register service: %v", err)
    31  	}
    32  
    33  	if err := stack.Start(); err != nil {
    34  		log.Fatalf("Failed to start the protocol stack: %v", err)
    35  	}
    36  	if err := stack.Restart(); err != nil {
    37  		log.Fatalf("Failed to restart the protocol stack: %v", err)
    38  	}
    39  	if err := stack.Stop(); err != nil {
    40  		log.Fatalf("Failed to stop the protocol stack: %v", err)
    41  	}
    42  
    43  }