github.com/rohankumardubey/proxyfs@v0.0.0-20210108201508-653efa9ab00e/pfsagentd/jrpc.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  )
     7  
     8  type jrpcRequestMethodAndIDStruct struct {
     9  	Method string `json:"method"`
    10  	ID     uint64 `json:"id"`
    11  }
    12  
    13  type jrpcRequestStruct struct {
    14  	JSONrpc string         `json:"jsonrpc"`
    15  	Method  string         `json:"method"`
    16  	ID      uint64         `json:"id"`
    17  	Params  [1]interface{} `json:"params"`
    18  }
    19  
    20  type jrpcResponseIDStruct struct {
    21  	ID uint64 `json:"id"`
    22  }
    23  
    24  type jrpcResponseIDAndErrorStruct struct {
    25  	ID    uint64 `json:"id"`
    26  	Error string `json:"error"`
    27  }
    28  
    29  type jrpcResponseNoErrorStruct struct {
    30  	ID     uint64      `json:"id"`
    31  	Result interface{} `json:"result"`
    32  }
    33  
    34  type jrpcResponseWithErrorStruct struct {
    35  	ID     uint64      `json:"id"`
    36  	Error  string      `json:"error"`
    37  	Result interface{} `json:"result"`
    38  }
    39  
    40  func jrpcMarshalRequest(requestMethod string, request interface{}) (requestID uint64, requestBuf []byte, marshalErr error) {
    41  	var (
    42  		jrpcRequest *jrpcRequestStruct
    43  	)
    44  
    45  	globals.Lock()
    46  	requestID = globals.jrpcLastID + 1
    47  	globals.jrpcLastID = requestID
    48  	globals.Unlock()
    49  
    50  	jrpcRequest = &jrpcRequestStruct{
    51  		JSONrpc: "2.0",
    52  		Method:  requestMethod,
    53  		ID:      requestID,
    54  		Params:  [1]interface{}{request},
    55  	}
    56  
    57  	requestBuf, marshalErr = json.Marshal(jrpcRequest)
    58  
    59  	return
    60  }
    61  
    62  func jrpcMarshalResponse(requestID uint64, responseError error, response interface{}) (responseBuf []byte, marshalErr error) {
    63  	var (
    64  		jrpcResponse interface{}
    65  	)
    66  
    67  	if nil == responseError {
    68  		if nil == response {
    69  			jrpcResponse = &jrpcResponseIDStruct{
    70  				ID: requestID,
    71  			}
    72  		} else {
    73  			jrpcResponse = &jrpcResponseNoErrorStruct{
    74  				ID:     requestID,
    75  				Result: response,
    76  			}
    77  		}
    78  	} else {
    79  		if nil == response {
    80  			jrpcResponse = &jrpcResponseIDAndErrorStruct{
    81  				ID:    requestID,
    82  				Error: responseError.Error(),
    83  			}
    84  		} else {
    85  			jrpcResponse = &jrpcResponseWithErrorStruct{
    86  				ID:     requestID,
    87  				Error:  responseError.Error(),
    88  				Result: response,
    89  			}
    90  		}
    91  	}
    92  
    93  	responseBuf, marshalErr = json.Marshal(jrpcResponse)
    94  
    95  	return
    96  }
    97  
    98  func jrpcUnmarshalRequestForMethodAndID(requestBuf []byte) (requestMethod string, requestID uint64, unmarshalErr error) {
    99  	var (
   100  		jrpcRequest *jrpcRequestMethodAndIDStruct
   101  	)
   102  
   103  	jrpcRequest = &jrpcRequestMethodAndIDStruct{}
   104  
   105  	unmarshalErr = json.Unmarshal(requestBuf, jrpcRequest)
   106  
   107  	if nil == unmarshalErr {
   108  		requestMethod = jrpcRequest.Method
   109  		requestID = jrpcRequest.ID
   110  	}
   111  
   112  	return
   113  }
   114  
   115  func jrpcUnmarshalRequest(requestID uint64, requestBuf []byte, request interface{}) (unmarshalErr error) {
   116  	var (
   117  		jrpcRequest *jrpcRequestStruct
   118  	)
   119  
   120  	jrpcRequest = &jrpcRequestStruct{
   121  		Params: [1]interface{}{request},
   122  	}
   123  
   124  	unmarshalErr = json.Unmarshal(requestBuf, jrpcRequest)
   125  
   126  	if (nil == unmarshalErr) && (requestID != jrpcRequest.ID) {
   127  		unmarshalErr = fmt.Errorf("requestID mismatch")
   128  	}
   129  
   130  	return
   131  }
   132  
   133  func jrpcUnmarshalResponseForIDAndError(responseBuf []byte) (requestID uint64, responseErr error, unmarshalErr error) {
   134  	var (
   135  		jrpcResponse *jrpcResponseIDAndErrorStruct
   136  	)
   137  
   138  	jrpcResponse = &jrpcResponseIDAndErrorStruct{}
   139  
   140  	unmarshalErr = json.Unmarshal(responseBuf, jrpcResponse)
   141  
   142  	if nil == unmarshalErr {
   143  		requestID = jrpcResponse.ID
   144  		if "" == jrpcResponse.Error {
   145  			responseErr = nil
   146  		} else {
   147  			responseErr = fmt.Errorf("%s", jrpcResponse.Error)
   148  		}
   149  	}
   150  
   151  	return
   152  }
   153  
   154  func jrpcUnmarshalResponse(requestID uint64, responseBuf []byte, response interface{}) (unmarshalErr error) {
   155  	var (
   156  		jrpcResponse *jrpcResponseWithErrorStruct
   157  	)
   158  
   159  	jrpcResponse = &jrpcResponseWithErrorStruct{
   160  		Result: response,
   161  	}
   162  
   163  	unmarshalErr = json.Unmarshal(responseBuf, jrpcResponse)
   164  
   165  	if (nil == unmarshalErr) && (requestID != jrpcResponse.ID) {
   166  		unmarshalErr = fmt.Errorf("requestID mismatch")
   167  	}
   168  
   169  	return
   170  }