-
Notifications
You must be signed in to change notification settings - Fork 1
/
native_service.go
59 lines (49 loc) · 1.56 KB
/
native_service.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
51
52
53
54
55
56
57
58
59
package native
import (
"context"
"time"
"github.com/golang/protobuf/proto"
"github.com/ampchain/go-amp/contractsdk/go/code"
"github.com/ampchain/go-amp/contractsdk/go/exec"
pb "github.com/ampchain/go-amp/contractsdk/go/pb"
pbrpc "github.com/ampchain/go-amp/contractsdk/go/pbrpc"
"google.golang.org/grpc"
)
var (
_ pbrpc.NativeCodeServer = (*nativeCodeService)(nil)
)
type nativeCodeService struct {
contract code.Contract
rpcClient *grpc.ClientConn
lastping time.Time
}
func newNativeCodeService(sockpath string, contract code.Contract) *nativeCodeService {
conn, err := grpc.Dial("unix:"+sockpath, grpc.WithInsecure())
if err != nil {
panic(err)
}
return &nativeCodeService{
contract: contract,
rpcClient: conn,
lastping: time.Now(),
}
}
func (s *nativeCodeService) bridgeCall(method string, request proto.Message, response proto.Message) error {
// NOTE sync with contract.proto's package name
fullmethod := "/AmpChain.contract.svc.Syscall/" + method
return s.rpcClient.Invoke(context.Background(), fullmethod, request, response)
}
func (s *nativeCodeService) Call(ctx context.Context, request *pb.NativeCallRequest) (*pb.NativeCallResponse, error) {
exec.RunContract(request.GetCtxid(), s.contract, s.bridgeCall)
return new(pb.NativeCallResponse), nil
}
func (s *nativeCodeService) Ping(ctx context.Context, request *pb.PingRequest) (*pb.PingResponse, error) {
s.lastping = time.Now()
return &pb.PingResponse{}, nil
}
func (s *nativeCodeService) LastpingTime() time.Time {
return s.lastping
}
func (s *nativeCodeService) Close() error {
return nil
}