server.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. This file is part of go-ethereum
  3. go-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. go-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. package rpc
  15. import (
  16. "fmt"
  17. "net"
  18. "net/http"
  19. "github.com/ethereum/go-ethereum/logger"
  20. "github.com/ethereum/go-ethereum/xeth"
  21. )
  22. var jsonlogger = logger.NewLogger("JSON")
  23. type JsonRpcServer struct {
  24. quit chan bool
  25. listener net.Listener
  26. pipe *xeth.JSXEth
  27. }
  28. func (s *JsonRpcServer) exitHandler() {
  29. out:
  30. for {
  31. select {
  32. case <-s.quit:
  33. s.listener.Close()
  34. break out
  35. }
  36. }
  37. jsonlogger.Infoln("Shutdown JSON-RPC server")
  38. }
  39. func (s *JsonRpcServer) Stop() {
  40. close(s.quit)
  41. }
  42. func (s *JsonRpcServer) Start() {
  43. jsonlogger.Infoln("Starting JSON-RPC server")
  44. go s.exitHandler()
  45. h := apiHandler(&EthereumApi{pipe: s.pipe})
  46. http.Handle("/", h)
  47. err := http.Serve(s.listener, nil)
  48. // FIX Complains on shutdown due to listner already being closed
  49. if err != nil {
  50. jsonlogger.Errorln("Error on JSON-RPC interface:", err)
  51. }
  52. }
  53. func NewJsonRpcServer(pipe *xeth.JSXEth, port int) (*JsonRpcServer, error) {
  54. sport := fmt.Sprintf(":%d", port)
  55. l, err := net.Listen("tcp", sport)
  56. if err != nil {
  57. return nil, err
  58. }
  59. return &JsonRpcServer{
  60. listener: l,
  61. quit: make(chan bool),
  62. pipe: pipe,
  63. }, nil
  64. }
  65. func apiHandler(xeth *EthereumApi) http.Handler {
  66. fn := func(w http.ResponseWriter, req *http.Request) {
  67. jsonlogger.Debugln("Handling request")
  68. reqParsed, reqerr := JSON.ParseRequestBody(req)
  69. if reqerr != nil {
  70. JSON.Send(w, &RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: ErrorParseRequest})
  71. return
  72. }
  73. var response interface{}
  74. reserr := JSON.GetRequestReply(xeth, &reqParsed, &response)
  75. if reserr != nil {
  76. jsonlogger.Errorln(reserr)
  77. JSON.Send(w, &RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: reserr.Error()})
  78. return
  79. }
  80. jsonlogger.Debugf("Generated response: %T %s", response, response)
  81. JSON.Send(w, &RpcSuccessResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: false, Result: response})
  82. }
  83. return http.HandlerFunc(fn)
  84. }