Browse Source

upgrades to debora/barak to allow shutting down barak

pull/102/head
Jae Kwon 10 years ago
parent
commit
53a1cd2fbf
5 changed files with 57 additions and 2 deletions
  1. +14
    -0
      cmd/barak/main.go
  2. +4
    -0
      cmd/barak/types/command.go
  3. +4
    -0
      cmd/barak/types/responses.go
  4. +10
    -0
      cmd/debora/commands.go
  5. +25
    -2
      cmd/debora/main.go

+ 14
- 0
cmd/barak/main.go View File

@ -22,6 +22,8 @@ import (
"github.com/tendermint/tendermint/rpc/server" "github.com/tendermint/tendermint/rpc/server"
) )
const BarakVersion = "0.0.1"
var Routes map[string]*rpcserver.RPCFunc var Routes map[string]*rpcserver.RPCFunc
func init() { func init() {
@ -72,6 +74,7 @@ func Status() (*ResponseStatus, error) {
barak_.mtx.Unlock() barak_.mtx.Unlock()
return &ResponseStatus{ return &ResponseStatus{
Version: BarakVersion,
Pid: pid, Pid: pid,
Nonce: nonce, Nonce: nonce,
Validators: validators, Validators: validators,
@ -96,6 +99,8 @@ func Run(authCommand AuthCommand) (interface{}, error) {
return OpenListener(c.Addr) return OpenListener(c.Addr)
case CommandCloseListener: case CommandCloseListener:
return CloseListener(c.Addr) return CloseListener(c.Addr)
case CommandQuit:
return Quit()
default: default:
return nil, errors.New("Invalid endpoint for command") return nil, errors.New("Invalid endpoint for command")
} }
@ -204,6 +209,15 @@ func CloseListener(addr string) (*ResponseCloseListener, error) {
return &ResponseCloseListener{}, nil 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 // Another barak instance registering its external


+ 4
- 0
cmd/barak/types/command.go View File

@ -24,6 +24,7 @@ const (
commandTypeServeFile = 0x04 commandTypeServeFile = 0x04
commandTypeOpenListener = 0x05 commandTypeOpenListener = 0x05
commandTypeCloseListener = 0x06 commandTypeCloseListener = 0x06
commandTypeQuit = 0x07
) )
// for binary.readReflect // for binary.readReflect
@ -35,6 +36,7 @@ var _ = binary.RegisterInterface(
binary.ConcreteType{CommandServeFile{}, commandTypeServeFile}, binary.ConcreteType{CommandServeFile{}, commandTypeServeFile},
binary.ConcreteType{CommandOpenListener{}, commandTypeOpenListener}, binary.ConcreteType{CommandOpenListener{}, commandTypeOpenListener},
binary.ConcreteType{CommandCloseListener{}, commandTypeCloseListener}, binary.ConcreteType{CommandCloseListener{}, commandTypeCloseListener},
binary.ConcreteType{CommandQuit{}, commandTypeQuit},
) )
type CommandStartProcess struct { type CommandStartProcess struct {
@ -63,3 +65,5 @@ type CommandOpenListener struct {
type CommandCloseListener struct { type CommandCloseListener struct {
Addr string Addr string
} }
type CommandQuit struct{}

+ 4
- 0
cmd/barak/types/responses.go View File

@ -5,6 +5,7 @@ import (
) )
type ResponseStatus struct { type ResponseStatus struct {
Version string
Pid int Pid int
Nonce int64 Nonce int64
Validators []Validator Validators []Validator
@ -31,3 +32,6 @@ type ResponseOpenListener struct {
type ResponseCloseListener struct { type ResponseCloseListener struct {
} }
type ResponseQuit struct {
}

+ 10
- 0
cmd/debora/commands.go View File

@ -100,6 +100,16 @@ func DownloadFile(privKey acm.PrivKey, remote string, command btypes.CommandServ
return n, nil 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. // Utility method to get nonce from the remote.


+ 25
- 2
cmd/debora/main.go View File

@ -106,12 +106,12 @@ func main() {
}, },
cli.Command{ cli.Command{
Name: "open", Name: "open",
Usage: "open listener",
Usage: "open barak listener",
Action: cliOpenListener, Action: cliOpenListener,
}, },
cli.Command{ cli.Command{
Name: "close", Name: "close",
Usage: "close listener",
Usage: "close barka listener",
Action: cliCloseListener, Action: cliCloseListener,
}, },
cli.Command{ cli.Command{
@ -119,6 +119,11 @@ func main() {
Usage: "download file <remote-path> <local-path-prefix>", Usage: "download file <remote-path> <local-path-prefix>",
Action: cliDownloadFile, Action: cliDownloadFile,
}, },
cli.Command{
Name: "quit",
Usage: "quit barak",
Action: cliQuit,
},
} }
app.Run(os.Args) app.Run(os.Args)
} }
@ -330,3 +335,21 @@ func cliDownloadFile(c *cli.Context) {
} }
wg.Wait() 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()
}

Loading…
Cancel
Save