diff --git a/cmd/tmsp/cli.go b/cmd/tmsp/cli.go index 1eab662e1..e4550b8ed 100644 --- a/cmd/tmsp/cli.go +++ b/cmd/tmsp/cli.go @@ -1,9 +1,11 @@ package main import ( + "bufio" "fmt" "net" "os" + "strings" . "github.com/tendermint/go-common" "github.com/tendermint/go-wire" @@ -12,6 +14,9 @@ import ( "github.com/codegangsta/cli" ) +// connection is a global variable so it can be reused by the console +var conn net.Conn + func main() { app := cli.NewApp() app.Name = "cli" @@ -24,6 +29,13 @@ func main() { }, } 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", Usage: "Append a new tx to application", @@ -53,19 +65,44 @@ func main() { }, }, } + app.Before = before 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 func cmdAppendTx(c *cli.Context) { 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])}) if err != nil { Exit(err.Error()) @@ -75,10 +112,6 @@ func cmdAppendTx(c *cli.Context) { // Get application Merkle root hash func cmdGetHash(c *cli.Context) { - conn, err := Connect(c.GlobalString("address")) - if err != nil { - Exit(err.Error()) - } res, err := makeRequest(conn, types.RequestGetHash{}) if err != nil { Exit(err.Error()) @@ -88,11 +121,7 @@ func cmdGetHash(c *cli.Context) { // Commit the application state 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 { Exit(err.Error()) } @@ -101,11 +130,7 @@ func cmdCommit(c *cli.Context) { // Roll back the application state to the latest commit 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 { Exit(err.Error()) } diff --git a/server/server.go b/server/server.go index 1ed2dec4f..e5945c81c 100644 --- a/server/server.go +++ b/server/server.go @@ -3,6 +3,7 @@ package server import ( "bufio" "fmt" + "io" "net" "strings" @@ -86,7 +87,11 @@ func handleRequests(appC types.AppContext, closeConn chan error, conn net.Conn, var req types.Request wire.ReadBinaryPtr(&req, bufReader, 0, &n, &err) 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 } count++