github.com/aergoio/aergo@v1.3.1/examples/component/main.go (about) 1 /** 2 * @file 3 * @copyright defined in aergo/LICENSE.txt 4 */ 5 package main 6 7 import ( 8 "encoding/json" 9 "fmt" 10 "time" 11 12 "github.com/aergoio/aergo-lib/log" 13 "github.com/aergoio/aergo/examples/component/message" 14 "github.com/aergoio/aergo/examples/component/server" 15 "github.com/aergoio/aergo/examples/component/service" 16 "github.com/aergoio/aergo/pkg/component" 17 ) 18 19 func main() { 20 21 compHub := component.NewComponentHub() 22 23 testServer := &server.TestServer{} 24 testServer.BaseComponent = component.NewBaseComponent("TestServer", testServer, log.Default()) 25 26 helloService := service.NexExampleServie("Den") 27 28 compHub.Register(testServer) 29 compHub.Register(helloService) 30 compHub.Start() 31 32 // request and go through 33 testServer.RequestTo(message.HelloService, &message.HelloReq{Who: "Roger"}) 34 35 // request and wait 36 rawResponse, err := compHub.RequestFuture(message.HelloService, 37 &component.CompStatReq{SentTime: time.Now()}, 38 time.Second, "examples/component.main").Result() 39 if err != nil { 40 fmt.Println(err) 41 } else { 42 response := rawResponse.(*component.CompStatRsp) 43 fmt.Printf("RequestFuture Test Result: %v\n", response) 44 } 45 46 // collect all component's statuses 47 statics, _ := compHub.Statistics(time.Second, "") 48 if data, err := json.MarshalIndent(statics, "", "\t"); err != nil { 49 fmt.Println(err) 50 } else { 51 fmt.Printf("All Component's Statistics: %s\n", data) 52 } 53 54 time.Sleep(1 * time.Second) 55 56 compHub.Stop() 57 }