Browse Source

interactive tmsp console

pull/1780/head
Ethan Buchman 9 years ago
parent
commit
93308a3e6f
2 changed files with 49 additions and 19 deletions
  1. +43
    -18
      cmd/tmsp/cli.go
  2. +6
    -1
      server/server.go

+ 43
- 18
cmd/tmsp/cli.go View File

@ -1,9 +1,11 @@
package main package main
import ( import (
"bufio"
"fmt" "fmt"
"net" "net"
"os" "os"
"strings"
. "github.com/tendermint/go-common" . "github.com/tendermint/go-common"
"github.com/tendermint/go-wire" "github.com/tendermint/go-wire"
@ -12,6 +14,9 @@ import (
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
) )
// connection is a global variable so it can be reused by the console
var conn net.Conn
func main() { func main() {
app := cli.NewApp() app := cli.NewApp()
app.Name = "cli" app.Name = "cli"
@ -24,6 +29,13 @@ func main() {
}, },
} }
app.Commands = []cli.Command{ app.Commands = []cli.Command{
{
Name: "console",
Usage: "Start an interactive tmsp console for multiple commands",
Action: func(c *cli.Context) {
cmdConsole(app, c)
},
},
{ {
Name: "append_tx", Name: "append_tx",
Usage: "Append a new tx to application", Usage: "Append a new tx to application",
@ -53,19 +65,44 @@ func main() {
}, },
}, },
} }
app.Before = before
app.Run(os.Args) app.Run(os.Args)
} }
func before(c *cli.Context) error {
if conn == nil {
var err error
conn, err = Connect(c.GlobalString("address"))
if err != nil {
Exit(err.Error())
}
}
return nil
}
//-------------------------------------------------------------------------------- //--------------------------------------------------------------------------------
func cmdConsole(app *cli.App, c *cli.Context) {
for {
fmt.Printf("> ")
bufReader := bufio.NewReader(os.Stdin)
line, more, err := bufReader.ReadLine()
if more {
Exit("input is too long")
} else if err != nil {
Exit(err.Error())
}
args := []string{"tmsp"}
args = append(args, strings.Split(string(line), " ")...)
app.Run(args)
}
}
// Append a new tx to application // Append a new tx to application
func cmdAppendTx(c *cli.Context) { func cmdAppendTx(c *cli.Context) {
args := c.Args() // Args to AppendTx args := c.Args() // Args to AppendTx
conn, err := Connect(c.GlobalString("address"))
if err != nil {
Exit(err.Error())
}
res, err := makeRequest(conn, types.RequestAppendTx{[]byte(args[0])}) res, err := makeRequest(conn, types.RequestAppendTx{[]byte(args[0])})
if err != nil { if err != nil {
Exit(err.Error()) Exit(err.Error())
@ -75,10 +112,6 @@ func cmdAppendTx(c *cli.Context) {
// Get application Merkle root hash // Get application Merkle root hash
func cmdGetHash(c *cli.Context) { func cmdGetHash(c *cli.Context) {
conn, err := Connect(c.GlobalString("address"))
if err != nil {
Exit(err.Error())
}
res, err := makeRequest(conn, types.RequestGetHash{}) res, err := makeRequest(conn, types.RequestGetHash{})
if err != nil { if err != nil {
Exit(err.Error()) Exit(err.Error())
@ -88,11 +121,7 @@ func cmdGetHash(c *cli.Context) {
// Commit the application state // Commit the application state
func cmdCommit(c *cli.Context) { func cmdCommit(c *cli.Context) {
conn, err := Connect(c.GlobalString("address"))
if err != nil {
Exit(err.Error())
}
_, err = makeRequest(conn, types.RequestCommit{})
_, err := makeRequest(conn, types.RequestCommit{})
if err != nil { if err != nil {
Exit(err.Error()) Exit(err.Error())
} }
@ -101,11 +130,7 @@ func cmdCommit(c *cli.Context) {
// Roll back the application state to the latest commit // Roll back the application state to the latest commit
func cmdRollback(c *cli.Context) { func cmdRollback(c *cli.Context) {
conn, err := Connect(c.GlobalString("address"))
if err != nil {
Exit(err.Error())
}
_, err = makeRequest(conn, types.RequestRollback{})
_, err := makeRequest(conn, types.RequestRollback{})
if err != nil { if err != nil {
Exit(err.Error()) Exit(err.Error())
} }


+ 6
- 1
server/server.go View File

@ -3,6 +3,7 @@ package server
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"io"
"net" "net"
"strings" "strings"
@ -86,7 +87,11 @@ func handleRequests(appC types.AppContext, closeConn chan error, conn net.Conn,
var req types.Request var req types.Request
wire.ReadBinaryPtr(&req, bufReader, 0, &n, &err) wire.ReadBinaryPtr(&req, bufReader, 0, &n, &err)
if err != nil { if err != nil {
closeConn <- fmt.Errorf("Error in handleRequests: %v", err.Error())
if err == io.EOF {
closeConn <- fmt.Errorf("Connection closed by client")
} else {
closeConn <- fmt.Errorf("Error in handleRequests: %v", err.Error())
}
return return
} }
count++ count++


Loading…
Cancel
Save