github.com/decred/dcrlnd@v0.7.6/cmd/dcrlncli/cmd_custom.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/hex"
     5  	"fmt"
     6  
     7  	"github.com/decred/dcrlnd/lnrpc"
     8  	"github.com/urfave/cli"
     9  )
    10  
    11  var sendCustomCommand = cli.Command{
    12  	Name: "sendcustom",
    13  	Flags: []cli.Flag{
    14  		cli.StringFlag{
    15  			Name: "peer",
    16  		},
    17  		cli.Uint64Flag{
    18  			Name: "type",
    19  		},
    20  		cli.StringFlag{
    21  			Name: "data",
    22  		},
    23  	},
    24  	Action: actionDecorator(sendCustom),
    25  }
    26  
    27  func sendCustom(ctx *cli.Context) error {
    28  	ctxc := getContext()
    29  	client, cleanUp := getClient(ctx)
    30  	defer cleanUp()
    31  
    32  	peer, err := hex.DecodeString(ctx.String("peer"))
    33  	if err != nil {
    34  		return err
    35  	}
    36  
    37  	msgType := ctx.Uint64("type")
    38  
    39  	data, err := hex.DecodeString(ctx.String("data"))
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	_, err = client.SendCustomMessage(
    45  		ctxc,
    46  		&lnrpc.SendCustomMessageRequest{
    47  			Peer: peer,
    48  			Type: uint32(msgType),
    49  			Data: data,
    50  		},
    51  	)
    52  
    53  	return err
    54  }
    55  
    56  var subscribeCustomCommand = cli.Command{
    57  	Name:   "subscribecustom",
    58  	Action: actionDecorator(subscribeCustom),
    59  }
    60  
    61  func subscribeCustom(ctx *cli.Context) error {
    62  	ctxc := getContext()
    63  	client, cleanUp := getClient(ctx)
    64  	defer cleanUp()
    65  
    66  	stream, err := client.SubscribeCustomMessages(
    67  		ctxc,
    68  		&lnrpc.SubscribeCustomMessagesRequest{},
    69  	)
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	for {
    75  		msg, err := stream.Recv()
    76  		if err != nil {
    77  			return err
    78  		}
    79  
    80  		fmt.Printf("Received from peer %x: type=%d, data=%x\n",
    81  			msg.Peer, msg.Type, msg.Data)
    82  	}
    83  }