From a09051438c9fab9d8aae869a5dfb36835bc2e9f0 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Wed, 8 Apr 2015 11:35:17 -0700 Subject: [PATCH] Intermediate... working on debora --- binary/reflect.go | 4 +++- common/os.go | 22 ++++++++++++++++++ node/node.go | 16 +------------ p2p/switch.go | 11 +++++++++ process/process.go | 37 +++++++++++++++++++++++-------- rpc/core_client/client.go | 2 +- rpc/core_client/client_methods.go | 2 ++ 7 files changed, 68 insertions(+), 26 deletions(-) create mode 100644 common/os.go diff --git a/binary/reflect.go b/binary/reflect.go index 251c040c..345a6f38 100644 --- a/binary/reflect.go +++ b/binary/reflect.go @@ -178,7 +178,9 @@ func RegisterType(info *TypeInfo) *TypeInfo { continue } jsonName := field.Tag.Get("json") - if jsonName == "" { + if jsonName == "-" { + continue + } else if jsonName == "" { jsonName = field.Name } structFields = append(structFields, StructFieldInfo{ diff --git a/common/os.go b/common/os.go new file mode 100644 index 00000000..8d14bd72 --- /dev/null +++ b/common/os.go @@ -0,0 +1,22 @@ +package common + +import ( + "fmt" + "os" + "os/signal" +) + +func TrapSignal(cb func()) { + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + go func() { + for sig := range c { + fmt.Printf("captured %v, exiting...\n", sig) + if cb != nil { + cb() + } + os.Exit(1) + } + }() + select {} +} diff --git a/node/node.go b/node/node.go index f97842a4..198f2d13 100644 --- a/node/node.go +++ b/node/node.go @@ -2,7 +2,6 @@ package node import ( "os" - "os/signal" bc "github.com/tendermint/tendermint/blockchain" . "github.com/tendermint/tendermint/common" @@ -166,20 +165,7 @@ func RunNode() { } // Sleep forever and then... - trapSignal(func() { + TrapSignal(func() { n.Stop() }) } - -func trapSignal(cb func()) { - c := make(chan os.Signal, 1) - signal.Notify(c, os.Interrupt) - go func() { - for sig := range c { - log.Info(Fmt("captured %v, exiting..", sig)) - cb() - os.Exit(1) - } - }() - select {} -} diff --git a/p2p/switch.go b/p2p/switch.go index cc60e4d3..b7b6aa32 100644 --- a/p2p/switch.go +++ b/p2p/switch.go @@ -19,6 +19,17 @@ type Reactor interface { Receive(chId byte, peer *Peer, msgBytes []byte) } +//-------------------------------------- + +type BaseReactor struct{} + +func (_ BaseReactor) Start(sw *Switch) {} +func (_ BaseReactor) Stop() {} +func (_ BaseReactor) GetChannels() []*ChannelDescriptor { return nil } +func (_ BaseReactor) AddPeer(peer *Peer) {} +func (_ BaseReactor) RemovePeer(peer *Peer, reason interface{}) {} +func (_ BaseReactor) Receive(chId byte, peer *Peer, msgBytes []byte) {} + //----------------------------------------------------------------------------- /* diff --git a/process/process.go b/process/process.go index 99dc6440..f7994940 100644 --- a/process/process.go +++ b/process/process.go @@ -19,8 +19,12 @@ func makeFile(prefix string) *os.File { } type Process struct { - Cmd *exec.Cmd - Output *os.File + Label string + ExecPath string + StartTime time.Time + Cmd *exec.Cmd `json:"-"` + Output *os.File `json:"-"` + ExitState *os.ProcessState `json:"-"` } const ( @@ -28,9 +32,11 @@ const ( ProcessModeDaemon ) -func CreateProcess(mode int, name string, args ...string) *Process { - out := makeFile(name) - cmd := exec.Command(name, args...) +// execPath: command name +// args: args to command. (should not include name) +func Create(mode int, label string, execPath string, args ...string) *Process { + out := makeFile(label) + cmd := exec.Command(execPath, args...) switch mode { case ProcessModeStd: cmd.Stdout = io.MultiWriter(os.Stdout, out) @@ -48,14 +54,27 @@ func CreateProcess(mode int, name string, args ...string) *Process { fmt.Printf("Success!") } return &Process{ - Cmd: cmd, - Output: out, + Label: label, + ExecPath: execPath, + StartTime: time.Now(), + Cmd: cmd, + Output: out, } } -func Watch(proc *Process) { +func Wait(proc *Process) error { exitErr := proc.Cmd.Wait() if exitErr != nil { - fmt.Println("%v", exitErr) + fmt.Printf("Process exit: %v\n", exitErr) + proc.ExitState = exitErr.(*exec.ExitError).ProcessState + } + return exitErr +} + +func Stop(proc *Process, kill bool) error { + if kill { + return proc.Cmd.Process.Kill() + } else { + return proc.Cmd.Process.Signal(os.Interrupt) } } diff --git a/rpc/core_client/client.go b/rpc/core_client/client.go index b7ec0378..0b99f25e 100644 --- a/rpc/core_client/client.go +++ b/rpc/core_client/client.go @@ -10,7 +10,7 @@ import ( "net/url" "reflect" // Uncomment to use go:generate - // _ "github.com/ebuchman/go-rpc-gen" + // _ "github.com/tendermint/go-rpc-gen" ) type Response struct { diff --git a/rpc/core_client/client_methods.go b/rpc/core_client/client_methods.go index 921083a1..938fad01 100644 --- a/rpc/core_client/client_methods.go +++ b/rpc/core_client/client_methods.go @@ -1,3 +1,5 @@ +// File generated by github.com/ebuchman/rpc-gen + package core_client import (