github.com/turingchain2020/turingchain@v1.1.21/cmd/tools/gencode/dappcode/rpc/rpc.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package rpc
     6  
     7  import (
     8  	"github.com/turingchain2020/turingchain/cmd/tools/gencode/base"
     9  	"github.com/turingchain2020/turingchain/cmd/tools/types"
    10  )
    11  
    12  func init() {
    13  
    14  	base.RegisterCodeFile(rpcCodeFile{})
    15  }
    16  
    17  type rpcCodeFile struct {
    18  	base.DappCodeFile
    19  }
    20  
    21  func (c rpcCodeFile) GetDirName() string {
    22  
    23  	return "rpc"
    24  }
    25  
    26  func (c rpcCodeFile) GetFiles() map[string]string {
    27  
    28  	return map[string]string{
    29  		rpcName:   rpcContent,
    30  		typesName: typesContent,
    31  	}
    32  }
    33  
    34  func (c rpcCodeFile) GetFileReplaceTags() []string {
    35  
    36  	return []string{types.TagExecName, types.TagImportPath, types.TagClassName}
    37  }
    38  
    39  var (
    40  	rpcName    = "rpc.go"
    41  	rpcContent = `package rpc
    42  
    43  
    44  /* 
    45   * 实现json rpc和grpc service接口
    46   * json rpc用Jrpc结构作为接收实例
    47   * grpc使用channelClient结构作为接收实例
    48  */
    49  
    50  `
    51  
    52  	typesName    = "types.go"
    53  	typesContent = `package rpc
    54  
    55  import (
    56  	${EXECNAME}types "${IMPORTPATH}/${EXECNAME}/types"
    57  	rpctypes "github.com/turingchain2020/turingchain/rpc/types"
    58  )
    59  
    60  /* 
    61   * rpc相关结构定义和初始化
    62  */
    63  
    64  // 实现grpc的service接口
    65  type channelClient struct {
    66  	rpctypes.ChannelClient
    67  }
    68  
    69  // Jrpc 实现json rpc调用实例
    70  type Jrpc struct {
    71  	cli *channelClient
    72  }
    73  
    74  // Grpc grpc
    75  type Grpc struct {
    76  	*channelClient
    77  }
    78  
    79  // Init init rpc
    80  func Init(name string, s rpctypes.RPCServer) {
    81  	cli := &channelClient{}
    82  	grpc := &Grpc{channelClient: cli}
    83  	cli.Init(name, s, &Jrpc{cli: cli}, grpc)
    84  	//存在grpc service时注册grpc server,需要生成对应的pb.go文件
    85  	${EXECNAME}types.Register${CLASSNAME}Server(s.GRPC(), grpc)
    86  }`
    87  )