2018-04-17 14:11:13 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2018-05-11 06:37:31 +00:00
|
|
|
"context"
|
2018-05-03 11:24:07 +00:00
|
|
|
"fmt"
|
2018-04-17 14:11:13 +00:00
|
|
|
"reflect"
|
|
|
|
|
|
|
|
cda "git.loafle.net/commons/di-go/annotation"
|
|
|
|
cdr "git.loafle.net/commons/di-go/registry"
|
2018-05-11 06:37:31 +00:00
|
|
|
logging "git.loafle.net/commons/logging-go"
|
|
|
|
crc "git.loafle.net/commons/rpc-go/client"
|
2018-05-10 10:18:00 +00:00
|
|
|
occp "git.loafle.net/overflow/commons-go/config/probe"
|
|
|
|
"git.loafle.net/overflow/probe/client/probe"
|
2018-04-17 14:11:13 +00:00
|
|
|
// For annotation
|
|
|
|
_ "git.loafle.net/overflow/commons-go/core/annotation"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ProbeClientServiceType = reflect.TypeOf((*ProbeClientService)(nil))
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cdr.RegisterType(ProbeClientServiceType)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProbeClientService struct {
|
|
|
|
cda.TypeAnnotation `annotation:"@overflow:RPCService()"`
|
|
|
|
|
2018-04-19 15:03:58 +00:00
|
|
|
DiscoveryService *DiscoveryService `annotation:"@Inject()"`
|
2018-04-17 14:11:13 +00:00
|
|
|
|
|
|
|
EncryptionKey string
|
2018-05-11 06:37:31 +00:00
|
|
|
|
|
|
|
client *crc.Client
|
2018-04-17 14:11:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ProbeClientService) InitService() error {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ProbeClientService) StartService() error {
|
2018-05-10 10:18:00 +00:00
|
|
|
client, err := probe.New(s.HandleResponse, s.DiscoveryService)
|
2018-04-18 14:56:13 +00:00
|
|
|
if nil != err {
|
2018-05-03 11:24:07 +00:00
|
|
|
return fmt.Errorf("ProbeClientService: StartService failed %v", err)
|
2018-04-18 14:56:13 +00:00
|
|
|
}
|
|
|
|
s.client = client
|
|
|
|
|
2018-04-17 14:11:13 +00:00
|
|
|
if err := s.client.Start(); nil != err {
|
2018-05-03 11:24:07 +00:00
|
|
|
return fmt.Errorf("ProbeClientService: StartService failed %v", err)
|
2018-04-17 14:11:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ProbeClientService) StopService() {
|
2018-05-11 06:37:31 +00:00
|
|
|
if err := s.client.Stop(context.Background()); nil != err {
|
|
|
|
logging.Logger().Error(err)
|
|
|
|
}
|
|
|
|
|
2018-04-17 14:11:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ProbeClientService) DestroyService() {
|
2018-05-11 06:37:31 +00:00
|
|
|
|
2018-04-17 14:11:13 +00:00
|
|
|
}
|
|
|
|
|
2018-05-10 10:18:00 +00:00
|
|
|
func (s *ProbeClientService) HandleResponse(method string, param string) {
|
|
|
|
switch method {
|
|
|
|
case occp.HTTPResponseHeaderValue_Probe_Method_EncryptionKey:
|
|
|
|
s.EncryptionKey = param
|
|
|
|
case occp.HTTPResponseHeaderValue_Probe_Method_Delete:
|
|
|
|
}
|
|
|
|
|
2018-04-17 14:11:13 +00:00
|
|
|
}
|
2018-05-11 06:37:31 +00:00
|
|
|
|
|
|
|
func (s *ProbeClientService) Call(result interface{}, method string, params ...interface{}) error {
|
|
|
|
if nil == s.client {
|
|
|
|
return fmt.Errorf("rpc client is not valid")
|
|
|
|
}
|
|
|
|
return s.client.Call(result, method, params...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ProbeClientService) Send(method string, params ...interface{}) error {
|
|
|
|
if nil == s.client {
|
|
|
|
return fmt.Errorf("rpc client is not valid")
|
|
|
|
}
|
|
|
|
return s.client.Send(method, params...)
|
|
|
|
}
|