github.com/aergoio/aergo@v1.3.1/examples/component/service/service.go (about) 1 /** 2 * @file 3 * @copyright defined in aergo/LICENSE.txt 4 */ 5 6 package service 7 8 import ( 9 "fmt" 10 11 "github.com/aergoio/aergo-actor/actor" 12 "github.com/aergoio/aergo-lib/log" 13 "github.com/aergoio/aergo/examples/component/message" 14 "github.com/aergoio/aergo/pkg/component" 15 ) 16 17 type ExampleService struct { 18 *component.BaseComponent 19 myname string 20 } 21 22 func NexExampleServie(myname string) *ExampleService { 23 actor := &ExampleService{ 24 25 myname: myname, 26 } 27 actor.BaseComponent = component.NewBaseComponent(message.HelloService, actor, log.Default()) 28 29 return actor 30 } 31 32 func (es *ExampleService) BeforeStart() { 33 // add init logics for this service 34 } 35 36 func (es *ExampleService) AfterStart() { 37 // add init logics for this service 38 } 39 40 func (es *ExampleService) BeforeStop() { 41 42 // add stop logics for this service 43 } 44 45 func (es *ExampleService) Statistics() *map[string]interface{} { 46 return nil 47 } 48 49 func (es *ExampleService) Receive(context actor.Context) { 50 51 switch msg := context.Message().(type) { 52 case *message.HelloReq: 53 context.Respond( 54 &message.HelloRsp{ 55 Greeting: fmt.Sprintf("Hello %s, I'm %s", msg.Who, es.myname), 56 }) 57 } 58 59 }