From d9bfe82d9399c8a1e5779d7c7ee062d9472875ef Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Thu, 23 Apr 2015 18:33:20 -0700 Subject: [PATCH] rpc.StartHTTPServer listener bug fix --- cmd/barak/main.go | 13 +++++-------- node/node.go | 3 +-- p2p/listener.go | 2 ++ p2p/switch_test.go | 2 ++ rpc/http_server.go | 14 +++++++------- rpc/test/tests_test.go | 2 -- 6 files changed, 17 insertions(+), 19 deletions(-) diff --git a/cmd/barak/main.go b/cmd/barak/main.go index b59186e17..a73c63868 100644 --- a/cmd/barak/main.go +++ b/cmd/barak/main.go @@ -19,7 +19,6 @@ import ( "github.com/tendermint/tendermint/binary" . "github.com/tendermint/tendermint/cmd/barak/types" . "github.com/tendermint/tendermint/common" - "github.com/tendermint/tendermint/p2p" pcm "github.com/tendermint/tendermint/process" "github.com/tendermint/tendermint/rpc" ) @@ -99,25 +98,23 @@ func main() { fmt.Printf("Barak: %v\n", barak) // Start rpc server. - listener := p2p.NewDefaultListener("tcp", options.ListenAddress, false) mux := http.NewServeMux() mux.HandleFunc("/download", ServeFile) mux.HandleFunc("/register", Register) // TODO: mux.HandleFunc("/upload", UploadFile) rpc.RegisterRPCFuncs(mux, Routes) - rpc.StartHTTPServer(listener, mux) + rpc.StartHTTPServer(options.ListenAddress, mux) // Register this barak with central listener - extAddress := listener.ExternalAddress().String() for _, registry := range barak.registries { go func(registry string) { - var response ResponseRegister - _, err = rpc.Call(registry, "register", Arr(extAddress), &response) + resp, err := http.Get(registry + "/register") if err != nil { fmt.Printf("Error registering to registry %v:\n %v\n", registry, err) - } else { - fmt.Printf("Successfully registered with registry %v\n", registry) + return } + body, _ := ioutil.ReadAll(resp.Body) + fmt.Printf("Successfully registered with registry %v\n %v\n", registry, string(body)) }(registry) } diff --git a/node/node.go b/node/node.go index d0f14915d..52e2d26fd 100644 --- a/node/node.go +++ b/node/node.go @@ -162,11 +162,10 @@ func (n *Node) StartRPC() { core.SetSwitch(n.sw) listenAddr := config.App().GetString("RPC.HTTP.ListenAddr") - listener := p2p.NewDefaultListener("tcp", listenAddr, false) mux := http.NewServeMux() rpc.RegisterEventsHandler(mux, n.evsw) rpc.RegisterRPCFuncs(mux, core.Routes) - rpc.StartHTTPServer(listener, mux) + rpc.StartHTTPServer(listenAddr, mux) } func (n *Node) Switch() *p2p.Switch { diff --git a/p2p/listener.go b/p2p/listener.go index 967a3f5fe..e80881259 100644 --- a/p2p/listener.go +++ b/p2p/listener.go @@ -137,6 +137,8 @@ func (l *DefaultListener) ExternalAddress() *NetAddress { return l.extAddr } +// NOTE: The returned listener is already Accept()'ing. +// So it's not suitable to pass into http.Serve(). func (l *DefaultListener) NetListener() net.Listener { return l.listener } diff --git a/p2p/switch_test.go b/p2p/switch_test.go index fc3ed41db..98bf4b4da 100644 --- a/p2p/switch_test.go +++ b/p2p/switch_test.go @@ -77,11 +77,13 @@ func makeSwitchPair(t testing.TB, initSwitch func(*Switch) *Switch) (*Switch, *S s1.SetNodeInfo(&types.NodeInfo{ Moniker: "switch1", Network: "testing", + Version: "123.123.123", }) s2 := initSwitch(NewSwitch()) s2.SetNodeInfo(&types.NodeInfo{ Moniker: "switch2", Network: "testing", + Version: "123.123.123", }) // Start switches diff --git a/rpc/http_server.go b/rpc/http_server.go index 9feb672a4..6d3c6ed05 100644 --- a/rpc/http_server.go +++ b/rpc/http_server.go @@ -13,17 +13,17 @@ import ( "github.com/tendermint/tendermint/alert" "github.com/tendermint/tendermint/binary" . "github.com/tendermint/tendermint/common" - "github.com/tendermint/tendermint/p2p" ) -func StartHTTPServer(listener p2p.Listener, handler http.Handler) { - log.Info(Fmt("Starting RPC HTTP server on ext:%v int:%v", - listener.ExternalAddress(), - listener.InternalAddress())) +func StartHTTPServer(listenAddr string, handler http.Handler) { + log.Info(Fmt("Starting RPC HTTP server on %v", listenAddr)) go func() { - netListener := listener.(*p2p.DefaultListener).NetListener() + listener, err := net.Listen("tcp", listenAddr) + if err != nil { + Exit(Fmt("Failed to listen to %v", listenAddr)) + } res := http.Serve( - netListener, + listener, RecoverAndLogHandler(handler), ) log.Crit("RPC HTTP server stopped", "result", res) diff --git a/rpc/test/tests_test.go b/rpc/test/tests_test.go index 311505d0e..b2381eff0 100644 --- a/rpc/test/tests_test.go +++ b/rpc/test/tests_test.go @@ -16,7 +16,6 @@ func testStatus(t *testing.T, typ string) { if err != nil { t.Fatal(err) } - fmt.Println(">>>", resp) if resp.Network != config.App().GetString("Network") { t.Fatal(fmt.Errorf("Network mismatch: got %s expected %s", resp.Network, config.App().Get("Network"))) @@ -29,7 +28,6 @@ func testGenPriv(t *testing.T, typ string) { if err != nil { t.Fatal(err) } - fmt.Println(">>>", resp) if len(resp.PrivAccount.Address) == 0 { t.Fatal("Failed to generate an address") }