// 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 }
// Start boots up the Ethereum protocol, starts interacting with the P2P network // and opens up the IPC based JSOn RPC API for accessing the exposed APIs. func (g *Geth) Start() error { // Assemble and start the protocol stack err := g.stack.Start() if err != nil { return err } // Attach an IPC endpoint to the stack (this should really have been done by the stack..) if runtime.GOOS == "windows" { g.ipcEndpoint = common.DefaultIpcPath() } else { g.ipcEndpoint = filepath.Join(g.stack.DataDir(), "geth.ipc") } if g.ipcSocket, err = rpc.CreateIPCListener(g.ipcEndpoint); err != nil { return err } // Iterate over all the APIs provided by the stack and expose them g.ipcServer = rpc.NewServer() for _, api := range g.stack.APIs() { log15.Debug("Exposing/extending API...", "namespace", api.Namespace, "runner", fmt.Sprintf("%T", api.Service)) g.ipcServer.RegisterName(api.Namespace, api.Service) } log15.Info("Starting IPC listener...", "endpoint", g.ipcEndpoint) go g.handleIPC() return nil }
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 }