Browse Source

rpc.StartHTTPServer listener bug fix

pull/61/head
Jae Kwon 10 years ago
parent
commit
d9bfe82d93
6 changed files with 17 additions and 19 deletions
  1. +5
    -8
      cmd/barak/main.go
  2. +1
    -2
      node/node.go
  3. +2
    -0
      p2p/listener.go
  4. +2
    -0
      p2p/switch_test.go
  5. +7
    -7
      rpc/http_server.go
  6. +0
    -2
      rpc/test/tests_test.go

+ 5
- 8
cmd/barak/main.go View File

@ -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)
}


+ 1
- 2
node/node.go View File

@ -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 {


+ 2
- 0
p2p/listener.go View File

@ -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
}


+ 2
- 0
p2p/switch_test.go View File

@ -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


+ 7
- 7
rpc/http_server.go View File

@ -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)


+ 0
- 2
rpc/test/tests_test.go View File

@ -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")
}


Loading…
Cancel
Save