github.com/klaytn/klaytn@v1.12.1/node/node_example_test.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2015 The go-ethereum Authors
     3  // This file is part of go-ethereum.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from node/node_example_test.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package node_test
    22  
    23  import (
    24  	"fmt"
    25  
    26  	"github.com/klaytn/klaytn/log"
    27  	"github.com/klaytn/klaytn/networks/p2p"
    28  	"github.com/klaytn/klaytn/networks/rpc"
    29  	"github.com/klaytn/klaytn/node"
    30  )
    31  
    32  // SampleService is a trivial network service that can be attached to a node for
    33  // life cycle management.
    34  //
    35  // The following methods are needed to implement a node.Service:
    36  //  - Protocols() []p2p.Protocol - devp2p protocols the service can communicate on
    37  //  - APIs() []rpc.API           - api methods the service wants to expose on rpc channels
    38  //  - Start() error              - method invoked when the node is ready to start the service
    39  //  - Stop() error               - method invoked when the node terminates the service
    40  type SampleService struct{}
    41  
    42  func (s *SampleService) Protocols() []p2p.Protocol              { return nil }
    43  func (s *SampleService) APIs() []rpc.API                        { return nil }
    44  func (s *SampleService) Start(p2p.Server) error                 { fmt.Println("Service starting..."); return nil }
    45  func (s *SampleService) Stop() error                            { fmt.Println("Service stopping..."); return nil }
    46  func (s *SampleService) Components() []interface{}              { return nil }
    47  func (s *SampleService) SetComponents(components []interface{}) {}
    48  
    49  func ExampleService() {
    50  	// Create a network node to run protocols with the default values.
    51  	stack, err := node.New(&node.Config{})
    52  	if err != nil {
    53  		log.Fatalf("Failed to create network node: %v", err)
    54  	}
    55  	// Create and register a simple network service. This is done through the definition
    56  	// of a node.ServiceConstructor that will instantiate a node.Service. The reason for
    57  	// the factory method approach is to support service restarts without relying on the
    58  	// individual implementations' support for such operations.
    59  	constructor := func(context *node.ServiceContext) (node.Service, error) {
    60  		return new(SampleService), nil
    61  	}
    62  	if err := stack.Register(constructor); err != nil {
    63  		log.Fatalf("Failed to register service: %v", err)
    64  	}
    65  	// Boot up the entire protocol stack, do a restart and terminate
    66  	if err := stack.Start(); err != nil {
    67  		log.Fatalf("Failed to start the protocol stack: %v", err)
    68  	}
    69  	if err := stack.Restart(); err != nil {
    70  		log.Fatalf("Failed to restart the protocol stack: %v", err)
    71  	}
    72  	if err := stack.Stop(); err != nil {
    73  		log.Fatalf("Failed to stop the protocol stack: %v", err)
    74  	}
    75  	// Output:
    76  	// Service starting...
    77  	// Service stopping...
    78  	// Service starting...
    79  	// Service stopping...
    80  }