diff options
author | obscuren <geffobscura@gmail.com> | 2014-12-20 09:34:12 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-12-20 09:34:12 +0800 |
commit | 3983dd2428137211f84f299f9ce8690c22f50afd (patch) | |
tree | 3a2dc53b365e6f377fc82a3514150d1297fe549c /rpc/server.go | |
parent | 7daa8c2f6eb25511c6a54ad420709af911fc6748 (diff) | |
parent | 0a9dc1536c5d776844d6947a0090ff7e1a7c6ab4 (diff) | |
download | go-tangerine-63766f5809b5f59c77e93bf02278c7d1c20f781e.tar go-tangerine-63766f5809b5f59c77e93bf02278c7d1c20f781e.tar.gz go-tangerine-63766f5809b5f59c77e93bf02278c7d1c20f781e.tar.bz2 go-tangerine-63766f5809b5f59c77e93bf02278c7d1c20f781e.tar.lz go-tangerine-63766f5809b5f59c77e93bf02278c7d1c20f781e.tar.xz go-tangerine-63766f5809b5f59c77e93bf02278c7d1c20f781e.tar.zst go-tangerine-63766f5809b5f59c77e93bf02278c7d1c20f781e.zip |
Merge branch 'release/v0.7.10'vv0.7.10
Diffstat (limited to 'rpc/server.go')
-rw-r--r-- | rpc/server.go | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/rpc/server.go b/rpc/server.go new file mode 100644 index 000000000..983dc6c33 --- /dev/null +++ b/rpc/server.go @@ -0,0 +1,67 @@ +package rpc + +import ( + "fmt" + "net" + "net/rpc" + "net/rpc/jsonrpc" + + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/xeth" +) + +var jsonlogger = logger.NewLogger("JSON") + +type JsonRpcServer struct { + quit chan bool + listener net.Listener + pipe *xeth.JSXEth +} + +func (s *JsonRpcServer) exitHandler() { +out: + for { + select { + case <-s.quit: + s.listener.Close() + break out + } + } + + jsonlogger.Infoln("Shutdown JSON-RPC server") +} + +func (s *JsonRpcServer) Stop() { + close(s.quit) +} + +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) + } +} + +func NewJsonRpcServer(pipe *xeth.JSXEth, port int) (*JsonRpcServer, error) { + sport := fmt.Sprintf(":%d", port) + l, err := net.Listen("tcp", sport) + if err != nil { + return nil, err + } + + return &JsonRpcServer{ + listener: l, + quit: make(chan bool), + pipe: pipe, + }, nil +} |