diff options
author | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-04-21 01:34:50 +0800 |
---|---|---|
committer | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-04-21 01:34:50 +0800 |
commit | 99e825ad96e7b3f655f170a52ad6e408b6feb311 (patch) | |
tree | 06f41da20ebc0b759f839c396e5bc1ad79cc3055 /rpc/http.go | |
parent | 3d7c1b8194e509f61fb64a129242b3170ecffd08 (diff) | |
parent | 61885aa965282d5879b9c4fbb740e96e9b680558 (diff) | |
download | go-tangerine-99e825ad96e7b3f655f170a52ad6e408b6feb311.tar go-tangerine-99e825ad96e7b3f655f170a52ad6e408b6feb311.tar.gz go-tangerine-99e825ad96e7b3f655f170a52ad6e408b6feb311.tar.bz2 go-tangerine-99e825ad96e7b3f655f170a52ad6e408b6feb311.tar.lz go-tangerine-99e825ad96e7b3f655f170a52ad6e408b6feb311.tar.xz go-tangerine-99e825ad96e7b3f655f170a52ad6e408b6feb311.tar.zst go-tangerine-99e825ad96e7b3f655f170a52ad6e408b6feb311.zip |
Merge pull request #734 from bas-vk/issue-729
admin.StopRPC added to console
Diffstat (limited to 'rpc/http.go')
-rw-r--r-- | rpc/http.go | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/rpc/http.go b/rpc/http.go index 790442a28..f9c646908 100644 --- a/rpc/http.go +++ b/rpc/http.go @@ -5,7 +5,6 @@ import ( "fmt" "io" "io/ioutil" - "net" "net/http" "github.com/ethereum/go-ethereum/logger" @@ -15,6 +14,7 @@ import ( ) var rpclogger = logger.NewLogger("RPC") +var rpclistener *stoppableTCPListener const ( jsonrpcver = "2.0" @@ -22,11 +22,19 @@ const ( ) func Start(pipe *xeth.XEth, config RpcConfig) error { - l, err := net.Listen("tcp", fmt.Sprintf("%s:%d", config.ListenAddress, config.ListenPort)) + if rpclistener != nil { + if fmt.Sprintf("%s:%d", config.ListenAddress, config.ListenPort) != rpclistener.Addr().String() { + return fmt.Errorf("RPC service already running on %s ", rpclistener.Addr().String()) + } + return nil // RPC service already running on given host/port + } + + l, err := newStoppableTCPListener(fmt.Sprintf("%s:%d", config.ListenAddress, config.ListenPort)) if err != nil { rpclogger.Errorf("Can't listen on %s:%d: %v", config.ListenAddress, config.ListenPort, err) return err } + rpclistener = l var handler http.Handler if len(config.CorsDomain) > 0 { @@ -35,9 +43,9 @@ func Start(pipe *xeth.XEth, config RpcConfig) error { opts.AllowedOrigins = []string{config.CorsDomain} c := cors.New(opts) - handler = c.Handler(JSONRPC(pipe)) + handler = newStoppableHandler(c.Handler(JSONRPC(pipe)), l.stop) } else { - handler = JSONRPC(pipe) + handler = newStoppableHandler(JSONRPC(pipe), l.stop) } go http.Serve(l, handler) @@ -45,6 +53,15 @@ func Start(pipe *xeth.XEth, config RpcConfig) error { return nil } +func Stop() error { + if rpclistener != nil { + rpclistener.Stop() + rpclistener = nil + } + + return nil +} + // JSONRPC returns a handler that implements the Ethereum JSON-RPC API. func JSONRPC(pipe *xeth.XEth) http.Handler { api := NewEthereumApi(pipe) |