@@ -55,7 +55,9 @@ type Node struct {
5555 serviceFuncs []ServiceConstructor // Service constructors (in dependency order)
5656 services map [reflect.Type ]Service // Currently running services
5757
58- rpcAPIs []rpc.API // List of APIs currently provided by the node
58+ rpcAPIs []rpc.API // List of APIs currently provided by the node
59+ inprocHandler * rpc.Server // In-process RPC request handler to process the API requests
60+
5961 ipcEndpoint string // IPC endpoint to listen at (empty = IPC disabled)
6062 ipcListener net.Listener // IPC RPC listener socket to serve API requests
6163 ipcHandler * rpc.Server // IPC RPC request handler to process the API requests
@@ -217,23 +219,51 @@ func (n *Node) startRPC(services map[reflect.Type]Service) error {
217219 apis = append (apis , service .APIs ()... )
218220 }
219221 // Start the various API endpoints, terminating all in case of errors
222+ if err := n .startInProc (apis ); err != nil {
223+ return err
224+ }
220225 if err := n .startIPC (apis ); err != nil {
226+ n .stopInProc ()
221227 return err
222228 }
223229 if err := n .startHTTP (n .httpEndpoint , apis , n .httpWhitelist , n .httpCors ); err != nil {
224230 n .stopIPC ()
231+ n .stopInProc ()
225232 return err
226233 }
227234 if err := n .startWS (n .wsEndpoint , apis , n .wsWhitelist , n .wsDomains ); err != nil {
228235 n .stopHTTP ()
229236 n .stopIPC ()
237+ n .stopInProc ()
230238 return err
231239 }
232240 // All API endpoints started successfully
233241 n .rpcAPIs = apis
234242 return nil
235243}
236244
245+ // startInProc initializes an in-process RPC endpoint.
246+ func (n * Node ) startInProc (apis []rpc.API ) error {
247+ // Register all the APIs exposed by the services
248+ handler := rpc .NewServer ()
249+ for _ , api := range apis {
250+ if err := handler .RegisterName (api .Namespace , api .Service ); err != nil {
251+ return err
252+ }
253+ glog .V (logger .Debug ).Infof ("InProc registered %T under '%s'" , api .Service , api .Namespace )
254+ }
255+ n .inprocHandler = handler
256+ return nil
257+ }
258+
259+ // stopInProc terminates the in-process RPC endpoint.
260+ func (n * Node ) stopInProc () {
261+ if n .inprocHandler != nil {
262+ n .inprocHandler .Stop ()
263+ n .inprocHandler = nil
264+ }
265+ }
266+
237267// startIPC initializes and starts the IPC RPC endpoint.
238268func (n * Node ) startIPC (apis []rpc.API ) error {
239269 // Short circuit if the IPC endpoint isn't being exposed
@@ -468,6 +498,19 @@ func (n *Node) Restart() error {
468498 return nil
469499}
470500
501+ // Attach creates an RPC client attached to an in-process API handler.
502+ func (n * Node ) Attach () (rpc.Client , error ) {
503+ n .lock .RLock ()
504+ defer n .lock .RUnlock ()
505+
506+ // Short circuit if the node's not running
507+ if n .server == nil {
508+ return nil , ErrNodeStopped
509+ }
510+ // Otherwise attach to the API and return
511+ return rpc .NewInProcRPCClient (n .inprocHandler ), nil
512+ }
513+
471514// Server retrieves the currently running P2P network layer. This method is meant
472515// only to inspect fields of the currently running server, life cycle management
473516// should be left to this Node entity.
@@ -506,6 +549,16 @@ func (n *Node) IPCEndpoint() string {
506549 return n .ipcEndpoint
507550}
508551
552+ // HTTPEndpoint retrieves the current HTTP endpoint used by the protocol stack.
553+ func (n * Node ) HTTPEndpoint () string {
554+ return n .httpEndpoint
555+ }
556+
557+ // WSEndpoint retrieves the current WS endpoint used by the protocol stack.
558+ func (n * Node ) WSEndpoint () string {
559+ return n .wsEndpoint
560+ }
561+
509562// EventMux retrieves the event multiplexer used by all the network services in
510563// the current protocol stack.
511564func (n * Node ) EventMux () * event.TypeMux {
0 commit comments