forked from aergoio/aergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpccommon.go
50 lines (41 loc) · 986 Bytes
/
grpccommon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* @file
* @copyright defined in aergo/LICENSE.txt
*/
package util
import (
"fmt"
"github.com/aergoio/aergo/cmd/aergocli/util/encoding/json"
"github.com/aergoio/aergo/types"
protobuf "github.com/golang/protobuf/proto"
"google.golang.org/grpc"
)
type ConnClient struct {
types.AergoRPCServiceClient
conn *grpc.ClientConn
}
func GetClient(serverAddr string, opts []grpc.DialOption) interface{} {
conn, err := grpc.Dial(serverAddr, opts...)
if err != nil || conn == nil {
fmt.Println(err)
panic("connection failed")
}
connClient := &ConnClient{
AergoRPCServiceClient: types.NewAergoRPCServiceClient(conn),
conn: conn,
}
return connClient
}
func (c *ConnClient) Close() {
c.conn.Close()
c.conn = nil
}
// JSON converts protobuf message(struct) to json notation
func JSON(pb protobuf.Message) string {
jsonout, err := json.MarshalIndent(pb, "", " ")
if err != nil {
fmt.Printf("Failed: %s\n", err.Error())
return ""
}
return string(jsonout)
}