cmd, common, core, eth, node, rpc, tests, whisper, xeth: use protocol stacks
This commit is contained in:
@ -18,6 +18,7 @@ package node
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
|
||||
"github.com/ethereum/go-ethereum/ethdb"
|
||||
"github.com/ethereum/go-ethereum/event"
|
||||
@ -28,18 +29,39 @@ import (
|
||||
// the protocol stack, that is passed to all constructors to be optionally used;
|
||||
// as well as utility methods to operate on the service environment.
|
||||
type ServiceContext struct {
|
||||
dataDir string // Data directory for protocol persistence
|
||||
EventMux *event.TypeMux // Event multiplexer used for decoupled notifications
|
||||
datadir string // Data directory for protocol persistence
|
||||
services map[string]Service // Index of the already constructed services
|
||||
EventMux *event.TypeMux // Event multiplexer used for decoupled notifications
|
||||
}
|
||||
|
||||
// Database opens an existing database with the given name (or creates one if no
|
||||
// previous can be found) from within the node's data directory. If the node is
|
||||
// an ephemeral one, a memory database is returned.
|
||||
func (ctx *ServiceContext) Database(name string, cache int) (ethdb.Database, error) {
|
||||
if ctx.dataDir == "" {
|
||||
if ctx.datadir == "" {
|
||||
return ethdb.NewMemDatabase()
|
||||
}
|
||||
return ethdb.NewLDBDatabase(filepath.Join(ctx.dataDir, name), cache)
|
||||
return ethdb.NewLDBDatabase(filepath.Join(ctx.datadir, name), cache)
|
||||
}
|
||||
|
||||
// Service retrieves an already constructed service registered under a given id.
|
||||
func (ctx *ServiceContext) Service(id string) Service {
|
||||
return ctx.services[id]
|
||||
}
|
||||
|
||||
// SingletonService retrieves an already constructed service using a specific type
|
||||
// implementing the Service interface. This is a utility function for scenarios
|
||||
// where it is known that only one instance of a given service type is running,
|
||||
// allowing to access services without needing to know their specific id with
|
||||
// which they were registered.
|
||||
func (ctx *ServiceContext) SingletonService(service interface{}) (string, error) {
|
||||
for id, running := range ctx.services {
|
||||
if reflect.TypeOf(running) == reflect.ValueOf(service).Elem().Type() {
|
||||
reflect.ValueOf(service).Elem().Set(reflect.ValueOf(running))
|
||||
return id, nil
|
||||
}
|
||||
}
|
||||
return "", ErrServiceUnknown
|
||||
}
|
||||
|
||||
// ServiceConstructor is the function signature of the constructors needed to be
|
||||
@ -58,8 +80,9 @@ type Service interface {
|
||||
// Protocol retrieves the P2P protocols the service wishes to start.
|
||||
Protocols() []p2p.Protocol
|
||||
|
||||
// Start spawns any goroutines required by the service.
|
||||
Start() error
|
||||
// Start is called after all services have been constructed and the networking
|
||||
// layer was also initialized to spawn any goroutines required by the service.
|
||||
Start(server *p2p.Server) error
|
||||
|
||||
// Stop terminates all goroutines belonging to the service, blocking until they
|
||||
// are all terminated.
|
||||
|
Reference in New Issue
Block a user