diff options
author | Taylor Gerring <taylor.gerring@gmail.com> | 2015-01-13 13:25:29 +0800 |
---|---|---|
committer | Taylor Gerring <taylor.gerring@gmail.com> | 2015-01-13 13:25:29 +0800 |
commit | 32bed50ba107ff69ff489e4df6f935fd404a5caa (patch) | |
tree | 282d230fca9f73660ccaeafc9db0f0185c33efa0 /rpc/server.go | |
parent | c9985bf563888d5f346408d2ff174167e8b65880 (diff) | |
download | dexon-32bed50ba107ff69ff489e4df6f935fd404a5caa.tar dexon-32bed50ba107ff69ff489e4df6f935fd404a5caa.tar.gz dexon-32bed50ba107ff69ff489e4df6f935fd404a5caa.tar.bz2 dexon-32bed50ba107ff69ff489e4df6f935fd404a5caa.tar.lz dexon-32bed50ba107ff69ff489e4df6f935fd404a5caa.tar.xz dexon-32bed50ba107ff69ff489e4df6f935fd404a5caa.tar.zst dexon-32bed50ba107ff69ff489e4df6f935fd404a5caa.zip |
begin conversion to rpc over http
Per specification at
https://github.com/ethereum/wiki/wiki/Generic-JSON-RPC
Diffstat (limited to 'rpc/server.go')
-rw-r--r-- | rpc/server.go | 45 |
1 files changed, 33 insertions, 12 deletions
diff --git a/rpc/server.go b/rpc/server.go index 983dc6c33..39b955dff 100644 --- a/rpc/server.go +++ b/rpc/server.go @@ -3,8 +3,7 @@ package rpc import ( "fmt" "net" - "net/rpc" - "net/rpc/jsonrpc" + "net/http" "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/xeth" @@ -38,17 +37,14 @@ func (s *JsonRpcServer) Stop() { func (s *JsonRpcServer) Start() { jsonlogger.Infoln("Starting JSON-RPC server") go s.exitHandler() - rpc.Register(&EthereumApi{pipe: s.pipe}) - rpc.HandleHTTP() - for { - conn, err := s.listener.Accept() - if err != nil { - jsonlogger.Infoln("Error starting JSON-RPC:", err) - break - } - jsonlogger.Debugln("Incoming request.") - go jsonrpc.ServeConn(conn) + h := apiHandler(&EthereumApi{pipe: s.pipe}) + http.Handle("/", h) + + err := http.Serve(s.listener, nil) + // TODO Complains on shutdown due to listner already being closed + if err != nil { + jsonlogger.Errorln("Error on JSON-RPC interface:", err) } } @@ -65,3 +61,28 @@ func NewJsonRpcServer(pipe *xeth.JSXEth, port int) (*JsonRpcServer, error) { pipe: pipe, }, nil } + +func apiHandler(xeth *EthereumApi) http.Handler { + fn := func(w http.ResponseWriter, req *http.Request) { + jsonlogger.Debugln("Handling request") + + reqParsed, reqerr := JSON.ParseRequestBody(req) + if reqerr != nil { + JSON.Send(w, &RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: ErrorParseRequest}) + return + } + + var response interface{} + reserr := JSON.GetRequestReply(xeth, &reqParsed, &response) + if reserr != nil { + jsonlogger.Errorln(reserr) + JSON.Send(w, &RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: reserr.Error()}) + return + } + + jsonlogger.Debugf("Generated response: %T %s", response, response) + JSON.Send(w, &RpcSuccessResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: false, Result: response}) + } + + return http.HandlerFunc(fn) +} |