diff options
author | Bas van Kervel <bas@ethdev.com> | 2015-10-15 22:07:19 +0800 |
---|---|---|
committer | Bas van Kervel <bas@ethdev.com> | 2015-12-14 23:34:05 +0800 |
commit | eae81465c1c815c317cd30e4de6bdf4d59df2340 (patch) | |
tree | b6f4b7787967a58416171adb79fd12ac29d89577 /cmd/gethrpctest/main.go | |
parent | 8db9d44ca9fb6baf406256cae491c475de2f4989 (diff) | |
download | dexon-eae81465c1c815c317cd30e4de6bdf4d59df2340.tar dexon-eae81465c1c815c317cd30e4de6bdf4d59df2340.tar.gz dexon-eae81465c1c815c317cd30e4de6bdf4d59df2340.tar.bz2 dexon-eae81465c1c815c317cd30e4de6bdf4d59df2340.tar.lz dexon-eae81465c1c815c317cd30e4de6bdf4d59df2340.tar.xz dexon-eae81465c1c815c317cd30e4de6bdf4d59df2340.tar.zst dexon-eae81465c1c815c317cd30e4de6bdf4d59df2340.zip |
rpc: new RPC implementation with pub/sub support
Diffstat (limited to 'cmd/gethrpctest/main.go')
-rw-r--r-- | cmd/gethrpctest/main.go | 65 |
1 files changed, 64 insertions, 1 deletions
diff --git a/cmd/gethrpctest/main.go b/cmd/gethrpctest/main.go index 7130980ac..636d329e4 100644 --- a/cmd/gethrpctest/main.go +++ b/cmd/gethrpctest/main.go @@ -23,15 +23,22 @@ import ( "log" "os" "os/signal" + "path/filepath" + "runtime" "github.com/ethereum/go-ethereum/accounts" + "github.com/ethereum/go-ethereum/cmd/utils" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/rpc/api" "github.com/ethereum/go-ethereum/rpc/codec" "github.com/ethereum/go-ethereum/rpc/comms" + rpc "github.com/ethereum/go-ethereum/rpc/v2" "github.com/ethereum/go-ethereum/tests" "github.com/ethereum/go-ethereum/whisper" "github.com/ethereum/go-ethereum/xeth" @@ -81,9 +88,14 @@ func main() { } log.Println("Initial test suite passed...") + if err := StartIPC(stack); err != nil { + log.Fatalf("Failed to start IPC interface: %v\n", err) + } + log.Println("IPC Interface started, accepting requests...") + // Start the RPC interface and wait until terminated if err := StartRPC(stack); err != nil { - log.Fatalf("Failed to start RPC instarface: %v", err) + log.Fatalf("Failed to start RPC interface: %v", err) } log.Println("RPC Interface started, accepting requests...") @@ -177,3 +189,54 @@ func StartRPC(stack *node.Node) error { } return comms.StartHttp(config, codec, api.Merge(apis...)) } + +// StartRPC initializes an IPC interface to the given protocol stack. +func StartIPC(stack *node.Node) error { + var ethereum *eth.Ethereum + if err := stack.Service(ðereum); err != nil { + return err + } + + endpoint := `\\.\pipe\geth.ipc` + if runtime.GOOS != "windows" { + endpoint = filepath.Join(common.DefaultDataDir(), "geth.ipc") + } + + config := comms.IpcConfig{ + Endpoint: endpoint, + } + + listener, err := comms.CreateListener(config) + if err != nil { + return err + } + + server := rpc.NewServer() + + // register package API's this node provides + offered := stack.RPCAPIs() + for _, api := range offered { + server.RegisterName(api.Namespace, api.Service) + glog.V(logger.Debug).Infof("Register %T@%s for IPC service\n", api.Service, api.Namespace) + } + + web3 := utils.NewPublicWeb3API(stack) + server.RegisterName("web3", web3) + net := utils.NewPublicNetAPI(stack.Server(), ethereum.NetVersion()) + server.RegisterName("net", net) + + go func() { + glog.V(logger.Info).Infof("Start IPC server on %s\n", config.Endpoint) + for { + conn, err := listener.Accept() + if err != nil { + glog.V(logger.Error).Infof("Unable to accept connection - %v\n", err) + } + + codec := rpc.NewJSONCodec(conn) + go server.ServeCodec(codec) + } + }() + + return nil +} |