diff --git a/cmd/barak/main.go b/cmd/barak/main.go index 9c08fbac0..1f07750d0 100644 --- a/cmd/barak/main.go +++ b/cmd/barak/main.go @@ -22,6 +22,8 @@ import ( "github.com/tendermint/tendermint/rpc/server" ) +const BarakVersion = "0.0.1" + var Routes map[string]*rpcserver.RPCFunc func init() { @@ -72,6 +74,7 @@ func Status() (*ResponseStatus, error) { barak_.mtx.Unlock() return &ResponseStatus{ + Version: BarakVersion, Pid: pid, Nonce: nonce, Validators: validators, @@ -96,6 +99,8 @@ func Run(authCommand AuthCommand) (interface{}, error) { return OpenListener(c.Addr) case CommandCloseListener: return CloseListener(c.Addr) + case CommandQuit: + return Quit() default: return nil, errors.New("Invalid endpoint for command") } @@ -204,6 +209,15 @@ func CloseListener(addr string) (*ResponseCloseListener, error) { return &ResponseCloseListener{}, nil } +func Quit() (*ResponseQuit, error) { + fmt.Println("Barak shutting down due to Quit()") + go func() { + time.Sleep(time.Second) + os.Exit(0) + }() + return &ResponseQuit{}, nil +} + //-------------------------------------------------------------------------------- // Another barak instance registering its external diff --git a/cmd/barak/types/command.go b/cmd/barak/types/command.go index 074502e06..7a8154dec 100644 --- a/cmd/barak/types/command.go +++ b/cmd/barak/types/command.go @@ -24,6 +24,7 @@ const ( commandTypeServeFile = 0x04 commandTypeOpenListener = 0x05 commandTypeCloseListener = 0x06 + commandTypeQuit = 0x07 ) // for binary.readReflect @@ -35,6 +36,7 @@ var _ = binary.RegisterInterface( binary.ConcreteType{CommandServeFile{}, commandTypeServeFile}, binary.ConcreteType{CommandOpenListener{}, commandTypeOpenListener}, binary.ConcreteType{CommandCloseListener{}, commandTypeCloseListener}, + binary.ConcreteType{CommandQuit{}, commandTypeQuit}, ) type CommandStartProcess struct { @@ -63,3 +65,5 @@ type CommandOpenListener struct { type CommandCloseListener struct { Addr string } + +type CommandQuit struct{} diff --git a/cmd/barak/types/responses.go b/cmd/barak/types/responses.go index f8b138d8b..40ea1eb25 100644 --- a/cmd/barak/types/responses.go +++ b/cmd/barak/types/responses.go @@ -5,6 +5,7 @@ import ( ) type ResponseStatus struct { + Version string Pid int Nonce int64 Validators []Validator @@ -31,3 +32,6 @@ type ResponseOpenListener struct { type ResponseCloseListener struct { } + +type ResponseQuit struct { +} diff --git a/cmd/debora/commands.go b/cmd/debora/commands.go index dfca07374..b29d6d272 100644 --- a/cmd/debora/commands.go +++ b/cmd/debora/commands.go @@ -100,6 +100,16 @@ func DownloadFile(privKey acm.PrivKey, remote string, command btypes.CommandServ return n, nil } +func Quit(privKey acm.PrivKey, remote string, command btypes.CommandQuit) (response btypes.ResponseQuit, err error) { + nonce, err := GetNonce(remote) + if err != nil { + return response, err + } + commandBytes, signature := SignCommand(privKey, nonce+1, command) + _, err = RunAuthCommand(remote, commandBytes, []acm.Signature{signature}, &response) + return response, err +} + //----------------------------------------------------------------------------- // Utility method to get nonce from the remote. diff --git a/cmd/debora/main.go b/cmd/debora/main.go index 323eab349..6f0357864 100644 --- a/cmd/debora/main.go +++ b/cmd/debora/main.go @@ -106,12 +106,12 @@ func main() { }, cli.Command{ Name: "open", - Usage: "open listener", + Usage: "open barak listener", Action: cliOpenListener, }, cli.Command{ Name: "close", - Usage: "close listener", + Usage: "close barka listener", Action: cliCloseListener, }, cli.Command{ @@ -119,6 +119,11 @@ func main() { Usage: "download file ", Action: cliDownloadFile, }, + cli.Command{ + Name: "quit", + Usage: "quit barak", + Action: cliQuit, + }, } app.Run(os.Args) } @@ -330,3 +335,21 @@ func cliDownloadFile(c *cli.Context) { } wg.Wait() } + +func cliQuit(c *cli.Context) { + command := btypes.CommandQuit{} + wg := sync.WaitGroup{} + for _, remote := range Config.Remotes { + wg.Add(1) + go func(remote string) { + defer wg.Done() + response, err := Quit(Config.PrivKey, remote, command) + if err != nil { + fmt.Printf("%v failure. %v\n", remote, err) + } else { + fmt.Printf("%v success. %v\n", remote, response) + } + }(remote) + } + wg.Wait() +}