2018-04-06 11:55:33 +00:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"git.loafle.net/commons/logging-go"
|
2018-04-25 09:56:25 +00:00
|
|
|
oci "git.loafle.net/overflow/central-api/golang"
|
2018-04-26 07:58:25 +00:00
|
|
|
occe "git.loafle.net/overflow/commons-go/config/external"
|
2018-04-06 11:55:33 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
2018-04-25 09:56:25 +00:00
|
|
|
var grpcClient oci.CentralAPIClient
|
2018-04-06 11:55:33 +00:00
|
|
|
|
2018-04-26 07:58:25 +00:00
|
|
|
func InitPackage(config *occe.GRPC) {
|
2018-04-10 05:21:48 +00:00
|
|
|
if nil == config {
|
|
|
|
return
|
|
|
|
}
|
2018-04-10 05:54:45 +00:00
|
|
|
|
|
|
|
conn, err := grpc.Dial(config.Address, grpc.WithInsecure())
|
|
|
|
if nil != err {
|
|
|
|
logging.Logger().Panic(err)
|
|
|
|
}
|
2018-04-25 09:56:25 +00:00
|
|
|
grpcClient = oci.NewCentralAPIClient(conn)
|
2018-04-10 05:54:45 +00:00
|
|
|
logging.Logger().Infof("GRPC: connected to %s", config.Address)
|
2018-04-10 05:21:48 +00:00
|
|
|
}
|
|
|
|
|
2018-04-26 07:58:25 +00:00
|
|
|
func StartPackage(config *occe.GRPC) {
|
2018-04-10 05:21:48 +00:00
|
|
|
if nil == config {
|
|
|
|
return
|
|
|
|
}
|
2018-04-06 11:55:33 +00:00
|
|
|
}
|
|
|
|
|
2018-04-26 07:58:25 +00:00
|
|
|
func StopPackage(config *occe.GRPC) {
|
2018-04-10 05:21:48 +00:00
|
|
|
if nil == config {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-26 07:58:25 +00:00
|
|
|
func DestroyPackage(config *occe.GRPC) {
|
2018-04-10 05:21:48 +00:00
|
|
|
if nil == config {
|
|
|
|
return
|
|
|
|
}
|
2018-04-06 11:55:33 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
var execMtx sync.RWMutex
|
|
|
|
|
|
|
|
func Exec(ctx context.Context, method string, params ...string) (string, error) {
|
|
|
|
if nil == grpcClient {
|
|
|
|
return "", fmt.Errorf("GRPC Client is not initialized")
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
sm := strings.Split(method, ".")
|
|
|
|
si := &oci.ServerInput{
|
|
|
|
Target: sm[0],
|
|
|
|
Method: sm[1],
|
|
|
|
Params: params,
|
|
|
|
}
|
|
|
|
|
|
|
|
execMtx.Lock()
|
|
|
|
defer execMtx.Unlock()
|
2018-04-25 09:56:25 +00:00
|
|
|
so, err := grpcClient.(oci.CentralAPIClient).Exec(ctx, si)
|
2018-04-06 11:55:33 +00:00
|
|
|
if nil != err {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return so.Result, nil
|
|
|
|
}
|