github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/node/node_example_test.go (about) 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 17 package node_test 18 19 import ( 20 "fmt" 21 "log" 22 23 "github.com/ethereumproject/go-ethereum/node" 24 "github.com/ethereumproject/go-ethereum/p2p" 25 "github.com/ethereumproject/go-ethereum/p2p/discover" 26 "github.com/ethereumproject/go-ethereum/rpc" 27 ) 28 29 // SampleService is a trivial network service that can be attached to a node for 30 // life cycle management. 31 // 32 // The following methods are needed to implement a node.Service: 33 // - Protocols() []p2p.Protocol - devp2p protocols the service can communicate on 34 // - APIs() []rpc.API - api methods the service wants to expose on rpc channels 35 // - Start() error - method invoked when the node is ready to start the service 36 // - Stop() error - method invoked when the node terminates the service 37 type SampleService struct{} 38 39 func (s *SampleService) Protocols() []p2p.Protocol { return nil } 40 func (s *SampleService) APIs() []rpc.API { return nil } 41 func (s *SampleService) Start(*p2p.Server) error { fmt.Println("Service starting..."); return nil } 42 func (s *SampleService) Stop() error { fmt.Println("Service stopping..."); return nil } 43 44 func ExampleUsage() { 45 // Create a network node to run protocols with the default values. The below list 46 // is only used to display each of the configuration options. All of these could 47 // have been ommited if the default behavior is desired. 48 nodeConfig := &node.Config{ 49 DataDir: "", // Empty uses ephemeral storage 50 PrivateKey: nil, // Nil generates a node key on the fly 51 Name: "", // Any textual node name is allowed 52 NoDiscovery: false, // Can disable discovering remote nodes 53 BootstrapNodes: []*discover.Node{}, // List of bootstrap nodes to use 54 ListenAddr: ":0", // Network interface to listen on 55 NAT: nil, // UPnP port mapper to use for crossing firewalls 56 Dialer: nil, // Custom dialer to use for establishing peer connections 57 NoDial: false, // Can prevent this node from dialing out 58 MaxPeers: 0, // Number of peers to allow 59 MaxPendingPeers: 0, // Number of peers allowed to handshake concurrently 60 } 61 stack, err := node.New(nodeConfig) 62 if err != nil { 63 log.Fatalf("Failed to create network node: %v", err) 64 } 65 // Create and register a simple network service. This is done through the definition 66 // of a node.ServiceConstructor that will instantiate a node.Service. The reason for 67 // the factory method approach is to support service restarts without relying on the 68 // individual implementations' support for such operations. 69 constructor := func(context *node.ServiceContext) (node.Service, error) { 70 return new(SampleService), nil 71 } 72 if err := stack.Register(constructor); err != nil { 73 log.Fatalf("Failed to register service: %v", err) 74 } 75 // Boot up the entire protocol stack, do a restart and terminate 76 if err := stack.Start(); err != nil { 77 log.Fatalf("Failed to start the protocol stack: %v", err) 78 } 79 if err := stack.Restart(); err != nil { 80 log.Fatalf("Failed to restart the protocol stack: %v", err) 81 } 82 if err := stack.Stop(); err != nil { 83 log.Fatalf("Failed to stop the protocol stack: %v", err) 84 } 85 // Output: 86 // Service starting... 87 // Service stopping... 88 // Service starting... 89 // Service stopping... 90 }