-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rpcclient: add getzmqnotifications RPC
- Loading branch information
1 parent
d537492
commit 85b6f7e
Showing
5 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package btcjson | ||
|
||
// GetZmqNotificationsCmd defines the getzmqnotifications JSON-RPC command. | ||
type GetZmqNotificationsCmd struct{} | ||
|
||
// NewGetZmqNotificationsCmd returns a new instance which can be used to issue a | ||
// getzmqnotifications JSON-RPC command. | ||
func NewGetZmqNotificationsCmd() *GetZmqNotificationsCmd { | ||
return &GetZmqNotificationsCmd{} | ||
} | ||
|
||
func init() { | ||
flags := UsageFlag(0) | ||
|
||
MustRegisterCmd("getzmqnotifications", (*GetZmqNotificationsCmd)(nil), flags) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package btcjson | ||
|
||
import ( | ||
"encoding/json" | ||
"net/url" | ||
) | ||
|
||
// GetZmqNotificationResult models the data returned from the getzmqnotifications command. | ||
type GetZmqNotificationResult []struct { | ||
Type string // Type of notification | ||
Address *url.URL // Address of the publisher | ||
HighWaterMark int // Outbound message high water mark | ||
} | ||
|
||
func (z *GetZmqNotificationResult) MarshalJSON() ([]byte, error) { | ||
var out []map[string]interface{} | ||
for _, notif := range *z { | ||
out = append(out, | ||
map[string]interface{}{ | ||
"type": notif.Type, | ||
"address": notif.Address.String(), | ||
"hwm": notif.HighWaterMark, | ||
}) | ||
} | ||
return json.Marshal(out) | ||
} | ||
|
||
// UnmarshalJSON satisfies the json.Unmarshaller interface | ||
func (z *GetZmqNotificationResult) UnmarshalJSON(bytes []byte) error { | ||
type basicNotification struct { | ||
Type string | ||
Address string | ||
Hwm int | ||
} | ||
|
||
var basics []basicNotification | ||
if err := json.Unmarshal(bytes, &basics); err != nil { | ||
return err | ||
} | ||
|
||
var notifications GetZmqNotificationResult | ||
for _, basic := range basics { | ||
|
||
address, err := url.Parse(basic.Address) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
notifications = append(notifications, struct { | ||
Type string | ||
Address *url.URL | ||
HighWaterMark int | ||
}{ | ||
Type: basic.Type, | ||
Address: address, | ||
HighWaterMark: basic.Hwm, | ||
}) | ||
} | ||
|
||
*z = notifications | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package rpcclient | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/btcsuite/btcd/btcjson" | ||
) | ||
|
||
// FutureGetZmqNotificationsResult is a future promise to deliver the result of | ||
// a GetZmqNotifications RPC invocation | ||
type FutureGetZmqNotificationsResult chan *Response | ||
|
||
// Receive waits for the response promised by the future and returns the unmarshalled | ||
// response, or an error if the request was unsuccessful. | ||
func (r FutureGetZmqNotificationsResult) Receive() (btcjson.GetZmqNotificationResult, error) { | ||
res, err := ReceiveFuture(r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var notifications btcjson.GetZmqNotificationResult | ||
if err := json.Unmarshal(res, ¬ifications); err != nil { | ||
return nil, err | ||
} | ||
return notifications, nil | ||
} | ||
|
||
// GetZmqNotificationsAsync returns an instance ofa type that can be used to get | ||
// the result of a custom RPC request at some future time by invoking the Receive | ||
// function on the returned instance. | ||
// | ||
// See GetZmqNotifications for the blocking version and more details. | ||
func (c *Client) GetZmqNotificationsAsync() FutureGetZmqNotificationsResult { | ||
return c.SendCmd(btcjson.NewGetZmqNotificationsCmd()) | ||
} | ||
|
||
// GetZmqNotifications returns information about the active ZeroMQ notifications. | ||
func (c *Client) GetZmqNotifications() (btcjson.GetZmqNotificationResult, error) { | ||
return c.GetZmqNotificationsAsync().Receive() | ||
} |