Skip to content

Commit

Permalink
rpcclient: Export sendCmd and response
Browse files Browse the repository at this point in the history
This facilitates using custom commands with rpcclient.

See #1083
  • Loading branch information
JeremyRand committed May 19, 2020
1 parent 9f0179f commit 00c1cdf
Show file tree
Hide file tree
Showing 9 changed files with 382 additions and 382 deletions.
132 changes: 66 additions & 66 deletions rpcclient/chain.go

Large diffs are not rendered by default.

54 changes: 27 additions & 27 deletions rpcclient/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (

// FutureDebugLevelResult is a future promise to deliver the result of a
// DebugLevelAsync RPC invocation (or an applicable error).
type FutureDebugLevelResult chan *response
type FutureDebugLevelResult chan *Response

// Receive waits for the response promised by the future and returns the result
// Receive waits for the Response promised by the future and returns the result
// of setting the debug logging level to the passed level specification or the
// list of of the available subsystems for the special keyword 'show'.
func (r FutureDebugLevelResult) Receive() (string, error) {
Expand All @@ -49,7 +49,7 @@ func (r FutureDebugLevelResult) Receive() (string, error) {
// NOTE: This is a btcd extension.
func (c *Client) DebugLevelAsync(levelSpec string) FutureDebugLevelResult {
cmd := btcjson.NewDebugLevelCmd(levelSpec)
return c.sendCmd(cmd)
return c.SendCmd(cmd)
}

// DebugLevel dynamically sets the debug logging level to the passed level
Expand All @@ -68,9 +68,9 @@ func (c *Client) DebugLevel(levelSpec string) (string, error) {

// FutureCreateEncryptedWalletResult is a future promise to deliver the error
// result of a CreateEncryptedWalletAsync RPC invocation.
type FutureCreateEncryptedWalletResult chan *response
type FutureCreateEncryptedWalletResult chan *Response

// Receive waits for and returns the error response promised by the future.
// Receive waits for and returns the error Response promised by the future.
func (r FutureCreateEncryptedWalletResult) Receive() error {
_, err := receiveFuture(r)
return err
Expand All @@ -85,7 +85,7 @@ func (r FutureCreateEncryptedWalletResult) Receive() error {
// NOTE: This is a btcwallet extension.
func (c *Client) CreateEncryptedWalletAsync(passphrase string) FutureCreateEncryptedWalletResult {
cmd := btcjson.NewCreateEncryptedWalletCmd(passphrase)
return c.sendCmd(cmd)
return c.SendCmd(cmd)
}

// CreateEncryptedWallet requests the creation of an encrypted wallet. Wallets
Expand All @@ -102,9 +102,9 @@ func (c *Client) CreateEncryptedWallet(passphrase string) error {

// FutureListAddressTransactionsResult is a future promise to deliver the result
// of a ListAddressTransactionsAsync RPC invocation (or an applicable error).
type FutureListAddressTransactionsResult chan *response
type FutureListAddressTransactionsResult chan *Response

// Receive waits for the response promised by the future and returns information
// Receive waits for the Response promised by the future and returns information
// about all transactions associated with the provided addresses.
func (r FutureListAddressTransactionsResult) Receive() ([]btcjson.ListTransactionsResult, error) {
res, err := receiveFuture(r)
Expand Down Expand Up @@ -135,7 +135,7 @@ func (c *Client) ListAddressTransactionsAsync(addresses []btcutil.Address, accou
addrs = append(addrs, addr.EncodeAddress())
}
cmd := btcjson.NewListAddressTransactionsCmd(addrs, &account)
return c.sendCmd(cmd)
return c.SendCmd(cmd)
}

// ListAddressTransactions returns information about all transactions associated
Expand All @@ -148,9 +148,9 @@ func (c *Client) ListAddressTransactions(addresses []btcutil.Address, account st

// FutureGetBestBlockResult is a future promise to deliver the result of a
// GetBestBlockAsync RPC invocation (or an applicable error).
type FutureGetBestBlockResult chan *response
type FutureGetBestBlockResult chan *Response

// Receive waits for the response promised by the future and returns the hash
// Receive waits for the Response promised by the future and returns the hash
// and height of the block in the longest (best) chain.
func (r FutureGetBestBlockResult) Receive() (*chainhash.Hash, int32, error) {
res, err := receiveFuture(r)
Expand Down Expand Up @@ -183,7 +183,7 @@ func (r FutureGetBestBlockResult) Receive() (*chainhash.Hash, int32, error) {
// NOTE: This is a btcd extension.
func (c *Client) GetBestBlockAsync() FutureGetBestBlockResult {
cmd := btcjson.NewGetBestBlockCmd()
return c.sendCmd(cmd)
return c.SendCmd(cmd)
}

// GetBestBlock returns the hash and height of the block in the longest (best)
Expand All @@ -196,9 +196,9 @@ func (c *Client) GetBestBlock() (*chainhash.Hash, int32, error) {

// FutureGetCurrentNetResult is a future promise to deliver the result of a
// GetCurrentNetAsync RPC invocation (or an applicable error).
type FutureGetCurrentNetResult chan *response
type FutureGetCurrentNetResult chan *Response

// Receive waits for the response promised by the future and returns the network
// Receive waits for the Response promised by the future and returns the network
// the server is running on.
func (r FutureGetCurrentNetResult) Receive() (wire.BitcoinNet, error) {
res, err := receiveFuture(r)
Expand All @@ -225,7 +225,7 @@ func (r FutureGetCurrentNetResult) Receive() (wire.BitcoinNet, error) {
// NOTE: This is a btcd extension.
func (c *Client) GetCurrentNetAsync() FutureGetCurrentNetResult {
cmd := btcjson.NewGetCurrentNetCmd()
return c.sendCmd(cmd)
return c.SendCmd(cmd)
}

// GetCurrentNet returns the network the server is running on.
Expand All @@ -240,9 +240,9 @@ func (c *Client) GetCurrentNet() (wire.BitcoinNet, error) {
//
// NOTE: This is a btcsuite extension ported from
// github.com/decred/dcrrpcclient.
type FutureGetHeadersResult chan *response
type FutureGetHeadersResult chan *Response

// Receive waits for the response promised by the future and returns the
// Receive waits for the Response promised by the future and returns the
// getheaders result.
//
// NOTE: This is a btcsuite extension ported from
Expand Down Expand Up @@ -292,7 +292,7 @@ func (c *Client) GetHeadersAsync(blockLocators []chainhash.Hash, hashStop *chain
hash = hashStop.String()
}
cmd := btcjson.NewGetHeadersCmd(locators, hash)
return c.sendCmd(cmd)
return c.SendCmd(cmd)
}

// GetHeaders mimics the wire protocol getheaders and headers messages by
Expand All @@ -307,9 +307,9 @@ func (c *Client) GetHeaders(blockLocators []chainhash.Hash, hashStop *chainhash.

// FutureExportWatchingWalletResult is a future promise to deliver the result of
// an ExportWatchingWalletAsync RPC invocation (or an applicable error).
type FutureExportWatchingWalletResult chan *response
type FutureExportWatchingWalletResult chan *Response

// Receive waits for the response promised by the future and returns the
// Receive waits for the Response promised by the future and returns the
// exported wallet.
func (r FutureExportWatchingWalletResult) Receive() ([]byte, []byte, error) {
res, err := receiveFuture(r)
Expand Down Expand Up @@ -361,7 +361,7 @@ func (r FutureExportWatchingWalletResult) Receive() ([]byte, []byte, error) {
// NOTE: This is a btcwallet extension.
func (c *Client) ExportWatchingWalletAsync(account string) FutureExportWatchingWalletResult {
cmd := btcjson.NewExportWatchingWalletCmd(&account, btcjson.Bool(true))
return c.sendCmd(cmd)
return c.SendCmd(cmd)
}

// ExportWatchingWallet returns the raw bytes for a watching-only version of
Expand All @@ -376,9 +376,9 @@ func (c *Client) ExportWatchingWallet(account string) ([]byte, []byte, error) {

// FutureSessionResult is a future promise to deliver the result of a
// SessionAsync RPC invocation (or an applicable error).
type FutureSessionResult chan *response
type FutureSessionResult chan *Response

// Receive waits for the response promised by the future and returns the
// Receive waits for the Response promised by the future and returns the
// session result.
func (r FutureSessionResult) Receive() (*btcjson.SessionResult, error) {
res, err := receiveFuture(r)
Expand Down Expand Up @@ -410,7 +410,7 @@ func (c *Client) SessionAsync() FutureSessionResult {
}

cmd := btcjson.NewSessionCmd()
return c.sendCmd(cmd)
return c.SendCmd(cmd)
}

// Session returns details regarding a websocket client's current connection.
Expand All @@ -427,9 +427,9 @@ func (c *Client) Session() (*btcjson.SessionResult, error) {
//
// NOTE: This is a btcsuite extension ported from
// github.com/decred/dcrrpcclient.
type FutureVersionResult chan *response
type FutureVersionResult chan *Response

// Receive waits for the response promised by the future and returns the version
// Receive waits for the Response promised by the future and returns the version
// result.
//
// NOTE: This is a btcsuite extension ported from
Expand Down Expand Up @@ -461,7 +461,7 @@ func (r FutureVersionResult) Receive() (map[string]btcjson.VersionResult,
// github.com/decred/dcrrpcclient.
func (c *Client) VersionAsync() FutureVersionResult {
cmd := btcjson.NewVersionCmd()
return c.sendCmd(cmd)
return c.SendCmd(cmd)
}

// Version returns information about the server's JSON-RPC API versions.
Expand Down
50 changes: 25 additions & 25 deletions rpcclient/infrastructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ type jsonRequest struct {
method string
cmd interface{}
marshalledJSON []byte
responseChan chan *response
responseChan chan *Response
}

// BackendVersion represents the version of the backend the client is currently
Expand Down Expand Up @@ -312,9 +312,9 @@ type rawResponse struct {
Error *btcjson.RPCError `json:"error"`
}

// response is the raw bytes of a JSON-RPC result, or the error if the response
// Response is the raw bytes of a JSON-RPC result, or the error if the response
// error object was non-null.
type response struct {
type Response struct {
result []byte
err error
}
Expand Down Expand Up @@ -395,7 +395,7 @@ func (c *Client) handleMessage(msg []byte) {

// Deliver the response.
result, err := in.rawResponse.result()
request.responseChan <- &response{result: result, err: err}
request.responseChan <- &Response{result: result, err: err}
}

// shouldLogReadError returns whether or not the passed error, which is expected
Expand Down Expand Up @@ -725,7 +725,7 @@ func (c *Client) handleSendPostMessage(details *sendPostDetails) {
log.Tracef("Sending command [%s] with id %d", jReq.method, jReq.id)
httpResponse, err := c.httpClient.Do(details.httpRequest)
if err != nil {
jReq.responseChan <- &response{err: err}
jReq.responseChan <- &Response{err: err}
return
}

Expand All @@ -734,7 +734,7 @@ func (c *Client) handleSendPostMessage(details *sendPostDetails) {
httpResponse.Body.Close()
if err != nil {
err = fmt.Errorf("error reading json reply: %v", err)
jReq.responseChan <- &response{err: err}
jReq.responseChan <- &Response{err: err}
return
}

Expand All @@ -747,12 +747,12 @@ func (c *Client) handleSendPostMessage(details *sendPostDetails) {
// response bytes.
err = fmt.Errorf("status code: %d, response: %q",
httpResponse.StatusCode, string(respBytes))
jReq.responseChan <- &response{err: err}
jReq.responseChan <- &Response{err: err}
return
}

res, err := resp.result()
jReq.responseChan <- &response{result: res, err: err}
jReq.responseChan <- &Response{result: res, err: err}
}

// sendPostHandler handles all outgoing messages when the client is running
Expand All @@ -779,7 +779,7 @@ cleanup:
for {
select {
case details := <-c.sendPostChan:
details.jsonRequest.responseChan <- &response{
details.jsonRequest.responseChan <- &Response{
result: nil,
err: ErrClientShutdown,
}
Expand All @@ -800,7 +800,7 @@ func (c *Client) sendPostRequest(httpReq *http.Request, jReq *jsonRequest) {
// Don't send the message if shutting down.
select {
case <-c.shutdown:
jReq.responseChan <- &response{result: nil, err: ErrClientShutdown}
jReq.responseChan <- &Response{result: nil, err: ErrClientShutdown}
default:
}

Expand All @@ -813,17 +813,17 @@ func (c *Client) sendPostRequest(httpReq *http.Request, jReq *jsonRequest) {
// newFutureError returns a new future result channel that already has the
// passed error waitin on the channel with the reply set to nil. This is useful
// to easily return errors from the various Async functions.
func newFutureError(err error) chan *response {
responseChan := make(chan *response, 1)
responseChan <- &response{err: err}
func newFutureError(err error) chan *Response {
responseChan := make(chan *Response, 1)
responseChan <- &Response{err: err}
return responseChan
}

// receiveFuture receives from the passed futureResult channel to extract a
// reply or any errors. The examined errors include an error in the
// futureResult and the error in the reply from the server. This will block
// until the result is available on the passed channel.
func receiveFuture(f chan *response) ([]byte, error) {
func receiveFuture(f chan *Response) ([]byte, error) {
// Wait for a response on the returned channel.
r := <-f
return r.result, r.err
Expand All @@ -844,7 +844,7 @@ func (c *Client) sendPost(jReq *jsonRequest) {
bodyReader := bytes.NewReader(jReq.marshalledJSON)
httpReq, err := http.NewRequest("POST", url, bodyReader)
if err != nil {
jReq.responseChan <- &response{result: nil, err: err}
jReq.responseChan <- &Response{result: nil, err: err}
return
}
httpReq.Close = true
Expand Down Expand Up @@ -875,7 +875,7 @@ func (c *Client) sendRequest(jReq *jsonRequest) {
select {
case <-c.connEstablished:
default:
jReq.responseChan <- &response{err: ErrClientNotConnected}
jReq.responseChan <- &Response{err: ErrClientNotConnected}
return
}

Expand All @@ -884,18 +884,18 @@ func (c *Client) sendRequest(jReq *jsonRequest) {
// channel. Then send the marshalled request via the websocket
// connection.
if err := c.addRequest(jReq); err != nil {
jReq.responseChan <- &response{err: err}
jReq.responseChan <- &Response{err: err}
return
}
log.Tracef("Sending command [%s] with id %d", jReq.method, jReq.id)
c.sendMessage(jReq.marshalledJSON)
}

// sendCmd sends the passed command to the associated server and returns a
// SendCmd sends the passed command to the associated server and returns a
// response channel on which the reply will be delivered at some point in the
// future. It handles both websocket and HTTP POST mode depending on the
// configuration of the client.
func (c *Client) sendCmd(cmd interface{}) chan *response {
func (c *Client) SendCmd(cmd interface{}) chan *Response {
// Get the method associated with the command.
method, err := btcjson.CmdMethod(cmd)
if err != nil {
Expand All @@ -910,7 +910,7 @@ func (c *Client) sendCmd(cmd interface{}) chan *response {
}

// Generate the request and send it along with a channel to respond on.
responseChan := make(chan *response, 1)
responseChan := make(chan *Response, 1)
jReq := &jsonRequest{
id: id,
method: method,
Expand All @@ -923,13 +923,13 @@ func (c *Client) sendCmd(cmd interface{}) chan *response {
return responseChan
}

// sendCmdAndWait sends the passed command to the associated server, waits
// SendCmdAndWait sends the passed command to the associated server, waits
// for the reply, and returns the result from it. It will return the error
// field in the reply if there is one.
func (c *Client) sendCmdAndWait(cmd interface{}) (interface{}, error) {
func (c *Client) SendCmdAndWait(cmd interface{}) (interface{}, error) {
// Marshal the command to JSON-RPC, send it to the connected server, and
// wait for a response on the returned channel.
return receiveFuture(c.sendCmd(cmd))
return receiveFuture(c.SendCmd(cmd))
}

// Disconnected returns whether or not the server is disconnected. If a
Expand Down Expand Up @@ -1010,7 +1010,7 @@ func (c *Client) Disconnect() {
if c.config.DisableAutoReconnect {
for e := c.requestList.Front(); e != nil; e = e.Next() {
req := e.Value.(*jsonRequest)
req.responseChan <- &response{
req.responseChan <- &Response{
result: nil,
err: ErrClientDisconnect,
}
Expand Down Expand Up @@ -1038,7 +1038,7 @@ func (c *Client) Shutdown() {
// Send the ErrClientShutdown error to any pending requests.
for e := c.requestList.Front(); e != nil; e = e.Next() {
req := e.Value.(*jsonRequest)
req.responseChan <- &response{
req.responseChan <- &Response{
result: nil,
err: ErrClientShutdown,
}
Expand Down

0 comments on commit 00c1cdf

Please sign in to comment.