From d790229a33b1f3e6256ca6916be17a0cc3663076 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Sun, 25 Jan 2015 14:50:43 -0600 Subject: [PATCH 1/8] Move HTTP transport to sub package of RPC --- cmd/utils/cmd.go | 4 +-- eth/backend.go | 2 +- rpc/{ => http}/server.go | 65 +++++++++++++++++++++------------------- rpc/json.go | 19 ++++++------ rpc/message.go | 14 ++++----- rpc/packages.go | 15 ++++++++-- 6 files changed, 66 insertions(+), 53 deletions(-) rename rpc/{ => http}/server.go (58%) diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go index 95a7f89c8d..13fbc7c64a 100644 --- a/cmd/utils/cmd.go +++ b/cmd/utils/cmd.go @@ -38,7 +38,7 @@ import ( "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/miner" "github.com/ethereum/go-ethereum/rlp" - "github.com/ethereum/go-ethereum/rpc" + rpchttp "github.com/ethereum/go-ethereum/rpc/http" "github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/websocket" "github.com/ethereum/go-ethereum/xeth" @@ -193,7 +193,7 @@ func KeyTasks(keyManager *crypto.KeyManager, KeyRing string, GenAddr bool, Secre func StartRpc(ethereum *eth.Ethereum, RpcPort int) { var err error - ethereum.RpcServer, err = rpc.NewJsonRpcServer(xeth.NewJSXEth(ethereum), RpcPort) + ethereum.RpcServer, err = rpchttp.NewRpcHttpServer(xeth.NewJSXEth(ethereum), RpcPort) if err != nil { clilogger.Errorf("Could not start RPC interface (port %v): %v", RpcPort, err) } else { diff --git a/eth/backend.go b/eth/backend.go index c3c7d12878..fbe080618b 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -66,7 +66,7 @@ type Ethereum struct { txSub event.Subscription blockSub event.Subscription - RpcServer *rpc.JsonRpcServer + RpcServer rpc.RpcServer keyManager *crypto.KeyManager clientIdentity p2p.ClientIdentity diff --git a/rpc/server.go b/rpc/http/server.go similarity index 58% rename from rpc/server.go rename to rpc/http/server.go index a4cf1aa7c0..a8b116076f 100644 --- a/rpc/server.go +++ b/rpc/http/server.go @@ -14,7 +14,7 @@ You should have received a copy of the GNU General Public License along with go-ethereum. If not, see . */ -package rpc +package rpchttp import ( "fmt" @@ -22,18 +22,34 @@ import ( "net/http" "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/xeth" ) -var jsonlogger = logger.NewLogger("JSON") +var rpchttplogger = logger.NewLogger("RPC-HTTP") +var JSON rpc.JsonWrapper -type JsonRpcServer struct { +func NewRpcHttpServer(pipe *xeth.JSXEth, port int) (*RpcHttpServer, error) { + sport := fmt.Sprintf(":%d", port) + l, err := net.Listen("tcp", sport) + if err != nil { + return nil, err + } + + return &RpcHttpServer{ + listener: l, + quit: make(chan bool), + pipe: pipe, + }, nil +} + +type RpcHttpServer struct { quit chan bool listener net.Listener pipe *xeth.JSXEth } -func (s *JsonRpcServer) exitHandler() { +func (s *RpcHttpServer) exitHandler() { out: for { select { @@ -43,61 +59,48 @@ out: } } - jsonlogger.Infoln("Shutdown JSON-RPC server") + rpchttplogger.Infoln("Shutdown RPC-HTTP server") } -func (s *JsonRpcServer) Stop() { +func (s *RpcHttpServer) Stop() { close(s.quit) } -func (s *JsonRpcServer) Start() { - jsonlogger.Infoln("Starting JSON-RPC server") +func (s *RpcHttpServer) Start() { + rpchttplogger.Infoln("Starting RPC-HTTP server") go s.exitHandler() - h := apiHandler(&EthereumApi{pipe: s.pipe}) + api := rpc.NewEthereumApi(s.pipe) + h := s.apiHandler(api) http.Handle("/", h) err := http.Serve(s.listener, nil) // FIX Complains on shutdown due to listner already being closed if err != nil { - jsonlogger.Errorln("Error on JSON-RPC interface:", err) + rpchttplogger.Errorln("Error on RPC-HTTP interface:", err) } } -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 -} - -func apiHandler(xeth *EthereumApi) http.Handler { +func (s *RpcHttpServer) apiHandler(xeth *rpc.EthereumApi) http.Handler { fn := func(w http.ResponseWriter, req *http.Request) { - jsonlogger.Debugln("Handling request") + rpchttplogger.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}) + JSON.Send(w, &rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: rpc.ErrorParseRequest}) return } var response interface{} reserr := xeth.GetRequestReply(&reqParsed, &response) if reserr != nil { - jsonlogger.Errorln(reserr) - JSON.Send(w, &RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: reserr.Error()}) + rpchttplogger.Errorln(reserr) + JSON.Send(w, &rpc.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}) + rpchttplogger.Debugf("Generated response: %T %s", response, response) + JSON.Send(w, &rpc.RpcSuccessResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: false, Result: response}) } return http.HandlerFunc(fn) diff --git a/rpc/json.go b/rpc/json.go index 5446cb3c10..9bd2048183 100644 --- a/rpc/json.go +++ b/rpc/json.go @@ -18,25 +18,28 @@ package rpc import ( "encoding/json" + "github.com/ethereum/go-ethereum/logger" "io" "net/http" ) -type jsonWrapper struct{} +var rpclogger = logger.NewLogger("RPC") -func (self jsonWrapper) Send(writer io.Writer, v interface{}) (n int, err error) { +type JsonWrapper struct{} + +func (self JsonWrapper) Send(writer io.Writer, v interface{}) (n int, err error) { var payload []byte payload, err = json.Marshal(v) if err != nil { - jsonlogger.Fatalln("Error marshalling JSON", err) + rpclogger.Fatalln("Error marshalling JSON", err) return 0, err } - jsonlogger.Infof("Sending payload: %s", payload) + rpclogger.Infof("Sending payload: %s", payload) return writer.Write(payload) } -func (self jsonWrapper) ParseRequestBody(req *http.Request) (RpcRequest, error) { +func (self JsonWrapper) ParseRequestBody(req *http.Request) (RpcRequest, error) { var reqParsed RpcRequest // Convert JSON to native types @@ -46,12 +49,10 @@ func (self jsonWrapper) ParseRequestBody(req *http.Request) (RpcRequest, error) err := d.Decode(&reqParsed) if err != nil { - jsonlogger.Errorln("Error decoding JSON: ", err) + rpclogger.Errorln("Error decoding JSON: ", err) return reqParsed, err } - jsonlogger.DebugDetailf("Parsed request: %s", reqParsed) + rpclogger.DebugDetailf("Parsed request: %s", reqParsed) return reqParsed, nil } - -var JSON jsonWrapper diff --git a/rpc/message.go b/rpc/message.go index 63de734b83..caf50a6c0d 100644 --- a/rpc/message.go +++ b/rpc/message.go @@ -67,7 +67,7 @@ func (req *RpcRequest) ToGetBlockArgs() (*GetBlockArgs, error) { if err != nil { return nil, NewErrorResponse(ErrorDecodeArgs) } - jsonlogger.DebugDetailf("%T %v", args, args) + rpclogger.DebugDetailf("%T %v", args, args) return args, nil } @@ -82,7 +82,7 @@ func (req *RpcRequest) ToNewTxArgs() (*NewTxArgs, error) { if err != nil { return nil, NewErrorResponse(ErrorDecodeArgs) } - jsonlogger.DebugDetailf("%T %v", args, args) + rpclogger.DebugDetailf("%T %v", args, args) return args, nil } @@ -97,7 +97,7 @@ func (req *RpcRequest) ToPushTxArgs() (*PushTxArgs, error) { if err != nil { return nil, NewErrorResponse(ErrorDecodeArgs) } - jsonlogger.DebugDetailf("%T %v", args, args) + rpclogger.DebugDetailf("%T %v", args, args) return args, nil } @@ -113,7 +113,7 @@ func (req *RpcRequest) ToGetStorageArgs() (*GetStorageArgs, error) { if err != nil { return nil, NewErrorResponse(ErrorDecodeArgs) } - jsonlogger.DebugDetailf("%T %v", args, args) + rpclogger.DebugDetailf("%T %v", args, args) return args, nil } @@ -128,7 +128,7 @@ func (req *RpcRequest) ToGetTxCountArgs() (*GetTxCountArgs, error) { if err != nil { return nil, NewErrorResponse(ErrorDecodeArgs) } - jsonlogger.DebugDetailf("%T %v", args, args) + rpclogger.DebugDetailf("%T %v", args, args) return args, nil } @@ -143,7 +143,7 @@ func (req *RpcRequest) ToGetBalanceArgs() (*GetBalanceArgs, error) { if err != nil { return nil, NewErrorResponse(ErrorDecodeArgs) } - jsonlogger.DebugDetailf("%T %v", args, args) + rpclogger.DebugDetailf("%T %v", args, args) return args, nil } @@ -158,7 +158,7 @@ func (req *RpcRequest) ToGetCodeAtArgs() (*GetCodeAtArgs, error) { if err != nil { return nil, NewErrorResponse(ErrorDecodeArgs) } - jsonlogger.DebugDetailf("%T %v", args, args) + rpclogger.DebugDetailf("%T %v", args, args) return args, nil } diff --git a/rpc/packages.go b/rpc/packages.go index e87e0db7ca..8b59650691 100644 --- a/rpc/packages.go +++ b/rpc/packages.go @@ -33,6 +33,15 @@ import ( "github.com/ethereum/go-ethereum/xeth" ) +type RpcServer interface { + Start() + Stop() +} + +func NewEthereumApi(xeth *xeth.JSXEth) *EthereumApi { + return &EthereumApi{pipe: xeth} +} + type EthereumApi struct { pipe *xeth.JSXEth } @@ -103,7 +112,7 @@ func (p *EthereumApi) GetStorageAt(args *GetStorageArgs, reply *interface{}) err i, _ := new(big.Int).SetString(args.Key, 10) hx = ethutil.Bytes2Hex(i.Bytes()) } - jsonlogger.Debugf("GetStorageAt(%s, %s)\n", args.Address, hx) + rpclogger.Debugf("GetStorageAt(%s, %s)\n", args.Address, hx) value := state.Storage(ethutil.Hex2Bytes(hx)) *reply = GetStorageAtRes{Address: args.Address, Key: args.Key, Value: value.Str()} return nil @@ -159,7 +168,7 @@ func (p *EthereumApi) GetCodeAt(args *GetCodeAtArgs, reply *interface{}) error { func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error { // Spec at https://github.com/ethereum/wiki/wiki/Generic-JSON-RPC - jsonlogger.DebugDetailf("%T %s", req.Params, req.Params) + rpclogger.DebugDetailf("%T %s", req.Params, req.Params) switch req.Method { case "eth_coinbase": return p.GetCoinbase(reply) @@ -203,6 +212,6 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error return NewErrorResponse(ErrorNotImplemented) } - jsonlogger.DebugDetailf("Reply: %T %s", reply, reply) + rpclogger.DebugDetailf("Reply: %T %s", reply, reply) return nil } From 2f0166b94583cc17fab28f3e4f1214f220d6c4a3 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 27 Jan 2015 12:26:30 -0600 Subject: [PATCH 2/8] Indicate port in startup log message --- rpc/http/server.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/rpc/http/server.go b/rpc/http/server.go index a8b116076f..631d471108 100644 --- a/rpc/http/server.go +++ b/rpc/http/server.go @@ -40,6 +40,7 @@ func NewRpcHttpServer(pipe *xeth.JSXEth, port int) (*RpcHttpServer, error) { listener: l, quit: make(chan bool), pipe: pipe, + port: port, }, nil } @@ -47,6 +48,7 @@ type RpcHttpServer struct { quit chan bool listener net.Listener pipe *xeth.JSXEth + port int } func (s *RpcHttpServer) exitHandler() { @@ -67,7 +69,7 @@ func (s *RpcHttpServer) Stop() { } func (s *RpcHttpServer) Start() { - rpchttplogger.Infoln("Starting RPC-HTTP server") + rpchttplogger.Infof("Starting RPC-HTTP server on port %d", s.port) go s.exitHandler() api := rpc.NewEthereumApi(s.pipe) From b8e7b8e2e1b88e40090c9b741b4cd7000689ae9e Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 27 Jan 2015 12:27:14 -0600 Subject: [PATCH 3/8] New simplified Websocket transport Uses rpc.EthereumApi abstraction --- rpc/websocket/server.go | 124 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 rpc/websocket/server.go diff --git a/rpc/websocket/server.go b/rpc/websocket/server.go new file mode 100644 index 0000000000..f854fe502e --- /dev/null +++ b/rpc/websocket/server.go @@ -0,0 +1,124 @@ +/* + This file is part of go-ethereum + + go-ethereum is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + go-ethereum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with go-ethereum. If not, see . +*/ +package websocket + +import ( + "fmt" + "net" + "net/http" + + ws "code.google.com/p/go.net/websocket" + "github.com/ethereum/go-ethereum/eth" + "github.com/ethereum/go-ethereum/event/filter" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/rpc" + "github.com/ethereum/go-ethereum/xeth" +) + +var wslogger = logger.NewLogger("RPC-WS") + +type WebSocketServer struct { + eth *eth.Ethereum + filterManager *filter.FilterManager + port int + doneCh chan bool + listener net.Listener +} + +func NewWebSocketServer(eth *eth.Ethereum, port int) (*WebSocketServer, error) { + sport := fmt.Sprintf(":%d", port) + l, err := net.Listen("tcp", sport) + if err != nil { + return nil, err + } + + filterManager := filter.NewFilterManager(eth.EventMux()) + go filterManager.Start() + + return &WebSocketServer{eth, + filterManager, + port, + make(chan bool), + l, + }, nil +} + +func (self *WebSocketServer) handlerLoop() { + for { + select { + case <-self.doneCh: + wslogger.Infoln("Shutdown RPC-WS server") + return + } + } +} + +func (self *WebSocketServer) Stop() { + close(self.doneCh) +} + +func (self *WebSocketServer) Start() { + wslogger.Infof("Starting RPC-WS server on port %d", self.port) + go self.handlerLoop() + + api := rpc.NewEthereumApi(xeth.NewJSXEth(self.eth)) + h := self.apiHandler(api) + http.Handle("/ws", h) + + err := http.Serve(self.listener, nil) + if err != nil { + wslogger.Errorln("Error on RPC-WS interface:", err) + } +} + +func (s *WebSocketServer) apiHandler(xeth *rpc.EthereumApi) http.Handler { + fn := func(w http.ResponseWriter, req *http.Request) { + h := sockHandler(xeth) + s := ws.Server{Handler: h} + s.ServeHTTP(w, req) + } + + return http.HandlerFunc(fn) +} + +func sockHandler(xeth *rpc.EthereumApi) ws.Handler { + fn := func(conn *ws.Conn) { + for { + // FIX wslogger does not output to console + wslogger.Debugln("Handling request") + var reqParsed rpc.RpcRequest + + if err := ws.JSON.Receive(conn, &reqParsed); err != nil { + wslogger.Debugln(rpc.ErrorParseRequest) + ws.JSON.Send(conn, rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: rpc.ErrorParseRequest}) + continue + } + + var response interface{} + reserr := xeth.GetRequestReply(&reqParsed, &response) + if reserr != nil { + wslogger.Errorln(reserr) + ws.JSON.Send(conn, rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: reserr.Error()}) + continue + } + + wslogger.Debugf("Generated response: %T %s", response, response) + ws.JSON.Send(conn, rpc.RpcSuccessResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: false, Result: response}) + } + } + return fn +} From 5f50fe7a4a6218bedf78333d751b57166932464a Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 27 Jan 2015 12:28:58 -0600 Subject: [PATCH 4/8] Update CLI to use new Websocket RPC MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use “wsport” flag to change default port --- cmd/ethereum/flags.go | 2 ++ cmd/ethereum/main.go | 2 +- cmd/utils/cmd.go | 16 ++++++++++++---- eth/backend.go | 4 ++++ 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/cmd/ethereum/flags.go b/cmd/ethereum/flags.go index f829744dc9..e0fbbb0086 100644 --- a/cmd/ethereum/flags.go +++ b/cmd/ethereum/flags.go @@ -41,6 +41,7 @@ var ( StartRpc bool StartWebSockets bool RpcPort int + WsPort int NatType string PMPGateway string OutboundPort string @@ -96,6 +97,7 @@ func Init() { flag.StringVar(&PMPGateway, "pmp", "", "Gateway IP for PMP") flag.IntVar(&MaxPeer, "maxpeer", 30, "maximum desired peers") flag.IntVar(&RpcPort, "rpcport", 8080, "port to start json-rpc server on") + flag.IntVar(&WsPort, "wsport", 40404, "port to start websocket rpc server on") flag.BoolVar(&StartRpc, "rpc", false, "start rpc server") flag.BoolVar(&StartWebSockets, "ws", false, "start websocket server") flag.BoolVar(&NonInteractive, "y", false, "non-interactive mode (say yes to confirmations)") diff --git a/cmd/ethereum/main.go b/cmd/ethereum/main.go index b816c678e7..92dbf5a6f1 100644 --- a/cmd/ethereum/main.go +++ b/cmd/ethereum/main.go @@ -131,7 +131,7 @@ func main() { } if StartWebSockets { - utils.StartWebSockets(ethereum) + utils.StartWebSockets(ethereum, WsPort) } utils.StartEthereum(ethereum, UseSeed) diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go index 13fbc7c64a..437e0c037c 100644 --- a/cmd/utils/cmd.go +++ b/cmd/utils/cmd.go @@ -39,8 +39,9 @@ import ( "github.com/ethereum/go-ethereum/miner" "github.com/ethereum/go-ethereum/rlp" rpchttp "github.com/ethereum/go-ethereum/rpc/http" + rpcws "github.com/ethereum/go-ethereum/rpc/websocket" "github.com/ethereum/go-ethereum/state" - "github.com/ethereum/go-ethereum/websocket" + // "github.com/ethereum/go-ethereum/websocket" "github.com/ethereum/go-ethereum/xeth" ) @@ -201,11 +202,18 @@ func StartRpc(ethereum *eth.Ethereum, RpcPort int) { } } -func StartWebSockets(eth *eth.Ethereum) { +func StartWebSockets(eth *eth.Ethereum, wsPort int) { clilogger.Infoln("Starting WebSockets") - sock := websocket.NewWebSocketServer(eth) - go sock.Serv() + // sock := websocket.NewWebSocketServer(eth) + // go sock.Serv() + var err error + eth.WsServer, err = rpcws.NewWebSocketServer(eth, wsPort) + if err != nil { + clilogger.Errorf("Could not start RPC interface (port %v): %v", wsPort, err) + } else { + go eth.WsServer.Start() + } } var gminer *miner.Miner diff --git a/eth/backend.go b/eth/backend.go index fbe080618b..ab30a1b389 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -67,6 +67,7 @@ type Ethereum struct { blockSub event.Subscription RpcServer rpc.RpcServer + WsServer rpc.RpcServer keyManager *crypto.KeyManager clientIdentity p2p.ClientIdentity @@ -276,6 +277,9 @@ func (s *Ethereum) Stop() { if s.RpcServer != nil { s.RpcServer.Stop() } + if s.WsServer != nil { + s.WsServer.Stop() + } s.txPool.Stop() s.eventMux.Stop() s.blockPool.Stop() From a38bca3438960ab1eec6a522eebc6622a1096881 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 27 Jan 2015 12:40:52 -0600 Subject: [PATCH 5/8] Add wsport flag to Mist --- cmd/mist/flags.go | 2 ++ cmd/mist/main.go | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/cmd/mist/flags.go b/cmd/mist/flags.go index 8a67cdd0a7..f530394c25 100644 --- a/cmd/mist/flags.go +++ b/cmd/mist/flags.go @@ -43,6 +43,7 @@ var ( StartRpc bool StartWebSockets bool RpcPort int + WsPort int UseUPnP bool NatType string OutboundPort string @@ -111,6 +112,7 @@ func Init() { flag.BoolVar(&UseUPnP, "upnp", true, "enable UPnP support") flag.IntVar(&MaxPeer, "maxpeer", 30, "maximum desired peers") flag.IntVar(&RpcPort, "rpcport", 8080, "port to start json-rpc server on") + flag.IntVar(&WsPort, "wsport", 40404, "port to start websocket rpc server on") flag.BoolVar(&StartRpc, "rpc", false, "start rpc server") flag.BoolVar(&StartWebSockets, "ws", false, "start websocket server") flag.BoolVar(&NonInteractive, "y", false, "non-interactive mode (say yes to confirmations)") diff --git a/cmd/mist/main.go b/cmd/mist/main.go index eb0a80addf..bcfb7f2ece 100644 --- a/cmd/mist/main.go +++ b/cmd/mist/main.go @@ -73,7 +73,7 @@ func run() error { } if StartWebSockets { - utils.StartWebSockets(ethereum) + utils.StartWebSockets(ethereum, WsPort) } gui := NewWindow(ethereum, config, ethereum.ClientIdentity().(*p2p.SimpleClientIdentity), KeyRing, LogLevel) From dd3f38fe5b283f3c728823cb2d4e844712017e48 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 27 Jan 2015 14:16:34 -0600 Subject: [PATCH 6/8] Rename transport to ws Cleanup object naming for clarity --- cmd/utils/cmd.go | 2 +- rpc/{websocket => ws}/server.go | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) rename rpc/{websocket => ws}/server.go (80%) diff --git a/cmd/utils/cmd.go b/cmd/utils/cmd.go index 437e0c037c..9cdd8b5d1d 100644 --- a/cmd/utils/cmd.go +++ b/cmd/utils/cmd.go @@ -39,7 +39,7 @@ import ( "github.com/ethereum/go-ethereum/miner" "github.com/ethereum/go-ethereum/rlp" rpchttp "github.com/ethereum/go-ethereum/rpc/http" - rpcws "github.com/ethereum/go-ethereum/rpc/websocket" + rpcws "github.com/ethereum/go-ethereum/rpc/ws" "github.com/ethereum/go-ethereum/state" // "github.com/ethereum/go-ethereum/websocket" "github.com/ethereum/go-ethereum/xeth" diff --git a/rpc/websocket/server.go b/rpc/ws/server.go similarity index 80% rename from rpc/websocket/server.go rename to rpc/ws/server.go index f854fe502e..9be305b6c4 100644 --- a/rpc/websocket/server.go +++ b/rpc/ws/server.go @@ -14,14 +14,14 @@ You should have received a copy of the GNU General Public License along with go-ethereum. If not, see . */ -package websocket +package ws import ( "fmt" "net" "net/http" - ws "code.google.com/p/go.net/websocket" + "code.google.com/p/go.net/websocket" "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/event/filter" "github.com/ethereum/go-ethereum/logger" @@ -88,23 +88,23 @@ func (self *WebSocketServer) Start() { func (s *WebSocketServer) apiHandler(xeth *rpc.EthereumApi) http.Handler { fn := func(w http.ResponseWriter, req *http.Request) { h := sockHandler(xeth) - s := ws.Server{Handler: h} + s := websocket.Server{Handler: h} s.ServeHTTP(w, req) } return http.HandlerFunc(fn) } -func sockHandler(xeth *rpc.EthereumApi) ws.Handler { - fn := func(conn *ws.Conn) { +func sockHandler(xeth *rpc.EthereumApi) websocket.Handler { + fn := func(conn *websocket.Conn) { for { // FIX wslogger does not output to console wslogger.Debugln("Handling request") var reqParsed rpc.RpcRequest - if err := ws.JSON.Receive(conn, &reqParsed); err != nil { + if err := websocket.JSON.Receive(conn, &reqParsed); err != nil { wslogger.Debugln(rpc.ErrorParseRequest) - ws.JSON.Send(conn, rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: rpc.ErrorParseRequest}) + websocket.JSON.Send(conn, rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: rpc.ErrorParseRequest}) continue } @@ -112,12 +112,12 @@ func sockHandler(xeth *rpc.EthereumApi) ws.Handler { reserr := xeth.GetRequestReply(&reqParsed, &response) if reserr != nil { wslogger.Errorln(reserr) - ws.JSON.Send(conn, rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: reserr.Error()}) + websocket.JSON.Send(conn, rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: reserr.Error()}) continue } wslogger.Debugf("Generated response: %T %s", response, response) - ws.JSON.Send(conn, rpc.RpcSuccessResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: false, Result: response}) + websocket.JSON.Send(conn, rpc.RpcSuccessResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: false, Result: response}) } } return fn From d8c6168d11dda55ffb10bc70601e74e347c156e1 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 27 Jan 2015 14:26:42 -0600 Subject: [PATCH 7/8] Return a websocker.Handler of the function --- rpc/ws/server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rpc/ws/server.go b/rpc/ws/server.go index 9be305b6c4..c275031f41 100644 --- a/rpc/ws/server.go +++ b/rpc/ws/server.go @@ -120,5 +120,5 @@ func sockHandler(xeth *rpc.EthereumApi) websocket.Handler { websocket.JSON.Send(conn, rpc.RpcSuccessResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: false, Result: response}) } } - return fn + return websocket.Handler(fn) } From e9d017ba082bfd624267c974fa93a77da6269756 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Tue, 27 Jan 2015 14:34:01 -0600 Subject: [PATCH 8/8] Rename api var for clarity --- rpc/http/server.go | 4 ++-- rpc/ws/server.go | 9 ++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/rpc/http/server.go b/rpc/http/server.go index 631d471108..93b52a634f 100644 --- a/rpc/http/server.go +++ b/rpc/http/server.go @@ -83,7 +83,7 @@ func (s *RpcHttpServer) Start() { } } -func (s *RpcHttpServer) apiHandler(xeth *rpc.EthereumApi) http.Handler { +func (s *RpcHttpServer) apiHandler(api *rpc.EthereumApi) http.Handler { fn := func(w http.ResponseWriter, req *http.Request) { rpchttplogger.Debugln("Handling request") @@ -94,7 +94,7 @@ func (s *RpcHttpServer) apiHandler(xeth *rpc.EthereumApi) http.Handler { } var response interface{} - reserr := xeth.GetRequestReply(&reqParsed, &response) + reserr := api.GetRequestReply(&reqParsed, &response) if reserr != nil { rpchttplogger.Errorln(reserr) JSON.Send(w, &rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: reserr.Error()}) diff --git a/rpc/ws/server.go b/rpc/ws/server.go index c275031f41..248ebc585d 100644 --- a/rpc/ws/server.go +++ b/rpc/ws/server.go @@ -85,9 +85,9 @@ func (self *WebSocketServer) Start() { } } -func (s *WebSocketServer) apiHandler(xeth *rpc.EthereumApi) http.Handler { +func (s *WebSocketServer) apiHandler(api *rpc.EthereumApi) http.Handler { fn := func(w http.ResponseWriter, req *http.Request) { - h := sockHandler(xeth) + h := sockHandler(api) s := websocket.Server{Handler: h} s.ServeHTTP(w, req) } @@ -95,10 +95,9 @@ func (s *WebSocketServer) apiHandler(xeth *rpc.EthereumApi) http.Handler { return http.HandlerFunc(fn) } -func sockHandler(xeth *rpc.EthereumApi) websocket.Handler { +func sockHandler(api *rpc.EthereumApi) websocket.Handler { fn := func(conn *websocket.Conn) { for { - // FIX wslogger does not output to console wslogger.Debugln("Handling request") var reqParsed rpc.RpcRequest @@ -109,7 +108,7 @@ func sockHandler(xeth *rpc.EthereumApi) websocket.Handler { } var response interface{} - reserr := xeth.GetRequestReply(&reqParsed, &response) + reserr := api.GetRequestReply(&reqParsed, &response) if reserr != nil { wslogger.Errorln(reserr) websocket.JSON.Send(conn, rpc.RpcErrorResponse{JsonRpc: reqParsed.JsonRpc, ID: reqParsed.ID, Error: true, ErrorText: reserr.Error()})