// NewInProcRPCClient will start a new RPC server for the given node and returns a client to interact with it. func NewInProcRPCClient(stack *node.Node) *inProcClient { server := rpc.NewServer() offered := stack.APIs() for _, api := range offered { server.RegisterName(api.Namespace, api.Service) } web3 := node.NewPublicWeb3API(stack) server.RegisterName("web3", web3) var ethereum *eth.Ethereum if err := stack.Service(ðereum); err == nil { net := eth.NewPublicNetAPI(stack.Server(), ethereum.NetVersion()) server.RegisterName("net", net) } else { glog.V(logger.Warn).Infof("%v\n", err) } buf := &buf{ requests: make(chan []byte), responses: make(chan []byte), } client := &inProcClient{ server: server, buf: buf, } go func() { server.ServeCodec(rpc.NewJSONCodec(client.buf)) }() return client }
// StartWS starts a websocket JSON-RPC API server. func StartWS(stack *node.Node, ctx *cli.Context) error { for _, api := range stack.APIs() { if adminApi, ok := api.Service.(*node.PrivateAdminAPI); ok { address := ctx.GlobalString(WSListenAddrFlag.Name) port := ctx.GlobalInt(WSAllowedDomainsFlag.Name) allowedDomains := ctx.GlobalString(WSAllowedDomainsFlag.Name) apiStr := "" if ctx.GlobalIsSet(WSApiFlag.Name) { apiStr = ctx.GlobalString(WSApiFlag.Name) } _, err := adminApi.StartWS(address, port, allowedDomains, apiStr) return err } } glog.V(logger.Error).Infof("Unable to start RPC-WS interface, could not find admin API") return errors.New("Unable to start RPC-WS interface") }
func StartIPC(stack *node.Node, ctx *cli.Context) error { var ethereum *eth.Ethereum if err := stack.Service(ðereum); err != nil { return err } endpoint := IPCSocketPath(ctx) listener, err := rpc.CreateIPCListener(endpoint) if err != nil { return err } server := rpc.NewServer() // register package API's this node provides offered := stack.APIs() for _, api := range offered { server.RegisterName(api.Namespace, api.Service) glog.V(logger.Debug).Infof("Register %T under namespace '%s' for IPC service\n", api.Service, api.Namespace) } go func() { glog.V(logger.Info).Infof("Start IPC server on %s\n", 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 }