begin conversion to rpc over http
Per specification at https://github.com/ethereum/wiki/wiki/Generic-JSON-RPC
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user