From 8a50faaf47a320e334e2e05116a4ec963d79ee58 Mon Sep 17 00:00:00 2001 From: Tadge Dryja Date: Wed, 30 Dec 2015 22:58:15 -0400 Subject: [PATCH] add functions to rpc.proto, start lnshell --- .gitignore | 1 + cmd/lncli/shell.go | 24 ++++++++++--- cmd/lnshell/commands.go | 63 ++++++++++++++++++++++++++++++++ cmd/lnshell/lnshellmain.go | 74 ++++++++++++++++++++++++++++++++++++++ lnrpc/rpc.proto | 24 ++++++++++++- 5 files changed, 180 insertions(+), 6 deletions(-) create mode 100644 cmd/lnshell/commands.go create mode 100644 cmd/lnshell/lnshellmain.go diff --git a/.gitignore b/.gitignore index 5e85d365..47b35e36 100644 --- a/.gitignore +++ b/.gitignore @@ -31,5 +31,6 @@ cmd/cmd **.hex cmd/lncli/lncli +cmd/lnshell/lnshell test_wal/* diff --git a/cmd/lncli/shell.go b/cmd/lncli/shell.go index 42172b93..808d69a2 100644 --- a/cmd/lncli/shell.go +++ b/cmd/lncli/shell.go @@ -6,14 +6,24 @@ import ( "log" "os" "strings" + "time" "github.com/codegangsta/cli" "google.golang.org/grpc" - "li.lan/labs/plasma/lnrpc" ) func shell(z *cli.Context) { fmt.Printf("LN shell v0.0FTW\n") + // opts := []grpc.DialOption{grpc.WithInsecure()} + // conn, err := grpc.Dial("localhost:10000", opts...) + // if err != nil { + // log.Fatal(err) + // } + // state, err := conn.State() + // if err != nil { + // log.Fatal(err) + // } + // fmt.Printf("connection state: %s\n", state.String()) for { reader := bufio.NewReaderSize(os.Stdin, 4000) fmt.Printf("->") @@ -47,7 +57,6 @@ func Shellparse(cmdslice []string) error { // Help() // return nil // } - // if cmd == "pbx" { // err = PbxConnect(args) // if err != nil { @@ -121,15 +130,20 @@ func RpcConnect(args []string) error { return err } fmt.Printf("connection state: %s\n", state.String()) - - lnClient := lnrpc.NewLightningClient(conn) + time.Sleep(time.Second * 2) + // lnClient := lnrpc.NewLightningClient(conn) // lnClient.NewAddress(nil, nil, nil) // crashes + state, err = conn.State() + if err != nil { + return err + } + fmt.Printf("connection state: %s\n", state.String()) + err = conn.Close() if err != nil { return err } - return nil } diff --git a/cmd/lnshell/commands.go b/cmd/lnshell/commands.go new file mode 100644 index 00000000..1ba5f218 --- /dev/null +++ b/cmd/lnshell/commands.go @@ -0,0 +1,63 @@ +package main + +import ( + "fmt" + "time" + + "google.golang.org/grpc" +) + +// connects via grpc to the ln node. default (hardcoded?) local:10K +func RpcConnect(args []string) error { + // client := getClient(ctx) + opts := []grpc.DialOption{grpc.WithInsecure()} + conn, err := grpc.Dial("localhost:10000", opts...) + if err != nil { + return err + } + state, err := conn.State() + if err != nil { + return err + } + fmt.Printf("connection state: %s\n", state.String()) + time.Sleep(time.Second * 2) + // lnClient := lnrpc.NewLightningClient(conn) + // lnClient.NewAddress(nil, nil, nil) // crashes + + state, err = conn.State() + if err != nil { + return err + } + fmt.Printf("connection state: %s\n", state.String()) + + err = conn.Close() + if err != nil { + return err + } + return nil +} + +func LnConnect(args []string) error { + fmt.Printf("lnconnect, %d args\n", len(args)) + return nil +} + +// LnListen listens on the default port for incoming connections +func LnListen(args []string) error { + + fmt.Printf("will start TCP port listener\n") + return nil +} + +// For testing. Syntax: lnhi hello world +func LnChat(args []string) error { + + var chat string + for _, s := range args { + chat += s + " " + } + // msg := append([]byte{lnwire.MSGID_TEXTCHAT}, []byte(chat)...) + + fmt.Printf("will send text message: %s\n", chat) + return nil +} diff --git a/cmd/lnshell/lnshellmain.go b/cmd/lnshell/lnshellmain.go new file mode 100644 index 00000000..6e523727 --- /dev/null +++ b/cmd/lnshell/lnshellmain.go @@ -0,0 +1,74 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "strings" +) + +func main() { + fmt.Printf("LNShell v0.0. \n") + fmt.Printf("Connects to LN daemon, default on 127.0.0.1:10000.\n") + shellPrompt() + return +} + +func shellPrompt() { + for { + reader := bufio.NewReaderSize(os.Stdin, 4000) + fmt.Printf("->") + msg, err := reader.ReadString('\n') + if err != nil { + log.Fatal(err) + } + cmdslice := strings.Fields(msg) + if len(cmdslice) < 1 { + continue + } + fmt.Printf("entered command: %s\n", msg) + err = Shellparse(cmdslice) + if err != nil { + log.Fatal(err) + } + } +} + +func Shellparse(cmdslice []string) error { + var err error + var args []string + cmd := cmdslice[0] + if len(cmdslice) > 1 { + args = cmdslice[1:] + } + if cmd == "exit" || cmd == "quit" { + return fmt.Errorf("User exit") + } + + if cmd == "lnhi" { + err = LnChat(args) + if err != nil { + fmt.Printf("LN chat error: %s\n", err) + } + return nil + } + if cmd == "lnc" { + err = LnConnect(args) + if err != nil { + fmt.Printf("LN connect error: %s\n", err) + } + return nil + } + + if cmd == "rpc" { + err = RpcConnect(args) + if err != nil { + fmt.Printf("RPC connect error: %s\n", err) + } + return nil + } + + fmt.Printf("Command not recognized.\n") + return nil +} diff --git a/lnrpc/rpc.proto b/lnrpc/rpc.proto index 7ca0c2da..94549cdf 100644 --- a/lnrpc/rpc.proto +++ b/lnrpc/rpc.proto @@ -3,8 +3,11 @@ syntax = "proto3"; package lnrpc; service Lightning { - rpc SendMany(SendManyRequest) returns (SendManyResponse); + rpc SendMany(SendManyRequest) returns (SendManyResponse); rpc NewAddress(NewAddressRequest) returns (NewAddressResponse); + rpc TCPListen(TCPListenRequest) returns (TCPListenResponse); + rpc LNConnect(LNConnectRequest) returns (LnConnectResponse); + rpc LNChat(LnChatRequest) returns (LnChatResponse); } message SendManyRequest { @@ -19,3 +22,22 @@ message NewAddressRequest {} message NewAddressResponse { string address = 1; } + +message TCPListenRequest{ + string hostport = 1; +} +message TCPListenResponse{} + +message LNConnectRequest{ + string idAtHost = 1; +} +message LnChatResponse{ + bytes lnID = 1; +} + +message LnChatRequest{ + bytes destID = 1; + string msg = 2; +} + +message LnChatResponse{}