| 
									
										
										
										
											2015-11-05 23:57:57 +02:00
										 |  |  | // Copyright 2015 The go-ethereum Authors | 
					
						
							|  |  |  | // This file is part of the go-ethereum library. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The go-ethereum library is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | // it under the terms of the GNU Lesser General Public License as published by | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | // (at your option) any later version. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The go-ethereum library is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
					
						
							|  |  |  | // GNU Lesser General Public License for more details. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // You should have received a copy of the GNU Lesser General Public License | 
					
						
							|  |  |  | // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package node_test | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"log" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/node" | 
					
						
							|  |  |  | 	"github.com/ethereum/go-ethereum/p2p" | 
					
						
							| 
									
										
										
										
											2015-12-16 10:58:01 +01:00
										 |  |  | 	"github.com/ethereum/go-ethereum/rpc" | 
					
						
							| 
									
										
										
										
											2015-11-05 23:57:57 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // SampleService is a trivial network service that can be attached to a node for | 
					
						
							|  |  |  | // life cycle management. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // The following methods are needed to implement a node.Service: | 
					
						
							|  |  |  | //  - Protocols() []p2p.Protocol - devp2p protocols the service can communicate on | 
					
						
							| 
									
										
										
										
											2016-02-02 19:06:43 +02:00
										 |  |  | //  - APIs() []rpc.API           - api methods the service wants to expose on rpc channels | 
					
						
							| 
									
										
										
										
											2015-11-05 23:57:57 +02:00
										 |  |  | //  - Start() error              - method invoked when the node is ready to start the service | 
					
						
							|  |  |  | //  - Stop() error               - method invoked when the node terminates the service | 
					
						
							|  |  |  | type SampleService struct{} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (s *SampleService) Protocols() []p2p.Protocol { return nil } | 
					
						
							| 
									
										
										
										
											2015-10-15 16:07:19 +02:00
										 |  |  | func (s *SampleService) APIs() []rpc.API           { return nil } | 
					
						
							| 
									
										
										
										
											2015-11-17 18:33:25 +02:00
										 |  |  | func (s *SampleService) Start(*p2p.Server) error   { fmt.Println("Service starting..."); return nil } | 
					
						
							|  |  |  | func (s *SampleService) Stop() error               { fmt.Println("Service stopping..."); return nil } | 
					
						
							| 
									
										
										
										
											2015-11-05 23:57:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-09 09:52:26 +01:00
										 |  |  | func ExampleService() { | 
					
						
							| 
									
										
										
										
											2017-04-12 16:27:23 +02:00
										 |  |  | 	// Create a network node to run protocols with the default values. | 
					
						
							|  |  |  | 	stack, err := node.New(&node.Config{}) | 
					
						
							| 
									
										
										
										
											2015-11-05 23:57:57 +02:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		log.Fatalf("Failed to create network node: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-02-07 11:40:36 +01:00
										 |  |  | 	defer stack.Close() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-05 23:57:57 +02:00
										 |  |  | 	// Create and register a simple network service. This is done through the definition | 
					
						
							|  |  |  | 	// of a node.ServiceConstructor that will instantiate a node.Service. The reason for | 
					
						
							|  |  |  | 	// the factory method approach is to support service restarts without relying on the | 
					
						
							|  |  |  | 	// individual implementations' support for such operations. | 
					
						
							|  |  |  | 	constructor := func(context *node.ServiceContext) (node.Service, error) { | 
					
						
							|  |  |  | 		return new(SampleService), nil | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-11-26 18:35:44 +02:00
										 |  |  | 	if err := stack.Register(constructor); err != nil { | 
					
						
							| 
									
										
										
										
											2015-11-05 23:57:57 +02:00
										 |  |  | 		log.Fatalf("Failed to register service: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// Boot up the entire protocol stack, do a restart and terminate | 
					
						
							|  |  |  | 	if err := stack.Start(); err != nil { | 
					
						
							|  |  |  | 		log.Fatalf("Failed to start the protocol stack: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if err := stack.Restart(); err != nil { | 
					
						
							|  |  |  | 		log.Fatalf("Failed to restart the protocol stack: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if err := stack.Stop(); err != nil { | 
					
						
							|  |  |  | 		log.Fatalf("Failed to stop the protocol stack: %v", err) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// Output: | 
					
						
							| 
									
										
										
										
											2015-11-17 18:33:25 +02:00
										 |  |  | 	// Service starting... | 
					
						
							|  |  |  | 	// Service stopping... | 
					
						
							|  |  |  | 	// Service starting... | 
					
						
							|  |  |  | 	// Service stopping... | 
					
						
							| 
									
										
										
										
											2015-11-05 23:57:57 +02:00
										 |  |  | } |