node_example_test.go 3.0 KB

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