diff --git a/client/http_client.go b/client/http_client.go index 0c8bb6363..c11f8b3e3 100644 --- a/client/http_client.go +++ b/client/http_client.go @@ -11,8 +11,7 @@ import ( "reflect" "strings" - // cmn "github.com/tendermint/go-common" - rpctypes "github.com/tendermint/go-rpc/types" + types "github.com/tendermint/go-rpc/types" wire "github.com/tendermint/go-wire" ) @@ -28,7 +27,7 @@ func makeHTTPDialer(remoteAddr string) (string, func(string, string) (net.Conn, var protocol, address string if len(parts) != 2 { log.Warn("WARNING (go-rpc): Please use fully formed listening addresses, including the tcp:// or unix:// prefix") - protocol = rpctypes.SocketType(remoteAddr) + protocol = types.SocketType(remoteAddr) address = remoteAddr } else { protocol, address = parts[0], parts[1] @@ -73,7 +72,7 @@ func (c *ClientJSONRPC) Call(method string, params map[string]interface{}, resul func (c *ClientJSONRPC) call(method string, params map[string]interface{}, result interface{}) (interface{}, error) { // Make request and get responseBytes - request := rpctypes.RPCRequest{ + request := types.RPCRequest{ JSONRPC: "2.0", Method: method, Params: params, @@ -144,7 +143,7 @@ func unmarshalResponseBytes(responseBytes []byte, result interface{}) (interface // into the correct type // log.Notice("response", "response", string(responseBytes)) var err error - response := &rpctypes.RPCResponse{} + response := &types.RPCResponse{} err = json.Unmarshal(responseBytes, response) if err != nil { return nil, fmt.Errorf("Error unmarshalling rpc response: %v", err) diff --git a/client/ws_client.go b/client/ws_client.go index e5135d0af..d27e499dd 100644 --- a/client/ws_client.go +++ b/client/ws_client.go @@ -9,7 +9,7 @@ import ( "github.com/gorilla/websocket" cmn "github.com/tendermint/go-common" - rpctypes "github.com/tendermint/go-rpc/types" + types "github.com/tendermint/go-rpc/types" ) const ( @@ -96,7 +96,7 @@ func (wsc *WSClient) receiveEventsRoutine() { wsc.Stop() break } else { - var response rpctypes.RPCResponse + var response types.RPCResponse err := json.Unmarshal(data, &response) if err != nil { log.Info("WSClient failed to parse message", "error", err, "data", string(data)) @@ -118,7 +118,7 @@ func (wsc *WSClient) receiveEventsRoutine() { // subscribe to an event func (wsc *WSClient) Subscribe(eventid string) error { - err := wsc.WriteJSON(rpctypes.RPCRequest{ + err := wsc.WriteJSON(types.RPCRequest{ JSONRPC: "2.0", ID: "", Method: "subscribe", @@ -129,7 +129,7 @@ func (wsc *WSClient) Subscribe(eventid string) error { // unsubscribe from an event func (wsc *WSClient) Unsubscribe(eventid string) error { - err := wsc.WriteJSON(rpctypes.RPCRequest{ + err := wsc.WriteJSON(types.RPCRequest{ JSONRPC: "2.0", ID: "", Method: "unsubscribe", diff --git a/rpc_test.go b/rpc_test.go index 2c45c4c13..c77799199 100644 --- a/rpc_test.go +++ b/rpc_test.go @@ -5,14 +5,14 @@ import ( "testing" "time" - rpcclient "github.com/tendermint/go-rpc/client" - rpcserver "github.com/tendermint/go-rpc/server" - rpctypes "github.com/tendermint/go-rpc/types" + client "github.com/tendermint/go-rpc/client" + server "github.com/tendermint/go-rpc/server" + types "github.com/tendermint/go-rpc/types" wire "github.com/tendermint/go-wire" ) // Client and Server should work over tcp or unix sockets -var ( +const ( tcpAddr = "tcp://0.0.0.0:46657" unixAddr = "unix:///tmp/go-rpc.sock" // NOTE: must remove file for test to run again @@ -32,8 +32,8 @@ var _ = wire.RegisterInterface( ) // Define some routes -var Routes = map[string]*rpcserver.RPCFunc{ - "status": rpcserver.NewRPCFunc(StatusResult, "arg"), +var Routes = map[string]*server.RPCFunc{ + "status": server.NewRPCFunc(StatusResult, "arg"), } // an rpc function @@ -43,23 +43,24 @@ func StatusResult(v string) (Result, error) { // launch unix and tcp servers func init() { + mux := http.NewServeMux() - rpcserver.RegisterRPCFuncs(mux, Routes) - wm := rpcserver.NewWebsocketManager(Routes, nil) + server.RegisterRPCFuncs(mux, Routes) + wm := server.NewWebsocketManager(Routes, nil) mux.HandleFunc(websocketEndpoint, wm.WebsocketHandler) go func() { - _, err := rpcserver.StartHTTPServer(tcpAddr, mux) + _, err := server.StartHTTPServer(tcpAddr, mux) if err != nil { panic(err) } }() mux2 := http.NewServeMux() - rpcserver.RegisterRPCFuncs(mux2, Routes) - wm = rpcserver.NewWebsocketManager(Routes, nil) + server.RegisterRPCFuncs(mux2, Routes) + wm = server.NewWebsocketManager(Routes, nil) mux2.HandleFunc(websocketEndpoint, wm.WebsocketHandler) go func() { - _, err := rpcserver.StartHTTPServer(unixAddr, mux2) + _, err := server.StartHTTPServer(unixAddr, mux2) if err != nil { panic(err) } @@ -70,7 +71,7 @@ func init() { } -func testURI(t *testing.T, cl *rpcclient.ClientURI) { +func testURI(t *testing.T, cl *client.ClientURI) { val := "acbd" params := map[string]interface{}{ "arg": val, @@ -86,7 +87,7 @@ func testURI(t *testing.T, cl *rpcclient.ClientURI) { } } -func testJSONRPC(t *testing.T, cl *rpcclient.ClientJSONRPC) { +func testJSONRPC(t *testing.T, cl *client.ClientJSONRPC) { val := "acbd" params := map[string]interface{}{ "arg": val, @@ -102,12 +103,12 @@ func testJSONRPC(t *testing.T, cl *rpcclient.ClientJSONRPC) { } } -func testWS(t *testing.T, cl *rpcclient.WSClient) { +func testWS(t *testing.T, cl *client.WSClient) { val := "acbd" params := map[string]interface{}{ "arg": val, } - err := cl.WriteJSON(rpctypes.RPCRequest{ + err := cl.WriteJSON(types.RPCRequest{ JSONRPC: "2.0", ID: "", Method: "status", @@ -132,27 +133,27 @@ func testWS(t *testing.T, cl *rpcclient.WSClient) { //------------- func TestURI_TCP(t *testing.T) { - cl := rpcclient.NewClientURI(tcpAddr) + cl := client.NewClientURI(tcpAddr) testURI(t, cl) } func TestURI_UNIX(t *testing.T) { - cl := rpcclient.NewClientURI(unixAddr) + cl := client.NewClientURI(unixAddr) testURI(t, cl) } func TestJSONRPC_TCP(t *testing.T) { - cl := rpcclient.NewClientJSONRPC(tcpAddr) + cl := client.NewClientJSONRPC(tcpAddr) testJSONRPC(t, cl) } func TestJSONRPC_UNIX(t *testing.T) { - cl := rpcclient.NewClientJSONRPC(unixAddr) + cl := client.NewClientJSONRPC(unixAddr) testJSONRPC(t, cl) } func TestWS_TCP(t *testing.T) { - cl := rpcclient.NewWSClient(tcpAddr, websocketEndpoint) + cl := client.NewWSClient(tcpAddr, websocketEndpoint) _, err := cl.Start() if err != nil { t.Fatal(err) @@ -161,7 +162,7 @@ func TestWS_TCP(t *testing.T) { } func TestWS_UNIX(t *testing.T) { - cl := rpcclient.NewWSClient(unixAddr, websocketEndpoint) + cl := client.NewWSClient(unixAddr, websocketEndpoint) _, err := cl.Start() if err != nil { t.Fatal(err) @@ -170,7 +171,7 @@ func TestWS_UNIX(t *testing.T) { } func TestHexStringArg(t *testing.T) { - cl := rpcclient.NewClientURI(tcpAddr) + cl := client.NewClientURI(tcpAddr) // should NOT be handled as hex val := "0xabc" params := map[string]interface{}{ @@ -188,7 +189,7 @@ func TestHexStringArg(t *testing.T) { } func TestQuotedStringArg(t *testing.T) { - cl := rpcclient.NewClientURI(tcpAddr) + cl := client.NewClientURI(tcpAddr) // should NOT be unquoted val := "\"abc\"" params := map[string]interface{}{ diff --git a/server/http_server.go b/server/http_server.go index 26163cf12..24b9f18af 100644 --- a/server/http_server.go +++ b/server/http_server.go @@ -11,9 +11,7 @@ import ( "strings" "time" - . "github.com/tendermint/go-common" - . "github.com/tendermint/go-rpc/types" - //"github.com/tendermint/go-wire" + types "github.com/tendermint/go-rpc/types" ) func StartHTTPServer(listenAddr string, handler http.Handler) (listener net.Listener, err error) { @@ -24,14 +22,14 @@ func StartHTTPServer(listenAddr string, handler http.Handler) (listener net.List log.Warn("WARNING (go-rpc): Please use fully formed listening addresses, including the tcp:// or unix:// prefix") // we used to allow addrs without tcp/unix prefix by checking for a colon // TODO: Deprecate - proto = SocketType(listenAddr) + proto = types.SocketType(listenAddr) addr = listenAddr // return nil, fmt.Errorf("Invalid listener address %s", lisenAddr) } else { proto, addr = parts[0], parts[1] } - log.Notice(Fmt("Starting RPC HTTP server on %s socket %v", proto, addr)) + log.Notice(fmt.Sprintf("Starting RPC HTTP server on %s socket %v", proto, addr)) listener, err = net.Listen(proto, addr) if err != nil { return nil, fmt.Errorf("Failed to listen to %v: %v", listenAddr, err) @@ -47,7 +45,7 @@ func StartHTTPServer(listenAddr string, handler http.Handler) (listener net.List return listener, nil } -func WriteRPCResponseHTTP(w http.ResponseWriter, res RPCResponse) { +func WriteRPCResponseHTTP(w http.ResponseWriter, res types.RPCResponse) { // jsonBytes := wire.JSONBytesPretty(res) jsonBytes, err := json.Marshal(res) if err != nil { @@ -83,13 +81,13 @@ func RecoverAndLogHandler(handler http.Handler) http.Handler { if e := recover(); e != nil { // If RPCResponse - if res, ok := e.(RPCResponse); ok { + if res, ok := e.(types.RPCResponse); ok { WriteRPCResponseHTTP(rww, res) } else { // For the rest, log.Error("Panic in RPC HTTP handler", "error", e, "stack", string(debug.Stack())) rww.WriteHeader(http.StatusInternalServerError) - WriteRPCResponseHTTP(rww, NewRPCResponse("", nil, Fmt("Internal Server Error: %v", e))) + WriteRPCResponseHTTP(rww, types.NewRPCResponse("", nil, fmt.Sprintf("Internal Server Error: %v", e))) } }