github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/network/node/neatnode.go (about)

     1  package node
     2  
     3  import (
     4  	"reflect"
     5  
     6  	"github.com/neatlab/neatio/chain/log"
     7  	"github.com/neatlab/neatio/network/p2p"
     8  	"github.com/neatlab/neatio/network/rpc"
     9  )
    10  
    11  func (n *Node) RpcAPIs() []rpc.API {
    12  	return n.rpcAPIs
    13  }
    14  
    15  func (n *Node) Start1() error {
    16  	n.lock.Lock()
    17  	defer n.lock.Unlock()
    18  
    19  	if err := n.openDataDir(); err != nil {
    20  		return err
    21  	}
    22  
    23  	if n.server == nil {
    24  		return ErrNodeStopped
    25  	}
    26  
    27  	services := n.services
    28  
    29  	running := n.server
    30  
    31  	started := []reflect.Type{}
    32  	for kind, service := range services {
    33  
    34  		if err := service.Start(running); err != nil {
    35  			for _, kind := range started {
    36  				services[kind].Stop()
    37  			}
    38  			running.Stop()
    39  
    40  			return err
    41  		}
    42  
    43  		started = append(started, kind)
    44  	}
    45  
    46  	if err := n.startRPC1(services); err != nil {
    47  		for _, service := range services {
    48  			service.Stop()
    49  		}
    50  		running.Stop()
    51  		return err
    52  	}
    53  
    54  	n.services = services
    55  	n.server = running
    56  	n.stop = make(chan struct{})
    57  
    58  	return nil
    59  }
    60  
    61  func (n *Node) GatherServices() error {
    62  
    63  	services := make(map[reflect.Type]Service)
    64  	for _, constructor := range n.serviceFuncs {
    65  
    66  		ctx := &ServiceContext{
    67  			config:         n.config,
    68  			services:       make(map[reflect.Type]Service),
    69  			EventMux:       n.eventmux,
    70  			AccountManager: n.accman,
    71  		}
    72  		for kind, s := range services {
    73  			ctx.services[kind] = s
    74  		}
    75  
    76  		service, err := constructor(ctx)
    77  		if err != nil {
    78  			return err
    79  		}
    80  		kind := reflect.TypeOf(service)
    81  		if _, exists := services[kind]; exists {
    82  			return &DuplicateServiceError{Kind: kind}
    83  		}
    84  		services[kind] = service
    85  	}
    86  
    87  	n.services = services
    88  
    89  	return nil
    90  }
    91  
    92  func (n *Node) GatherProtocols() []p2p.Protocol {
    93  
    94  	protocols := make([]p2p.Protocol, 0)
    95  	for _, service := range n.services {
    96  		protocols = append(protocols, service.Protocols()...)
    97  	}
    98  
    99  	return protocols
   100  }
   101  
   102  func (n *Node) GetHTTPHandler() (*rpc.Server, error) {
   103  
   104  	whitelist := make(map[string]bool)
   105  	for _, module := range n.config.HTTPModules {
   106  		whitelist[module] = true
   107  	}
   108  
   109  	handler := rpc.NewServer()
   110  	for _, api := range n.rpcAPIs {
   111  		if whitelist[api.Namespace] || (len(whitelist) == 0 && api.Public) {
   112  			if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
   113  				return nil, err
   114  			}
   115  			n.log.Debug("HTTP registered", "service", api.Service, "namespace", api.Namespace)
   116  		}
   117  	}
   118  
   119  	n.httpEndpoint = ""
   120  	n.httpListener = nil
   121  	n.httpHandler = nil
   122  
   123  	return handler, nil
   124  }
   125  
   126  func (n *Node) GetWSHandler() (*rpc.Server, error) {
   127  
   128  	whitelist := make(map[string]bool)
   129  	for _, module := range n.config.WSModules {
   130  		whitelist[module] = true
   131  	}
   132  
   133  	handler := rpc.NewServer()
   134  	for _, api := range n.rpcAPIs {
   135  		if n.config.WSExposeAll || whitelist[api.Namespace] || (len(whitelist) == 0 && api.Public) {
   136  			if err := handler.RegisterName(api.Namespace, api.Service); err != nil {
   137  				return nil, err
   138  			}
   139  			log.Debug("WebSocket registered", "service", api.Service, "namespace", api.Namespace)
   140  		}
   141  	}
   142  
   143  	n.wsEndpoint = ""
   144  	n.wsListener = nil
   145  	n.wsHandler = nil
   146  
   147  	return handler, nil
   148  }
   149  
   150  func (n *Node) startRPC1(services map[reflect.Type]Service) error {
   151  
   152  	apis := n.apis()
   153  	for _, service := range services {
   154  		apis = append(apis, service.APIs()...)
   155  	}
   156  
   157  	if err := n.startInProc(apis); err != nil {
   158  		return err
   159  	}
   160  	if err := n.startIPC(apis); err != nil {
   161  		n.stopInProc()
   162  		return err
   163  	}
   164  
   165  	n.rpcAPIs = apis
   166  	return nil
   167  }
   168  
   169  func (n *Node) GetLogger() log.Logger {
   170  	return n.log
   171  }