Browse Source

Merge pull request #3 from tendermint/develop

use EventSwitch interface; less logging
pull/456/head
Ethan Buchman 8 years ago
committed by GitHub
parent
commit
e6e3853dc7
3 changed files with 21 additions and 15 deletions
  1. +8
    -4
      client/http_client.go
  2. +12
    -10
      server/handlers.go
  3. +1
    -1
      types/types.go

+ 8
- 4
client/http_client.go View File

@ -119,7 +119,7 @@ func (c *ClientURI) call(method string, params map[string]interface{}, result in
if err != nil {
return nil, err
}
log.Info(Fmt("URI request to %v (%v): %v", c.address, method, values))
//log.Info(Fmt("URI request to %v (%v): %v", c.address, method, values))
resp, err := c.client.PostForm(c.address+"/"+method, values)
if err != nil {
return nil, err
@ -138,19 +138,23 @@ func unmarshalResponseBytes(responseBytes []byte, result interface{}) (interface
// read response
// if rpc/core/types is imported, the result will unmarshal
// into the correct type
// log.Notice("response", "response", string(responseBytes))
var err error
response := &rpctypes.RPCResponse{}
err = json.Unmarshal(responseBytes, response)
if err != nil {
return nil, err
return nil, errors.New(Fmt("Error unmarshalling rpc response: %v", err))
}
errorStr := response.Error
if errorStr != "" {
return nil, errors.New(errorStr)
return nil, errors.New(Fmt("Response error: %v", errorStr))
}
// unmarshal the RawMessage into the result
result = wire.ReadJSONPtr(result, *response.Result, &err)
return result, err
if err != nil {
return nil, errors.New(Fmt("Error unmarshalling rpc response result: %v", err))
}
return result, nil
}
func argsToURLValues(args map[string]interface{}) (url.Values, error) {


+ 12
- 10
server/handlers.go View File

@ -107,7 +107,7 @@ func makeJSONRPCHandler(funcMap map[string]*RPCFunc) http.HandlerFunc {
var request RPCRequest
err := json.Unmarshal(b, &request)
if err != nil {
WriteRPCResponseHTTP(w, NewRPCResponse("", nil, err.Error()))
WriteRPCResponseHTTP(w, NewRPCResponse("", nil, fmt.Sprintf("Error unmarshalling request: %v", err.Error())))
return
}
if len(r.URL.Path) > 1 {
@ -125,14 +125,14 @@ func makeJSONRPCHandler(funcMap map[string]*RPCFunc) http.HandlerFunc {
}
args, err := jsonParamsToArgs(rpcFunc, request.Params)
if err != nil {
WriteRPCResponseHTTP(w, NewRPCResponse(request.ID, nil, err.Error()))
WriteRPCResponseHTTP(w, NewRPCResponse(request.ID, nil, fmt.Sprintf("Error converting json params to arguments: %v", err.Error())))
return
}
returns := rpcFunc.f.Call(args)
log.Info("HTTPJSONRPC", "method", request.Method, "args", args, "returns", returns)
result, err := unreflectResult(returns)
if err != nil {
WriteRPCResponseHTTP(w, NewRPCResponse(request.ID, result, err.Error()))
WriteRPCResponseHTTP(w, NewRPCResponse(request.ID, result, fmt.Sprintf("Error unreflecting result: %v", err.Error())))
return
}
WriteRPCResponseHTTP(w, NewRPCResponse(request.ID, result, ""))
@ -201,16 +201,17 @@ func makeHTTPHandler(rpcFunc *RPCFunc) func(http.ResponseWriter, *http.Request)
}
// All other endpoints
return func(w http.ResponseWriter, r *http.Request) {
log.Debug("HTTP HANDLER", "req", r)
args, err := httpParamsToArgs(rpcFunc, r)
if err != nil {
WriteRPCResponseHTTP(w, NewRPCResponse("", nil, err.Error()))
WriteRPCResponseHTTP(w, NewRPCResponse("", nil, fmt.Sprintf("Error converting http params to args: %v", err.Error())))
return
}
returns := rpcFunc.f.Call(args)
log.Info("HTTPRestRPC", "method", r.URL.Path, "args", args, "returns", returns)
result, err := unreflectResult(returns)
if err != nil {
WriteRPCResponseHTTP(w, NewRPCResponse("", nil, err.Error()))
WriteRPCResponseHTTP(w, NewRPCResponse("", nil, fmt.Sprintf("Error unreflecting result: %v", err.Error())))
return
}
WriteRPCResponseHTTP(w, NewRPCResponse("", result, ""))
@ -228,6 +229,7 @@ func httpParamsToArgs(rpcFunc *RPCFunc, r *http.Request) ([]reflect.Value, error
for i, name := range argNames {
ty := argTypes[i]
arg := GetParam(r, name)
//log.Notice("param to arg", "ty", ty, "name", name, "arg", arg)
values[i], err = _jsonStringToArg(ty, arg)
if err != nil {
return nil, err
@ -271,11 +273,11 @@ type wsConnection struct {
pingTicker *time.Ticker
funcMap map[string]*RPCFunc
evsw *events.EventSwitch
evsw events.EventSwitch
}
// new websocket connection wrapper
func NewWSConnection(baseConn *websocket.Conn, funcMap map[string]*RPCFunc, evsw *events.EventSwitch) *wsConnection {
func NewWSConnection(baseConn *websocket.Conn, funcMap map[string]*RPCFunc, evsw events.EventSwitch) *wsConnection {
wsc := &wsConnection{
remoteAddr: baseConn.RemoteAddr().String(),
baseConn: baseConn,
@ -341,7 +343,7 @@ func (wsc *wsConnection) GetRemoteAddr() string {
}
// Implements WSRPCConnection
func (wsc *wsConnection) GetEventSwitch() *events.EventSwitch {
func (wsc *wsConnection) GetEventSwitch() events.EventSwitch {
return wsc.evsw
}
@ -472,10 +474,10 @@ func (wsc *wsConnection) writeRoutine() {
type WebsocketManager struct {
websocket.Upgrader
funcMap map[string]*RPCFunc
evsw *events.EventSwitch
evsw events.EventSwitch
}
func NewWebsocketManager(funcMap map[string]*RPCFunc, evsw *events.EventSwitch) *WebsocketManager {
func NewWebsocketManager(funcMap map[string]*RPCFunc, evsw events.EventSwitch) *WebsocketManager {
return &WebsocketManager{
funcMap: funcMap,
evsw: evsw,


+ 1
- 1
types/types.go View File

@ -68,7 +68,7 @@ func NewRPCResponse(id string, res interface{}, err string) RPCResponse {
// *wsConnection implements this interface.
type WSRPCConnection interface {
GetRemoteAddr() string
GetEventSwitch() *events.EventSwitch
GetEventSwitch() events.EventSwitch
WriteRPCResponse(resp RPCResponse)
TryWriteRPCResponse(resp RPCResponse) bool
}


Loading…
Cancel
Save