Пример #1
0
// 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
}
Пример #2
0
func StartIPC(stack *node.Node, ctx *cli.Context) error {
	var ethereum *eth.Ethereum
	if err := stack.Service(&ethereum); 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

}