probe/client/central/cnetral.go

76 lines
1.8 KiB
Go
Raw Normal View History

2018-04-14 08:57:01 +00:00
package central
import (
"fmt"
"net/url"
"path"
"reflect"
cdr "git.loafle.net/commons/di-go/registry"
logging "git.loafle.net/commons/logging-go"
crc "git.loafle.net/commons/rpc-go/client"
crpj "git.loafle.net/commons/rpc-go/protocol/json"
crr "git.loafle.net/commons/rpc-go/registry"
csc "git.loafle.net/commons/server-go/client"
csswc "git.loafle.net/commons/server-go/socket/web/client"
cur "git.loafle.net/commons/util-go/reflect"
"git.loafle.net/overflow/probe/config"
)
func newConnector(name string, entryPath string) (*csswc.Connectors, error) {
config := getConfig()
if nil == config {
return nil, fmt.Errorf("Config is not available")
}
u := url.URL{
Scheme: "ws",
Host: config.Central.Address,
}
u.Path = path.Join(u.Path, entryPath)
_connector := config.Central.Connector.Clone()
connector, ok := _connector.(*csswc.Connectors)
if !ok {
return nil, fmt.Errorf("Cannot convert to Connectors type")
}
connector.Name = name
connector.URL = u.String()
return connector, nil
}
func newClient(name string, connector csc.Connector, services []interface{}) *crc.Client {
codec := crpj.NewClientCodec()
var rpcRegistry crr.RPCRegistry
if nil != services && 0 < len(services) {
rpcRegistry = crr.NewRPCRegistry()
rpcRegistry.RegisterServices(services...)
}
return &crc.Client{
Connector: connector,
Codec: codec,
RPCInvoker: rpcRegistry,
Name: name,
}
}
func getConfig() *config.Config {
_config, err := cdr.GetInstanceByName("Config")
if nil != err {
logging.Logger().Error(err)
return nil
}
config, ok := _config.(*config.Config)
if !ok {
_, pkg, n := cur.GetTypeInfo(reflect.TypeOf(_config))
logging.Logger().Errorf("Cannot convert [%s]%s to Config type", pkg, n)
return nil
}
return config
}