2018-04-06 11:55:33 +00:00
|
|
|
package grpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"git.loafle.net/commons/logging-go"
|
|
|
|
oci "git.loafle.net/overflow/central_api/golang"
|
2018-04-10 05:21:48 +00:00
|
|
|
occeg "git.loafle.net/overflow/commons-go/config/external/grpc"
|
2018-04-06 11:55:33 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
var grpcClient oci.OverflowApiServerClient
|
|
|
|
|
2018-04-10 05:21:48 +00:00
|
|
|
func InitPackage(config *occeg.GRPConfig) {
|
|
|
|
if nil == config {
|
|
|
|
return
|
|
|
|
}
|
2018-04-09 11:37:54 +00:00
|
|
|
go func() {
|
|
|
|
conn, err := grpc.Dial("192.168.1.50:50006", grpc.WithInsecure())
|
|
|
|
if nil != err {
|
|
|
|
logging.Logger().Panic(err)
|
|
|
|
}
|
|
|
|
grpcClient = oci.NewOverflowApiServerClient(conn)
|
|
|
|
}()
|
2018-04-10 05:21:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func StartPackage(config *occeg.GRPConfig) {
|
|
|
|
if nil == config {
|
|
|
|
return
|
|
|
|
}
|
2018-04-06 11:55:33 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-10 05:21:48 +00:00
|
|
|
func StopPackage(config *occeg.GRPConfig) {
|
|
|
|
if nil == config {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func DestroyPackage(config *occeg.GRPConfig) {
|
|
|
|
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()
|
|
|
|
so, err := grpcClient.(oci.OverflowApiServerClient).Exec(ctx, si)
|
|
|
|
if nil != err {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return so.Result, nil
|
|
|
|
}
|