| 28 | func (s *SampleService) Stop() error { fmt.Println("Service stopping..."); return nil } |
| 29 | |
| 30 | func ExampleService() { |
| 31 | // Create a network node to run protocols with the default values. |
| 32 | stack, err := node.New(&node.Config{}) |
| 33 | if err != nil { |
| 34 | log.Fatalf("Failed to create network node: %v", err) |
| 35 | } |
| 36 | // Create and register a simple network service. This is done through the definition |
| 37 | // of a node.ServiceConstructor that will instantiate a node.Service. The reason for |
| 38 | // the factory method approach is to support service restarts without relying on the |
| 39 | // individual implementations' support for such operations. |
| 40 | constructor := func(context *node.ServiceContext) (node.Service, error) { |
| 41 | return new(SampleService), nil |
| 42 | } |
| 43 | if err := stack.Register(constructor); err != nil { |
| 44 | log.Fatalf("Failed to register service: %v", err) |
| 45 | } |
| 46 | // Boot up the entire protocol stack, do a restart and terminate |
| 47 | if err := stack.Start(); err != nil { |
| 48 | log.Fatalf("Failed to start the protocol stack: %v", err) |
| 49 | } |
| 50 | if err := stack.Restart(); err != nil { |
| 51 | log.Fatalf("Failed to restart the protocol stack: %v", err) |
| 52 | } |
| 53 | if err := stack.Stop(); err != nil { |
| 54 | log.Fatalf("Failed to stop the protocol stack: %v", err) |
| 55 | } |
| 56 | // Output: |
| 57 | // Service starting... |
| 58 | // Service stopping... |
| 59 | // Service starting... |
| 60 | // Service stopping... |
| 61 | } |