2024-01-06 18:21:09 +00:00
|
|
|
package node
|
2024-01-06 11:33:46 +00:00
|
|
|
|
|
|
|
import (
|
2024-01-06 18:26:03 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/config"
|
2024-01-29 19:33:40 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
2024-01-09 17:51:54 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/protocol"
|
2024-01-07 09:16:18 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/service"
|
2024-01-30 03:31:05 +00:00
|
|
|
_default "git.lumeweb.com/LumeWeb/libs5-go/service/default"
|
2024-01-06 11:33:46 +00:00
|
|
|
bolt "go.etcd.io/bbolt"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2024-01-29 04:59:43 +00:00
|
|
|
type Node struct {
|
2024-01-29 06:55:36 +00:00
|
|
|
nodeConfig *config.NodeConfig
|
|
|
|
services service.Services
|
2024-01-07 14:02:39 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 04:59:43 +00:00
|
|
|
func (n *Node) Services() service.Services {
|
2024-01-07 08:56:50 +00:00
|
|
|
return n.services
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 06:10:04 +00:00
|
|
|
func NewNode(config *config.NodeConfig, services service.Services) *Node {
|
|
|
|
return &Node{
|
2024-01-29 06:55:36 +00:00
|
|
|
nodeConfig: config,
|
|
|
|
services: services, // Services are passed in, not created here
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-29 04:59:43 +00:00
|
|
|
func (n *Node) IsStarted() bool {
|
2024-01-29 06:10:04 +00:00
|
|
|
return n.services.IsStarted()
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 04:59:43 +00:00
|
|
|
func (n *Node) Config() *config.NodeConfig {
|
2024-01-06 11:33:46 +00:00
|
|
|
return n.nodeConfig
|
|
|
|
}
|
|
|
|
|
2024-01-29 04:59:43 +00:00
|
|
|
func (n *Node) Logger() *zap.Logger {
|
2024-01-06 11:33:46 +00:00
|
|
|
if n.nodeConfig != nil {
|
|
|
|
return n.nodeConfig.Logger
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-29 04:59:43 +00:00
|
|
|
func (n *Node) Db() *bolt.DB {
|
2024-01-06 11:33:46 +00:00
|
|
|
if n.nodeConfig != nil {
|
2024-01-06 18:15:45 +00:00
|
|
|
return n.nodeConfig.DB
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-29 04:59:43 +00:00
|
|
|
func (n *Node) Start() error {
|
2024-01-29 06:10:04 +00:00
|
|
|
protocol.RegisterProtocols()
|
2024-01-30 03:35:00 +00:00
|
|
|
protocol.RegisterSignedProtocols()
|
2024-01-06 14:46:01 +00:00
|
|
|
|
2024-01-29 06:10:04 +00:00
|
|
|
return n.services.Start()
|
|
|
|
}
|
2024-01-10 11:33:21 +00:00
|
|
|
|
2024-01-30 20:46:00 +00:00
|
|
|
func (n *Node) Init() error {
|
|
|
|
return n.services.Init()
|
|
|
|
}
|
|
|
|
|
2024-01-29 06:10:04 +00:00
|
|
|
func (n *Node) Stop() error {
|
|
|
|
return n.services.Stop()
|
2024-01-06 14:46:01 +00:00
|
|
|
}
|
2024-01-29 06:10:04 +00:00
|
|
|
|
2024-01-29 04:59:43 +00:00
|
|
|
func (n *Node) WaitOnConnectedPeers() {
|
2024-01-29 06:10:04 +00:00
|
|
|
n.services.P2P().WaitOnConnectedPeers()
|
2024-01-09 14:11:36 +00:00
|
|
|
}
|
2024-01-24 07:32:49 +00:00
|
|
|
|
2024-01-29 19:26:10 +00:00
|
|
|
func (n *Node) NetworkId() string {
|
|
|
|
return n.services.P2P().NetworkId()
|
|
|
|
}
|
|
|
|
|
2024-01-29 19:33:40 +00:00
|
|
|
func (n *Node) NodeId() *encoding.NodeId {
|
|
|
|
return n.services.P2P().NodeId()
|
|
|
|
}
|
|
|
|
|
2024-01-29 06:10:04 +00:00
|
|
|
func DefaultNode(config *config.NodeConfig) *Node {
|
|
|
|
params := service.ServiceParams{
|
|
|
|
Logger: config.Logger,
|
|
|
|
Config: config,
|
|
|
|
Db: config.DB,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize services first
|
2024-01-30 03:31:05 +00:00
|
|
|
p2pService := _default.NewP2P(params)
|
|
|
|
registryService := _default.NewRegistry(params)
|
|
|
|
httpService := _default.NewHTTP(params)
|
|
|
|
storageService := _default.NewStorage(params)
|
2024-01-29 06:10:04 +00:00
|
|
|
|
|
|
|
// Aggregate services
|
|
|
|
services := NewServices(ServicesParams{
|
|
|
|
P2P: p2pService,
|
|
|
|
Registry: registryService,
|
|
|
|
HTTP: httpService,
|
2024-01-29 07:12:02 +00:00
|
|
|
Storage: storageService,
|
2024-01-29 06:10:04 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Now create the node with the services
|
|
|
|
return NewNode(config, services)
|
|
|
|
}
|