add functions to rpc.proto, start lnshell

This commit is contained in:
Tadge Dryja 2015-12-30 22:58:15 -04:00 committed by Olaoluwa Osuntokun
parent 6647bdd2b8
commit 8a50faaf47
5 changed files with 180 additions and 6 deletions

1
.gitignore vendored
View File

@ -31,5 +31,6 @@ cmd/cmd
**.hex
cmd/lncli/lncli
cmd/lnshell/lnshell
test_wal/*

View File

@ -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
}

63
cmd/lnshell/commands.go Normal file
View File

@ -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
}

View File

@ -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
}

View File

@ -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{}