mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-27 03:31:42 +00:00
move from tendermint/tendermint
This commit is contained in:
133
client/http_client.go
Normal file
133
client/http_client.go
Normal file
@ -0,0 +1,133 @@
|
||||
package rpcclient
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
. "github.com/tendermint/go-common"
|
||||
"github.com/tendermint/go-rpc/types"
|
||||
"github.com/tendermint/go-wire"
|
||||
)
|
||||
|
||||
// JSON rpc takes params as a slice
|
||||
type ClientJSONRPC struct {
|
||||
remote string
|
||||
}
|
||||
|
||||
func NewClientJSONRPC(remote string) *ClientJSONRPC {
|
||||
return &ClientJSONRPC{remote}
|
||||
}
|
||||
|
||||
func (c *ClientJSONRPC) Call(method string, params []interface{}) (interface{}, error) {
|
||||
return CallHTTP_JSONRPC(c.remote, method, params)
|
||||
}
|
||||
|
||||
// URI takes params as a map
|
||||
type ClientURI struct {
|
||||
remote string
|
||||
}
|
||||
|
||||
func NewClientURI(remote string) *ClientURI {
|
||||
if !strings.HasSuffix(remote, "/") {
|
||||
remote = remote + "/"
|
||||
}
|
||||
return &ClientURI{remote}
|
||||
}
|
||||
|
||||
func (c *ClientURI) Call(method string, params map[string]interface{}) (interface{}, error) {
|
||||
return CallHTTP_URI(c.remote, method, params)
|
||||
}
|
||||
|
||||
func CallHTTP_JSONRPC(remote string, method string, params []interface{}) (interface{}, error) {
|
||||
// Make request and get responseBytes
|
||||
request := rpctypes.RPCRequest{
|
||||
JSONRPC: "2.0",
|
||||
Method: method,
|
||||
Params: params,
|
||||
ID: "",
|
||||
}
|
||||
requestBytes := wire.JSONBytes(request)
|
||||
requestBuf := bytes.NewBuffer(requestBytes)
|
||||
log.Info(Fmt("RPC request to %v: %v", remote, string(requestBytes)))
|
||||
httpResponse, err := http.Post(remote, "text/json", requestBuf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer httpResponse.Body.Close()
|
||||
responseBytes, err := ioutil.ReadAll(httpResponse.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Info(Fmt("RPC response: %v", string(responseBytes)))
|
||||
return unmarshalResponseBytes(responseBytes)
|
||||
}
|
||||
|
||||
func CallHTTP_URI(remote string, method string, params map[string]interface{}) (interface{}, error) {
|
||||
values, err := argsToURLValues(params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Info(Fmt("URI request to %v: %v", remote, values))
|
||||
resp, err := http.PostForm(remote+method, values)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
responseBytes, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return unmarshalResponseBytes(responseBytes)
|
||||
}
|
||||
|
||||
//------------------------------------------------
|
||||
|
||||
func unmarshalResponseBytes(responseBytes []byte) (interface{}, error) {
|
||||
// read response
|
||||
// if rpc/core/types is imported, the result will unmarshal
|
||||
// into the correct type
|
||||
var err error
|
||||
response := &rpctypes.RPCResponse{}
|
||||
wire.ReadJSON(response, responseBytes, &err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
errorStr := response.Error
|
||||
if errorStr != "" {
|
||||
return nil, errors.New(errorStr)
|
||||
}
|
||||
return response.Result, err
|
||||
}
|
||||
|
||||
func argsToURLValues(args map[string]interface{}) (url.Values, error) {
|
||||
values := make(url.Values)
|
||||
if len(args) == 0 {
|
||||
return values, nil
|
||||
}
|
||||
err := argsToJson(args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for key, val := range args {
|
||||
values.Set(key, val.(string))
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
|
||||
func argsToJson(args map[string]interface{}) error {
|
||||
var n int
|
||||
var err error
|
||||
for k, v := range args {
|
||||
buf := new(bytes.Buffer)
|
||||
wire.WriteJSON(v, buf, &n, &err)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
args[k] = buf.String()
|
||||
}
|
||||
return nil
|
||||
}
|
7
client/log.go
Normal file
7
client/log.go
Normal file
@ -0,0 +1,7 @@
|
||||
package rpcclient
|
||||
|
||||
import (
|
||||
"github.com/tendermint/log15"
|
||||
)
|
||||
|
||||
var log = log15.New("module", "rpcclient")
|
119
client/ws_client.go
Normal file
119
client/ws_client.go
Normal file
@ -0,0 +1,119 @@
|
||||
package rpcclient
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
. "github.com/tendermint/go-common"
|
||||
"github.com/tendermint/go-rpc/types"
|
||||
"github.com/tendermint/go-wire"
|
||||
)
|
||||
|
||||
const (
|
||||
wsResultsChannelCapacity = 10
|
||||
wsWriteTimeoutSeconds = 10
|
||||
)
|
||||
|
||||
type WSClient struct {
|
||||
QuitService
|
||||
Address string
|
||||
*websocket.Conn
|
||||
ResultsCh chan rpctypes.Result // closes upon WSClient.Stop()
|
||||
}
|
||||
|
||||
// create a new connection
|
||||
func NewWSClient(addr string) *WSClient {
|
||||
wsClient := &WSClient{
|
||||
Address: addr,
|
||||
Conn: nil,
|
||||
ResultsCh: make(chan rpctypes.Result, wsResultsChannelCapacity),
|
||||
}
|
||||
wsClient.QuitService = *NewQuitService(log, "WSClient", wsClient)
|
||||
return wsClient
|
||||
}
|
||||
|
||||
func (wsc *WSClient) OnStart() error {
|
||||
wsc.QuitService.OnStart()
|
||||
err := wsc.dial()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
go wsc.receiveEventsRoutine()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (wsc *WSClient) dial() error {
|
||||
// Dial
|
||||
dialer := websocket.DefaultDialer
|
||||
rHeader := http.Header{}
|
||||
con, _, err := dialer.Dial(wsc.Address, rHeader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Set the ping/pong handlers
|
||||
con.SetPingHandler(func(m string) error {
|
||||
// NOTE: https://github.com/gorilla/websocket/issues/97
|
||||
log.Debug("Client received ping, writing pong")
|
||||
go con.WriteControl(websocket.PongMessage, []byte(m), time.Now().Add(time.Second*wsWriteTimeoutSeconds))
|
||||
return nil
|
||||
})
|
||||
con.SetPongHandler(func(m string) error {
|
||||
log.Debug("Client received pong")
|
||||
// NOTE: https://github.com/gorilla/websocket/issues/97
|
||||
return nil
|
||||
})
|
||||
wsc.Conn = con
|
||||
return nil
|
||||
}
|
||||
|
||||
func (wsc *WSClient) OnStop() {
|
||||
wsc.QuitService.OnStop()
|
||||
// ResultsCh is closed in receiveEventsRoutine.
|
||||
}
|
||||
|
||||
func (wsc *WSClient) receiveEventsRoutine() {
|
||||
for {
|
||||
log.Notice("Waiting for wsc message ...")
|
||||
_, data, err := wsc.ReadMessage()
|
||||
if err != nil {
|
||||
log.Info("WSClient failed to read message", "error", err, "data", string(data))
|
||||
wsc.Stop()
|
||||
break
|
||||
} else {
|
||||
var response rpctypes.RPCResponse
|
||||
wire.ReadJSON(&response, data, &err)
|
||||
if err != nil {
|
||||
log.Info("WSClient failed to parse message", "error", err, "data", string(data))
|
||||
wsc.Stop()
|
||||
break
|
||||
}
|
||||
wsc.ResultsCh <- response.Result
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
close(wsc.ResultsCh)
|
||||
}
|
||||
|
||||
// subscribe to an event
|
||||
func (wsc *WSClient) Subscribe(eventid string) error {
|
||||
err := wsc.WriteJSON(rpctypes.RPCRequest{
|
||||
JSONRPC: "2.0",
|
||||
ID: "",
|
||||
Method: "subscribe",
|
||||
Params: []interface{}{eventid},
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// unsubscribe from an event
|
||||
func (wsc *WSClient) Unsubscribe(eventid string) error {
|
||||
err := wsc.WriteJSON(rpctypes.RPCRequest{
|
||||
JSONRPC: "2.0",
|
||||
ID: "",
|
||||
Method: "unsubscribe",
|
||||
Params: []interface{}{eventid},
|
||||
})
|
||||
return err
|
||||
}
|
Reference in New Issue
Block a user