github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/network/node/utils_test.go (about) 1 package node 2 3 import ( 4 "reflect" 5 6 "github.com/neatlab/neatio/network/p2p" 7 "github.com/neatlab/neatio/network/rpc" 8 ) 9 10 type NoopService struct{} 11 12 func (s *NoopService) Protocols() []p2p.Protocol { return nil } 13 func (s *NoopService) APIs() []rpc.API { return nil } 14 func (s *NoopService) Start(*p2p.Server) error { return nil } 15 func (s *NoopService) Stop() error { return nil } 16 17 func NewNoopService(*ServiceContext) (Service, error) { return new(NoopService), nil } 18 19 type NoopServiceA struct{ NoopService } 20 type NoopServiceB struct{ NoopService } 21 type NoopServiceC struct{ NoopService } 22 23 func NewNoopServiceA(*ServiceContext) (Service, error) { return new(NoopServiceA), nil } 24 func NewNoopServiceB(*ServiceContext) (Service, error) { return new(NoopServiceB), nil } 25 func NewNoopServiceC(*ServiceContext) (Service, error) { return new(NoopServiceC), nil } 26 27 type InstrumentedService struct { 28 protocols []p2p.Protocol 29 apis []rpc.API 30 start error 31 stop error 32 33 protocolsHook func() 34 startHook func(*p2p.Server) 35 stopHook func() 36 } 37 38 func NewInstrumentedService(*ServiceContext) (Service, error) { return new(InstrumentedService), nil } 39 40 func (s *InstrumentedService) Protocols() []p2p.Protocol { 41 if s.protocolsHook != nil { 42 s.protocolsHook() 43 } 44 return s.protocols 45 } 46 47 func (s *InstrumentedService) APIs() []rpc.API { 48 return s.apis 49 } 50 51 func (s *InstrumentedService) Start(server *p2p.Server) error { 52 if s.startHook != nil { 53 s.startHook(server) 54 } 55 return s.start 56 } 57 58 func (s *InstrumentedService) Stop() error { 59 if s.stopHook != nil { 60 s.stopHook() 61 } 62 return s.stop 63 } 64 65 type InstrumentingWrapper func(base ServiceConstructor) ServiceConstructor 66 67 func InstrumentingWrapperMaker(base ServiceConstructor, kind reflect.Type) ServiceConstructor { 68 return func(ctx *ServiceContext) (Service, error) { 69 obj, err := base(ctx) 70 if err != nil { 71 return nil, err 72 } 73 wrapper := reflect.New(kind) 74 wrapper.Elem().Field(0).Set(reflect.ValueOf(obj).Elem()) 75 76 return wrapper.Interface().(Service), nil 77 } 78 } 79 80 type InstrumentedServiceA struct{ InstrumentedService } 81 type InstrumentedServiceB struct{ InstrumentedService } 82 type InstrumentedServiceC struct{ InstrumentedService } 83 84 func InstrumentedServiceMakerA(base ServiceConstructor) ServiceConstructor { 85 return InstrumentingWrapperMaker(base, reflect.TypeOf(InstrumentedServiceA{})) 86 } 87 88 func InstrumentedServiceMakerB(base ServiceConstructor) ServiceConstructor { 89 return InstrumentingWrapperMaker(base, reflect.TypeOf(InstrumentedServiceB{})) 90 } 91 92 func InstrumentedServiceMakerC(base ServiceConstructor) ServiceConstructor { 93 return InstrumentingWrapperMaker(base, reflect.TypeOf(InstrumentedServiceC{})) 94 } 95 96 type OneMethodApi struct { 97 fun func() 98 } 99 100 func (api *OneMethodApi) TheOneMethod() { 101 if api.fun != nil { 102 api.fun() 103 } 104 }