Add Core actor

This commit is contained in:
Neil Alexander 2019-08-28 19:53:52 +01:00
parent aa0770546e
commit 1f658cce76
No known key found for this signature in database
GPG Key ID: A02A2019A2BB0944

View File

@ -33,7 +33,7 @@ type Core struct {
log *log.Logger log *log.Logger
} }
func (c *Core) init() error { func (c *Core) _init() error {
// TODO separate init and start functions // TODO separate init and start functions
// Init sets up structs // Init sets up structs
// Start launches goroutines that depend on structs being set up // Start launches goroutines that depend on structs being set up
@ -85,34 +85,35 @@ func (c *Core) init() error {
// If any static peers were provided in the configuration above then we should // If any static peers were provided in the configuration above then we should
// configure them. The loop ensures that disconnected peers will eventually // configure them. The loop ensures that disconnected peers will eventually
// be reconnected with. // be reconnected with.
func (c *Core) addPeerLoop() { func (c *Core) _addPeerLoop() {
for { // Get the peers from the config - these could change!
// the peers from the config - these could change!
current := c.config.GetCurrent() current := c.config.GetCurrent()
// Add peers from the Peers section // Add peers from the Peers section
for _, peer := range current.Peers { for _, peer := range current.Peers {
go c.AddPeer(peer, "") go c.AddPeer(peer, "") // TODO: this should be acted and not in a goroutine?
time.Sleep(time.Second) time.Sleep(time.Second)
} }
// Add peers from the InterfacePeers section // Add peers from the InterfacePeers section
for intf, intfpeers := range current.InterfacePeers { for intf, intfpeers := range current.InterfacePeers {
for _, peer := range intfpeers { for _, peer := range intfpeers {
go c.AddPeer(peer, intf) go c.AddPeer(peer, intf) // TODO: this should be acted and not in a goroutine?
time.Sleep(time.Second) time.Sleep(time.Second)
} }
} }
// Sit for a while // Sit for a while
time.Sleep(time.Minute) time.AfterFunc(time.Minute, func() {
} c.Act(c, c._addPeerLoop)
})
} }
// UpdateConfig updates the configuration in Core with the provided // UpdateConfig updates the configuration in Core with the provided
// config.NodeConfig and then signals the various module goroutines to // config.NodeConfig and then signals the various module goroutines to
// reconfigure themselves if needed. // reconfigure themselves if needed.
func (c *Core) UpdateConfig(config *config.NodeConfig) { func (c *Core) UpdateConfig(config *config.NodeConfig) {
c.Act(nil, func() {
c.log.Debugln("Reloading node configuration...") c.log.Debugln("Reloading node configuration...")
// Replace the active configuration with the supplied one // Replace the active configuration with the supplied one
@ -121,6 +122,7 @@ func (c *Core) UpdateConfig(config *config.NodeConfig) {
// Notify the router and switch about the new configuration // Notify the router and switch about the new configuration
c.router.Act(c, c.router.reconfigure) c.router.Act(c, c.router.reconfigure)
c.switchTable.Act(c, c.switchTable.reconfigure) c.switchTable.Act(c, c.switchTable.reconfigure)
})
} }
// Start starts up Yggdrasil using the provided config.NodeConfig, and outputs // Start starts up Yggdrasil using the provided config.NodeConfig, and outputs
@ -128,7 +130,15 @@ func (c *Core) UpdateConfig(config *config.NodeConfig) {
// TCP and UDP sockets, a multicast discovery socket, an admin socket, router, // TCP and UDP sockets, a multicast discovery socket, an admin socket, router,
// switch and DHT node. A config.NodeState is returned which contains both the // switch and DHT node. A config.NodeState is returned which contains both the
// current and previous configurations (from reconfigures). // current and previous configurations (from reconfigures).
func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) (*config.NodeState, error) { func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) (conf *config.NodeState, err error) {
phony.Block(c, func() {
conf, err = c._start(nc, log)
})
return
}
// This function is unsafe and should only be ran by the core actor.
func (c *Core) _start(nc *config.NodeConfig, log *log.Logger) (*config.NodeState, error) {
c.log = log c.log = log
c.config = config.NodeState{ c.config = config.NodeState{
@ -144,8 +154,7 @@ func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) (*config.NodeState,
} }
c.log.Infoln("Starting up...") c.log.Infoln("Starting up...")
c._init()
c.init()
if err := c.link.init(c); err != nil { if err := c.link.init(c); err != nil {
c.log.Errorln("Failed to start link interfaces") c.log.Errorln("Failed to start link interfaces")
@ -162,7 +171,7 @@ func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) (*config.NodeState,
return nil, err return nil, err
} }
go c.addPeerLoop() c.Act(c, c._addPeerLoop)
c.log.Infoln("Startup complete") c.log.Infoln("Startup complete")
return &c.config, nil return &c.config, nil
@ -170,5 +179,10 @@ func (c *Core) Start(nc *config.NodeConfig, log *log.Logger) (*config.NodeState,
// Stop shuts down the Yggdrasil node. // Stop shuts down the Yggdrasil node.
func (c *Core) Stop() { func (c *Core) Stop() {
phony.Block(c, c._stop)
}
// This function is unsafe and should only be ran by the core actor.
func (c *Core) _stop() {
c.log.Infoln("Stopping...") c.log.Infoln("Stopping...")
} }