Use protobuf enums

This commit is contained in:
Jae Kwon 2016-01-31 19:56:02 -08:00
parent 028cc4aa06
commit 012abc437b
8 changed files with 126 additions and 102 deletions

View File

@ -106,7 +106,7 @@ func (cli *TMSPClient) sendRequestsRoutine() {
return
}
// log.Debug("Sent request", "requestType", reflect.TypeOf(reqres.Request), "request", reqres.Request)
if reqres.Request.Type == types.RequestTypeFlush {
if reqres.Request.Type == types.MessageType_Flush {
err = cli.bufWriter.Flush()
if err != nil {
cli.StopForError(err)
@ -127,7 +127,7 @@ func (cli *TMSPClient) recvResponseRoutine() {
return
}
switch res.Type {
case types.ResponseTypeException:
case types.MessageType_Exception:
// XXX After setting cli.err, release waiters (e.g. reqres.Done())
cli.StopForError(errors.New(res.Error))
default:
@ -268,7 +268,7 @@ func (cli *TMSPClient) queueRequest(req *types.Request) *reqRes {
// Maybe auto-flush, or unset auto-flush
switch req.Type {
case types.RequestTypeFlush:
case types.MessageType_Flush:
cli.flushTimer.Unset()
default:
cli.flushTimer.Set()
@ -280,7 +280,7 @@ func (cli *TMSPClient) queueRequest(req *types.Request) *reqRes {
//----------------------------------------
func resMatchesReq(req *types.Request, res *types.Response) (ok bool) {
return req.Type+0x10 == res.Type
return req.Type == res.Type
}
type reqRes struct {

View File

@ -309,8 +309,8 @@ func makeRequest(conn net.Conn, req *types.Request) (*types.Response, error) {
if err != nil {
return nil, err
}
if resFlush.Type != types.ResponseTypeFlush {
return nil, errors.New(Fmt("Expected types.ResponseTypesFlush but got %v instead", resFlush.Type))
if resFlush.Type != types.MessageType_Flush {
return nil, errors.New(Fmt("Expected types.MessageType_Flush but got %v instead", resFlush.Type))
}
return res, nil

View File

@ -39,7 +39,7 @@ func TestStream(t *testing.T) {
// Process response
switch res.Type {
case types.ResponseTypeAppendTx:
case types.MessageType_AppendTx:
counter += 1
if types.RetCode(res.Code) != types.RetCodeOK {
t.Error("AppendTx failed with ret_code", res.Code)
@ -54,7 +54,7 @@ func TestStream(t *testing.T) {
close(done)
}()
}
case types.ResponseTypeFlush:
case types.MessageType_Flush:
// ignore
default:
t.Error("Unexpected response type", res.Type)

View File

@ -96,26 +96,26 @@ func handleRequests(mtx *sync.Mutex, app types.Application, closeConn chan error
func handleRequest(app types.Application, req *types.Request, responses chan<- *types.Response) {
switch req.Type {
case types.RequestTypeEcho:
case types.MessageType_Echo:
responses <- types.ResponseEcho(string(req.Data))
case types.RequestTypeFlush:
case types.MessageType_Flush:
responses <- types.ResponseFlush()
case types.RequestTypeInfo:
case types.MessageType_Info:
data := app.Info()
responses <- types.ResponseInfo(data)
case types.RequestTypeSetOption:
case types.MessageType_SetOption:
logStr := app.SetOption(req.Key, req.Value)
responses <- types.ResponseSetOption(logStr)
case types.RequestTypeAppendTx:
case types.MessageType_AppendTx:
code, result, logStr := app.AppendTx(req.Data)
responses <- types.ResponseAppendTx(code, result, logStr)
case types.RequestTypeCheckTx:
case types.MessageType_CheckTx:
code, result, logStr := app.CheckTx(req.Data)
responses <- types.ResponseCheckTx(code, result, logStr)
case types.RequestTypeGetHash:
case types.MessageType_GetHash:
hash, logStr := app.GetHash()
responses <- types.ResponseGetHash(hash, logStr)
case types.RequestTypeQuery:
case types.MessageType_Query:
code, result, logStr := app.Query(req.Data)
responses <- types.ResponseQuery(code, result, logStr)
default:
@ -134,7 +134,7 @@ func handleResponses(closeConn chan error, responses <-chan *types.Response, con
closeConn <- fmt.Errorf("Error in handleResponses: %v", err.Error())
return
}
if res.Type == types.ResponseTypeFlush {
if res.Type == types.MessageType_Flush {
err = bufWriter.Flush()
if err != nil {
closeConn <- fmt.Errorf("Error in handleResponses: %v", err.Error())

View File

@ -61,7 +61,7 @@ func makeRequest(conn net.Conn, req *types.Request) (*types.Response, error) {
if err != nil {
return nil, err
}
if resFlush.Type != types.ResponseTypeFlush {
if resFlush.Type != types.MessageType_Flush {
return nil, errors.New(Fmt("Expected flush response but got something else: %v", resFlush.Type))
}

View File

@ -7,53 +7,28 @@ import (
"github.com/tendermint/go-wire"
)
const (
RequestTypeEcho = uint32(0x01)
RequestTypeFlush = uint32(0x02)
RequestTypeInfo = uint32(0x03)
RequestTypeSetOption = uint32(0x04)
// reserved for GetOption = uint32(0x05)
ResponseTypeException = uint32(0x10)
ResponseTypeEcho = uint32(0x11)
ResponseTypeFlush = uint32(0x12)
ResponseTypeInfo = uint32(0x13)
ResponseTypeSetOption = uint32(0x14)
// reserved for GetOption = uint32(0x15)
RequestTypeAppendTx = uint32(0x21)
RequestTypeCheckTx = uint32(0x22)
RequestTypeGetHash = uint32(0x23)
RequestTypeQuery = uint32(0x24)
ResponseTypeAppendTx = uint32(0x31)
ResponseTypeCheckTx = uint32(0x32)
ResponseTypeGetHash = uint32(0x33)
ResponseTypeQuery = uint32(0x34)
)
func RequestEcho(message string) *Request {
return &Request{
Type: RequestTypeEcho,
Type: MessageType_Echo,
Data: []byte(message),
}
}
func RequestFlush() *Request {
return &Request{
Type: RequestTypeFlush,
Type: MessageType_Flush,
}
}
func RequestInfo() *Request {
return &Request{
Type: RequestTypeInfo,
Type: MessageType_Info,
}
}
func RequestSetOption(key string, value string) *Request {
return &Request{
Type: RequestTypeSetOption,
Type: MessageType_SetOption,
Key: key,
Value: value,
}
@ -61,27 +36,27 @@ func RequestSetOption(key string, value string) *Request {
func RequestAppendTx(txBytes []byte) *Request {
return &Request{
Type: RequestTypeAppendTx,
Type: MessageType_AppendTx,
Data: txBytes,
}
}
func RequestCheckTx(txBytes []byte) *Request {
return &Request{
Type: RequestTypeCheckTx,
Type: MessageType_CheckTx,
Data: txBytes,
}
}
func RequestGetHash() *Request {
return &Request{
Type: RequestTypeGetHash,
Type: MessageType_GetHash,
}
}
func RequestQuery(queryBytes []byte) *Request {
return &Request{
Type: RequestTypeQuery,
Type: MessageType_Query,
Data: queryBytes,
}
}
@ -90,41 +65,41 @@ func RequestQuery(queryBytes []byte) *Request {
func ResponseException(errStr string) *Response {
return &Response{
Type: ResponseTypeException,
Type: MessageType_Exception,
Error: errStr,
}
}
func ResponseEcho(message string) *Response {
return &Response{
Type: ResponseTypeEcho,
Type: MessageType_Echo,
Data: []byte(message),
}
}
func ResponseFlush() *Response {
return &Response{
Type: ResponseTypeFlush,
Type: MessageType_Flush,
}
}
func ResponseInfo(info string) *Response {
return &Response{
Type: ResponseTypeInfo,
Type: MessageType_Info,
Data: []byte(info),
}
}
func ResponseSetOption(log string) *Response {
return &Response{
Type: ResponseTypeSetOption,
Type: MessageType_SetOption,
Log: log,
}
}
func ResponseAppendTx(code RetCode, result []byte, log string) *Response {
return &Response{
Type: ResponseTypeAppendTx,
Type: MessageType_AppendTx,
Code: uint32(code),
Data: result,
Log: log,
@ -133,7 +108,7 @@ func ResponseAppendTx(code RetCode, result []byte, log string) *Response {
func ResponseCheckTx(code RetCode, result []byte, log string) *Response {
return &Response{
Type: ResponseTypeCheckTx,
Type: MessageType_CheckTx,
Code: uint32(code),
Data: result,
Log: log,
@ -142,7 +117,7 @@ func ResponseCheckTx(code RetCode, result []byte, log string) *Response {
func ResponseGetHash(hash []byte, log string) *Response {
return &Response{
Type: ResponseTypeGetHash,
Type: MessageType_GetHash,
Data: hash,
Log: log,
}
@ -150,7 +125,7 @@ func ResponseGetHash(hash []byte, log string) *Response {
func ResponseQuery(code RetCode, result []byte, log string) *Response {
return &Response{
Type: ResponseTypeQuery,
Type: MessageType_Query,
Code: uint32(code),
Data: result,
Log: log,

View File

@ -23,11 +23,56 @@ var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
type MessageType int32
const (
MessageType_NullMessage MessageType = 0
MessageType_Echo MessageType = 1
MessageType_Flush MessageType = 2
MessageType_Info MessageType = 3
MessageType_SetOption MessageType = 4
MessageType_Exception MessageType = 5
MessageType_AppendTx MessageType = 17
MessageType_CheckTx MessageType = 18
MessageType_GetHash MessageType = 19
MessageType_Query MessageType = 20
)
var MessageType_name = map[int32]string{
0: "NullMessage",
1: "Echo",
2: "Flush",
3: "Info",
4: "SetOption",
5: "Exception",
17: "AppendTx",
18: "CheckTx",
19: "GetHash",
20: "Query",
}
var MessageType_value = map[string]int32{
"NullMessage": 0,
"Echo": 1,
"Flush": 2,
"Info": 3,
"SetOption": 4,
"Exception": 5,
"AppendTx": 17,
"CheckTx": 18,
"GetHash": 19,
"Query": 20,
}
func (x MessageType) String() string {
return proto.EnumName(MessageType_name, int32(x))
}
func (MessageType) EnumDescriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
type Request struct {
Type uint32 `protobuf:"varint,1,opt,name=type" json:"type,omitempty"`
Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"`
Key string `protobuf:"bytes,3,opt,name=key" json:"key,omitempty"`
Value string `protobuf:"bytes,4,opt,name=value" json:"value,omitempty"`
Type MessageType `protobuf:"varint,1,opt,name=type,enum=types.MessageType" json:"type,omitempty"`
Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"`
Key string `protobuf:"bytes,3,opt,name=key" json:"key,omitempty"`
Value string `protobuf:"bytes,4,opt,name=value" json:"value,omitempty"`
}
func (m *Request) Reset() { *m = Request{} }
@ -36,11 +81,11 @@ func (*Request) ProtoMessage() {}
func (*Request) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
type Response struct {
Type uint32 `protobuf:"varint,1,opt,name=type" json:"type,omitempty"`
Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"`
Code uint32 `protobuf:"varint,3,opt,name=code" json:"code,omitempty"`
Error string `protobuf:"bytes,4,opt,name=error" json:"error,omitempty"`
Log string `protobuf:"bytes,5,opt,name=log" json:"log,omitempty"`
Type MessageType `protobuf:"varint,1,opt,name=type,enum=types.MessageType" json:"type,omitempty"`
Data []byte `protobuf:"bytes,2,opt,name=data,proto3" json:"data,omitempty"`
Code uint32 `protobuf:"varint,3,opt,name=code" json:"code,omitempty"`
Error string `protobuf:"bytes,4,opt,name=error" json:"error,omitempty"`
Log string `protobuf:"bytes,5,opt,name=log" json:"log,omitempty"`
}
func (m *Response) Reset() { *m = Response{} }
@ -51,19 +96,27 @@ func (*Response) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1}
func init() {
proto.RegisterType((*Request)(nil), "types.Request")
proto.RegisterType((*Response)(nil), "types.Response")
proto.RegisterEnum("types.MessageType", MessageType_name, MessageType_value)
}
var fileDescriptor0 = []byte{
// 165 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0x8c, 0x8f, 0xb1, 0xae, 0xc2, 0x30,
0x0c, 0x45, 0xd5, 0xd7, 0xe6, 0x01, 0x16, 0x48, 0x10, 0x31, 0x64, 0x44, 0x9d, 0x98, 0x60, 0xe0,
0x4f, 0xb2, 0x31, 0x06, 0x6a, 0x31, 0x50, 0xd5, 0x21, 0x49, 0x91, 0xfa, 0xf7, 0xd8, 0xae, 0xd8,
0x59, 0xa2, 0x73, 0x8f, 0xa2, 0xab, 0x6b, 0xd8, 0x95, 0x29, 0x62, 0x3e, 0xeb, 0x7b, 0x8a, 0x89,
0x0a, 0x59, 0xa3, 0xa1, 0xbd, 0xc2, 0xc2, 0xe3, 0x6b, 0xc4, 0x5c, 0xac, 0x85, 0x46, 0x9c, 0xab,
0x0e, 0xd5, 0x71, 0xe3, 0x95, 0xc5, 0x75, 0xa1, 0x04, 0xf7, 0xc7, 0x6e, 0xed, 0x95, 0xed, 0x16,
0xea, 0x27, 0x4e, 0xae, 0x66, 0xb5, 0xf2, 0x82, 0x76, 0x0f, 0xe6, 0x1d, 0xfa, 0x11, 0x5d, 0xa3,
0x6e, 0x0e, 0xed, 0x00, 0x4b, 0x8f, 0x39, 0xd2, 0x90, 0xf1, 0xe7, 0x6e, 0x76, 0x77, 0xea, 0x50,
0xcb, 0xf9, 0x9f, 0xb0, 0xb4, 0x63, 0x4a, 0x94, 0xbe, 0xed, 0x1a, 0x64, 0x45, 0x4f, 0x0f, 0x67,
0xe6, 0x15, 0x8c, 0xb7, 0x7f, 0x3d, 0xec, 0xf2, 0x09, 0x00, 0x00, 0xff, 0xff, 0xce, 0x9d, 0x3d,
0x4f, 0xed, 0x00, 0x00, 0x00,
// 284 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xa4, 0x91, 0x4d, 0x4e, 0xc3, 0x30,
0x10, 0x85, 0x49, 0x9b, 0xd0, 0x64, 0xd2, 0x82, 0x3b, 0x74, 0x91, 0x25, 0xea, 0x02, 0x21, 0x16,
0x45, 0x82, 0x13, 0x20, 0x54, 0x7e, 0x16, 0x80, 0x30, 0xbd, 0x40, 0x48, 0x86, 0x06, 0x35, 0xc4,
0x26, 0xb6, 0x51, 0xb3, 0xe5, 0x00, 0x9c, 0x19, 0xdb, 0x01, 0x89, 0x3d, 0x9b, 0xe8, 0x7d, 0x5f,
0x46, 0xf3, 0x46, 0x32, 0x4c, 0x75, 0x27, 0x49, 0x9d, 0xfa, 0xef, 0x42, 0xb6, 0x42, 0x0b, 0x8c,
0x3c, 0xcc, 0xdf, 0x60, 0xc4, 0xe9, 0xdd, 0x90, 0xd2, 0x78, 0x04, 0xa1, 0x73, 0x59, 0x70, 0x18,
0x1c, 0xef, 0x9d, 0xe1, 0xa2, 0x9f, 0xbe, 0x23, 0xa5, 0xf2, 0x35, 0xad, 0x2c, 0x70, 0xff, 0x1f,
0x11, 0xc2, 0x32, 0xd7, 0x79, 0x36, 0xb0, 0x73, 0x63, 0xee, 0x33, 0x32, 0x18, 0x6e, 0xa8, 0xcb,
0x86, 0x56, 0x25, 0xdc, 0x45, 0x9c, 0x41, 0xf4, 0x91, 0xd7, 0x86, 0xb2, 0xd0, 0xbb, 0x1e, 0xe6,
0x9f, 0x01, 0xc4, 0x9c, 0x94, 0x14, 0x8d, 0xa2, 0x7f, 0x15, 0x5a, 0x57, 0x88, 0x92, 0x7c, 0xe3,
0x84, 0xfb, 0xec, 0x2a, 0xa9, 0x6d, 0x45, 0xfb, 0x5b, 0xe9, 0xc1, 0x9d, 0x56, 0x8b, 0x75, 0x16,
0xf5, 0xa7, 0xd9, 0x78, 0xf2, 0x15, 0x40, 0xfa, 0xa7, 0x05, 0xf7, 0x21, 0xbd, 0x37, 0x75, 0xfd,
0xa3, 0xd8, 0x0e, 0xc6, 0x10, 0x2e, 0x8b, 0x4a, 0xb0, 0x00, 0x13, 0x88, 0xae, 0x6a, 0xa3, 0x2a,
0x36, 0x70, 0xf2, 0xb6, 0x79, 0x11, 0x6c, 0x88, 0x13, 0x48, 0x9e, 0x48, 0x3f, 0x48, 0xfd, 0x2a,
0x1a, 0x16, 0x3a, 0x5c, 0x6e, 0x0b, 0xea, 0x31, 0xc2, 0x31, 0xc4, 0x17, 0x52, 0x52, 0x53, 0xae,
0xb6, 0x6c, 0x8a, 0x29, 0x8c, 0x2e, 0x2b, 0x2a, 0x36, 0x16, 0xd0, 0xc1, 0x35, 0xe9, 0x9b, 0xdc,
0xee, 0x3b, 0x70, 0xab, 0x1f, 0x0d, 0xb5, 0x1d, 0x9b, 0x3d, 0xef, 0xfa, 0x27, 0x39, 0xff, 0x0e,
0x00, 0x00, 0xff, 0xff, 0x14, 0xe1, 0x50, 0x1f, 0xa7, 0x01, 0x00, 0x00,
}

View File

@ -1,35 +1,30 @@
syntax = "proto3";
package types;
// This file is copied from http://github.com/tendermint/tmsp
//----------------------------------------
// Message types
/*
RequestTypeEcho = 0x01;
RequestTypeFlush = 0x02;
RequestTypeInfo = 0x03;
RequestTypeSetOption = 0x04;
RequestTypeAppendTx = 0x21;
RequestTypeCheckTx = 0x22;
RequestTypeGetHash = 0x23;
RequestTypeQuery = 0x24;
enum MessageType {
NullMessage = 0x00;
ResponseTypeEcho = 0x11;
ResponseTypeFlush = 0x12;
ResponseTypeInfo = 0x13;
ResponseTypeSetOption = 0x14;
ResponseTypeAppendTx = 0x31;
ResponseTypeCheckTx = 0x32;
ResponseTypeGetHash = 0x33;
ResponseTypeQuery = 0x34;
*/
Echo = 0x01;
Flush = 0x02;
Info = 0x03;
SetOption = 0x04;
Exception = 0x05;
AppendTx = 0x11;
CheckTx = 0x12;
GetHash = 0x13;
Query = 0x14;
}
//----------------------------------------
// Request types
message Request {
uint32 type = 1;
MessageType type = 1;
bytes data = 2;
string key = 3;
string value = 4;
@ -39,9 +34,10 @@ message Request {
// Response types
message Response {
uint32 type = 1;
MessageType type = 1;
bytes data = 2;
uint32 code = 3;
string error = 4;
string log = 5;
}